Skip to content

Commit

Permalink
feat: Add netbox_dcim_manufacturer
Browse files Browse the repository at this point in the history
  • Loading branch information
amhn committed Nov 18, 2022
1 parent 2aa2edd commit f214f6a
Show file tree
Hide file tree
Showing 5 changed files with 501 additions and 0 deletions.
82 changes: 82 additions & 0 deletions examples/resources/netbox_dcim_manufacturer/resource.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
resource "netbox_dcim_manufacturer" "manufacturer_test" {
name = "Test manufacturer"
slug = "Test manufacturer"
description = "manufacturer for testing"

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

custom_field {
name = "cf_boolean"
type = "boolean"
value = "true"
}

custom_field {
name = "cf_date"
type = "date"
value = "2020-12-25"
}

custom_field {
name = "cf_text"
type = "text"
value = "some text"
}

custom_field {
name = "cf_integer"
type = "integer"
value = "10"
}

custom_field {
name = "cf_selection"
type = "select"
value = "1"
}

custom_field {
name = "cf_url"
type = "url"
value = "https://github.com"
}

custom_field {
name = "cf_multi_selection"
type = "multiselect"
value = jsonencode([
"0",
"1"
])
}

custom_field {
name = "cf_json"
type = "json"
value = jsonencode({
stringvalue = "string"
boolvalue = false
dictionary = {
numbervalue = 5
}
})
}

custom_field {
name = "cf_object"
type = "object"
value = 1
}

custom_field {
name = "cf_multi_object"
type = "multiobject"
value = jsonencode([
1,
2
])
}
}
16 changes: 16 additions & 0 deletions netbox/dcim/init_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package dcim_test

import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/smutel/terraform-provider-netbox/v4/netbox"
)

var testAccProviders map[string]*schema.Provider
var testAccProvider *schema.Provider

func init() {
testAccProvider = netbox.Provider()
testAccProviders = map[string]*schema.Provider{
"netbox": testAccProvider,
}
}
281 changes: 281 additions & 0 deletions netbox/dcim/resource_netbox_dcim_manufacturer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,281 @@
package dcim

import (
"context"
"strconv"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
netboxclient "github.com/smutel/go-netbox/v3/netbox/client"
"github.com/smutel/go-netbox/v3/netbox/client/dcim"
"github.com/smutel/go-netbox/v3/netbox/models"
"github.com/smutel/terraform-provider-netbox/v4/netbox/internal/customfield"
"github.com/smutel/terraform-provider-netbox/v4/netbox/internal/requestmodifier"
"github.com/smutel/terraform-provider-netbox/v4/netbox/internal/tag"
"github.com/smutel/terraform-provider-netbox/v4/netbox/internal/util"
)

func ResourceNetboxDcimManufacturer() *schema.Resource {
return &schema.Resource{
Description: "Manage a tag (extra module) within Netbox.",
CreateContext: resourceNetboxDcimManufacturerCreate,
ReadContext: resourceNetboxDcimManufacturerRead,
UpdateContext: resourceNetboxDcimManufacturerUpdate,
DeleteContext: resourceNetboxDcimManufacturerDelete,
Exists: resourceNetboxDcimManufacturerExists,
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},

Schema: map[string]*schema.Schema{
"content_type": {
Type: schema.TypeString,
Computed: true,
Description: "The content type of this manufacturer (dcim module).",
},
"created": {
Type: schema.TypeString,
Computed: true,
Description: "Date when this manufacturer was created.",
},
"custom_field": &customfield.CustomFieldSchema,
"description": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.StringLenBetween(0, 100),
Description: "The description of this manufacturer (dcim module).",
},
"devicetype_count": {
Type: schema.TypeInt,
Computed: true,
Description: "The number of device types of this manufacturer (dcim module).",
},
"inventoryitem_count": {
Type: schema.TypeInt,
Computed: true,
Description: "The number of inventory items of this manufacturer (dcim module).",
},
"last_updated": {
Type: schema.TypeString,
Computed: true,
Description: "Date when this manufacturer was last updated.",
},
"name": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringLenBetween(1, 100),
Description: "The name of this manufacturer (dcim module).",
},
"platform_count": {
Type: schema.TypeInt,
Computed: true,
Description: "The number of platforms of this manufacturer (dcim module).",
},
"slug": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringLenBetween(1, 100),
Description: "The slug of this manufacturer (dcim module).",
},
"tag": &tag.TagSchema,
"url": {
Type: schema.TypeString,
Computed: true,
Description: "The link to this manufacturer (dcim module).",
},
},
}
}

var manufacturerRequiredFields = []string{
"created",
"last_updated",
"name",
"slug",
"tags",
}

func resourceNetboxDcimManufacturerCreate(ctx context.Context, d *schema.ResourceData,
m interface{}) diag.Diagnostics {
client := m.(*netboxclient.NetBoxAPI)

resourceCustomFields := d.Get("custom_field").(*schema.Set).List()
customFields := customfield.ConvertCustomFieldsFromTerraformToAPI(nil, resourceCustomFields)
name := d.Get("name").(string)
slug := d.Get("slug").(string)
tags := d.Get("tag").(*schema.Set).List()

newResource := &models.Manufacturer{
CustomFields: customFields,
Description: d.Get("description").(string),
Name: &name,
Slug: &slug,
Tags: tag.ConvertTagsToNestedTags(tags),
}

resource := dcim.NewDcimManufacturersCreateParams().WithData(newResource)

resourceCreated, err := client.Dcim.DcimManufacturersCreate(resource, nil)
if err != nil {
return diag.FromErr(err)
}

d.SetId(strconv.FormatInt(resourceCreated.Payload.ID, 10))

return resourceNetboxDcimManufacturerRead(ctx, d, m)
}

