Skip to content

Commit

Permalink
feat: link instance group with organization (#85)
Browse files Browse the repository at this point in the history
* feat: link instance group with organization

* fix: support mac arm64

* fix: add read resource

* fix: makefile
  • Loading branch information
Tioborto authored Dec 10, 2024
1 parent 2c7b8d6 commit 21dbb14
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 3 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ 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:
go build -o ${BINARY}

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
Expand Down
1 change: 1 addition & 0 deletions awx/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
101 changes: 101 additions & 0 deletions awx/resource_organization_instance_group.go
Original file line number Diff line number Diff line change
@@ -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
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down

0 comments on commit 21dbb14

Please sign in to comment.