forked from denouche/terraform-provider-awx
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11 from quentinleclerc/add-inventory-instance-gro…
…up-association Add inventory instance group association
- Loading branch information
Showing
4 changed files
with
172 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters