Skip to content

Commit 21dbb14

Browse files
authored
feat: link instance group with organization (#85)
* feat: link instance group with organization * fix: support mac arm64 * fix: add read resource * fix: makefile
1 parent 2c7b8d6 commit 21dbb14

5 files changed

Lines changed: 107 additions & 3 deletions

File tree

Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@ NAMESPACE=denouche
44
NAME=awx
55
BINARY=terraform-provider-${NAME}
66
VERSION=0.1
7-
OS_ARCH=linux_amd64 #darwin_amd64
8-
7+
OS_ARCH=linux_amd64 # darwin_amd64
98
default: install
109

1110
build:
1211
go build -o ${BINARY}
1312

1413
release:
1514
GOOS=darwin GOARCH=amd64 go build -o ./bin/${BINARY}_${VERSION}_darwin_amd64
15+
GOOS=darwin GOARCH=arm64 go build -o ./bin/${BINARY}_${VERSION}_darwin_arm64
1616
GOOS=freebsd GOARCH=386 go build -o ./bin/${BINARY}_${VERSION}_freebsd_386
1717
GOOS=freebsd GOARCH=amd64 go build -o ./bin/${BINARY}_${VERSION}_freebsd_amd64
1818
GOOS=freebsd GOARCH=arm go build -o ./bin/${BINARY}_${VERSION}_freebsd_arm

awx/provider.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ func Provider() *schema.Provider {
6666
"awx_job_template_notification_template_success": resourceJobTemplateNotificationTemplateSuccess(),
6767
"awx_notification_template": resourceNotificationTemplate(),
6868
"awx_organization": resourceOrganization(),
69+
"awx_organization_instance_group": resourceOrganizationInstanceGroup(),
6970
"awx_organization_galaxy_credential": resourceOrganizationsGalaxyCredentials(),
7071
"awx_project": resourceProject(),
7172
"awx_schedule": resourceSchedule(),
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
/*
2+
This resource lets you attach or detach an instance group to an organization
3+
4+
Example Usage
5+
6+
```hcl
7+
resource "awx_organization" "default" {
8+
name = "default"
9+
}
10+
11+
resource "awx_instance_group" "default" {
12+
name = "my-default"
13+
}
14+
15+
resource "awx_organization_instance_group" "default" {
16+
organization_id = awx_organization.default.id
17+
instance_group_id = awx_instance_group.default.id
18+
}
19+
```
20+
21+
*/
22+
package awx
23+
24+
import (
25+
"context"
26+
"fmt"
27+
"strconv"
28+
29+
awx "github.com/denouche/goawx/client"
30+
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
31+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
32+
)
33+
34+
func resourceOrganizationInstanceGroup() *schema.Resource {
35+
return &schema.Resource{
36+
CreateContext: resourceOrganizationInstanceGroupCreate,
37+
DeleteContext: resourceOrganizationInstanceGroupDelete,
38+
ReadContext: resourceOrganizationInstanceGroupRead,
39+
40+
Schema: map[string]*schema.Schema{
41+
"organization_id": {
42+
Type: schema.TypeInt,
43+
Required: true,
44+
ForceNew: true,
45+
},
46+
"instance_group_id": {
47+
Type: schema.TypeInt,
48+
Required: true,
49+
ForceNew: true,
50+
},
51+
},
52+
}
53+
}
54+
55+
func resourceOrganizationInstanceGroupRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
56+
var diags diag.Diagnostics
57+
return diags
58+
}
59+
60+
func resourceOrganizationInstanceGroupCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
61+
var diags diag.Diagnostics
62+
client := m.(*awx.AWX)
63+
awxService := client.OrganizationsService
64+
OrganizationID := d.Get("organization_id").(int)
65+
_, err := awxService.GetOrganizationsByID(OrganizationID, make(map[string]string))
66+
if err != nil {
67+
return buildDiagNotFoundFail("organization", OrganizationID, err)
68+
}
69+
70+
result, err := awxService.AssociateInstanceGroups(OrganizationID, map[string]interface{}{
71+
"id": d.Get("instance_group_id").(int),
72+
}, map[string]string{})
73+
74+
if err != nil {
75+
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())
76+
}
77+
78+
d.SetId(strconv.Itoa(result.ID))
79+
return diags
80+
}
81+
82+
func resourceOrganizationInstanceGroupDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
83+
var diags diag.Diagnostics
84+
client := m.(*awx.AWX)
85+
awxService := client.OrganizationsService
86+
OrganizationID := d.Get("organization_id").(int)
87+
res, err := awxService.GetOrganizationsByID(OrganizationID, make(map[string]string))
88+
if err != nil {
89+
return buildDiagNotFoundFail("organization", OrganizationID, err)
90+
}
91+
92+
_, err = awxService.DisAssociateInstanceGroups(res.ID, map[string]interface{}{
93+
"id": d.Get("instance_group_id").(int),
94+
}, map[string]string{})
95+
if err != nil {
96+
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()))
97+
}
98+
99+
d.SetId("")
100+
return diags
101+
}

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ module github.com/denouche/terraform-provider-awx
33
go 1.20
44

55
require (
6-
github.com/denouche/goawx v0.20.1
6+
github.com/denouche/goawx v0.21.1
77
github.com/gruntwork-io/terratest v0.31.2
88
github.com/hashicorp/terraform-plugin-sdk/v2 v2.30.0
99
github.com/stretchr/testify v1.8.3

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,8 @@ github.com/denouche/goawx v0.20.0 h1:wGrHMrAE1qLaE3DoYseCV8yIIfeIVw/HDNRw2zx0CwA
9999
github.com/denouche/goawx v0.20.0/go.mod h1:MppzSteoj2xgfiqiRWW/Bf1a8z2FrRyvah1z0J2vJTY=
100100
github.com/denouche/goawx v0.20.1 h1:Mnkk1QxGPBvo3U4FDhcdHFrSMwIT+IwAkLDVQ7dCAoc=
101101
github.com/denouche/goawx v0.20.1/go.mod h1:MppzSteoj2xgfiqiRWW/Bf1a8z2FrRyvah1z0J2vJTY=
102+
github.com/denouche/goawx v0.21.1 h1:ImTBH6/L3u8QInlPHMWO02su4lQPHudj52ZvwKeL86A=
103+
github.com/denouche/goawx v0.21.1/go.mod h1:Bdo/LeUgeemE9Xt4bOVFVO6GJMxxUcduhQPDD5+yQ1A=
102104
github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=
103105
github.com/dimchansky/utfbom v1.1.0/go.mod h1:rO41eb7gLfo8SF1jd9F8HplJm1Fewwi4mQvIirEdv+8=
104106
github.com/dnaeon/go-vcr v1.0.1/go.mod h1:aBB1+wY4s93YsC3HHjMBMrwTj2R9FHDzUr9KyGc8n1E=

0 commit comments

Comments
 (0)