Skip to content

Commit

Permalink
Add attributes to server
Browse files Browse the repository at this point in the history
Add some additional attributes to the server model.

Re-position some attributes to more closely match the
json response of GET .../server/<id>. This means that
the terraform code will more closely resemble the REST
state.
  • Loading branch information
stuart-mclaren-hpe committed Dec 9, 2024
1 parent 09fe45b commit dba1611
Show file tree
Hide file tree
Showing 3 changed files with 135 additions and 13 deletions.
10 changes: 7 additions & 3 deletions examples/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,15 @@ resource "hpegl_pc_hypervisor_cluster" "my_hypervisor_cluster" {
}
}

resource "hpegl_pc_server" "my_server" {
system_id = data.hpegl_pc_system.my_system.id
hypervisor_cluster_id = "acd4daea-e5e3-5f35-8be3-ce4a4b6d946c"

resource "hpegl_pc_server" "test" {
system_id = "126fd201-9e6e-5e31-9ffb-a766265b1fd3"
esx_root_credential_id = "cccfcad1-85b7-4162-b16e-f7cadc2c46b5"
ilo_admin_credential_id = "dddfcad1-85b7-4162-b16e-f7cadc2c46b5"
hypervisor_host = {
hypervisor_cluster_id = "acd4daea-e5e3-5f35-8be3-ce4a4b6d946c"
hypervisor_host_ip = "16.182.105.217"
}
server_network = [
{
data_ip_infos = [
Expand Down
88 changes: 85 additions & 3 deletions internal/resources/server/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/HewlettPackard/hpegl-pcbe-terraform-resources/internal/client"
"github.com/HewlettPackard/hpegl-pcbe-terraform-resources/internal/constants"
"github.com/HewlettPackard/hpegl-pcbe-terraform-resources/internal/sdk/systems/privatecloudbusiness"
"github.com/hashicorp/terraform-plugin-framework/attr"
"github.com/hashicorp/terraform-plugin-framework/diag"
tfpath "github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/resource"
Expand Down Expand Up @@ -199,7 +200,6 @@ func doRead(
return
}

// If this doesn't match, something is wrong
if server.GetHypervisorHost() == nil {
(*diagsP).AddError(
"error reading server",
Expand All @@ -209,7 +209,6 @@ func doRead(
return
}

// If this doesn't match, something is wrong
hypervisorClusterID := server.GetHypervisorHost().GetHypervisorClusterId()
if hypervisorClusterID == nil {
(*diagsP).AddError(
Expand All @@ -220,6 +219,89 @@ func doRead(
return
}

// If this doesn't match, something is wrong
if *hypervisorClusterID != (*dataP).HypervisorHost.HypervisorClusterId.ValueString() {
(*diagsP).AddError(
"error reading server",
"'hypervisor cluster id' mismatch "+
(*dataP).HypervisorHost.HypervisorClusterId.ValueString()+
" != "+*hypervisorClusterID,
)

return
}

hypervisorHostIP := server.GetHypervisorHost().GetHypervisorHostIp()
if hypervisorHostIP == nil {
(*diagsP).AddError(
"error reading server",
"'hypervisor host ip' is nil",
)

return
}

// If this doesn't match, something is wrong
if *hypervisorHostIP != (*dataP).HypervisorHost.HypervisorHostIp.ValueString() {
(*diagsP).AddError(
"error reading server",
"'hypervisor host ip' mismatch "+
(*dataP).HypervisorHost.HypervisorHostIp.ValueString()+
" != "+*hypervisorHostIP,
)

return
}

hypervisorClusterName := server.GetHypervisorHost().GetHypervisorClusterName()
if hypervisorClusterName == nil {
(*diagsP).AddError(
"error reading server",
"'hypervisor host cluster name' is nil",
)

return
}

hypervisorHostID := server.GetHypervisorHost().GetId()
if hypervisorHostID == nil {
(*diagsP).AddError(
"error reading server",
"'hypervisor host id' is nil",
)

return
}

hypervisorHostName := server.GetHypervisorHost().GetName()
if hypervisorHostName == nil {
(*diagsP).AddError(
"error reading server",
"'hypervisor host name' is nil",
)

return
}

value := map[string]attr.Value{
"hypervisor_cluster_id": types.StringValue(*hypervisorClusterID),
"hypervisor_cluster_name": types.StringValue(*hypervisorClusterName),
"hypervisor_host_ip": types.StringValue(*hypervisorHostIP),
"id": types.StringValue(*hypervisorHostID),
"name": types.StringValue(*hypervisorHostName),
}

hypervisorHost, diags := NewHypervisorHostValue(
(*dataP).HypervisorHost.AttributeTypes(ctx), value,
)

(*diagsP).Append(diags...)
if (*diagsP).HasError() {
return
}

(*dataP).HypervisorHost = hypervisorHost

if server.GetSerialNumber() == nil {
(*diagsP).AddError(
"error reading server",
Expand Down Expand Up @@ -252,7 +334,7 @@ func doCreate(
dataP *ServerModel,
diagsP *diag.Diagnostics,
) {
hciClusterUUID := (*dataP).HypervisorClusterId.ValueString()
hciClusterUUID := (*dataP).HypervisorHost.HypervisorClusterId.ValueString()
esxRootCredentialID := (*dataP).EsxRootCredentialId.ValueString()
systemID := (*dataP).SystemId.ValueString()
iloAdminCredentialID := (*dataP).IloAdminCredentialId.ValueString()
Expand Down
50 changes: 43 additions & 7 deletions test/server/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,12 @@ func TestAccServerResource(t *testing.T) {
config := providerConfig + `
resource "hpegl_pc_server" "test" {
system_id = "126fd201-9e6e-5e31-9ffb-a766265b1fd3"
hypervisor_cluster_id = "acd4daea-e5e3-5f35-8be3-ce4a4b6d946c"
esx_root_credential_id = "cccfcad1-85b7-4162-b16e-f7cadc2c46b5"
ilo_admin_credential_id = "dddfcad1-85b7-4162-b16e-f7cadc2c46b5"
hypervisor_host = {
hypervisor_cluster_id = "acd4daea-e5e3-5f35-8be3-ce4a4b6d946c"
hypervisor_host_ip = "16.182.105.217"
}
server_network = [
{
data_ip_infos = [
Expand All @@ -102,11 +105,24 @@ func TestAccServerResource(t *testing.T) {
"hpegl_pc_server.test",
"name",
),
resource.TestCheckResourceAttrSet(
"hpegl_pc_server.test",
"hypervisor_host.name",
),
resource.TestCheckResourceAttrSet(
"hpegl_pc_server.test",
"hypervisor_host.hypervisor_cluster_name",
),
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", "hypervisor_cluster_id"),
checkUUIDAttr("hpegl_pc_server.test", "esx_root_credential_id"),
checkUUIDAttr("hpegl_pc_server.test", "ilo_admin_credential_id"),
checkUUIDAttr("hpegl_pc_server.test", "hypervisor_host.hypervisor_cluster_id"),
checkUUIDAttr("hpegl_pc_server.test", "hypervisor_host.id"),
}

if simulation {
Expand All @@ -121,11 +137,6 @@ func TestAccServerResource(t *testing.T) {
"system_id",
"126fd201-9e6e-5e31-9ffb-a766265b1fd3",
),
resource.TestCheckResourceAttr(
"hpegl_pc_server.test",
"hypervisor_cluster_id",
"acd4daea-e5e3-5f35-8be3-ce4a4b6d946c",
),
resource.TestCheckResourceAttr(
"hpegl_pc_server.test",
"esx_root_credential_id",
Expand All @@ -141,6 +152,31 @@ func TestAccServerResource(t *testing.T) {
"name",
"16.182.105.217",
),
resource.TestCheckResourceAttr(
"hpegl_pc_server.test",
"hypervisor_host.hypervisor_cluster_id",
"acd4daea-e5e3-5f35-8be3-ce4a4b6d946c",
),
resource.TestCheckResourceAttr(
"hpegl_pc_server.test",
"hypervisor_host.hypervisor_cluster_name",
"5305-CL",
),
resource.TestCheckResourceAttr(
"hpegl_pc_server.test",
"hypervisor_host.name",
"16.182.105.217",
),
resource.TestCheckResourceAttr(
"hpegl_pc_server.test",
"hypervisor_host.id",
"530b1894-9bd0-5627-9362-565aff1e5cbd",
),
resource.TestCheckResourceAttr(
"hpegl_pc_server.test",
"hypervisor_host.hypervisor_host_ip",
"16.182.105.217",
),
)
}

Expand Down

0 comments on commit dba1611

Please sign in to comment.