From 21dbb1492ff66a07002d1898eb3d7588158b3ee3 Mon Sep 17 00:00:00 2001 From: Alexandre Bortoluzzi Date: Tue, 10 Dec 2024 10:08:20 +0100 Subject: [PATCH] feat: link instance group with organization (#85) * feat: link instance group with organization * fix: support mac arm64 * fix: add read resource * fix: makefile --- Makefile | 4 +- awx/provider.go | 1 + awx/resource_organization_instance_group.go | 101 ++++++++++++++++++++ go.mod | 2 +- go.sum | 2 + 5 files changed, 107 insertions(+), 3 deletions(-) create mode 100644 awx/resource_organization_instance_group.go diff --git a/Makefile b/Makefile index 9098f21e..704cd343 100644 --- a/Makefile +++ b/Makefile @@ -4,8 +4,7 @@ NAMESPACE=denouche NAME=awx BINARY=terraform-provider-${NAME} VERSION=0.1 -OS_ARCH=linux_amd64 #darwin_amd64 - +OS_ARCH=linux_amd64 # darwin_amd64 default: install build: @@ -13,6 +12,7 @@ build: release: GOOS=darwin GOARCH=amd64 go build -o ./bin/${BINARY}_${VERSION}_darwin_amd64 + GOOS=darwin GOARCH=arm64 go build -o ./bin/${BINARY}_${VERSION}_darwin_arm64 GOOS=freebsd GOARCH=386 go build -o ./bin/${BINARY}_${VERSION}_freebsd_386 GOOS=freebsd GOARCH=amd64 go build -o ./bin/${BINARY}_${VERSION}_freebsd_amd64 GOOS=freebsd GOARCH=arm go build -o ./bin/${BINARY}_${VERSION}_freebsd_arm diff --git a/awx/provider.go b/awx/provider.go index af1b7ef0..e86f4a82 100644 --- a/awx/provider.go +++ b/awx/provider.go @@ -66,6 +66,7 @@ func Provider() *schema.Provider { "awx_job_template_notification_template_success": resourceJobTemplateNotificationTemplateSuccess(), "awx_notification_template": resourceNotificationTemplate(), "awx_organization": resourceOrganization(), + "awx_organization_instance_group": resourceOrganizationInstanceGroup(), "awx_organization_galaxy_credential": resourceOrganizationsGalaxyCredentials(), "awx_project": resourceProject(), "awx_schedule": resourceSchedule(), diff --git a/awx/resource_organization_instance_group.go b/awx/resource_organization_instance_group.go new file mode 100644 index 00000000..49f31af2 --- /dev/null +++ b/awx/resource_organization_instance_group.go @@ -0,0 +1,101 @@ +/* +This resource lets you attach or detach an instance group to an organization + +Example Usage + +```hcl +resource "awx_organization" "default" { + name = "default" +} + +resource "awx_instance_group" "default" { + name = "my-default" +} + +resource "awx_organization_instance_group" "default" { + organization_id = awx_organization.default.id + instance_group_id = awx_instance_group.default.id +} +``` + +*/ +package awx + +import ( + "context" + "fmt" + "strconv" + + awx "github.com/denouche/goawx/client" + "github.com/hashicorp/terraform-plugin-sdk/v2/diag" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" +) + +func resourceOrganizationInstanceGroup() *schema.Resource { + return &schema.Resource{ + CreateContext: resourceOrganizationInstanceGroupCreate, + DeleteContext: resourceOrganizationInstanceGroupDelete, + ReadContext: resourceOrganizationInstanceGroupRead, + + Schema: map[string]*schema.Schema{ + "organization_id": { + Type: schema.TypeInt, + Required: true, + ForceNew: true, + }, + "instance_group_id": { + Type: schema.TypeInt, + Required: true, + ForceNew: true, + }, + }, + } +} + +func resourceOrganizationInstanceGroupRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { + var diags diag.Diagnostics + return diags +} + +func resourceOrganizationInstanceGroupCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { + var diags diag.Diagnostics + client := m.(*awx.AWX) + awxService := client.OrganizationsService + OrganizationID := d.Get("organization_id").(int) + _, err := awxService.GetOrganizationsByID(OrganizationID, make(map[string]string)) + if err != nil { + return buildDiagNotFoundFail("organization", OrganizationID, err) + } + + result, err := awxService.AssociateInstanceGroups(OrganizationID, map[string]interface{}{ + "id": d.Get("instance_group_id").(int), + }, map[string]string{}) + + if err != nil { + return buildDiagnosticsMessage("Create: Organization not AssociateInstanceGroups", "Fail to associate Instance Group %v with Organization %v, got error: %s", d.Get("instance_group_id").(int), d.Get("organization_id").(int), err.Error()) + } + + d.SetId(strconv.Itoa(result.ID)) + return diags +} + +func resourceOrganizationInstanceGroupDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { + var diags diag.Diagnostics + client := m.(*awx.AWX) + awxService := client.OrganizationsService + OrganizationID := d.Get("organization_id").(int) + res, err := awxService.GetOrganizationsByID(OrganizationID, make(map[string]string)) + if err != nil { + return buildDiagNotFoundFail("organization", OrganizationID, err) + } + + _, err = awxService.DisAssociateInstanceGroups(res.ID, map[string]interface{}{ + "id": d.Get("instance_group_id").(int), + }, map[string]string{}) + if err != nil { + return buildDiagDeleteFail("Organization DisAssociateInstanceGroups", fmt.Sprintf("Fail to disassociate Instance Group %v from Organization %v, got error: %s ", d.Get("instance_group_id").(int), d.Get("organization_id").(int), err.Error())) + } + + d.SetId("") + return diags +} diff --git a/go.mod b/go.mod index d9a20728..ee432aa5 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module github.com/denouche/terraform-provider-awx go 1.20 require ( - github.com/denouche/goawx v0.20.1 + github.com/denouche/goawx v0.21.1 github.com/gruntwork-io/terratest v0.31.2 github.com/hashicorp/terraform-plugin-sdk/v2 v2.30.0 github.com/stretchr/testify v1.8.3 diff --git a/go.sum b/go.sum index 1fabba86..bfc138e8 100644 --- a/go.sum +++ b/go.sum @@ -99,6 +99,8 @@ github.com/denouche/goawx v0.20.0 h1:wGrHMrAE1qLaE3DoYseCV8yIIfeIVw/HDNRw2zx0CwA github.com/denouche/goawx v0.20.0/go.mod h1:MppzSteoj2xgfiqiRWW/Bf1a8z2FrRyvah1z0J2vJTY= github.com/denouche/goawx v0.20.1 h1:Mnkk1QxGPBvo3U4FDhcdHFrSMwIT+IwAkLDVQ7dCAoc= github.com/denouche/goawx v0.20.1/go.mod h1:MppzSteoj2xgfiqiRWW/Bf1a8z2FrRyvah1z0J2vJTY= +github.com/denouche/goawx v0.21.1 h1:ImTBH6/L3u8QInlPHMWO02su4lQPHudj52ZvwKeL86A= +github.com/denouche/goawx v0.21.1/go.mod h1:Bdo/LeUgeemE9Xt4bOVFVO6GJMxxUcduhQPDD5+yQ1A= github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ= github.com/dimchansky/utfbom v1.1.0/go.mod h1:rO41eb7gLfo8SF1jd9F8HplJm1Fewwi4mQvIirEdv+8= github.com/dnaeon/go-vcr v1.0.1/go.mod h1:aBB1+wY4s93YsC3HHjMBMrwTj2R9FHDzUr9KyGc8n1E=