Skip to content

Commit

Permalink
enh: Set default value for object_type
Browse files Browse the repository at this point in the history
  • Loading branch information
smutel committed Oct 31, 2020
1 parent c238629 commit edca694
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 31 deletions.
39 changes: 20 additions & 19 deletions examples/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ data "netbox_dcim_site" "site_test" {
}

resource "netbox_ipam_vlan_group" "vlan_group_test" {
name = "Test_VlanGroup"
slug = "Test_VlanGroup"
name = "Test_VlanGroup"
slug = "Test_VlanGroup"
site_id = data.netbox_dcim_site.site_test.id
}

Expand All @@ -39,13 +39,13 @@ data "netbox_ipam_role" "vlan_role_backup" {
}

resource "netbox_ipam_vlan" "vlan_test" {
vlan_id = 100
name = "Test_Vlan"
site_id = netbox_ipam_vlan_group.vlan_group_test.site_id
description = "VLAN created by terraform"
vlan_id = 100
name = "Test_Vlan"
site_id = netbox_ipam_vlan_group.vlan_group_test.site_id
description = "VLAN created by terraform"
vlan_group_id = netbox_ipam_vlan_group.vlan_group_test.id
tenant_id = netbox_tenancy_tenant.tenant_test.id
role_id = data.netbox_ipam_role.vlan_role_production.id
tenant_id = netbox_tenancy_tenant.tenant_test.id
role_id = data.netbox_ipam_role.vlan_role_production.id

tag {
name = "tag1"
Expand All @@ -59,11 +59,11 @@ resource "netbox_ipam_vlan" "vlan_test" {
}

resource "netbox_ipam_prefix" "prefix_test" {
prefix = "192.168.56.0/24"
vlan_id = netbox_ipam_vlan.vlan_test.id
prefix = "192.168.56.0/24"
vlan_id = netbox_ipam_vlan.vlan_test.id
description = "Prefix created by terraform"
site_id = netbox_ipam_vlan_group.vlan_group_test.site_id
role_id = data.netbox_ipam_role.vlan_role_production.id
site_id = netbox_ipam_vlan_group.vlan_group_test.site_id
role_id = data.netbox_ipam_role.vlan_role_production.id

tag {
name = "tag1"
Expand All @@ -77,10 +77,11 @@ resource "netbox_ipam_prefix" "prefix_test" {
}

resource "netbox_ipam_ip_addresses" "ip_test" {
address = "192.168.56.1/24"
status = "active"
tenant_id = netbox_tenancy_tenant.tenant_test.id
object_id = netbox_virtualization_interface.interface_test.id
address = "192.168.56.1/24"
status = "active"
tenant_id = netbox_tenancy_tenant.tenant_test.id
object_id = netbox_virtualization_interface.interface_test.id
object_type = netbox_virtualization_interface.interface_test.type

tag {
name = "tag1"
Expand Down Expand Up @@ -109,8 +110,8 @@ resource "netbox_virtualization_vm" "vm_test" {
}

resource "netbox_virtualization_interface" "interface_test" {
name = "default"
name = "default"
virtualmachine_id = netbox_virtualization_vm.vm_test.id
mac_address = "AA:AA:AA:AA:AA:AA"
description = "Interface de test"
mac_address = "AA:AA:AA:AA:AA:AA"
description = "Interface de test"
}
29 changes: 17 additions & 12 deletions netbox/resource_netbox_ipam_ip_addresses.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func resourceNetboxIpamIPAddresses() *schema.Resource {
"object_type": {
Type: schema.TypeString,
Optional: true,
Default: "",
Default: "virtualization.vminterface",
ValidateFunc: validation.StringInSlice([]string{
"virtualization.vminterface", "dcim.interface"}, false),
},
Expand Down Expand Up @@ -111,7 +111,7 @@ func resourceNetboxIpamIPAddressesCreate(d *schema.ResourceData,
description := d.Get("description").(string)
dnsName := d.Get("dns_name").(string)
objectID := int64(d.Get("object_id").(int))
interfaceType := d.Get("object_type").(string)
objectType := d.Get("object_type").(string)
natInsideID := int64(d.Get("nat_inside_id").(int))
natOutsideID := int64(d.Get("nat_outside_id").(int))
role := d.Get("role").(string)
Expand All @@ -131,7 +131,7 @@ func resourceNetboxIpamIPAddressesCreate(d *schema.ResourceData,

if objectID != 0 {
newResource.AssignedObjectID = &objectID
newResource.AssignedObjectType = interfaceType
newResource.AssignedObjectType = objectType
}

if natInsideID != 0 {
Expand Down Expand Up @@ -197,8 +197,11 @@ func resourceNetboxIpamIPAddressesRead(d *schema.ResourceData,
}
}

if err = d.Set("object_type",
resource.AssignedObjectType); err != nil {
objectType := resource.AssignedObjectType
if objectType == "" {
objectType = "virtualization.vminterface"
}
if err = d.Set("object_type", objectType); err != nil {
return err
}

Expand Down Expand Up @@ -296,15 +299,17 @@ func resourceNetboxIpamIPAddressesUpdate(d *schema.ResourceData,
params.DNSName = d.Get("dns_name").(string)
}

if d.HasChange("object_type") {
params.AssignedObjectType = d.Get("object_type").(string)
}

if d.HasChange("object_id") {
if d.HasChange("object_id") || d.HasChange("object_type") {
objectID := int64(d.Get("object_id").(int))
if objectID != 0 {
params.AssignedObjectID = &objectID
params.AssignedObjectID = &objectID

var objectType string
if params.AssignedObjectType == "" {
objectType = "virtualization.vminterface"
} else {
objectType = d.Get("object_type").(string)
}
params.AssignedObjectType = objectType
}

if d.HasChange("nat_inside_id") {
Expand Down

0 comments on commit edca694

Please sign in to comment.