Skip to content

Commit

Permalink
fix: check return value of d.Set
Browse files Browse the repository at this point in the history
Resolves all instances of "return value of `d.Set` is not checked" from `errcheck`.

Signed-off-by: Ryan Johnson <[email protected]>
  • Loading branch information
tenthirtyam committed Oct 25, 2024
1 parent dd7cc3d commit 39a985b
Show file tree
Hide file tree
Showing 8 changed files with 304 additions and 111 deletions.
8 changes: 6 additions & 2 deletions vmc/data_source_vmc_customer_subnets.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,12 @@ func dataSourceVmcCustomerSubnetsRead(d *schema.ResourceData, m interface{}) err
return HandleDataSourceReadError("Customer Subnets", err)
}

d.Set("ids", ids)
d.Set("customer_available_zones", compatibleSubnets.CustomerAvailableZones)
if err := d.Set("ids", ids); err != nil {
return fmt.Errorf("error setting ids: %v", err)
}
if err := d.Set("customer_available_zones", compatibleSubnets.CustomerAvailableZones); err != nil {
return fmt.Errorf("error setting customer_available_zones: %v", err)
}
d.SetId(fmt.Sprintf("%s-%s", orgID, accountID))
return nil
}
10 changes: 8 additions & 2 deletions vmc/data_source_vmc_org.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
package vmc

import (
"fmt"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/vmware/terraform-provider-vmc/vmc/connector"
"github.com/vmware/vsphere-automation-sdk-go/services/vmc"
Expand Down Expand Up @@ -43,8 +45,12 @@ func dataSourceVmcOrgRead(d *schema.ResourceData, m interface{}) error {
return HandleDataSourceReadError("VMC Organization", err)
}
d.SetId(orgID)
d.Set("display_name", org.DisplayName)
d.Set("name", org.Name)
if err := d.Set("display_name", org.DisplayName); err != nil {
return fmt.Errorf("error setting display_name: %v", err)
}
if err := d.Set("name", org.Name); err != nil {
return fmt.Errorf("error setting name: %v", err)
}

return nil
}
128 changes: 92 additions & 36 deletions vmc/data_source_vmc_sddc.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,55 +175,111 @@ func dataSourceVmcSddcRead(d *schema.ResourceData, m interface{}) error {
}

d.SetId(sddc.Id)

