Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions docs/resources/inventory_instance_groups.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "awx_inventory_instance_groups Resource - terraform-provider-awx"
subcategory: ""
description: |-
Associates an instance group to an inventory
---

# awx_inventory_instance_groups (Resource)

Associates an instance group to an inventory



<!-- schema generated by tfplugindocs -->
## Schema

### Required

- `instance_group_id` (Number) The ID of the instance group to associate with the inventory
- `inventory_id` (Number) The ID of the inventory to associate the instance group with

### Read-Only

- `id` (String) The ID of this resource.
1 change: 1 addition & 0 deletions internal/awx/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ func Provider() *schema.Provider { //nolint:funlen
"awx_inventory_group": resourceInventoryGroup(),
"awx_inventory_source": resourceInventorySource(),
"awx_inventory": resourceInventory(),
"awx_inventory_instance_groups": resourceInventoryInstanceGroups(),
"awx_job_template_credential": resourceJobTemplateCredentials(),
"awx_job_template_instance_groups": resourceJobTemplateInstanceGroups(),
"awx_job_template": resourceJobTemplate(),
Expand Down
77 changes: 77 additions & 0 deletions internal/awx/resource_inventory_instance_groups.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package awx

import (
"context"
"strconv"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
awx "github.com/josh-silvas/terraform-provider-awx/tools/goawx"
"github.com/josh-silvas/terraform-provider-awx/tools/utils"
)

func resourceInventoryInstanceGroups() *schema.Resource {
return &schema.Resource{
Description: "Associates an instance group to an inventory",
CreateContext: resourceInventoryInstanceGroupsCreate,
DeleteContext: resourceInventoryInstanceGroupsDelete,
ReadContext: resourceInventoryInstanceGroupsRead,

Schema: map[string]*schema.Schema{

"inventory_id": {
Type: schema.TypeInt,
Required: true,
ForceNew: true,
Description: "The ID of the inventory to associate the instance group with",
},
"instance_group_id": {
Type: schema.TypeInt,
Required: true,
ForceNew: true,
Description: "The ID of the instance group to associate with the inventory",
},
},
}
}

func resourceInventoryInstanceGroupsCreate(_ context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
client := m.(*awx.AWX)
inventoryID := d.Get("inventory_id").(int)
if _, err := client.InventoriesService.GetInventoryByID(inventoryID, make(map[string]string)); err != nil {
return utils.DiagNotFound("Inventory InstanceGroup", inventoryID, err)
}

result, err := client.InventoriesService.AssociateInstanceGroups(inventoryID, map[string]interface{}{
"id": d.Get("instance_group_id").(int),
}, map[string]string{})

if err != nil {
return utils.DiagCreate("Inventory AssociateInstanceGroups", err)
}

d.SetId(strconv.Itoa(result.ID))
return nil
}

func resourceInventoryInstanceGroupsRead(_ context.Context, _ *schema.ResourceData, _ interface{}) diag.Diagnostics {
return nil
}

func resourceInventoryInstanceGroupsDelete(_ context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
client := m.(*awx.AWX)
inventoryID := d.Get("inventory_id").(int)
res, err := client.InventoriesService.GetInventoryByID(inventoryID, make(map[string]string))
if err != nil {
return utils.DiagNotFound("Inventory InstanceGroup", inventoryID, err)
}

if _, err = client.InventoriesService.DisAssociateInstanceGroups(res.ID, map[string]interface{}{
"id": d.Get("instance_group_id").(int),
}, map[string]string{}); err != nil {
return utils.DiagDelete("Inventory DisAssociateInstanceGroups", inventoryID, err)
}

d.SetId("")
return nil
}
69 changes: 69 additions & 0 deletions tools/goawx/inventories.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,3 +174,72 @@ func (i *InventoriesService) DeleteInventory(id int) (*Inventory, error) {

return result, nil
}

// DisAssociateInstanceGroups remove InstanceGroup from an awx Inventory.
func (i *InventoriesService) DisAssociateInstanceGroups(id int, data map[string]interface{}, _ map[string]string) (*Inventory, error) {
result := new(Inventory)
endpoint := fmt.Sprintf("%s%d/instance_groups/", inventoriesAPIEndpoint, id)
data["disassociate"] = true
mandatoryFields = []string{"id"}
validate, status := ValidateParams(data, mandatoryFields)
if !status {
err := fmt.Errorf("mandatory input arguments are absent: %s", validate)
return nil, err
}
payload, err := json.Marshal(data)
if err != nil {
return nil, err
}
resp, err := i.client.Requester.PostJSON(endpoint, bytes.NewReader(payload), result, nil)
if resp != nil {
func() {
if err := resp.Body.Close(); err != nil {
fmt.Println(err)
}
}()
}
if err != nil {
return nil, err
}

if err := CheckResponse(resp); err != nil {
return nil, err
}

return result, nil
}

// AssociateInstanceGroups adding InstanceGroup to Inventory.
func (i *InventoriesService) AssociateInstanceGroups(id int, data map[string]interface{}, _ map[string]string) (*Inventory, error) {
result := new(Inventory)

endpoint := fmt.Sprintf("%s%d/instance_groups/", inventoriesAPIEndpoint, id)
data["associate"] = true
mandatoryFields = []string{"id"}
validate, status := ValidateParams(data, mandatoryFields)
if !status {
err := fmt.Errorf("mandatory input arguments are absent: %s", validate)
return nil, err
}
payload, err := json.Marshal(data)
if err != nil {
return nil, err
}
resp, err := i.client.Requester.PostJSON(endpoint, bytes.NewReader(payload), result, nil)
if resp != nil {
func() {
if err := resp.Body.Close(); err != nil {
fmt.Println(err)
}
}()
}
if err != nil {
return nil, err
}

if err := CheckResponse(resp); err != nil {
return nil, err
}

return result, nil
}