forked from e-breuninger/terraform-provider-netbox
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathresource_netbox_inventory_item_role.go
More file actions
159 lines (129 loc) · 4.47 KB
/
Copy pathresource_netbox_inventory_item_role.go
File metadata and controls
159 lines (129 loc) · 4.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package netbox
import (
"strconv"
"github.com/fbreckle/go-netbox/netbox/client/dcim"
"github.com/fbreckle/go-netbox/netbox/models"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
)
func resourceNetboxInventoryItemRole() *schema.Resource {
return &schema.Resource{
Create: resourceNetboxInventoryItemRoleCreate,
Read: resourceNetboxInventoryItemRoleRead,
Update: resourceNetboxInventoryItemRoleUpdate,
Delete: resourceNetboxInventoryItemRoleDelete,
Description: `:meta:subcategory:Data Center Inventory Management (DCIM):From the [official documentation](https://docs.netbox.dev/en/stable/models/dcim/inventoryitemrole/):
> Inventory items can be organized by functional roles, which are fully customizable by the user. For example, you might create roles for power supplies, fans, interface optics, etc.`,
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
},
"slug": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringLenBetween(1, 100),
},
"color_hex": {
Type: schema.TypeString,
Required: true,
},
"description": {
Type: schema.TypeString,
Optional: true,
},
tagsKey: tagsSchema,
customFieldsKey: customFieldsSchema,
},
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},
}
}
func resourceNetboxInventoryItemRoleCreate(d *schema.ResourceData, m interface{}) error {
api := m.(*providerState)
data := models.InventoryItemRole{
Name: strToPtr(d.Get("name").(string)),
Slug: strToPtr(d.Get("slug").(string)),
Description: getOptionalStr(d, "description", false),
Color: getOptionalStr(d, "color_hex", false),
}
var err error
data.Tags, err = getNestedTagListFromResourceDataSet(api, d.Get(tagsAllKey))
if err != nil {
return err
}
ct, ok := d.GetOk(customFieldsKey)
if ok {
data.CustomFields = ct
}
params := dcim.NewDcimInventoryItemRolesCreateParams().WithData(&data)
res, err := api.Dcim.DcimInventoryItemRolesCreate(params, nil)
if err != nil {
return err
}
d.SetId(strconv.FormatInt(res.GetPayload().ID, 10))
return resourceNetboxInventoryItemRoleRead(d, m)
}
func resourceNetboxInventoryItemRoleRead(d *schema.ResourceData, m interface{}) error {
api := m.(*providerState)
id, _ := strconv.ParseInt(d.Id(), 10, 64)
params := dcim.NewDcimInventoryItemRolesReadParams().WithID(id)
res, err := api.Dcim.DcimInventoryItemRolesRead(params, nil)
if err != nil {
if errresp, ok := err.(*dcim.DcimInventoryItemRolesReadDefault); ok {
if errresp.Code() == 404 {
// If the ID is updated to blank, this tells Terraform the resource no longer exists (maybe it was destroyed out of band). Just like the destroy callback, the Read function should gracefully handle this case. https://www.terraform.io/docs/extend/writing-custom-providers.html
d.SetId("")
return nil
}
}
return err
}
role := res.GetPayload()
d.Set("name", role.Name)
d.Set("slug", role.Slug)
d.Set("color_hex", role.Color)
d.Set("description", role.Description)
cf := getCustomFields(res.GetPayload().CustomFields)
if cf != nil {
d.Set(customFieldsKey, cf)
}
api.readTags(d, res.GetPayload().Tags)
return nil
}
func resourceNetboxInventoryItemRoleUpdate(d *schema.ResourceData, m interface{}) error {
api := m.(*providerState)
id, _ := strconv.ParseInt(d.Id(), 10, 64)
data := models.InventoryItemRole{
Name: strToPtr(d.Get("name").(string)),
Slug: strToPtr(d.Get("slug").(string)),
Description: getOptionalStr(d, "description", true),
Color: getOptionalStr(d, "color_hex", false),
}
var err error
data.Tags, err = getNestedTagListFromResourceDataSet(api, d.Get(tagsAllKey))
if err != nil {
return err
}
ct, ok := d.GetOk(customFieldsKey)
if ok {
data.CustomFields = ct
}
params := dcim.NewDcimInventoryItemRolesPartialUpdateParams().WithID(id).WithData(&data)
_, err = api.Dcim.DcimInventoryItemRolesPartialUpdate(params, nil)
if err != nil {
return err
}
return resourceNetboxInventoryItemRoleRead(d, m)
}
func resourceNetboxInventoryItemRoleDelete(d *schema.ResourceData, m interface{}) error {
api := m.(*providerState)
id, _ := strconv.ParseInt(d.Id(), 10, 64)
params := dcim.NewDcimInventoryItemRolesDeleteParams().WithID(id)
_, err := api.Dcim.DcimInventoryItemRolesDelete(params, nil)
if err != nil {
return err
}
return nil
}