diff --git a/examples/resources/netbox_virtualization_cluster/import.sh b/examples/resources/netbox_virtualization_cluster/import.sh new file mode 100644 index 000000000..05e585494 --- /dev/null +++ b/examples/resources/netbox_virtualization_cluster/import.sh @@ -0,0 +1,2 @@ +# Clusters can be imported by id +terraform import netbox_virtualization_cluster.cluster_test 1 diff --git a/netbox/virtualization/data_netbox_virtualization_cluster.go b/netbox/virtualization/data_netbox_virtualization_cluster.go index 4df87c6bd..71f3cec14 100644 --- a/netbox/virtualization/data_netbox_virtualization_cluster.go +++ b/netbox/virtualization/data_netbox_virtualization_cluster.go @@ -26,6 +26,10 @@ func DataNetboxVirtualizationCluster() *schema.Resource { Required: true, ValidateFunc: validation.StringLenBetween(1, 100), }, + "site_id": { + Type: schema.TypeInt, + Computed: true, + }, }, } } @@ -57,6 +61,9 @@ func dataNetboxVirtualizationClusterRead(ctx context.Context, d *schema.Resource if err = d.Set("content_type", util.ConvertURIContentType(r.URL)); err != nil { return diag.FromErr(err) } + if err = d.Set("site_id", util.GetNestedSiteID(r.Site)); err != nil { + return diag.FromErr(err) + } return nil } diff --git a/netbox/virtualization/resource_netbox_virtualization_vm.go b/netbox/virtualization/resource_netbox_virtualization_vm.go index 98bc052b7..680561b58 100644 --- a/netbox/virtualization/resource_netbox_virtualization_vm.go +++ b/netbox/virtualization/resource_netbox_virtualization_vm.go @@ -102,6 +102,11 @@ func ResourceNetboxVirtualizationVM() *schema.Resource { Default: nil, Description: "ID of the role for this VM (virtualization module).", }, + "site_id": { + Type: schema.TypeInt, + Optional: true, + Description: "ID of the site where this VM (virtualization module) is attached. If cluster_id is set and the cluster resides in a site, this must be set and the same as the cluster's site", + }, "status": { Type: schema.TypeString, Optional: true, @@ -188,6 +193,10 @@ func resourceNetboxVirtualizationVMCreate(ctx context.Context, d *schema.Resourc newResource.Role = &roleID } + if siteID := int64(d.Get("site_id").(int)); siteID != 0 { + newResource.Site = &siteID + } + if tenantID != 0 { newResource.Tenant = &tenantID } @@ -284,6 +293,9 @@ func resourceNetboxVirtualizationVMRead(ctx context.Context, d *schema.ResourceD if err = d.Set("role_id", util.GetNestedRoleID(resource.Role)); err != nil { return diag.FromErr(err) } + if err = d.Set("site_id", util.GetNestedSiteID(resource.Site)); err != nil { + return diag.FromErr(err) + } if err = d.Set("status", resource.Status.Value); err != nil { return diag.FromErr(err) @@ -391,6 +403,14 @@ func resourceNetboxVirtualizationVMUpdate(ctx context.Context, d *schema.Resourc } } + if d.HasChange("site_id") { + siteID := int64(d.Get("site_id").(int)) + params.Site = &siteID + if siteID == 0 { + emptyFields["site"] = nil + } + } + if d.HasChange("status") { status := d.Get("status").(string) params.Status = status