Skip to content

Commit

Permalink
feat: Add function to modify API requests
Browse files Browse the repository at this point in the history
  • Loading branch information
amhn authored and smutel committed Oct 22, 2022
1 parent 2c267c2 commit 9ed4488
Show file tree
Hide file tree
Showing 1,615 changed files with 30,571 additions and 4,228 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ require (
github.com/go-openapi/strfmt v0.21.3
github.com/hashicorp/terraform-plugin-docs v0.10.1
github.com/hashicorp/terraform-plugin-sdk/v2 v2.21.0
github.com/smutel/go-netbox/v3 v3.2.2
github.com/smutel/go-netbox/v3 v3.2.3
)

require (
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -286,8 +286,8 @@ github.com/shopspring/decimal v1.3.1/go.mod h1:DKyhrW/HYNuLGql+MJL6WCR6knT2jwCFR
github.com/sirupsen/logrus v1.4.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo=
github.com/sirupsen/logrus v1.4.1/go.mod h1:ni0Sbl8bgC9z8RoU9G6nDWqqs/fq4eDPysMBDgk/93Q=
github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE=
github.com/smutel/go-netbox/v3 v3.2.2 h1:iFHaPBsrd7/j5zL/tM1A02+5bgxM4igk6OwSz5YjvVY=
github.com/smutel/go-netbox/v3 v3.2.2/go.mod h1:JhFZZHyBu2ePquMEakDmlcCnPzHF4/Ji8wq6cwlCtio=
github.com/smutel/go-netbox/v3 v3.2.3 h1:kO8MfXFE+DlKI07miYoqZ+/2Mk3lJbBGsEz2T1sOfIw=
github.com/smutel/go-netbox/v3 v3.2.3/go.mod h1:JhFZZHyBu2ePquMEakDmlcCnPzHF4/Ji8wq6cwlCtio=
github.com/spf13/cast v1.3.1/go.mod h1:Qx5cxh0v+4UWYiBimWS+eyWzqEqokIECu5etghLkUJE=
github.com/spf13/cast v1.5.0 h1:rj3WzYc11XZaIZMPKmwP96zkFEnnAmV8s6XbB2aY32w=
github.com/spf13/cast v1.5.0/go.mod h1:SpXXQ5YoyJw6s3/6cMTQuxvgRl3PCJiyaX9p6b155UU=
Expand Down
101 changes: 28 additions & 73 deletions netbox/resource_netbox_ipam_ip_addresses.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,6 @@ func resourceNetboxIpamIPAddresses() *schema.Resource {
Type: schema.TypeBool,
Optional: true,
Default: false,
ForceNew: true,
Description: "Set this resource as primary IPv4 (false by default).",
},
"role": {
Expand Down Expand Up @@ -204,7 +203,6 @@ func resourceNetboxIpamIPAddressesCreate(ctx context.Context, d *schema.Resource
natInsideID := int64(d.Get("nat_inside_id").(int))
objectID := int64(d.Get("object_id").(int))
objectType := d.Get("object_type").(string)
primaryIP4 := d.Get("primary_ip4").(bool)
role := d.Get("role").(string)
status := d.Get("status").(string)
tags := d.Get("tag").(*schema.Set).List()
Expand All @@ -225,17 +223,6 @@ func resourceNetboxIpamIPAddressesCreate(ctx context.Context, d *schema.Resource
newResource.NatInside = &natInsideID
}

var info InfosForPrimary
if primaryIP4 && objectID != 0 {
if objectType == VMInterfaceType {
var err error
info, err = getInfoForPrimary(m, objectID)
if err != nil {
return diag.FromErr(err)
}
}
}

if objectID != 0 {
newResource.AssignedObjectID = &objectID
newResource.AssignedObjectType = &objectType
Expand Down Expand Up @@ -268,9 +255,15 @@ func resourceNetboxIpamIPAddressesCreate(ctx context.Context, d *schema.Resource
}

d.SetId(strconv.FormatInt(*addressid, 10))
err := updatePrimaryStatus(client, info, *addressid)
if err != nil {
return diag.FromErr(err)
if primaryIP := d.Get("primary_ip4").(bool); primaryIP {
vmID, err := getVMIDForInterface(client, objectID)
if err != nil {
return diag.FromErr(err)
}
err = updatePrimaryStatus(client, vmID, *addressid, primaryIP)
if err != nil {
return diag.FromErr(err)
}
}

return resourceNetboxIpamIPAddressesRead(ctx, d, m)
Expand Down Expand Up @@ -336,36 +329,15 @@ func resourceNetboxIpamIPAddressesRead(ctx context.Context, d *schema.ResourceDa
return diag.FromErr(err)
}

var assignedObjectID *int64
assignedObjectID = nil
if resource.AssignedObjectID != nil {
assignedObjectID = resource.AssignedObjectID

var info InfosForPrimary
if *resource.AssignedObjectID != 0 {
if *resource.AssignedObjectType == VMInterfaceType {
var err error
info, err = getInfoForPrimary(m, *resource.AssignedObjectID)
if err != nil {
return diag.FromErr(err)
}

if info.vmPrimaryIP4ID == resource.ID {
if err = d.Set("primary_ip4", true); err != nil {
return diag.FromErr(err)
}
} else {
if err = d.Set("primary_ip4", false); err != nil {
return diag.FromErr(err)
}
}
}
}
} else if err = d.Set("primary_ip4", false); err != nil {
isPrimary, err := isprimary(m, resource.AssignedObjectID, resource.ID, (*resource.Family.Value == 4))
if err != nil {
return diag.FromErr(err)
}
if err = d.Set("primary_ip4", isPrimary); err != nil {
return diag.FromErr(err)
}

if err = d.Set("object_id", assignedObjectID); err != nil {
if err = d.Set("object_id", resource.AssignedObjectID); err != nil {
return diag.FromErr(err)
}

Expand Down Expand Up @@ -526,36 +498,19 @@ func resourceNetboxIpamIPAddressesUpdate(ctx context.Context, d *schema.Resource
return diag.FromErr(err)
}

/*
* if primary_ip4 || d.HasChange("primary_ip4") {
* var info InfosForPrimary
* objectID := int64(d.Get("object_id").(int))
* objectType := d.Get("object_type").(string)
* isPrimary := d.Get("primary_ip4").(bool)
* if objectID != 0 {
* if objectType == VMInterfaceType {
* var err error
* info, err = getInfoForPrimary(m, objectID)
* if err != nil {
* return diag.FromErr(err)
* }
* }
* }
*
* var ipID int64
* ipID = 0
* if isPrimary {
* ipID, err = strconv.ParseInt(d.Id(), 10, 64)
* if err != nil {
* return diag.Errorf("Unable to convert ID into int64")
* }
* }
* err = updatePrimaryStatus(client, info, ipID)
* if err != nil {
* return diag.FromErr(err)
* }
* }
*/
if (d.HasChange("object_id") && d.Get("primary_ip4").(bool)) ||
(!d.HasChange("object_id") && d.HasChange("primary_ip4")) ||
(d.HasChange("primary_ip4") && d.Get("primary_ip4").(bool)) {
objectID := int64(d.Get("object_id").(int))
vmID, err := getVMIDForInterface(client, objectID)
if err != nil {
return diag.FromErr(err)
}
err = updatePrimaryStatus(client, vmID, resourceID, d.Get("primary_ip4").(bool))
if err != nil {
return diag.FromErr(err)
}
}

return resourceNetboxIpamIPAddressesRead(ctx, d, m)
}
Expand Down
92 changes: 66 additions & 26 deletions netbox/resource_netbox_virtualization_vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"fmt"
"regexp"
"strconv"
"strings"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
Expand All @@ -32,6 +31,7 @@ func resourceNetboxVirtualizationVM() *schema.Resource {
"cluster_id": {
Type: schema.TypeInt,
Required: true,
Default: nil,
Description: "ID of the cluster which host this VM (virtualization module).",
},
"comments": {
Expand Down Expand Up @@ -74,7 +74,7 @@ func resourceNetboxVirtualizationVM() *schema.Resource {
"disk": {
Type: schema.TypeInt,
Optional: true,
Default: 0,
Default: nil,
Description: "The size in GB of the disk for this VM (virtualization module).",
},
"local_context_data": {
Expand All @@ -85,7 +85,7 @@ func resourceNetboxVirtualizationVM() *schema.Resource {
"memory": {
Type: schema.TypeInt,
Optional: true,
Default: 0,
Default: nil,
Description: "The size in MB of the memory of this VM (virtualization module).",
},
"name": {
Expand All @@ -97,13 +97,13 @@ func resourceNetboxVirtualizationVM() *schema.Resource {
"platform_id": {
Type: schema.TypeInt,
Optional: true,
Default: 0,
Default: nil,
Description: "ID of the platform for this VM (virtualization module).",
},
"role_id": {
Type: schema.TypeInt,
Optional: true,
Default: 0,
Default: nil,
Description: "ID of the role for this VM (virtualization module).",
},
"status": {
Expand Down Expand Up @@ -136,11 +136,13 @@ func resourceNetboxVirtualizationVM() *schema.Resource {
"tenant_id": {
Type: schema.TypeInt,
Optional: true,
Default: nil,
Description: "ID of the tenant where this VM (virtualization module) is attached.",
},
"vcpus": {
Type: schema.TypeString,
Optional: true,
Default: nil,
ValidateFunc: validation.StringMatch(
regexp.MustCompile("^[0-9]+((.[0-9]){0,1}[0-9]{0,1})$"),
"Must be like ^[0-9]+((.[0-9]){0,1}[0-9]{0,1})$"),
Expand Down Expand Up @@ -355,19 +357,34 @@ func resourceNetboxVirtualizationVMRead(ctx context.Context, d *schema.ResourceD
func resourceNetboxVirtualizationVMUpdate(ctx context.Context, d *schema.ResourceData,
m interface{}) diag.Diagnostics {
client := m.(*netboxclient.NetBoxAPI)
dropFields := []string{
"created",
"last_updated",
}
emptyFields := make(map[string]interface{})

params := &models.WritableVirtualMachineWithConfigContext{}

// Required parameters
name := d.Get("name").(string)
params.Name = &name
clusterID := int64(d.Get("cluster_id").(int))
params.Cluster = &clusterID
if d.HasChange("name") {
name := d.Get("name").(string)
params.Name = &name
} else {
dropFields = append(dropFields, "name")
}
if d.HasChange("cluster_id") {
clusterID := int64(d.Get("cluster_id").(int))
params.Cluster = &clusterID
} else {
dropFields = append(dropFields, "cluster")
}

if d.HasChange("comments") {
if comments, exist := d.GetOk("comments"); exist {
comments := d.Get("comments")
if comments != "" {
params.Comments = comments.(string)
} else {
params.Comments = " "
emptyFields["comments"] = ""
}
}

Expand All @@ -379,7 +396,11 @@ func resourceNetboxVirtualizationVMUpdate(ctx context.Context, d *schema.Resourc

if d.HasChange("disk") {
disk := int64(d.Get("disk").(int))
params.Disk = &disk
if disk != 0 {
params.Disk = &disk
} else {
emptyFields["disk"] = nil
}
}

if d.HasChange("local_context_data") {
Expand All @@ -395,41 +416,60 @@ func resourceNetboxVirtualizationVMUpdate(ctx context.Context, d *schema.Resourc

if d.HasChange("memory") {
memory := int64(d.Get("memory").(int))
params.Memory = &memory
if memory != 0 {
params.Memory = &memory
} else {
emptyFields["memory"] = nil
}
}

if d.HasChange("platform_id") {
platformID := int64(d.Get("platform_id").(int))
params.Platform = &platformID
if platformID != 0 {
params.Platform = &platformID
} else {
emptyFields["platform"] = nil
}
}

if d.HasChange("role_id") {
roleID := int64(d.Get("role_id").(int))
params.Role = &roleID
if roleID != 0 {
params.Role = &roleID
} else {
emptyFields["role"] = nil
}
}

if d.HasChange("status") {
status := d.Get("status").(string)
params.Status = status
}

tags := d.Get("tag").(*schema.Set).List()
params.Tags = convertTagsToNestedTags(tags)
if d.HasChange("tag") {
tags := d.Get("tag").(*schema.Set).List()
params.Tags = convertTagsToNestedTags(tags)
} else {
dropFields = append(dropFields, "tags")
}

if d.HasChange("tenant_id") {
tenantID := int64(d.Get("tenant_id").(int))
params.Tenant = &tenantID
if tenantID != 0 {
params.Tenant = &tenantID
} else {
emptyFields["tenant"] = nil
}
}

if _, ok := d.GetOk("vcpus"); d.HasChange("vcpus") && ok {
if d.HasChange("vcpus") {
vcpus := d.Get("vcpus").(string)

if !strings.Contains(vcpus, ".") {
vcpus = vcpus + ".00"
if vcpus != "" {
vcpusFloat, _ := strconv.ParseFloat(vcpus, 32)
params.Vcpus = &vcpusFloat
} else {
emptyFields["vcpus"] = nil
}

vcpusFloat, _ := strconv.ParseFloat(vcpus, 32)
params.Vcpus = &vcpusFloat
}

resource := virtualization.NewVirtualizationVirtualMachinesPartialUpdateParams().WithData(params)
Expand All @@ -442,7 +482,7 @@ func resourceNetboxVirtualizationVMUpdate(ctx context.Context, d *schema.Resourc
resource.SetID(resourceID)

_, err = client.Virtualization.VirtualizationVirtualMachinesPartialUpdate(
resource, nil)
resource, nil, newRequestModifierOperation(emptyFields, dropFields))
if err != nil {
return diag.FromErr(err)
}
Expand Down
Loading

0 comments on commit 9ed4488

Please sign in to comment.