diff --git a/examples/main.tf b/examples/main.tf index dd6ba9b0f..c3c2a8507 100644 --- a/examples/main.tf +++ b/examples/main.tf @@ -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 } @@ -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" @@ -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" @@ -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" @@ -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" } diff --git a/netbox/resource_netbox_ipam_ip_addresses.go b/netbox/resource_netbox_ipam_ip_addresses.go index b6e8265fd..d510194bb 100644 --- a/netbox/resource_netbox_ipam_ip_addresses.go +++ b/netbox/resource_netbox_ipam_ip_addresses.go @@ -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), }, @@ -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) @@ -131,7 +131,7 @@ func resourceNetboxIpamIPAddressesCreate(d *schema.ResourceData, if objectID != 0 { newResource.AssignedObjectID = &objectID - newResource.AssignedObjectType = interfaceType + newResource.AssignedObjectType = objectType } if natInsideID != 0 { @@ -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 } @@ -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") {