Skip to content

Commit

Permalink
refactor: move to hcx package
Browse files Browse the repository at this point in the history
Moves all resources and data sources from the `main` package to the `hcx` package.

Signed-off-by: Ryan Johnson <[email protected]>
  • Loading branch information
tenthirtyam committed Jan 29, 2025
1 parent 2d54c11 commit 5ac1cf6
Show file tree
Hide file tree
Showing 15 changed files with 217 additions and 218 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@
// The term "Broadcom" refers to Broadcom Inc. and/or its subsidiaries.
// SPDX-License-Identifier: MPL-2.0

package main
package hcx

import (
"context"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/vmware/terraform-provider-hcx/hcx"
)

// dataSourceComputeProfile defines the data source schema to retrieve information about a compute profile.
Expand All @@ -36,16 +35,16 @@ func dataSourceComputeProfile() *schema.Resource {
func dataSourceComputeProfileRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
var diags diag.Diagnostics

client := m.(*hcx.Client)
client := m.(*Client)

res, err := hcx.GetLocalCloudList(client)
res, err := GetLocalCloudList(client)
if err != nil {
return diag.FromErr(err)
}

network := d.Get("name").(string)

cp, err := hcx.GetComputeProfile(client, res.Data.Items[0].EndpointID, network)
cp, err := GetComputeProfile(client, res.Data.Items[0].EndpointID, network)

if err != nil {
return diag.FromErr(err)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@
// The term "Broadcom" refers to Broadcom Inc. and/or its subsidiaries.
// SPDX-License-Identifier: MPL-2.0

package main
package hcx

import (
"context"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/vmware/terraform-provider-hcx/hcx"
)

// dataSourceNetworkBacking defines a data source schema to retrieve information about a network backing.
Expand Down Expand Up @@ -47,13 +46,13 @@ func dataSourceNetworkBacking() *schema.Resource {
func dataSourceNetworkBackingRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
var diags diag.Diagnostics

client := m.(*hcx.Client)
client := m.(*Client)

network := d.Get("name").(string)
vcUUID := d.Get("vcuuid").(string)
networkType := d.Get("network_type").(string)

res, err := hcx.GetNetworkBacking(client, vcUUID, network, networkType)
res, err := GetNetworkBacking(client, vcUUID, network, networkType)

if err != nil {
return diag.FromErr(err)
Expand Down
7 changes: 3 additions & 4 deletions provider.go → hcx/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,17 @@
// The term "Broadcom" refers to Broadcom Inc. and/or its subsidiaries.
// SPDX-License-Identifier: MPL-2.0

package main
package hcx

import (
"context"

"github.com/hashicorp/go-cty/cty"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
hcx "github.com/vmware/terraform-provider-hcx/hcx"
)

// Provider -
// Provider returns the schema.Provider object configured with resources, data sources, and schema for the HCX provider.
func Provider() *schema.Provider {
return &schema.Provider{
Schema: map[string]*schema.Schema{
Expand Down Expand Up @@ -95,7 +94,7 @@ func providerConfigure(ctx context.Context, d *schema.ResourceData) (interface{}
allowUnverifiedSSL := d.Get("allow_unverified_ssl").(bool)
vmcToken := d.Get("vmc_token").(string)

c, err := hcx.NewClient(&hcxURL, &username, &password, &adminUsername, &adminPassword, &allowUnverifiedSSL, &vmcToken)
c, err := NewClient(&hcxURL, &username, &password, &adminUsername, &adminPassword, &allowUnverifiedSSL, &vmcToken)

if err != nil {
return nil, diag.FromErr(err)
Expand Down
21 changes: 10 additions & 11 deletions resource_activation.go → hcx/resource_activation.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@
// The term "Broadcom" refers to Broadcom Inc. and/or its subsidiaries.
// SPDX-License-Identifier: MPL-2.0

package main
package hcx

import (
"context"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/vmware/terraform-provider-hcx/hcx"
)

// resourceActivation defines the resource schema for managing activation configurations.
Expand Down Expand Up @@ -39,16 +38,16 @@ func resourceActivation() *schema.Resource {
// resourceActivationCreate creates the activation configuration resource.
func resourceActivationCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {

client := m.(*hcx.Client)
client := m.(*Client)

url := d.Get("url").(string)
activationkey := d.Get("activationkey").(string)

body := hcx.ActivateBody{
Data: hcx.ActivateData{
Items: []hcx.ActivateDataItem{
body := ActivateBody{
Data: ActivateData{
Items: []ActivateDataItem{
{
Config: hcx.ActivateDataItemConfig{
Config: ActivateDataItemConfig{
URL: url,
ActivationKey: activationkey,
},
Expand All @@ -58,14 +57,14 @@ func resourceActivationCreate(ctx context.Context, d *schema.ResourceData, m int
}

// First, check if already activated
res, err := hcx.GetActivate(client)
res, err := GetActivate(client)
if err != nil {
return diag.FromErr(err)
}

if len(res.Data.Items) == 0 {
// No activation config found
_, err := hcx.PostActivate(client, body)
_, err := PostActivate(client, body)

if err != nil {
return diag.FromErr(err)
Expand All @@ -83,9 +82,9 @@ func resourceActivationCreate(ctx context.Context, d *schema.ResourceData, m int
func resourceActivationRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
var diags diag.Diagnostics

client := m.(*hcx.Client)
client := m.(*Client)

res, err := hcx.GetActivate(client)
res, err := GetActivate(client)
if err != nil {
return diag.FromErr(err)
}
Expand Down
63 changes: 31 additions & 32 deletions resource_compute_profile.go → hcx/resource_compute_profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// The term "Broadcom" refers to Broadcom Inc. and/or its subsidiaries.
// SPDX-License-Identifier: MPL-2.0

package main
package hcx

import (
"bytes"
Expand All @@ -13,7 +13,6 @@ import (

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/vmware/terraform-provider-hcx/hcx"
)

// resourceComputeProfile defines the resource schema for managing compute profile configuration.
Expand Down Expand Up @@ -94,12 +93,12 @@ func resourceComputeProfile() *schema.Resource {
// resourceComputeProfileCreate creates the compute profile configuration.
func resourceComputeProfileCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {

client := m.(*hcx.Client)
client := m.(*Client)

name := d.Get("name").(string)
cluster := d.Get("cluster").(string)

res, err := hcx.GetVcInventory(client)
res, err := GetVcInventory(client)
if err != nil {
return diag.FromErr(err)
}
Expand All @@ -121,26 +120,26 @@ func resourceComputeProfileCreate(ctx context.Context, d *schema.ResourceData, m

// Get Datastore info
datastore := d.Get("datastore").(string)
datastoreFromAPI, err := hcx.GetVcDatastore(client, datastore, res.EntityID, clusterID)
datastoreFromAPI, err := GetVcDatastore(client, datastore, res.EntityID, clusterID)
if err != nil {
return diag.FromErr(err)
}

// Get DVS info
dvs := d.Get("dvs").(string)
dvsFromAPI, err := hcx.GetVcDvs(client, dvs, res.EntityID, clusterID)
dvsFromAPI, err := GetVcDvs(client, dvs, res.EntityID, clusterID)
if err != nil {
return diag.FromErr(err)
}

// Get Services from schema
services := d.Get("service").([]interface{})
servicesFromSchema := []hcx.Service{}
servicesFromSchema := []Service{}
for _, j := range services {
s := j.(map[string]interface{})
name := s["name"].(string)

sTmp := hcx.Service{
sTmp := Service{
Name: name,
}
servicesFromSchema = append(servicesFromSchema, sTmp)
Expand All @@ -152,40 +151,40 @@ func resourceComputeProfileCreate(ctx context.Context, d *schema.ResourceData, m
uplinkNetwork := d.Get("uplink_network").(string)
vmotionNetwork := d.Get("vmotion_network").(string)

networksList := []hcx.Network{}
np, err := hcx.GetNetworkProfileByID(client, managementNetwork)
networksList := []Network{}
np, err := GetNetworkProfileByID(client, managementNetwork)
if err != nil {
return diag.FromErr(err)
}
managementNetworkName := np.Name
managementNetworkID := np.ObjectID

np, err = hcx.GetNetworkProfileByID(client, replicationNetwork)
np, err = GetNetworkProfileByID(client, replicationNetwork)
if err != nil {
return diag.FromErr(err)
}
replicationNetworkName := np.Name
replicationNetworkID := np.ObjectID

np, err = hcx.GetNetworkProfileByID(client, uplinkNetwork)
np, err = GetNetworkProfileByID(client, uplinkNetwork)
if err != nil {
return diag.FromErr(err)
}
uplinkNetworkName := np.Name
uplinkNetworkID := np.ObjectID

np, err = hcx.GetNetworkProfileByID(client, vmotionNetwork)
np, err = GetNetworkProfileByID(client, vmotionNetwork)
if err != nil {
return diag.FromErr(err)
}
vmotionNetworkName := np.Name
vmotionNetworkID := np.ObjectID

netTmp := hcx.Network{
netTmp := Network{
Name: managementNetworkName,
ID: managementNetworkID,
Tags: []string{"management"},
Status: hcx.Status{
Status: Status{
State: "REALIZED",
},
}
Expand All @@ -203,11 +202,11 @@ func resourceComputeProfileCreate(ctx context.Context, d *schema.ResourceData, m
if found {
networksList[index].Tags = append(networksList[index].Tags, "replication")
} else {
netTmp := hcx.Network{
netTmp := Network{
Name: replicationNetworkName,
ID: replicationNetworkID,
Tags: []string{"replication"},
Status: hcx.Status{
Status: Status{
State: "REALIZED",
},
}
Expand All @@ -226,11 +225,11 @@ func resourceComputeProfileCreate(ctx context.Context, d *schema.ResourceData, m
if found {
networksList[index].Tags = append(networksList[index].Tags, "uplink")
} else {
netTmp := hcx.Network{
netTmp := Network{
Name: uplinkNetworkName,
ID: uplinkNetworkID,
Tags: []string{"uplink"},
Status: hcx.Status{
Status: Status{
State: "REALIZED",
},
}
Expand All @@ -249,30 +248,30 @@ func resourceComputeProfileCreate(ctx context.Context, d *schema.ResourceData, m
if found {
networksList[index].Tags = append(networksList[index].Tags, "vmotion")
} else {
netTmp := hcx.Network{
netTmp := Network{
Name: vmotionNetworkName,
ID: vmotionNetworkID,
Tags: []string{"vmotion"},
Status: hcx.Status{
Status: Status{
State: "REALIZED",
},
}
networksList = append(networksList, netTmp)
}

body := hcx.InsertComputeProfileBody{
body := InsertComputeProfileBody{
Name: name,
Services: servicesFromSchema,
Computes: []hcx.Compute{{
Computes: []Compute{{
ComputeID: res.EntityID,
ComputeType: "VC",
ComputeName: res.Name,
ID: res.Children[0].EntityID,
Name: res.Children[0].Name,
Type: res.Children[0].EntityType,
}},
DeploymentContainers: hcx.DeploymentContainer{
Computes: []hcx.Compute{{
DeploymentContainers: DeploymentContainer{
Computes: []Compute{{
ComputeID: res.EntityID,
ComputeType: "VC",
ComputeName: res.Name,
Expand All @@ -281,7 +280,7 @@ func resourceComputeProfileCreate(ctx context.Context, d *schema.ResourceData, m
Type: "ClusterComputeResource",
},
},
Storage: []hcx.Storage{{
Storage: []Storage{{
ComputeID: res.EntityID,
ComputeType: "VC",
ComputeName: res.Name,
Expand All @@ -291,7 +290,7 @@ func resourceComputeProfileCreate(ctx context.Context, d *schema.ResourceData, m
}},
},
Networks: networksList,
Switches: []hcx.Switch{{
Switches: []Switch{{
ComputeID: res.EntityID,
MaxMTU: dvsFromAPI.MaxMTU,
ID: dvsFromAPI.ID,
Expand All @@ -305,14 +304,14 @@ func resourceComputeProfileCreate(ctx context.Context, d *schema.ResourceData, m
return diag.FromErr(err)
}

res2, err := hcx.InsertComputeProfile(client, body)
res2, err := InsertComputeProfile(client, body)
if err != nil {
return diag.FromErr(err)
}

// Wait for task completion
for {
jr, err := hcx.GetTaskResult(client, res2.Data.InterconnectTaskID)
jr, err := GetTaskResult(client, res2.Data.InterconnectTaskID)
if err != nil {
return diag.FromErr(err)
}
Expand Down Expand Up @@ -352,16 +351,16 @@ func resourceComputeProfileUpdate(ctx context.Context, d *schema.ResourceData, m
func resourceComputeProfileDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
var diags diag.Diagnostics

client := m.(*hcx.Client)
client := m.(*Client)

res, err := hcx.DeleteComputeProfile(client, d.Id())
res, err := DeleteComputeProfile(client, d.Id())
if err != nil {
return diag.FromErr(err)
}

// Wait for task completion
for {
jr, err := hcx.GetTaskResult(client, res.Data.InterconnectTaskID)
jr, err := GetTaskResult(client, res.Data.InterconnectTaskID)
if err != nil {
return diag.FromErr(err)
}
Expand Down
Loading

0 comments on commit 5ac1cf6

Please sign in to comment.