Skip to content

Commit

Permalink
feat: Add site_id to cluster data source and to vm resource
Browse files Browse the repository at this point in the history
fixes: 191
  • Loading branch information
amhn authored and smutel committed Mar 14, 2023
1 parent b2e2106 commit a93a3c9
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 0 deletions.
2 changes: 2 additions & 0 deletions examples/resources/netbox_virtualization_cluster/import.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Clusters can be imported by id
terraform import netbox_virtualization_cluster.cluster_test 1
7 changes: 7 additions & 0 deletions netbox/virtualization/data_netbox_virtualization_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ func DataNetboxVirtualizationCluster() *schema.Resource {
Required: true,
ValidateFunc: validation.StringLenBetween(1, 100),
},
"site_id": {
Type: schema.TypeInt,
Computed: true,
},
},
}
}
Expand Down Expand Up @@ -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
}
20 changes: 20 additions & 0 deletions netbox/virtualization/resource_netbox_virtualization_vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit a93a3c9

Please sign in to comment.