forked from wdreeveii/terraform-provider-awx
/
resource_inventory_group.go
145 lines (121 loc) · 3.58 KB
/
resource_inventory_group.go
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
/*
*TBD*
Example Usage
```hcl
*TBD*
```
*/
package awx
import (
"context"
"fmt"
"strconv"
awx "github.com/denouche/goawx/client"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func resourceInventoryGroup() *schema.Resource {
return &schema.Resource{
CreateContext: resourceInventoryGroupCreate,
ReadContext: resourceInventoryGroupRead,
UpdateContext: resourceInventoryGroupUpdate,
DeleteContext: resourceInventoryGroupDelete,
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
},
"description": {
Type: schema.TypeString,
Optional: true,
Default: "",
},
"inventory_id": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},
"variables": {
Type: schema.TypeString,
Optional: true,
Default: "",
StateFunc: normalizeJsonYaml,
},
},
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
}
}
func resourceInventoryGroupCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
client := m.(*awx.AWX)
awxService := client.GroupService
result, err := awxService.CreateGroup(map[string]interface{}{
"name": d.Get("name").(string),
"description": d.Get("description").(string),
"inventory": d.Get("inventory_id").(string),
"variables": d.Get("variables").(string),
}, map[string]string{})
if err != nil {
return buildDiagCreateFail(diagElementInventoryGroupTitle, err)
}
d.SetId(strconv.Itoa(result.ID))
return resourceInventoryGroupRead(ctx, d, m)
}
func resourceInventoryGroupUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
client := m.(*awx.AWX)
awxService := client.GroupService
id, diags := convertStateIDToNummeric(diagElementInventoryGroupTitle, d)
if diags.HasError() {
return diags
}
_, err := awxService.UpdateGroup(id, map[string]interface{}{
"name": d.Get("name").(string),
"description": d.Get("description").(string),
"inventory": d.Get("inventory_id").(string),
"variables": d.Get("variables").(string),
}, nil)
if err != nil {
return buildDiagUpdateFail(diagElementInventoryGroupTitle, id, err)
}
return resourceInventoryGroupRead(ctx, d, m)
}
func resourceInventoryGroupDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
client := m.(*awx.AWX)
awxService := client.GroupService
id, diags := convertStateIDToNummeric(diagElementInventoryGroupTitle, d)
if diags.HasError() {
return diags
}
if _, err := awxService.DeleteGroup(id); err != nil {
return buildDiagDeleteFail(
diagElementInventoryGroupTitle,
fmt.Sprintf("ID: %v, got %s ",
id, err.Error()))
}
d.SetId("")
return nil
}
func resourceInventoryGroupRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
var diags diag.Diagnostics
client := m.(*awx.AWX)
awxService := client.GroupService
id, diags := convertStateIDToNummeric(diagElementInventoryGroupTitle, d)
if diags.HasError() {
return diags
}
res, err := awxService.GetGroupByID(id, make(map[string]string))
if err != nil {
return buildDiagNotFoundFail(diagElementInventoryGroupTitle, id, err)
}
d = setInventoryGroupResourceData(d, res)
return diags
}
func setInventoryGroupResourceData(d *schema.ResourceData, r *awx.Group) *schema.ResourceData {
d.Set("name", r.Name)
d.Set("description", r.Description)
d.Set("inventory_id", r.Inventory)
d.Set("variables", normalizeJsonYaml(r.Variables))
d.SetId(strconv.Itoa(r.ID))
return d
}