generated from hashicorp/terraform-provider-scaffolding
-
Notifications
You must be signed in to change notification settings - Fork 138
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Config contexts -- continue -- continue (#590)
* feat: Config Contexts * Make code up-to-date, switch type of config_context to String * go fmt + docs generate * add example * docs * update schema * remove duplicated * tests * fix lint errors * chore: add some newlines to the code --------- Co-authored-by: Mike Frost <[email protected]> Co-authored-by: Gennady Lipenkov <[email protected]> Co-authored-by: Fabian Breckle <[email protected]>
- Loading branch information
1 parent
7662074
commit afe33dc
Showing
12 changed files
with
1,008 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
--- | ||
# generated by https://github.com/fbreckle/terraform-plugin-docs | ||
page_title: "netbox_config_context Data Source - terraform-provider-netbox" | ||
subcategory: "Extras" | ||
description: |- | ||
--- | ||
|
||
# netbox_config_context (Data Source) | ||
|
||
|
||
|
||
|
||
|
||
<!-- schema generated by tfplugindocs --> | ||
## Schema | ||
|
||
### Required | ||
|
||
- `name` (String) | ||
|
||
### Read-Only | ||
|
||
- `cluster_groups` (List of Number) | ||
- `cluster_types` (List of Number) | ||
- `clusters` (List of Number) | ||
- `data` (String) | ||
- `description` (String) | ||
- `device_types` (List of Number) | ||
- `id` (String) The ID of this resource. | ||
- `locations` (List of Number) | ||
- `platforms` (List of Number) | ||
- `regions` (List of Number) | ||
- `roles` (List of Number) | ||
- `site_groups` (List of Number) | ||
- `sites` (List of Number) | ||
- `tags` (List of String) | ||
- `tenant_groups` (List of Number) | ||
- `tenants` (List of Number) | ||
- `weight` (Number) | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
--- | ||
# generated by https://github.com/fbreckle/terraform-plugin-docs | ||
page_title: "netbox_config_context Resource - terraform-provider-netbox" | ||
subcategory: "Extras" | ||
description: |- | ||
From the official documentation https://docs.netbox.dev/en/stable/models/extras/configcontext/: | ||
Context data is made available to devices and/or virtual machines based on their relationships to other objects in NetBox. For example, context data can be associated only with devices assigned to a particular site, or only to virtual machines in a certain cluster. | ||
--- | ||
|
||
# netbox_config_context (Resource) | ||
|
||
From the [official documentation](https://docs.netbox.dev/en/stable/models/extras/configcontext/): | ||
|
||
> Context data is made available to devices and/or virtual machines based on their relationships to other objects in NetBox. For example, context data can be associated only with devices assigned to a particular site, or only to virtual machines in a certain cluster. | ||
## Example Usage | ||
|
||
```terraform | ||
resource "netbox_config_context" "test" { | ||
name = "%s" | ||
data = jsonencode({"testkey" = "testval"}) | ||
} | ||
``` | ||
|
||
<!-- schema generated by tfplugindocs --> | ||
## Schema | ||
|
||
### Required | ||
|
||
- `data` (String) | ||
- `name` (String) | ||
|
||
### Optional | ||
|
||
- `cluster_groups` (Set of Number) | ||
- `cluster_types` (Set of Number) | ||
- `clusters` (Set of Number) | ||
- `description` (String) | ||
- `device_types` (Set of Number) | ||
- `locations` (Set of Number) | ||
- `platforms` (Set of Number) | ||
- `regions` (Set of Number) | ||
- `roles` (Set of Number) | ||
- `site_groups` (Set of Number) | ||
- `sites` (Set of Number) | ||
- `tags` (Set of String) | ||
- `tenant_groups` (Set of Number) | ||
- `tenants` (Set of Number) | ||
- `weight` (Number) Defaults to `1000`. | ||
|
||
### Read-Only | ||
|
||
- `id` (String) The ID of this resource. | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
resource "netbox_config_context" "test" { | ||
name = "%s" | ||
data = jsonencode({"testkey" = "testval"}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,254 @@ | ||
package netbox | ||
|
||
import ( | ||
"encoding/json" | ||
"errors" | ||
"strconv" | ||
|
||
"github.com/fbreckle/go-netbox/netbox/client" | ||
"github.com/fbreckle/go-netbox/netbox/client/extras" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
) | ||
|
||
func dataSourceNetboxConfigContext() *schema.Resource { | ||
return &schema.Resource{ | ||
Read: dataSourceNetboxConfigContextRead, | ||
Description: `:meta:subcategory:Extras:`, | ||
Schema: map[string]*schema.Schema{ | ||
"name": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
}, | ||
"description": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"weight": { | ||
Type: schema.TypeInt, | ||
Computed: true, | ||
}, | ||
"data": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"cluster_groups": { | ||
Type: schema.TypeList, | ||
Computed: true, | ||
Default: nil, | ||
Elem: &schema.Schema{ | ||
Type: schema.TypeInt, | ||
}, | ||
}, | ||
"cluster_types": { | ||
Type: schema.TypeList, | ||
Computed: true, | ||
Default: nil, | ||
Elem: &schema.Schema{ | ||
Type: schema.TypeInt, | ||
}, | ||
}, | ||
"clusters": { | ||
Type: schema.TypeList, | ||
Computed: true, | ||
Default: nil, | ||
Elem: &schema.Schema{ | ||
Type: schema.TypeInt, | ||
}, | ||
}, | ||
"device_types": { | ||
Type: schema.TypeList, | ||
Computed: true, | ||
Default: nil, | ||
Elem: &schema.Schema{ | ||
Type: schema.TypeInt, | ||
}, | ||
}, | ||
"locations": { | ||
Type: schema.TypeList, | ||
Computed: true, | ||
Default: nil, | ||
Elem: &schema.Schema{ | ||
Type: schema.TypeInt, | ||
}, | ||
}, | ||
"platforms": { | ||
Type: schema.TypeList, | ||
Computed: true, | ||
Default: nil, | ||
Elem: &schema.Schema{ | ||
Type: schema.TypeInt, | ||
}, | ||
}, | ||
"regions": { | ||
Type: schema.TypeList, | ||
Computed: true, | ||
Default: nil, | ||
Elem: &schema.Schema{ | ||
Type: schema.TypeInt, | ||
}, | ||
}, | ||
"roles": { | ||
Type: schema.TypeList, | ||
Computed: true, | ||
Default: nil, | ||
Elem: &schema.Schema{ | ||
Type: schema.TypeInt, | ||
}, | ||
}, | ||
"site_groups": { | ||
Type: schema.TypeList, | ||
Computed: true, | ||
Default: nil, | ||
Elem: &schema.Schema{ | ||
Type: schema.TypeInt, | ||
}, | ||
}, | ||
"sites": { | ||
Type: schema.TypeList, | ||
Computed: true, | ||
Default: nil, | ||
Elem: &schema.Schema{ | ||
Type: schema.TypeInt, | ||
}, | ||
}, | ||
"tenant_groups": { | ||
Type: schema.TypeList, | ||
Computed: true, | ||
Default: nil, | ||
Elem: &schema.Schema{ | ||
Type: schema.TypeInt, | ||
}, | ||
}, | ||
"tenants": { | ||
Type: schema.TypeList, | ||
Computed: true, | ||
Default: nil, | ||
Elem: &schema.Schema{ | ||
Type: schema.TypeInt, | ||
}, | ||
}, | ||
"tags": { | ||
Type: schema.TypeList, | ||
Computed: true, | ||
Default: nil, | ||
Elem: &schema.Schema{ | ||
Type: schema.TypeString, | ||
}, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func dataSourceNetboxConfigContextRead(d *schema.ResourceData, m interface{}) error { | ||
api := m.(*client.NetBoxAPI) | ||
|
||
name := d.Get("name").(string) | ||
params := extras.NewExtrasConfigContextsListParams() | ||
params.Name = &name | ||
limit := int64(2) // Limit of 2 is enough | ||
params.Limit = &limit | ||
|
||
res, err := api.Extras.ExtrasConfigContextsList(params, nil) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if *res.GetPayload().Count > int64(1) { | ||
return errors.New("more than one result. Specify a more narrow filter") | ||
} | ||
if *res.GetPayload().Count == int64(0) { | ||
return errors.New("no result") | ||
} | ||
result := res.GetPayload().Results[0] | ||
d.SetId(strconv.FormatInt(result.ID, 10)) | ||
d.Set("name", result.Name) | ||
d.Set("weight", result.Weight) | ||
|
||
if result.Data != nil { | ||
if jsonArr, err := json.Marshal(result.Data); err == nil { | ||
d.Set("data", string(jsonArr)) | ||
} | ||
} else { | ||
d.Set("data", nil) | ||
} | ||
|
||
clusterGroups := make([]int64, len(result.ClusterGroups)) | ||
for i, v := range result.ClusterGroups { | ||
clusterGroups[i] = int64(v.ID) | ||
} | ||
d.Set("cluster_groups", clusterGroups) | ||
|
||
clusterTypes := make([]int64, len(result.ClusterTypes)) | ||
for i, v := range result.ClusterTypes { | ||
clusterTypes[i] = int64(v.ID) | ||
} | ||
d.Set("cluster_types", clusterTypes) | ||
|
||
clusters := make([]int64, len(result.Clusters)) | ||
for i, v := range result.Clusters { | ||
clusters[i] = int64(v.ID) | ||
} | ||
d.Set("clusters", clusters) | ||
|
||
deviceTypes := make([]int64, len(result.DeviceTypes)) | ||
for i, v := range result.DeviceTypes { | ||
deviceTypes[i] = int64(v.ID) | ||
} | ||
d.Set("device_types", deviceTypes) | ||
|
||
locations := make([]int64, len(result.Locations)) | ||
for i, v := range result.Locations { | ||
locations[i] = int64(v.ID) | ||
} | ||
d.Set("locations", locations) | ||
|
||
platforms := make([]int64, len(result.Platforms)) | ||
for i, v := range result.Platforms { | ||
platforms[i] = int64(v.ID) | ||
} | ||
d.Set("platforms", platforms) | ||
|
||
regions := make([]int64, len(result.Regions)) | ||
for i, v := range result.Regions { | ||
regions[i] = int64(v.ID) | ||
} | ||
d.Set("regions", regions) | ||
|
||
roles := make([]int64, len(result.Roles)) | ||
for i, v := range result.Roles { | ||
roles[i] = int64(v.ID) | ||
} | ||
d.Set("roles", roles) | ||
|
||
siteGroups := make([]int64, len(result.SiteGroups)) | ||
for i, v := range result.SiteGroups { | ||
siteGroups[i] = int64(v.ID) | ||
} | ||
d.Set("site_groups", siteGroups) | ||
|
||
sites := make([]int64, len(result.Sites)) | ||
for i, v := range result.Sites { | ||
sites[i] = int64(v.ID) | ||
} | ||
d.Set("sites", sites) | ||
|
||
tenantGroups := make([]int64, len(result.TenantGroups)) | ||
for i, v := range result.TenantGroups { | ||
tenantGroups[i] = int64(v.ID) | ||
} | ||
d.Set("tenant_groups", tenantGroups) | ||
|
||
tenants := make([]int64, len(result.Tenants)) | ||
for i, v := range result.Tenants { | ||
tenants[i] = int64(v.ID) | ||
} | ||
d.Set("tenants", tenants) | ||
|
||
tags := make([]string, len(result.Tags)) | ||
for i, v := range result.Tags { | ||
tags[i] = string(v) | ||
} | ||
d.Set("tags", tags) | ||
|
||
return nil | ||
} |
Oops, something went wrong.