Skip to content

Commit

Permalink
server read - add name
Browse files Browse the repository at this point in the history
Add computed `name` value to tfstate file.
  • Loading branch information
stuart-mclaren-hpe committed Nov 29, 2024
1 parent 31c985b commit 65f9715
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 3 deletions.
38 changes: 35 additions & 3 deletions internal/resources/server/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,12 @@ func (r *Resource) Configure(
r.client = req.ProviderData.(*client.PCBeClient)
}

// doRead reads the server data from the PCBe API and prepares to update
// the model.
// Note: we check that returned fields match the 'required' value specified
// by the user in the Terraform configuration. For computed fields, we
// will set the value to the one returned by the API, even if the value
// has changed.
func doRead(
ctx context.Context,
client client.PCBeClient,
Expand Down Expand Up @@ -105,6 +111,7 @@ func doRead(
return
}

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

// If this doesn't match, something is wrong
if *(server.GetSystemId()) != systemID {
(*diagsP).AddError(
"error reading server",
Expand All @@ -136,16 +144,26 @@ func doRead(
return
}

if server.GetName() == nil {
// If this doesn't match, something is wrong
if server.GetHypervisorHost() == nil {
(*diagsP).AddError(
"error reading server",
"'name' is nil",
"'hypervisor host' is nil",
)

return
}

(*dataP).Name = types.StringValue(*(server.GetName()))
// If this doesn't match, something is wrong
hypervisorClusterID := server.GetHypervisorHost().GetHypervisorClusterId()
if hypervisorClusterID == nil {
(*diagsP).AddError(
"error reading server",
"'hypervisor cluster id' is nil",
)

return
}

if server.GetSerialNumber() == nil {
(*diagsP).AddError(
Expand All @@ -157,6 +175,20 @@ func doRead(
}

(*dataP).SerialNumber = types.StringValue(*(server.GetSerialNumber()))

if server.GetName() == nil {
(*diagsP).AddError(
"error reading server",
"'name' is nil",
)

return
}

(*dataP).Name = types.StringValue(*(server.GetName()))

// TODO: (API) Add esxRootCredentialId when FF-31524 is addressed
// TODO: (API) Add iloAdminCredentialId when FF-31525 is addressed
}

func doCreate(
Expand Down
9 changes: 9 additions & 0 deletions test/server/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@ func TestAccServerResource(t *testing.T) {
"name",
"16.182.105.217",
),
resource.TestCheckResourceAttrSet(
"hpegl_pc_server.test",
"name",
),
checkUUIDAttr("hpegl_pc_server.test", "id"),
checkUUIDAttr("hpegl_pc_server.test", "system_id"),
checkUUIDAttr("hpegl_pc_server.test", "hypervisor_cluster_id"),
Expand Down Expand Up @@ -113,6 +117,11 @@ func TestAccServerResource(t *testing.T) {
"ilo_admin_credential_id",
"dddfcad1-85b7-4162-b16e-f7cadc2c46b5",
),
resource.TestCheckResourceAttr(
"hpegl_pc_server.test",
"name",
"16.182.105.217",
),
)
}

Expand Down

0 comments on commit 65f9715

Please sign in to comment.