Skip to content

Commit

Permalink
feat: Config contexts -- continue -- continue (#590)
Browse files Browse the repository at this point in the history
* 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
4 people authored Jun 1, 2024
1 parent 7662074 commit afe33dc
Show file tree
Hide file tree
Showing 12 changed files with 1,008 additions and 0 deletions.
42 changes: 42 additions & 0 deletions docs/data-sources/config_context.md
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)


2 changes: 2 additions & 0 deletions docs/data-sources/devices.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,12 @@ Read-Only:
- `asset_tag` (String)
- `cluster_id` (Number)
- `comments` (String)
- `config_context` (String)
- `custom_fields` (Map of String)
- `description` (String)
- `device_id` (Number)
- `device_type_id` (Number)
- `local_context_data` (String)
- `location_id` (Number)
- `manufacturer_id` (Number)
- `model` (String)
Expand Down
55 changes: 55 additions & 0 deletions docs/resources/config_context.md
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.


7 changes: 7 additions & 0 deletions example/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -124,3 +124,10 @@ resource "netbox_webhook" "testwebhook" {
content_types = "dcim.site"
body_template = "Sample Body"
}

resource "netbox_config_context" "test" {
name = "My Config Context"
data = jsonencode(
{ "testkey" = "testval" }
)
}
4 changes: 4 additions & 0 deletions examples/resources/netbox_config_context/resource.tf
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"})
}
254 changes: 254 additions & 0 deletions netbox/data_source_netbox_config_context.go
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
}
Loading

0 comments on commit afe33dc

Please sign in to comment.