|
| 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 | +} |
0 commit comments