diff --git a/docs/resources/inventory_instance_groups.md b/docs/resources/inventory_instance_groups.md new file mode 100644 index 00000000..6e5b9444 --- /dev/null +++ b/docs/resources/inventory_instance_groups.md @@ -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. diff --git a/internal/awx/provider.go b/internal/awx/provider.go index ccdd01a2..396b6174 100644 --- a/internal/awx/provider.go +++ b/internal/awx/provider.go @@ -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(), diff --git a/internal/awx/resource_inventory_instance_groups.go b/internal/awx/resource_inventory_instance_groups.go new file mode 100644 index 00000000..e0501751 --- /dev/null +++ b/internal/awx/resource_inventory_instance_groups.go @@ -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 +} diff --git a/tools/goawx/inventories.go b/tools/goawx/inventories.go index 615c9e09..5c9c2742 100644 --- a/tools/goawx/inventories.go +++ b/tools/goawx/inventories.go @@ -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 +}