Skip to content

Commit

Permalink
refactor: use constants
Browse files Browse the repository at this point in the history
Applies and update to use constants to avoid repetition.

Signed-off-by: Ryan Johnson <[email protected]>
  • Loading branch information
tenthirtyam committed Jan 31, 2025
1 parent 920032f commit 8f9a2f5
Show file tree
Hide file tree
Showing 10 changed files with 84 additions and 40 deletions.
34 changes: 34 additions & 0 deletions hcx/constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

package constants

import "time"

const (
// HCX Cloud URLs
HcxBaseURL = "https://connect.hcx.vmware.com"
Expand All @@ -18,6 +20,38 @@ const (
// Network Types
NetworkTypeDvpg = "DistributedVirtualPortgroup"
NetworkTypeNsxSegment = "NsxtSegment"

// VMC
VmcMaxRetries = 12
VmcRetryInterval = 10 * time.Second
VmcActivationActiveStatus = "ACTIVE"
VmcActivationFailedStatus = "ACTIVATION_FAILED"
VmcDeactivationInactiveStatus = "DE-ACTIVATED"
VmcDeactivationFailedStatus = "DEACTIVATION_FAILED"

// Status
StoppedStatus = "STOPPED"
RunningStatus = "RUNNING"
FailedStatus = "FAILED"
SuccessStatus = "SUCCESS"
RealizedStatus = "REALIZED"

// Network Profile
DefaultNetworkProfileOrg = "DEFAULT"

// Location
DefaultLatitude = 0
DefaultLongitude = 0

// Compute Profile
DefaultComputeType = "VC"

// Single Sign-On
DefaultSsoProviderType = "PSC"

// Role Mappings
RoleSystemAdmin = "System Administrator"
RoleEnterpriseAdmin = "Enterprise Administrator"
)

var AllowedNetworkTypes = []string{
Expand Down
24 changes: 13 additions & 11 deletions hcx/resource_compute_profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (
"errors"
"time"

"github.com/vmware/terraform-provider-hcx/hcx/constants"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
Expand Down Expand Up @@ -185,7 +187,7 @@ func resourceComputeProfileCreate(ctx context.Context, d *schema.ResourceData, m
ID: managementNetworkID,
Tags: []string{"management"},
Status: Status{
State: "REALIZED",
State: constants.RealizedStatus,
},
}
networksList = append(networksList, netTmp)
Expand All @@ -207,7 +209,7 @@ func resourceComputeProfileCreate(ctx context.Context, d *schema.ResourceData, m
ID: replicationNetworkID,
Tags: []string{"replication"},
Status: Status{
State: "REALIZED",
State: constants.RealizedStatus,
},
}
networksList = append(networksList, netTmp)
Expand All @@ -230,7 +232,7 @@ func resourceComputeProfileCreate(ctx context.Context, d *schema.ResourceData, m
ID: uplinkNetworkID,
Tags: []string{"uplink"},
Status: Status{
State: "REALIZED",
State: constants.RealizedStatus,
},
}
networksList = append(networksList, netTmp)
Expand All @@ -253,7 +255,7 @@ func resourceComputeProfileCreate(ctx context.Context, d *schema.ResourceData, m
ID: vmotionNetworkID,
Tags: []string{"vmotion"},
Status: Status{
State: "REALIZED",
State: constants.RealizedStatus,
},
}
networksList = append(networksList, netTmp)
Expand All @@ -264,7 +266,7 @@ func resourceComputeProfileCreate(ctx context.Context, d *schema.ResourceData, m
Services: servicesFromSchema,
Computes: []Compute{{
ComputeID: res.EntityID,
ComputeType: "VC",
ComputeType: constants.DefaultComputeType,
ComputeName: res.Name,
ID: res.Children[0].EntityID,
Name: res.Children[0].Name,
Expand All @@ -273,7 +275,7 @@ func resourceComputeProfileCreate(ctx context.Context, d *schema.ResourceData, m
DeploymentContainers: DeploymentContainer{
Computes: []Compute{{
ComputeID: res.EntityID,
ComputeType: "VC",
ComputeType: constants.DefaultComputeType,
ComputeName: res.Name,
ID: clusterID,
Name: clusterName,
Expand All @@ -282,7 +284,7 @@ func resourceComputeProfileCreate(ctx context.Context, d *schema.ResourceData, m
},
Storage: []Storage{{
ComputeID: res.EntityID,
ComputeType: "VC",
ComputeType: constants.DefaultComputeType,
ComputeName: res.Name,
ID: datastoreFromAPI.ID,
Name: datastoreFromAPI.Name,
Expand Down Expand Up @@ -316,11 +318,11 @@ func resourceComputeProfileCreate(ctx context.Context, d *schema.ResourceData, m
return diag.FromErr(err)
}

if jr.Status == "SUCCESS" {
if jr.Status == constants.SuccessStatus {
break
}

if jr.Status == "FAILED" {
if jr.Status == constants.FailedStatus {
return diag.FromErr(errors.New("task failed"))
}

Expand Down Expand Up @@ -365,11 +367,11 @@ func resourceComputeProfileDelete(ctx context.Context, d *schema.ResourceData, m
return diag.FromErr(err)
}

if jr.Status == "SUCCESS" {
if jr.Status == constants.SuccessStatus {
break
}

if jr.Status == "FAILED" {
if jr.Status == constants.FailedStatus {
return diag.FromErr(errors.New("task failed"))
}

Expand Down
5 changes: 3 additions & 2 deletions hcx/resource_location.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package hcx

import (
"context"
"github.com/vmware/terraform-provider-hcx/hcx/constants"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
Expand Down Expand Up @@ -127,8 +128,8 @@ func resourceLocationDelete(ctx context.Context, d *schema.ResourceData, m inter
City: "",
Country: "",
CityASCII: "",
Latitude: 0,
Longitude: 0,
Latitude: constants.DefaultLatitude,
Longitude: constants.DefaultLongitude,
Province: "",
}

Expand Down
2 changes: 1 addition & 1 deletion hcx/resource_network_profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ func resourceNetworkProfileCreate(ctx context.Context, d *schema.ResourceData, m

body := NetworkProfileBody{
Name: name,
Organization: "DEFAULT",
Organization: constants.DefaultNetworkProfileOrg,
MTU: mtu,
Backings: []Backing{{
BackingID: networkiD.EntityID,
Expand Down
9 changes: 5 additions & 4 deletions hcx/resource_rolemapping.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package hcx

import (
"context"
"github.com/vmware/terraform-provider-hcx/hcx/constants"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
Expand Down Expand Up @@ -94,11 +95,11 @@ func resourceRoleMappingUpdate(ctx context.Context, d *schema.ResourceData, m in

body := []RoleMapping{
{
Role: "System Administrator",
Role: constants.RoleSystemAdmin,
UserGroups: adminGroups,
},
{
Role: "Enterprise Administrator",
Role: constants.RoleEnterpriseAdmin,
UserGroups: enterpriseGroups,
},
}
Expand All @@ -124,11 +125,11 @@ func resourceRoleMappingDelete(ctx context.Context, d *schema.ResourceData, m in
client := m.(*Client)
body := []RoleMapping{
{
Role: "System Administrator",
Role: constants.RoleSystemAdmin,
UserGroups: []string{},
},
{
Role: "Enterprise Administrator",
Role: constants.RoleEnterpriseAdmin,
UserGroups: []string{},
},
}
Expand Down
9 changes: 5 additions & 4 deletions hcx/resource_service_mesh.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"context"
"encoding/json"
"errors"
"github.com/vmware/terraform-provider-hcx/hcx/constants"
"time"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
Expand Down Expand Up @@ -202,11 +203,11 @@ func resourceServiceMeshCreate(ctx context.Context, d *schema.ResourceData, m in
return diag.FromErr(err)
}

if jr.Status == "SUCCESS" {
if jr.Status == constants.SuccessStatus {
break
}

if jr.Status == "FAILED" {
if jr.Status == constants.FailedStatus {
return diag.FromErr(errors.New("task failed"))
}

Expand Down Expand Up @@ -266,11 +267,11 @@ func resourceServiceMeshDelete(ctx context.Context, d *schema.ResourceData, m in
return diag.FromErr(err)
}

if jr.Status == "SUCCESS" {
if jr.Status == constants.SuccessStatus {
break
}

if jr.Status == "FAILED" {
if jr.Status == constants.FailedStatus {
return diag.FromErr(errors.New("task failed"))
}

Expand Down
4 changes: 2 additions & 2 deletions hcx/resource_site_pairing.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ func resourceSitePairingCreate(ctx context.Context, d *schema.ResourceData, m in
}

if jr.DidFail {
return diag.Errorf("Site pairing Job did failed")
return diag.Errorf("site pairing job failed")
}
time.Sleep(10 * time.Second)
count = count + 1
Expand Down Expand Up @@ -181,7 +181,7 @@ func resourceSitePairingCreate(ctx context.Context, d *schema.ResourceData, m in
}

if jr.DidFail {
return diag.Errorf("Site pairing Job did failed")
return diag.Errorf("site pairing job failed")
}
time.Sleep(10 * time.Second)
count = count + 1
Expand Down
9 changes: 5 additions & 4 deletions hcx/resource_sso.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package hcx

import (
"context"
"github.com/vmware/terraform-provider-hcx/hcx/constants"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
Expand Down Expand Up @@ -47,7 +48,7 @@ func resourceSSOCreate(ctx context.Context, d *schema.ResourceData, m interface{
{
Config: InsertSSODataItemConfig{
LookupServiceURL: url,
ProviderType: "PSC",
ProviderType: constants.DefaultSsoProviderType,
},
},
},
Expand All @@ -61,7 +62,7 @@ func resourceSSOCreate(ctx context.Context, d *schema.ResourceData, m interface{
}

if len(res.InsertSSOData.Items) == 0 {
// No SSO config found
// No SSO configuration found.
res, err := InsertSSO(client, body)

if err != nil {
Expand All @@ -72,7 +73,7 @@ func resourceSSOCreate(ctx context.Context, d *schema.ResourceData, m interface{
return resourceSSORead(ctx, d, m)
}

// Update existing SSO
// Update existing SSO configuration.
d.SetId(res.InsertSSOData.Items[0].Config.UUID)
return resourceSSOUpdate(ctx, d, m)

Expand All @@ -99,7 +100,7 @@ func resourceSSOUpdate(ctx context.Context, d *schema.ResourceData, m interface{
Config: InsertSSODataItemConfig{
LookupServiceURL: url,
UUID: d.Id(),
ProviderType: "PSC",
ProviderType: constants.DefaultSsoProviderType,
},
},
},
Expand Down
6 changes: 4 additions & 2 deletions hcx/resource_vcenter.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (

b64 "encoding/base64"

"github.com/vmware/terraform-provider-hcx/hcx/constants"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
Expand Down Expand Up @@ -86,7 +88,7 @@ func resourcevCenterCreate(ctx context.Context, d *schema.ResourceData, m interf
return diag.FromErr(err)
}

if jr.Result == "STOPPED" {
if jr.Result == constants.StoppedStatus {
break
}
time.Sleep(5 * time.Second)
Expand All @@ -104,7 +106,7 @@ func resourcevCenterCreate(ctx context.Context, d *schema.ResourceData, m interf
return diag.FromErr(err)
}

if jr.Result == "RUNNING" {
if jr.Result == constants.RunningStatus {
break
}
time.Sleep(5 * time.Second)
Expand Down
Loading

0 comments on commit 8f9a2f5

Please sign in to comment.