d.Set("sddc_name", sddc.Name)
d.Set("updated", sddc.Updated.String())
d.Set("user_id", sddc.UserId)
d.Set("updated_by_user_id", sddc.UpdatedByUserId)
d.Set("created", sddc.Created.String())
d.Set("version", sddc.Version)
d.Set("updated_by_user_name", sddc.UpdatedByUserName)
d.Set("user_name", sddc.UserName)
d.Set("org_id", sddc.OrgId)
d.Set("sddc_type", sddc.SddcType)
// the key "provider" is reserved by the Terraform SDK, however the same information
// is carried by the sddc.ResourceConfig.Provider variable
//d.Set("provider", sddc.Provider)
d.Set("account_link_state", sddc.AccountLinkState)
d.Set("sddc_access_state", sddc.SddcAccessState)
d.Set("sddc_type", sddc.SddcType)
d.Set("sddc_state", sddc.SddcState)
if err := d.Set("sddc_name", sddc.Name); err != nil {
return fmt.Errorf("error setting sddc_name: %v", err)
}
if err := d.Set("updated", sddc.Updated.String()); err != nil {
return fmt.Errorf("error setting updated: %v", err)
}
if err := d.Set("user_id", sddc.UserId); err != nil {
return fmt.Errorf("error setting user_id: %v", err)
}
if err := d.Set("updated_by_user_id", sddc.UpdatedByUserId); err != nil {
return fmt.Errorf("error setting updated_by_user_id: %v", err)
}
if err := d.Set("created", sddc.Created.String()); err != nil {
return fmt.Errorf("error setting created: %v", err)
}
if err := d.Set("version", sddc.Version); err != nil {
return fmt.Errorf("error setting version: %v", err)
}
if err := d.Set("updated_by_user_name", sddc.UpdatedByUserName); err != nil {
return fmt.Errorf("error setting updated_by_user_name: %v", err)
}
if err := d.Set("user_name", sddc.UserName); err != nil {
return fmt.Errorf("error setting user_name: %v", err)
}
if err := d.Set("org_id", sddc.OrgId); err != nil {
return fmt.Errorf("error setting org_id: %v", err)
}
if err := d.Set("sddc_type", sddc.SddcType); err != nil {
return fmt.Errorf("error setting sddc_type: %v", err)
}
if err := d.Set("account_link_state", sddc.AccountLinkState); err != nil {
return fmt.Errorf("error setting account_link_state: %v", err)
}
if err := d.Set("sddc_access_state", sddc.SddcAccessState); err != nil {
return fmt.Errorf("error setting sddc_access_state: %v", err)
}
if err := d.Set("sddc_state", sddc.SddcState); err != nil {
return fmt.Errorf("error setting sddc_state: %v", err)
}
if sddc.ResourceConfig != nil {
d.Set("vc_url", sddc.ResourceConfig.VcUrl)
d.Set("cloud_username", sddc.ResourceConfig.CloudUsername)
d.Set("nsxt_reverse_proxy_url", sddc.ResourceConfig.NsxApiPublicEndpointUrl)
d.Set("region", sddc.ResourceConfig.Region)
// Query the API for primary Cluster ID so only it's hosts can be added to the
if err := d.Set("vc_url", sddc.ResourceConfig.VcUrl); err != nil {
return fmt.Errorf("error setting vc_url: %v", err)
}
if err := d.Set("cloud_username", sddc.ResourceConfig.CloudUsername); err != nil {
return fmt.Errorf("error setting cloud_username: %v", err)
}
if err := d.Set("nsxt_reverse_proxy_url", sddc.ResourceConfig.NsxApiPublicEndpointUrl); err != nil {
return fmt.Errorf("error setting nsxt_reverse_proxy_url: %v", err)
}
if err := d.Set("region", sddc.ResourceConfig.Region); err != nil {
return fmt.Errorf("error setting region: %v", err)
}

// Query the API for primary Cluster ID so only its hosts can be added to the
// sddc host
primaryClusterClient := sddcs.NewPrimaryclusterClient(connectorWrapper)
primaryCluster, err := primaryClusterClient.Get(orgID, sddcID)
if err != nil {
return HandleReadError(d, "Primary Cluster", sddcID, err)
}
d.Set("num_host", getHostCountCluster(&sddc, primaryCluster.ClusterId))
d.Set("provider_type", sddc.ResourceConfig.Provider)
d.Set("availability_zones", sddc.ResourceConfig.AvailabilityZones)
d.Set("deployment_type", ConvertDeployType(*sddc.ResourceConfig.DeploymentType))
d.Set("sso_domain", *sddc.ResourceConfig.SsoDomain)
d.Set("skip_creating_vxlan", *sddc.ResourceConfig.SkipCreatingVxlan)
d.Set("nsxt_ui", *sddc.ResourceConfig.Nsxt)
if err := d.Set("num_host", getHostCountCluster(&sddc, primaryCluster.ClusterId)); err != nil {
return fmt.Errorf("error setting num_host: %v", err)
}
if err := d.Set("provider_type", sddc.ResourceConfig.Provider); err != nil {
return fmt.Errorf("error setting provider_type: %v", err)
}
if err := d.Set("availability_zones", sddc.ResourceConfig.AvailabilityZones); err != nil {
return fmt.Errorf("error setting availability_zones: %v", err)
}
if err := d.Set("deployment_type", ConvertDeployType(*sddc.ResourceConfig.DeploymentType)); err != nil {
return fmt.Errorf("error setting deployment_type: %v", err)
}
if err := d.Set("sso_domain", *sddc.ResourceConfig.SsoDomain); err != nil {
return fmt.Errorf("error setting sso_domain: %v", err)
}
if err := d.Set("skip_creating_vxlan", *sddc.ResourceConfig.SkipCreatingVxlan); err != nil {
return fmt.Errorf("error setting skip_creating_vxlan: %v", err)
}
if err := d.Set("nsxt_ui", *sddc.ResourceConfig.Nsxt); err != nil {
return fmt.Errorf("error setting nsxt_ui: %v", err)
}
if sddc.ResourceConfig.NsxCloudAdmin != nil {
d.Set("nsxt_cloudadmin", *sddc.ResourceConfig.NsxCloudAdmin)
if err := d.Set("nsxt_cloudadmin", *sddc.ResourceConfig.NsxCloudAdmin); err != nil {
return fmt.Errorf("error setting nsxt_cloudadmin: %v", err)
}
// Evade nil pointer dereference when user's access_token doesn't have NSX roles
if sddc.ResourceConfig.NsxCloudAdminPassword != nil {
_ = d.Set("nsxt_cloudadmin_password", *sddc.ResourceConfig.NsxCloudAdminPassword)
if err := d.Set("nsxt_cloudadmin_password", *sddc.ResourceConfig.NsxCloudAdminPassword); err != nil {
return fmt.Errorf("error setting nsxt_cloudadmin_password: %v", err)
}
}
if sddc.ResourceConfig.NsxCloudAuditPassword != nil {
_ = d.Set("nsxt_cloudaudit_password", *sddc.ResourceConfig.NsxCloudAuditPassword)
if err := d.Set("nsxt_cloudaudit_password", *sddc.ResourceConfig.NsxCloudAuditPassword); err != nil {
return fmt.Errorf("error setting nsxt_cloudaudit_password: %v", err)
}
}
if err := d.Set("nsxt_cloudaudit", *sddc.ResourceConfig.NsxCloudAudit); err != nil {
return fmt.Errorf("error setting nsxt_cloudaudit: %v", err)
}
if err := d.Set("nsxt_private_ip", *sddc.ResourceConfig.NsxMgrManagementIp); err != nil {
return fmt.Errorf("error setting nsxt_private_ip: %v", err)
}
if err := d.Set("nsxt_private_url", *sddc.ResourceConfig.NsxMgrLoginUrl); err != nil {
return fmt.Errorf("error setting nsxt_private_url: %v", err)
}
d.Set("nsxt_cloudaudit", *sddc.ResourceConfig.NsxCloudAudit)
d.Set("nsxt_private_ip", *sddc.ResourceConfig.NsxMgrManagementIp)
d.Set("nsxt_private_url", *sddc.ResourceConfig.NsxMgrLoginUrl)
}
}