func resourceNetboxDcimManufacturerRead(ctx context.Context, d *schema.ResourceData,
m interface{}) diag.Diagnostics {
client := m.(*netboxclient.NetBoxAPI)

resourceID := d.Id()
params := dcim.NewDcimManufacturersListParams().WithID(&resourceID)
resources, err := client.Dcim.DcimManufacturersList(params, nil)
if err != nil {
return diag.FromErr(err)
}

if len(resources.Payload.Results) != 1 {
d.SetId("")
return nil
}

resource := resources.Payload.Results[0]

if err = d.Set("content_type", util.ConvertURIContentType(resource.URL)); err != nil {
return diag.FromErr(err)
}
if err = d.Set("created", resource.Created.String()); err != nil {
return diag.FromErr(err)
}
resourceCustomFields := d.Get("custom_field").(*schema.Set).List()
customFields := customfield.UpdateCustomFieldsFromAPI(resourceCustomFields, resource.CustomFields)

if err = d.Set("custom_field", customFields); err != nil {
return diag.FromErr(err)
}
if err = d.Set("description", resource.Description); err != nil {
return diag.FromErr(err)
}
if err = d.Set("devicetype_count", resource.DevicetypeCount); err != nil {
return diag.FromErr(err)
}
if err = d.Set("inventoryitem_count", resource.InventoryitemCount); err != nil {
return diag.FromErr(err)
}
if err = d.Set("last_updated", resource.LastUpdated.String()); err != nil {
return diag.FromErr(err)
}
if err = d.Set("name", resource.Name); err != nil {
return diag.FromErr(err)
}
if err = d.Set("platform_count", resource.PlatformCount); err != nil {
return diag.FromErr(err)
}
if err = d.Set("slug", resource.Slug); err != nil {
return diag.FromErr(err)
}
if err = d.Set("tag", tag.ConvertNestedTagsToTags(resource.Tags)); err != nil {
return diag.FromErr(err)
}
if err = d.Set("url", resource.URL); err != nil {
return diag.FromErr(err)
}

return nil
}

func resourceNetboxDcimManufacturerUpdate(ctx context.Context, d *schema.ResourceData,
m interface{}) diag.Diagnostics {
client := m.(*netboxclient.NetBoxAPI)
modifiedFields := make(map[string]interface{})

resourceID, err := strconv.ParseInt(d.Id(), 10, 64)
if err != nil {
return diag.Errorf("Unable to convert ID into int64")
}
params := &models.Manufacturer{}

if d.HasChange("custom_field") {
stateCustomFields, resourceCustomFields := d.GetChange("custom_field")
customFields := customfield.ConvertCustomFieldsFromTerraformToAPI(stateCustomFields.(*schema.Set).List(), resourceCustomFields.(*schema.Set).List())
params.CustomFields = &customFields
}
if d.HasChange("description") {
description := d.Get("description").(string)
params.Description = description
modifiedFields["description"] = description
}
if d.HasChange("name") {
name := d.Get("name").(string)
params.Name = &name
}
if d.HasChange("slug") {
slug := d.Get("slug").(string)
params.Slug = &slug
}
if d.HasChange("tag") {
tags := d.Get("tag").(*schema.Set).List()
params.Tags = tag.ConvertTagsToNestedTags(tags)
}

resource := dcim.NewDcimManufacturersPartialUpdateParams().WithData(params)

resource.SetID(resourceID)

_, err = client.Dcim.DcimManufacturersPartialUpdate(resource, nil, requestmodifier.NewNetboxRequestModifier(modifiedFields, manufacturerRequiredFields))
if err != nil {
return diag.FromErr(err)
}

return resourceNetboxDcimManufacturerRead(ctx, d, m)
}

func resourceNetboxDcimManufacturerDelete(ctx context.Context, d *schema.ResourceData,
m interface{}) diag.Diagnostics {
client := m.(*netboxclient.NetBoxAPI)

resourceExists, err := resourceNetboxDcimManufacturerExists(d, m)
if err != nil {
return diag.FromErr(err)
}

if !resourceExists {
return nil
}

id, err := strconv.ParseInt(d.Id(), 10, 64)
if err != nil {
return diag.Errorf("Unable to convert ID into int64")
}

resource := dcim.NewDcimManufacturersDeleteParams().WithID(id)
if _, err := client.Dcim.DcimManufacturersDelete(resource, nil); err != nil {
return diag.FromErr(err)
}

return nil
}

func resourceNetboxDcimManufacturerExists(d *schema.ResourceData,
m interface{}) (b bool, e error) {
client := m.(*netboxclient.NetBoxAPI)
resourceExist := false

resourceID := d.Id()
params := dcim.NewDcimManufacturersListParams().WithID(&resourceID)
resources, err := client.Dcim.DcimManufacturersList(params, nil)
if err != nil {
return resourceExist, err
}

for _, resource := range resources.Payload.Results {
if strconv.FormatInt(resource.ID, 10) == d.Id() {
resourceExist = true
}
}

return resourceExist, nil
}
Loading

0 comments on commit f214f6a

Please sign in to comment.