Skip to content

Commit

Permalink
fix: Reduce complexity of resourceNetboxVirtualizationVMRead
Browse files Browse the repository at this point in the history
  • Loading branch information
amhn committed Nov 18, 2022
1 parent 47c6ddd commit c3456ba
Show file tree
Hide file tree
Showing 3 changed files with 146 additions and 143 deletions.
37 changes: 37 additions & 0 deletions netbox/internal/util/nested.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package util

import (
"github.com/smutel/go-netbox/v3/netbox/models"
)

func GetNestedIPAddressAddress(nested *models.NestedIPAddress) *string {
if nested == nil {
return nil
}

return nested.Address
}

func GetNestedPlatformID(nested *models.NestedPlatform) *int64 {
if nested == nil {
return nil
}

return &nested.ID
}

func GetNestedTenantID(nested *models.NestedTenant) *int64 {
if nested == nil {
return nil
}

return &nested.ID
}

func GetNestedRoleID(nested *models.NestedDeviceRole) *int64 {
if nested == nil {
return nil
}

return &nested.ID
}
43 changes: 33 additions & 10 deletions netbox/internal/util/util.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,28 @@
package util

import (
"encoding/json"
"fmt"
"regexp"
"strings"

"github.com/go-openapi/strfmt"
)

// Convert URL in content_type
func ConvertURIContentType(uri strfmt.URI) string {
uriSplit := strings.Split(uri.String(), "/")
firstLevel := uriSplit[len(uriSplit)-4]
secondLevel := uriSplit[len(uriSplit)-3]

slRegexp := regexp.MustCompile(`s$`)
secondLevel = strings.ReplaceAll(secondLevel, "-", "")
secondLevel = slRegexp.ReplaceAllString(secondLevel, "")

contentType := firstLevel + "." + secondLevel
return contentType
}

