-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathresource_inventory_group.go
137 lines (121 loc) · 3.5 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
package awx
import (
"fmt"
"strconv"
"github.com/hashicorp/terraform/helper/schema"
awxgo "github.com/mauromedda/awx-go"
)
func resourceInventoryGroupObject() *schema.Resource {
return &schema.Resource{
Create: resourceInventoryGroupCreate,
Read: resourceInventoryGroupRead,
Update: resourceInventoryGroupUpdate,
Delete: resourceInventoryGroupDelete,
Schema: map[string]*schema.Schema{
"name": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
"description": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Default: "",
},
"inventory_id": &schema.Schema{
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},
"variables": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Default: "",
StateFunc: normalizeJsonYaml,
},
},
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
}
}
func resourceInventoryGroupCreate(d *schema.ResourceData, m interface{}) error {
awx := m.(*awxgo.AWX)
awxService := awx.GroupService
_, res, _ := awxService.ListGroups(map[string]string{
"name": d.Get("name").(string),
"inventory": d.Get("inventory_id").(string),
})
if len(res.Results) >= 1 {
return fmt.Errorf("InventoryGroup %s with id %d already exists", res.Results[0].Name, res.Results[0].ID)
}
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 err
}
d.SetId(strconv.Itoa(result.ID))
return resourceInventoryGroupRead(d, m)
}
func resourceInventoryGroupUpdate(d *schema.ResourceData, m interface{}) error {
awx := m.(*awxgo.AWX)
awxService := awx.GroupService
id, err := strconv.Atoi(d.Id())
if err != nil {
return err
}
_, res, _ := awxService.ListGroups(map[string]string{"id": d.Id()})
if len(res.Results) >= 1 {
_, 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 err
}
return resourceInventoryGroupRead(d, m)
}
return fmt.Errorf("Group %s with id %d doesn't exist", d.Get("name").(string), id)
}
func resourceInventoryGroupDelete(d *schema.ResourceData, m interface{}) error {
awx := m.(*awxgo.AWX)
awxService := awx.GroupService
id, err := strconv.Atoi(d.Id())
if err != nil {
return fmt.Errorf("InventoryGroup %d not found", id)
}
if _, err := awxService.DeleteGroup(id); err != nil {
return err
}
d.SetId("")
return nil
}
func resourceInventoryGroupRead(d *schema.ResourceData, m interface{}) error {
awx := m.(*awxgo.AWX)
awxService := awx.GroupService
id, err := strconv.Atoi(d.Id())
if err != nil {
return fmt.Errorf("InventoryGroup %d not found", id)
}
_, res, err := awxService.ListGroups(map[string]string{
"name": d.Get("name").(string),
"inventory": d.Get("inventory_id").(string),
})
if err != nil {
return err
}
d = setInventoryGroupResourceData(d, res.Results[0])
return nil
}
func setInventoryGroupResourceData(d *schema.ResourceData, r *awxgo.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))
return d
}