Skip to content

Commit

Permalink
Add initial create for hypervisor server
Browse files Browse the repository at this point in the history
Note: internal/resources/server/server_resource_gen.go was
autogenerated.

This is a first step to supporting adding hypervisor servers.

In thise change we:

 - Update the schema model for `server`.
 - Also consume the model in the server `Create` function.

This is not yet complete - in particular the 'server_network'
parameters have yet to be implemented.
  • Loading branch information
stuart-mclaren-hpe committed Nov 29, 2024
1 parent 2e87712 commit 1c774cf
Show file tree
Hide file tree
Showing 7 changed files with 887 additions and 13 deletions.
1 change: 1 addition & 0 deletions internal/constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@ const (
AndFilter = " and "

TaskHypervisorCluster = "hypervisor-cluster" // task's "associatedResources" string
TaskHypervisorServer = "server" // task's "associatedResources" string
TaskDatastore = "datastore" // task's "associatedResources" string
)
90 changes: 85 additions & 5 deletions internal/resources/server/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@ package server
import (
"context"
"fmt"
"path"

"github.com/HewlettPackard/hpegl-pcbe-terraform-resources/internal/async"
"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/diag"
tfpath "github.com/hashicorp/terraform-plugin-framework/path"
Expand Down Expand Up @@ -143,16 +146,93 @@ func doRead(
}

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

if server.GetSerialNumber() == nil {
(*diagsP).AddError(
"error reading server",
"'serial number' is nil",
)

return
}

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

func doCreate(
_ context.Context,
_ client.PCBeClient,
ctx context.Context,
client client.PCBeClient,
dataP *ServerModel,
_ *diag.Diagnostics,
diagsP *diag.Diagnostics,
) {
// For now, just set the ID to a known value
(*dataP).Id = types.StringValue("697e8cbf-df7e-570c-a3c7-912d4ce8375a")
hciClusterUUID := (*dataP).HypervisorClusterId.ValueString()
esxRootCredentialId := (*dataP).EsxRootCredentialId.ValueString()

Check failure on line 169 in internal/resources/server/resource.go

View workflow job for this annotation

GitHub Actions / lint (1.22.3)

var-naming: var esxRootCredentialId should be esxRootCredentialID (revive)
systemId := (*dataP).SystemId.ValueString()

Check failure on line 170 in internal/resources/server/resource.go

View workflow job for this annotation

GitHub Actions / lint (1.22.3)

var-naming: var systemId should be systemID (revive)
iloAdminCredentialId := (*dataP).IloAdminCredentialId.ValueString()

Check failure on line 171 in internal/resources/server/resource.go

View workflow job for this annotation

GitHub Actions / lint (1.22.3)

var-naming: var iloAdminCredentialId should be iloAdminCredentialID (revive)
sysClient, sysHeaderOpts, err := client.NewSysClient(ctx)

if err != nil {
(*diagsP).AddError(
"error adding hypervisor server",
"could not create client: "+err.Error(),
)

return
}
prc := privatecloudbusiness.
V1beta1SystemsItemAddHypervisorServersRequestBuilderPostRequestConfiguration{}
prb := privatecloudbusiness.
NewV1beta1SystemsItemAddHypervisorServersPostRequestBody()
prb.SetEsxRootCredentialId(&esxRootCredentialId)
prb.SetIloAdminCredentialId(&iloAdminCredentialId)
prb.SetHypervisorClusterId(&hciClusterUUID)

_, err = sysClient.PrivateCloudBusiness().
V1beta1().
Systems().ById(systemId).
AddHypervisorServers().
Post(ctx, prb, &prc)
if err != nil {
(*diagsP).AddError(
"error adding hypervisor server",
"unexpected post error: "+err.Error(),
)

return
}

location := sysHeaderOpts.GetResponseHeaders().Get("Location")[0]
sysHeaderOpts.ResponseHeaders.Clear()
operationID := path.Base(location)
asyncOperation := async.New(
ctx,
client,
operationID,
constants.TaskHypervisorServer,
)

err = asyncOperation.Poll()
if err != nil {
(*diagsP).AddError(
"error adding hypervisor server",
"unexpected poll error: "+err.Error(),
)

return
}

uri, err := asyncOperation.GetAssociatedResourceURI()
if err != nil {
(*diagsP).AddError(
"error adding hypervisor server",
"failed to get associated resource uri: "+err.Error(),
)

return
}

serverId := path.Base(uri)

Check failure on line 234 in internal/resources/server/resource.go

View workflow job for this annotation

GitHub Actions / lint (1.22.3)

var-naming: var serverId should be serverID (revive)
(*dataP).Id = types.StringValue(serverId)
}

// TODO: (API) Implement create when server create API is implemented
Expand Down
Loading

0 comments on commit 1c774cf

Please sign in to comment.