Skip to content

Commit

Permalink
feat: Add tag parameter to tenant group resource
Browse files Browse the repository at this point in the history
  • Loading branch information
smutel committed May 17, 2022
1 parent 51f40ee commit 83f77f6
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 4 deletions.
9 changes: 9 additions & 0 deletions docs/resources/tenancy_tenant_group.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ Manage a tenant group within Netbox.
resource "netbox_tenancy_tenant_group" "tenant_group_test" {
name = "TestTenantGroup"
slug = "TestTenantGroup"
tag {
name = "tag1"
slug = "tag1"
}
}
```

Expand All @@ -17,6 +22,10 @@ The following arguments are supported:
* ``name`` - (Required) The name for this object.
* ``slug`` - (Required) The slug for this object.

The ``tag`` block (optional) supports:
* ``name`` - (Required) Name of the existing tag to associate with this resource.
* ``slug`` - (Required) Slug of the existing tag to associate with this resource.

## Attributes Reference

In addition to the above arguments, the following attributes are exported:
Expand Down
10 changes: 10 additions & 0 deletions examples/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,16 @@ resource "netbox_tenancy_tenant" "tenant_test" {
resource "netbox_tenancy_tenant_group" "tenant_group_test" {
name = "Test_TenantGroup"
slug = "Test_TenantGroup"

tag {
name = "tag1"
slug = "tag1"
}

tag {
name = "tag2"
slug = "tag2"
}
}

data "netbox_dcim_site" "site_test" {
Expand Down
31 changes: 27 additions & 4 deletions netbox/resource_netbox_tenancy_tenant_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,22 @@ func resourceNetboxTenancyTenantGroup() *schema.Resource {
regexp.MustCompile("^[-a-zA-Z0-9_]{1,50}$"),
"Must be like ^[-a-zA-Z0-9_]{1,50}$"),
},
"tag": {
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
},
"slug": {
Type: schema.TypeString,
Required: true,
},
},
},
},
},
}
}
Expand All @@ -46,10 +62,12 @@ func resourceNetboxTenancyTenantGroupCreate(d *schema.ResourceData,

groupName := d.Get("name").(string)
groupSlug := d.Get("slug").(string)
tags := d.Get("tag").(*schema.Set).List()

newResource := &models.WritableTenantGroup{
Name: &groupName,
Slug: &groupSlug,
Tags: convertTagsToNestedTags(tags),
}

resource := tenancy.NewTenancyTenantGroupsCreateParams().WithData(newResource)
Expand Down Expand Up @@ -85,6 +103,10 @@ func resourceNetboxTenancyTenantGroupRead(d *schema.ResourceData,
return err
}

if err = d.Set("tag", convertNestedTagsToTags(resource.Tags)); err != nil {
return err
}

return nil
}
}
Expand All @@ -103,10 +125,11 @@ func resourceNetboxTenancyTenantGroupUpdate(d *schema.ResourceData,
slug := d.Get("slug").(string)
params.Slug = &slug

if d.HasChange("name") {
name := d.Get("name").(string)
params.Name = &name
}
name := d.Get("name").(string)
params.Name = &name

tags := d.Get("tag").(*schema.Set).List()
params.Tags = convertTagsToNestedTags(tags)

resource := tenancy.NewTenancyTenantGroupsPartialUpdateParams().WithData(
params)
Expand Down

0 comments on commit 83f77f6

Please sign in to comment.