Expand Down
31 changes: 24 additions & 7 deletions vmc/resource_vmc_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,10 @@ func resourceCluster() *schema.Resource {
}

d.SetId(idParts[0])
d.Set("sddc_id", idParts[1])
if err := d.Set("sddc_id", idParts[1]); err != nil {
return nil, fmt.Errorf("error setting sddc_id: %v", err)
}

return []*schema.ResourceData{d}, nil
},
},
Expand Down Expand Up @@ -244,8 +247,13 @@ func resourceClusterRead(d *schema.ResourceData, m interface{}) error {
cluster["windows_licensing"] = *clusterConfig.MsftLicenseConfig.WindowsLicensing
}
}
d.Set("cluster_info", cluster)
d.Set("num_hosts", len(clusterConfig.EsxHostList))
if err := d.Set("cluster_info", cluster); err != nil {
return fmt.Errorf("error setting cluster_info: %v", err)
}
if err := d.Set("num_hosts", len(clusterConfig.EsxHostList)); err != nil {
return fmt.Errorf("error setting num_hosts: %v", err)
}

break
}
}
Expand All @@ -255,10 +263,19 @@ func resourceClusterRead(d *schema.ResourceData, m interface{}) error {
if err != nil {
return HandleReadError(d, "Cluster", clusterID, err)
}
d.Set("edrs_policy_type", *edrsPolicy.PolicyType)
d.Set("enable_edrs", edrsPolicy.EnableEdrs)
d.Set("max_hosts", *edrsPolicy.MaxHosts)
d.Set("min_hosts", *edrsPolicy.MinHosts)
if err := d.Set("edrs_policy_type", *edrsPolicy.PolicyType); err != nil {
return fmt.Errorf("error setting edrs_policy_type: %v", err)
}
if err := d.Set("enable_edrs", edrsPolicy.EnableEdrs); err != nil {
return fmt.Errorf("error setting enable_edrs: %v", err)
}
if err := d.Set("max_hosts", *edrsPolicy.MaxHosts); err != nil {
return fmt.Errorf("error setting max_hosts: %v", err)
}
if err := d.Set("min_hosts", *edrsPolicy.MinHosts); err != nil {
return fmt.Errorf("error setting min_hosts: %v", err)
}

return nil
}

Expand Down
26 changes: 20 additions & 6 deletions vmc/resource_vmc_public_ip.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,10 @@ func resourcePublicIP() *schema.Resource {
return nil, fmt.Errorf("invalid format for nsxt_reverse_proxy_url : %v", err)
}
d.SetId(idParts[0])
d.Set("nsxt_reverse_proxy_url", idParts[1])
if err := d.Set("nsxt_reverse_proxy_url", idParts[1]); err != nil {
return nil, fmt.Errorf("error setting nsxt_reverse_proxy_url: %v", err)
}

return []*schema.ResourceData{d}, nil
},
},
Expand Down Expand Up @@ -108,8 +111,12 @@ func resourcePublicIPRead(d *schema.ResourceData, m interface{}) error {
if err != nil {
return HandleReadError(d, "Public IP", uuid, err)
}
d.Set("ip", publicIP.Ip)
d.Set("display_name", publicIP.DisplayName)
if err := d.Set("ip", publicIP.Ip); err != nil {
return fmt.Errorf("error setting ip: %v", err)
}
if err := d.Set("display_name", publicIP.DisplayName); err != nil {
return fmt.Errorf("error setting display_name: %v", err)
}
} else {
displayName := d.Get("display_name").(string)
if len(displayName) > 0 {
Expand All @@ -121,8 +128,13 @@ func resourcePublicIPRead(d *schema.ResourceData, m interface{}) error {
publicIpsList := publicIPResultList.Results
for _, publicIP := range publicIpsList {
if displayName == *publicIP.DisplayName {
d.Set("ip", publicIP.Ip)
d.Set("display_name", publicIP.DisplayName)
if err := d.Set("ip", publicIP.Ip); err != nil {
return fmt.Errorf("error setting ip: %v", err)
}

if err := d.Set("display_name", publicIP.DisplayName); err != nil {
return fmt.Errorf("error setting display_name: %v", err)
}
break
}
}
Expand Down Expand Up @@ -156,7 +168,9 @@ func resourcePublicIPUpdate(d *schema.ResourceData, m interface{}) error {
return HandleUpdateError("Public IP", err)
}

d.Set("display_name", publicIP.DisplayName)
if err := d.Set("display_name", publicIP.DisplayName); err != nil {
return fmt.Errorf("error setting display_name: %v", err)
}
}

return resourcePublicIPRead(d, m)
Expand Down
Loading

0 comments on commit 39a985b

Please sign in to comment.