func ExpandToInt64Slice(v []interface{}) []int64 {
s := make([]int64, len(v))
for i, val := range v {
Expand All @@ -18,16 +34,23 @@ func ExpandToInt64Slice(v []interface{}) []int64 {
return s
}

// Convert URL in content_type
func ConvertURIContentType(uri strfmt.URI) string {
uriSplit := strings.Split(uri.String(), "/")
firstLevel := uriSplit[len(uriSplit)-4]
secondLevel := uriSplit[len(uriSplit)-3]
func Float2stringptr(vcpus *float64) *string {
if vcpus == nil {
return nil
}

slRegexp := regexp.MustCompile(`s$`)
secondLevel = strings.ReplaceAll(secondLevel, "-", "")
secondLevel = slRegexp.ReplaceAllString(secondLevel, "")
vcpustr := fmt.Sprintf("%v", *vcpus)
return &vcpustr
}

contentType := firstLevel + "." + secondLevel
return contentType
func GetLocalContextData(data interface{}) (*string, error) {
if data == nil {
return nil, nil
}
localContextDataJSON, err := json.Marshal(data)
if err != nil {
return nil, err
}
jsonstring := string(localContextDataJSON)
return &jsonstring, nil
}
209 changes: 76 additions & 133 deletions netbox/virtualization/resource_netbox_virtualization_vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package virtualization
import (
"context"
"encoding/json"
"fmt"
"regexp"
"strconv"

Expand Down Expand Up @@ -224,141 +223,85 @@ func resourceNetboxVirtualizationVMRead(ctx context.Context, d *schema.ResourceD
return diag.FromErr(err)
}

for _, resource := range resources.Payload.Results {
if strconv.FormatInt(resource.ID, 10) == d.Id() {
if err = d.Set("cluster_id", resource.Cluster.ID); err != nil {
return diag.FromErr(err)
}

var comments interface{}
if resource.Comments == "" {
comments = nil
} else {
comments = resource.Comments
}

if err = d.Set("comments", comments); err != nil {
return diag.FromErr(err)
}

if err = d.Set("content_type", util.ConvertURIContentType(resource.URL)); err != nil {
return diag.FromErr(err)
}

resourceCustomFields := d.Get("custom_field").(*schema.Set).List()
customFields := customfield.UpdateCustomFieldsFromAPI(resourceCustomFields, resource.CustomFields)

if err = d.Set("custom_field", customFields); err != nil {
return diag.FromErr(err)
}

if err = d.Set("disk", resource.Disk); err != nil {
return diag.FromErr(err)
}

if resource.LocalContextData != nil {
localContextDataJSON, err := json.Marshal(resource.LocalContextData)
if err != nil {
return diag.FromErr(err)
}
if err = d.Set("local_context_data",
string(localContextDataJSON)); err != nil {
return diag.FromErr(err)
}
} else {
d.Set("local_context_data", "")
}

if err = d.Set("memory", resource.Memory); err != nil {
return diag.FromErr(err)
}

if err = d.Set("name", resource.Name); err != nil {
return diag.FromErr(err)
}

if resource.Platform == nil {
if err = d.Set("platform_id", 0); err != nil {
return diag.FromErr(err)
}
} else {
if err = d.Set("platform_id", resource.Platform.ID); err != nil {
return diag.FromErr(err)
}
}

if resource.PrimaryIP == nil {
if err = d.Set("primary_ip", nil); err != nil {
return diag.FromErr(err)
}
} else {
if err = d.Set("primary_ip", resource.PrimaryIP.Address); err != nil {
return diag.FromErr(err)
}
}
if resource.PrimaryIp4 == nil {
if err = d.Set("primary_ip4", nil); err != nil {
return diag.FromErr(err)
}
} else {
if err = d.Set("primary_ip4", resource.PrimaryIp4.Address); err != nil {
return diag.FromErr(err)
}
}
if resource.PrimaryIp6 == nil {
if err = d.Set("primary_ip6", nil); err != nil {
return diag.FromErr(err)
}
} else {
if err = d.Set("primary_ip6", resource.PrimaryIp6.Address); err != nil {
return diag.FromErr(err)
}
}

if resource.Role == nil {
if err = d.Set("role_id", 0); err != nil {
return diag.FromErr(err)
}
} else {
if err = d.Set("role_id", resource.Role.ID); err != nil {
return diag.FromErr(err)
}
}

if err = d.Set("status", resource.Status.Value); err != nil {
return diag.FromErr(err)
}

if err = d.Set("tag", tag.ConvertNestedTagsToTags(resource.Tags)); err != nil {
return diag.FromErr(err)
}

if resource.Tenant == nil {
if err = d.Set("tenant_id", 0); err != nil {
return diag.FromErr(err)
}
} else {
if err = d.Set("tenant_id", resource.Tenant.ID); err != nil {
return diag.FromErr(err)
}
}

var vcpus string
if resource.Vcpus == nil {
vcpus = ""
} else {
vcpus = fmt.Sprintf("%v", *resource.Vcpus)
}
if err = d.Set("vcpus", vcpus); err != nil {
return diag.FromErr(err)
}

return nil
}
if len(resources.Payload.Results) != 1 {
d.SetId("")
return nil
}

d.SetId("")
resource := resources.Payload.Results[0]
if err = d.Set("cluster_id", resource.Cluster.ID); err != nil {
return diag.FromErr(err)
}

if err = d.Set("comments", resource.Comments); err != nil {
return diag.FromErr(err)
}

if err = d.Set("content_type", util.ConvertURIContentType(resource.URL)); err != nil {
return diag.FromErr(err)
}

resourceCustomFields := d.Get("custom_field").(*schema.Set).List()
customFields := customfield.UpdateCustomFieldsFromAPI(resourceCustomFields, resource.CustomFields)

if err = d.Set("custom_field", customFields); err != nil {
return diag.FromErr(err)
}

if err = d.Set("disk", resource.Disk); err != nil {
return diag.FromErr(err)
}

localContextDataJSON, err := util.GetLocalContextData(resource.LocalContextData)
if err != nil {
return diag.FromErr(err)
}
if err = d.Set("local_context_data", localContextDataJSON); err != nil {
return diag.FromErr(err)
}

if err = d.Set("memory", resource.Memory); err != nil {
return diag.FromErr(err)
}

if err = d.Set("name", resource.Name); err != nil {
return diag.FromErr(err)
}

if err = d.Set("platform_id", util.GetNestedPlatformID(resource.Platform)); err != nil {
return diag.FromErr(err)
}

if err = d.Set("primary_ip", util.GetNestedIPAddressAddress(resource.PrimaryIP)); err != nil {
return diag.FromErr(err)
}
if err = d.Set("primary_ip4", util.GetNestedIPAddressAddress(resource.PrimaryIp4)); err != nil {
return diag.FromErr(err)
}
if err = d.Set("primary_ip6", util.GetNestedIPAddressAddress(resource.PrimaryIp6)); err != nil {
return diag.FromErr(err)
}
if err = d.Set("role_id", util.GetNestedRoleID(resource.Role)); err != nil {
return diag.FromErr(err)
}

if err = d.Set("status", resource.Status.Value); err != nil {
return diag.FromErr(err)
}

if err = d.Set("tag", tag.ConvertNestedTagsToTags(resource.Tags)); err != nil {
return diag.FromErr(err)
}

if err = d.Set("tenant_id", util.GetNestedTenantID(resource.Tenant)); err != nil {
return diag.FromErr(err)
}

if err = d.Set("vcpus", util.Float2stringptr(resource.Vcpus)); err != nil {
return diag.FromErr(err)
}
return nil

}

func resourceNetboxVirtualizationVMUpdate(ctx context.Context, d *schema.ResourceData,
Expand Down

0 comments on commit c3456ba

Please sign in to comment.