Skip to content

Commit ad61e88

Browse files
authored
feat: organizations data source (#4)
1 parent e82ccb9 commit ad61e88

7 files changed

Lines changed: 86 additions & 8 deletions

awx/data_source_credentials.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ func dataSourceCredentialsRead(ctx context.Context, d *schema.ResourceData, m in
5252
var diags diag.Diagnostics
5353
client := m.(*awx.AWX)
5454

55-
creds, _, err := client.CredentialsService.ListCredentials(map[string]string{})
55+
creds, err := client.CredentialsService.ListCredentials(map[string]string{})
5656
if err != nil {
5757
diags = append(diags, diag.Diagnostic{
5858
Severity: diag.Error,

awx/data_source_organization.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import (
2323

2424
func dataSourceOrganization() *schema.Resource {
2525
return &schema.Resource{
26-
ReadContext: dataSourceOrganizationsRead,
26+
ReadContext: dataSourceOrganizationRead,
2727
Schema: map[string]*schema.Schema{
2828
"id": {
2929
Type: schema.TypeInt,
@@ -39,7 +39,7 @@ func dataSourceOrganization() *schema.Resource {
3939
}
4040
}
4141

42-
func dataSourceOrganizationsRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
42+
func dataSourceOrganizationRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
4343
var diags diag.Diagnostics
4444
client := m.(*awx.AWX)
4545
params := make(map[string]string)
@@ -58,7 +58,7 @@ func dataSourceOrganizationsRead(ctx context.Context, d *schema.ResourceData, m
5858
)
5959
return diags
6060
}
61-
organizations, _, err := client.OrganizationsService.ListOrganizations(params)
61+
organizations, err := client.OrganizationsService.ListOrganizations(params)
6262
if err != nil {
6363
return buildDiagnosticsMessage(
6464
"Get: Fail to fetch Inventory Group",

awx/data_source_organizations.go

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/*
2+
Use this data source to query Organizations.
3+
4+
Example Usage
5+
6+
```hcl
7+
data "awx_organizations" "all_orgs" {}
8+
```
9+
10+
*/
11+
package awx
12+
13+
import (
14+
"context"
15+
"strconv"
16+
"time"
17+
18+
awx "github.com/denouche/goawx/client"
19+
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
20+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
21+
)
22+
23+
func dataSourceOrganizations() *schema.Resource {
24+
return &schema.Resource{
25+
ReadContext: dataSourceOrganizationsRead,
26+
Schema: map[string]*schema.Schema{
27+
"organizations": {
28+
Type: schema.TypeList,
29+
Computed: true,
30+
Elem: &schema.Resource{
31+
Schema: map[string]*schema.Schema{
32+
"id": {
33+
Type: schema.TypeInt,
34+
Computed: true,
35+
},
36+
"name": {
37+
Type: schema.TypeString,
38+
Computed: true,
39+
},
40+
},
41+
},
42+
},
43+
},
44+
}
45+
}
46+
47+
func dataSourceOrganizationsRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
48+
var diags diag.Diagnostics
49+
client := m.(*awx.AWX)
50+
51+
parsedOrgs := make([]map[string]interface{}, 0)
52+
53+
orgs, err := client.OrganizationsService.ListOrganizations(map[string]string{})
54+
if err != nil {
55+
diags = append(diags, diag.Diagnostic{
56+
Severity: diag.Error,
57+
Summary: "Unable to fetch organizations",
58+
Detail: "Unable to fetch organizations from AWX API",
59+
})
60+
return diags
61+
}
62+
for _, c := range orgs {
63+
parsedOrgs = append(parsedOrgs, map[string]interface{}{
64+
"id": c.ID,
65+
"name": c.Name,
66+
})
67+
}
68+
69+
err = d.Set("organizations", parsedOrgs)
70+
if err != nil {
71+
return diag.FromErr(err)
72+
}
73+
74+
d.SetId(strconv.FormatInt(time.Now().Unix(), 10))
75+
76+
return diags
77+
}

awx/provider.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ func Provider() *schema.Provider {
8383
"awx_job_template": dataSourceJobTemplate(),
8484
"awx_notification_template": dataSourceNotificationTemplate(),
8585
"awx_organization": dataSourceOrganization(),
86+
"awx_organizations": dataSourceOrganizations(),
8687
"awx_project": dataSourceProject(),
8788
"awx_project_role": dataSourceProjectRole(),
8889
"awx_schedule": dataSourceSchedule(),

awx/resource_organization.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ func resourceOrganizationsDelete(ctx context.Context, d *schema.ResourceData, m
159159
return diags
160160
}
161161

162-
func setOrganizationsResourceData(d *schema.ResourceData, r *awx.Organizations) *schema.ResourceData {
162+
func setOrganizationsResourceData(d *schema.ResourceData, r *awx.Organization) *schema.ResourceData {
163163
d.Set("name", r.Name)
164164
d.Set("description", r.Description)
165165
d.Set("max_hosts", r.MaxHosts)

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.14
44

55
require (
6-
github.com/denouche/goawx v0.9.0
6+
github.com/denouche/goawx v0.10.0
77
github.com/gruntwork-io/terratest v0.31.2
88
github.com/hashicorp/terraform-plugin-sdk/v2 v2.10.1
99
github.com/stretchr/testify v1.7.0

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,8 +137,8 @@ github.com/davecgh/go-spew v0.0.0-20151105211317-5215b55f46b2/go.mod h1:J7Y8YcW2
137137
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
138138
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
139139
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
140-
github.com/denouche/goawx v0.9.0 h1:YLTr9lYVbgr6d85rL9DT6fiZ2GV6MnZ4IPF5mNEybBY=
141-
github.com/denouche/goawx v0.9.0/go.mod h1:MppzSteoj2xgfiqiRWW/Bf1a8z2FrRyvah1z0J2vJTY=
140+
github.com/denouche/goawx v0.10.0 h1:E5Uy0ldyveiHRKGYS32mIeT0NFiUEu51g8upYKtFpK4=
141+
github.com/denouche/goawx v0.10.0/go.mod h1:MppzSteoj2xgfiqiRWW/Bf1a8z2FrRyvah1z0J2vJTY=
142142
github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=
143143
github.com/dimchansky/utfbom v1.1.0/go.mod h1:rO41eb7gLfo8SF1jd9F8HplJm1Fewwi4mQvIirEdv+8=
144144
github.com/dnaeon/go-vcr v1.0.1/go.mod h1:aBB1+wY4s93YsC3HHjMBMrwTj2R9FHDzUr9KyGc8n1E=

0 commit comments

Comments
 (0)