Skip to content

Commit

Permalink
Add ilo networking stanza to server resource
Browse files Browse the repository at this point in the history
Add this stanza to server resource:

```
          ilo_network_info = {
                  ilo_ip = "16.182.105.216"
                  gateway = "16.182.104.1"
                  subnet_mask = "255.255.248.0"
          }
```

Note that naming is inconsistent, so we need to map between a list
of this format in the POST /add-hypervisor-server http body:

```
        "iloMgmtIpInfo": {
          "ipAddress": "1.2.3.4",
          "subnetMask": "255.255.255.0",
          "gateway": "1.2.3.1"
        }
```

and this (non-list) format in the GET /server http response body:

```
  "iloNetworkInfo": {
    "iloIp": "1.2.3.4",
    "subnetMask": "255.255.255.0",
    "gateway": "1.2.3.1"
  },
```
  • Loading branch information
stuart-mclaren-hpe committed Dec 11, 2024
1 parent e5dd3f3 commit d370612
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 49 deletions.
5 changes: 5 additions & 0 deletions examples/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ resource "hpegl_pc_server" "test" {
hypervisor_cluster_id = "acd4daea-e5e3-5f35-8be3-ce4a4b6d946c"
hypervisor_host_ip = "16.182.105.217"
}
ilo_network_info = {
ilo_ip = "16.182.105.216"
gateway = "16.182.104.1"
subnet_mask = "255.255.248.0"
}
server_network = [
{
data_ip_infos = [
Expand Down
112 changes: 64 additions & 48 deletions internal/resources/server/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (
tfpath "github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-framework/types/basetypes"
"github.com/hashicorp/terraform-plugin-log/tflog"
)

Expand All @@ -38,52 +37,79 @@ type Resource struct {
// input configuration and returns a slice of server network data which can be
// used in a POST request to the PCBe API to create a new server
// TODO: (API) Issue FF-31496 will prevent this from working currently.
func parseNetworksToPostFormat(serverNetworks basetypes.ListValue) (
func parseNetworksToPostFormat(dataP *ServerModel) (
[]privatecloudbusiness.
V1beta1SystemsItemAddHypervisorServersPostRequestBody_serverNetworkable,
error,
) {
var postRequestNetworks []privatecloudbusiness.
V1beta1SystemsItemAddHypervisorServersPostRequestBody_serverNetworkable

serverNetworks := (*dataP).ServerNetwork
if serverNetworks.IsNull() {
msg := "server network list is null"

return nil, errors.New(msg)

}

networks := serverNetworks.Elements()
for _, network := range networks {
serverNetwork, ok := network.(ServerNetworkValue)
if !ok {
msg := "server network element is not a ServerNetworkValue"

return nil, errors.New(msg)
}

infos := serverNetwork.DataIpInfos.Elements()
if len(infos) != 1 {
msg := "server network must be an array of length 1"

return nil, errors.New(msg)
}

dataIPInfosValue, ok := infos[0].(DataIpInfosValue)
if !ok {
msg := "data ip info element is not a DataIpInfosValue"

return nil, errors.New(msg)
}

ip := dataIPInfosValue.IpAddress.ValueString()
net := privatecloudbusiness.
NewV1beta1SystemsItemAddHypervisorServersPostRequestBody_serverNetwork()
dataIPInfos := privatecloudbusiness.
NewV1beta1SystemsItemAddHypervisorServersPostRequestBody_serverNetwork_dataIpInfos()
dataIPInfos.SetIpAddress(&ip)
dataIps := []privatecloudbusiness.
V1beta1SystemsItemAddHypervisorServersPostRequestBody_serverNetwork_dataIpInfosable{
dataIPInfos,
}
net.SetDataIpInfos(dataIps)
postRequestNetworks = []privatecloudbusiness.
V1beta1SystemsItemAddHypervisorServersPostRequestBody_serverNetworkable{net}
if len(networks) != 1 {
msg := "server network list should be of length 1"

return nil, errors.New(msg)
}

serverNetwork, ok := networks[0].(ServerNetworkValue)
if !ok {
msg := "server network element is not a ServerNetworkValue"

return nil, errors.New(msg)
}

infos := serverNetwork.DataIpInfos.Elements()
if len(infos) != 1 {
msg := "server network must be an array of length 1"

return nil, errors.New(msg)
}

dataIPInfosValue, ok := infos[0].(DataIpInfosValue)
if !ok {
msg := "data ip info element is not a DataIpInfosValue"

return nil, errors.New(msg)
}

ip := dataIPInfosValue.IpAddress.ValueString()
dataIPInfos := privatecloudbusiness.
NewV1beta1SystemsItemAddHypervisorServersPostRequestBody_serverNetwork_dataIpInfos()
dataIPInfos.SetIpAddress(&ip)
dataIps := []privatecloudbusiness.
V1beta1SystemsItemAddHypervisorServersPostRequestBody_serverNetwork_dataIpInfosable{
dataIPInfos,
}

iloIPInfos := privatecloudbusiness.
NewV1beta1SystemsItemAddHypervisorServersPostRequestBody_serverNetwork_iloMgmtIpInfo()

iloIP := (*dataP).IloNetworkInfo.IloIp.ValueString()
iloIPInfos.SetIpAddress(&iloIP)

iloSubnetMask := (*dataP).IloNetworkInfo.SubnetMask.ValueString()
iloIPInfos.SetSubnetMask(&iloSubnetMask)

iloGateway := (*dataP).IloNetworkInfo.Gateway.ValueString()
iloIPInfos.SetGateway(&iloGateway)

net := privatecloudbusiness.
NewV1beta1SystemsItemAddHypervisorServersPostRequestBody_serverNetwork()
net.SetDataIpInfos(dataIps)
net.SetIloMgmtIpInfo(iloIPInfos)

postRequestNetworks = []privatecloudbusiness.
V1beta1SystemsItemAddHypervisorServersPostRequestBody_serverNetworkable{net}

return postRequestNetworks, nil
}

Expand Down Expand Up @@ -338,16 +364,6 @@ func doCreate(
esxRootCredentialID := (*dataP).EsxRootCredentialId.ValueString()
systemID := (*dataP).SystemId.ValueString()
iloAdminCredentialID := (*dataP).IloAdminCredentialId.ValueString()
serverNetworks := (*dataP).ServerNetwork
if serverNetworks.IsNull() {
(*diagsP).AddError(
"error adding hypervisor server",
"server network list is null",
)

return

}
prc := privatecloudbusiness.
V1beta1SystemsItemAddHypervisorServersRequestBuilderPostRequestConfiguration{}
prb := privatecloudbusiness.
Expand All @@ -356,7 +372,7 @@ func doCreate(
prb.SetIloAdminCredentialId(&iloAdminCredentialID)
prb.SetHypervisorClusterId(&hciClusterUUID)

postRequestNetworks, err := parseNetworksToPostFormat(serverNetworks)
postRequestNetworks, err := parseNetworksToPostFormat(dataP)
if err != nil {
(*diagsP).AddError(
"error adding hypervisor server",
Expand Down
2 changes: 1 addition & 1 deletion internal/simulator/fixtures/servers/create/post.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"esxRootCredentialId":"cccfcad1-85b7-4162-b16e-f7cadc2c46b5","hypervisorClusterId":"acd4daea-e5e3-5f35-8be3-ce4a4b6d946c","iloAdminCredentialId":"dddfcad1-85b7-4162-b16e-f7cadc2c46b5","serverNetwork":[{"dataIpInfos":[{"ipAddress":"16.182.105.217"}]}]}
{"esxRootCredentialId":"cccfcad1-85b7-4162-b16e-f7cadc2c46b5","hypervisorClusterId":"acd4daea-e5e3-5f35-8be3-ce4a4b6d946c","iloAdminCredentialId":"dddfcad1-85b7-4162-b16e-f7cadc2c46b5","serverNetwork":[{"dataIpInfos":[{"ipAddress":"16.182.105.217"}],"iloMgmtIpInfo":{"gateway":"16.182.104.1","ipAddress":"16.182.105.216","subnetMask":"255.255.248.0"}}]}
36 changes: 36 additions & 0 deletions test/server/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,11 @@ func TestAccServerResource(t *testing.T) {
hypervisor_cluster_id = "acd4daea-e5e3-5f35-8be3-ce4a4b6d946c"
hypervisor_host_ip = "16.182.105.217"
}
ilo_network_info = {
ilo_ip = "16.182.105.216"
gateway = "16.182.104.1"
subnet_mask = "255.255.248.0"
}
server_network = [
{
data_ip_infos = [
Expand Down Expand Up @@ -117,6 +122,22 @@ func TestAccServerResource(t *testing.T) {
"hpegl_pc_server.test",
"hypervisor_host.hypervisor_host_ip",
),
resource.TestCheckResourceAttrSet(
"hpegl_pc_server.test",
"ilo_network_info.ilo_ip",
),
resource.TestCheckResourceAttrSet(
"hpegl_pc_server.test",
"ilo_network_info.gateway",
),
resource.TestCheckResourceAttrSet(
"hpegl_pc_server.test",
"ilo_network_info.subnet_mask",
),
resource.TestCheckResourceAttrSet(
"hpegl_pc_server.test",
"hypervisor_host.hypervisor_host_ip",
),
checkUUIDAttr("hpegl_pc_server.test", "id"),
checkUUIDAttr("hpegl_pc_server.test", "system_id"),
checkUUIDAttr("hpegl_pc_server.test", "esx_root_credential_id"),
Expand Down Expand Up @@ -172,6 +193,21 @@ func TestAccServerResource(t *testing.T) {
"hypervisor_host.id",
"530b1894-9bd0-5627-9362-565aff1e5cbd",
),
resource.TestCheckResourceAttr(
"hpegl_pc_server.test",
"ilo_network_info.ilo_ip",
"16.182.105.216",
),
resource.TestCheckResourceAttr(
"hpegl_pc_server.test",
"ilo_network_info.gateway",
"16.182.104.1",
),
resource.TestCheckResourceAttr(
"hpegl_pc_server.test",
"ilo_network_info.subnet_mask",
"255.255.248.0",
),
resource.TestCheckResourceAttr(
"hpegl_pc_server.test",
"hypervisor_host.hypervisor_host_ip",
Expand Down

0 comments on commit d370612

Please sign in to comment.