Skip to content

Commit

Permalink
feat: Add netbox_virtualization_vm_primary_ip resource
Browse files Browse the repository at this point in the history
  • Loading branch information
amhn authored and smutel committed Nov 18, 2022
1 parent 9961445 commit bc2e88a
Show file tree
Hide file tree
Showing 8 changed files with 564 additions and 27 deletions.
14 changes: 13 additions & 1 deletion examples/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -402,13 +402,18 @@ resource "netbox_ipam_ip_range" "range_test" {
}
}

resource "netbox_virtualization_vm_primary_ip" "name" {
virtualmachine_id = netbox_virtualization_vm.vm_test.id
primary_ip4_id = netbox_ipam_ip_addresses.ip_test.id
primary_ip6_id = netbox_ipam_ip_addresses.ip6_test.id
}

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
object_type = netbox_virtualization_interface.interface_test.type
primary_ip4 = true

tag {
name = "tag1"
Expand Down Expand Up @@ -493,6 +498,13 @@ resource "netbox_ipam_ip_addresses" "ip_test" {
}
}

resource "netbox_ipam_ip_addresses" "ip6_test" {
address = "2001:db8::1234/64"
status = "active"
object_id = netbox_virtualization_interface.interface_test.id
object_type = netbox_virtualization_interface.interface_test.type
}

resource "netbox_ipam_ip_addresses" "dynamic_ip_from_prefix" {
prefix = netbox_ipam_prefix.dynamic_prefix_test.id
description = "Dynamic IP in dynamic prefix created by terraform"
Expand Down
7 changes: 7 additions & 0 deletions examples/resources/netbox_ipam_ip_addresses/resource.tf
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,13 @@ resource "netbox_ipam_ip_addresses" "ip_test" {
}
}

resource "netbox_ipam_ip_addresses" "ip6_test" {
address = "2001:db8::1234/64"
status = "active"
object_id = netbox_virtualization_interface.interface_test.id
object_type = netbox_virtualization_interface.interface_test.type
}

resource "netbox_ipam_ip_addresses" "dynamic_ip_from_prefix" {
prefix = netbox_ipam_prefix.dynamic_prefix_test.id
description = "Dynamic IP in dynamic prefix created by terraform"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
resource "netbox_virtualization_vm_primary_ip" "name" {
virtualmachine_id = netbox_virtualization_vm.vm_test.id
primary_ip4_id = netbox_ipam_ip_addresses.ip_test.id
primary_ip6_id = netbox_ipam_ip_addresses.ip6_test.id
}
31 changes: 16 additions & 15 deletions netbox/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,21 +156,22 @@ func Provider() *schema.Provider {
"netbox_virtualization_cluster": dataNetboxVirtualizationCluster(),
},
ResourcesMap: map[string]*schema.Resource{
"netbox_ipam_aggregate": resourceNetboxIpamAggregate(),
"netbox_ipam_ip_addresses": resourceNetboxIpamIPAddresses(),
"netbox_ipam_ip_range": resourceNetboxIpamIPRange(),
"netbox_ipam_prefix": resourceNetboxIpamPrefix(),
"netbox_ipam_service": resourceNetboxIpamService(),
"netbox_ipam_vlan": resourceNetboxIpamVlan(),
"netbox_ipam_vlan_group": resourceNetboxIpamVlanGroup(),
"netbox_tenancy_contact": resourceNetboxTenancyContact(),
"netbox_tenancy_contact_assignment": resourceNetboxTenancyContactAssignment(),
"netbox_tenancy_contact_group": resourceNetboxTenancyContactGroup(),
"netbox_tenancy_contact_role": resourceNetboxTenancyContactRole(),
"netbox_tenancy_tenant": resourceNetboxTenancyTenant(),
"netbox_tenancy_tenant_group": resourceNetboxTenancyTenantGroup(),
"netbox_virtualization_interface": resourceNetboxVirtualizationInterface(),
"netbox_virtualization_vm": resourceNetboxVirtualizationVM(),
"netbox_ipam_aggregate": resourceNetboxIpamAggregate(),
"netbox_ipam_ip_addresses": resourceNetboxIpamIPAddresses(),
"netbox_ipam_ip_range": resourceNetboxIpamIPRange(),
"netbox_ipam_prefix": resourceNetboxIpamPrefix(),
"netbox_ipam_service": resourceNetboxIpamService(),
"netbox_ipam_vlan": resourceNetboxIpamVlan(),
"netbox_ipam_vlan_group": resourceNetboxIpamVlanGroup(),
"netbox_tenancy_contact": resourceNetboxTenancyContact(),
"netbox_tenancy_contact_assignment": resourceNetboxTenancyContactAssignment(),
"netbox_tenancy_contact_group": resourceNetboxTenancyContactGroup(),
"netbox_tenancy_contact_role": resourceNetboxTenancyContactRole(),
"netbox_tenancy_tenant": resourceNetboxTenancyTenant(),
"netbox_tenancy_tenant_group": resourceNetboxTenancyTenantGroup(),
"netbox_virtualization_interface": resourceNetboxVirtualizationInterface(),
"netbox_virtualization_vm": resourceNetboxVirtualizationVM(),
"netbox_virtualization_vm_primary_ip": resourceNetboxVirtualizationVMPrimaryIP(),
},
ConfigureContextFunc: configureProvider,
}
Expand Down
28 changes: 17 additions & 11 deletions netbox/resource_netbox_ipam_ip_addresses.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,11 @@ func resourceNetboxIpamIPAddresses() *schema.Resource {
Type: schema.TypeBool,
Optional: true,
Default: false,
Deprecated: "Use new netbox_virtualization_primary_ip resource instead",
Description: "Set this resource as primary IPv4 (false by default).",
DiffSuppressFunc: func(k, oldValue, newValue string, d *schema.ResourceData) bool {
return d.GetRawConfig().GetAttr("primary_ip4").IsNull()
},
},
"role": {
Type: schema.TypeString,
Expand Down Expand Up @@ -473,17 +477,19 @@ func resourceNetboxIpamIPAddressesUpdate(ctx context.Context, d *schema.Resource
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)
if !d.GetRawConfig().GetAttr("primary_ip4").IsNull() {
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)
}
}
}

Expand Down
46 changes: 46 additions & 0 deletions netbox/resource_netbox_virtualization_vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,24 @@ func resourceNetboxVirtualizationVM() *schema.Resource {
Default: nil,
Description: "ID of the platform for this VM (virtualization module).",
},
"primary_ip": {
Type: schema.TypeString,
Computed: true,
Default: nil,
Description: "Primary IP of this VM (virtualization module). Can be IPv4 or IPv6. See [Netbox docs|https://docs.netbox.dev/en/stable/models/virtualization/virtualmachine/] for more information.",
},
"primary_ip4": {
Type: schema.TypeString,
Computed: true,
Default: nil,
Description: "Primary IPv4 of this VM (virtualization module).",
},
"primary_ip6": {
Type: schema.TypeString,
Computed: true,
Default: nil,
Description: "Primary IPv6 of this VM (virtualization module).",
},
"role_id": {
Type: schema.TypeInt,
Optional: true,
Expand Down Expand Up @@ -283,6 +301,34 @@ func resourceNetboxVirtualizationVMRead(ctx context.Context, d *schema.ResourceD
}
}

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)
Expand Down
Loading

0 comments on commit bc2e88a

Please sign in to comment.