Skip to content

Commit 4f642ca

Browse files
authored
Merge pull request #11 from quentinleclerc/add-inventory-instance-group-association
Add inventory instance group association
2 parents 10b70e1 + 8307c92 commit 4f642ca

4 files changed

Lines changed: 172 additions & 0 deletions

File tree

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
---
2+
# generated by https://github.com/hashicorp/terraform-plugin-docs
3+
page_title: "awx_inventory_instance_groups Resource - terraform-provider-awx"
4+
subcategory: ""
5+
description: |-
6+
Associates an instance group to an inventory
7+
---
8+
9+
# awx_inventory_instance_groups (Resource)
10+
11+
Associates an instance group to an inventory
12+
13+
14+
15+
<!-- schema generated by tfplugindocs -->
16+
## Schema
17+
18+
### Required
19+
20+
- `instance_group_id` (Number) The ID of the instance group to associate with the inventory
21+
- `inventory_id` (Number) The ID of the inventory to associate the instance group with
22+
23+
### Read-Only
24+
25+
- `id` (String) The ID of this resource.

internal/awx/provider.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ func Provider() *schema.Provider { //nolint:funlen
6868
"awx_inventory_group": resourceInventoryGroup(),
6969
"awx_inventory_source": resourceInventorySource(),
7070
"awx_inventory": resourceInventory(),
71+
"awx_inventory_instance_groups": resourceInventoryInstanceGroups(),
7172
"awx_job_template_credential": resourceJobTemplateCredentials(),
7273
"awx_job_template_instance_groups": resourceJobTemplateInstanceGroups(),
7374
"awx_job_template": resourceJobTemplate(),
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package awx
2+
3+
import (
4+
"context"
5+
"strconv"
6+
7+
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
8+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
9+
awx "github.com/josh-silvas/terraform-provider-awx/tools/goawx"
10+
"github.com/josh-silvas/terraform-provider-awx/tools/utils"
11+
)
12+
13+
func resourceInventoryInstanceGroups() *schema.Resource {
14+
return &schema.Resource{
15+
Description: "Associates an instance group to an inventory",
16+
CreateContext: resourceInventoryInstanceGroupsCreate,
17+
DeleteContext: resourceInventoryInstanceGroupsDelete,
18+
ReadContext: resourceInventoryInstanceGroupsRead,
19+
20+
Schema: map[string]*schema.Schema{
21+
22+
"inventory_id": {
23+
Type: schema.TypeInt,
24+
Required: true,
25+
ForceNew: true,
26+
Description: "The ID of the inventory to associate the instance group with",
27+
},
28+
"instance_group_id": {
29+
Type: schema.TypeInt,
30+
Required: true,
31+
ForceNew: true,
32+
Description: "The ID of the instance group to associate with the inventory",
33+
},
34+
},
35+
}
36+
}
37+
38+
func resourceInventoryInstanceGroupsCreate(_ context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
39+
client := m.(*awx.AWX)
40+
inventoryID := d.Get("inventory_id").(int)
41+
if _, err := client.InventoriesService.GetInventoryByID(inventoryID, make(map[string]string)); err != nil {
42+
return utils.DiagNotFound("Inventory InstanceGroup", inventoryID, err)
43+
}
44+
45+
result, err := client.InventoriesService.AssociateInstanceGroups(inventoryID, map[string]interface{}{
46+
"id": d.Get("instance_group_id").(int),
47+
}, map[string]string{})
48+
49+
if err != nil {
50+
return utils.DiagCreate("Inventory AssociateInstanceGroups", err)
51+
}
52+
53+
d.SetId(strconv.Itoa(result.ID))
54+
return nil
55+
}
56+
57+
func resourceInventoryInstanceGroupsRead(_ context.Context, _ *schema.ResourceData, _ interface{}) diag.Diagnostics {
58+
return nil
59+
}
60+
61+
func resourceInventoryInstanceGroupsDelete(_ context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
62+
client := m.(*awx.AWX)
63+
inventoryID := d.Get("inventory_id").(int)
64+
res, err := client.InventoriesService.GetInventoryByID(inventoryID, make(map[string]string))
65+
if err != nil {
66+
return utils.DiagNotFound("Inventory InstanceGroup", inventoryID, err)
67+
}
68+
69+
if _, err = client.InventoriesService.DisAssociateInstanceGroups(res.ID, map[string]interface{}{
70+
"id": d.Get("instance_group_id").(int),
71+
}, map[string]string{}); err != nil {
72+
return utils.DiagDelete("Inventory DisAssociateInstanceGroups", inventoryID, err)
73+
}
74+
75+
d.SetId("")
76+
return nil
77+
}

tools/goawx/inventories.go

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,3 +174,72 @@ func (i *InventoriesService) DeleteInventory(id int) (*Inventory, error) {
174174

175175
return result, nil
176176
}
177+
178+
// DisAssociateInstanceGroups remove InstanceGroup from an awx Inventory.
179+
func (i *InventoriesService) DisAssociateInstanceGroups(id int, data map[string]interface{}, _ map[string]string) (*Inventory, error) {
180+
result := new(Inventory)
181+
endpoint := fmt.Sprintf("%s%d/instance_groups/", inventoriesAPIEndpoint, id)
182+
data["disassociate"] = true
183+
mandatoryFields = []string{"id"}
184+
validate, status := ValidateParams(data, mandatoryFields)
185+
if !status {
186+
err := fmt.Errorf("mandatory input arguments are absent: %s", validate)
187+
return nil, err
188+
}
189+
payload, err := json.Marshal(data)
190+
if err != nil {
191+
return nil, err
192+
}
193+
resp, err := i.client.Requester.PostJSON(endpoint, bytes.NewReader(payload), result, nil)
194+
if resp != nil {
195+
func() {
196+
if err := resp.Body.Close(); err != nil {
197+
fmt.Println(err)
198+
}
199+
}()
200+
}
201+
if err != nil {
202+
return nil, err
203+
}
204+
205+
if err := CheckResponse(resp); err != nil {
206+
return nil, err
207+
}
208+
209+
return result, nil
210+
}
211+
212+
// AssociateInstanceGroups adding InstanceGroup to Inventory.
213+
func (i *InventoriesService) AssociateInstanceGroups(id int, data map[string]interface{}, _ map[string]string) (*Inventory, error) {
214+
result := new(Inventory)
215+
216+
endpoint := fmt.Sprintf("%s%d/instance_groups/", inventoriesAPIEndpoint, id)
217+
data["associate"] = true
218+
mandatoryFields = []string{"id"}
219+
validate, status := ValidateParams(data, mandatoryFields)
220+
if !status {
221+
err := fmt.Errorf("mandatory input arguments are absent: %s", validate)
222+
return nil, err
223+
}
224+
payload, err := json.Marshal(data)
225+
if err != nil {
226+
return nil, err
227+
}
228+
resp, err := i.client.Requester.PostJSON(endpoint, bytes.NewReader(payload), result, nil)
229+
if resp != nil {
230+
func() {
231+
if err := resp.Body.Close(); err != nil {
232+
fmt.Println(err)
233+
}
234+
}()
235+
}
236+
if err != nil {
237+
return nil, err
238+
}
239+
240+
if err := CheckResponse(resp); err != nil {
241+
return nil, err
242+
}
243+
244+
return result, nil
245+
}

0 commit comments

Comments
 (0)