Skip to content

Commit 8cc9cb0

Browse files
authored
feat: fetch upstream
Merge pull request #16 from denouche/chore-fetch-upstream Chore fetch upstream
2 parents 5f78ec4 + d8175de commit 8cc9cb0

6 files changed

Lines changed: 287 additions & 8 deletions

awx/data_source_organization.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,15 +61,15 @@ func dataSourceOrganizationRead(ctx context.Context, d *schema.ResourceData, m i
6161
organizations, err := client.OrganizationsService.ListOrganizations(params)
6262
if err != nil {
6363
return buildDiagnosticsMessage(
64-
"Get: Fail to fetch Inventory Group",
65-
"Fail to find the group got: %s",
64+
"Get: Fail to fetch organization",
65+
"Fail to find the organization got: %s",
6666
err.Error(),
6767
)
6868
}
6969
if len(organizations) > 1 {
7070
return buildDiagnosticsMessage(
7171
"Get: find more than one Element",
72-
"The Query Returns more than one Group, %d",
72+
"The Query Returns more than one organization, %d",
7373
len(organizations),
7474
)
7575
return diags
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
/*
2+
*TBD*
3+
4+
Example Usage
5+
6+
```hcl
7+
resource "awx_organization" "myorg" {
8+
name = "My AWX Org"
9+
...
10+
}
11+
12+
data "awx_organization_role" "org_admins" {
13+
name = "Admin"
14+
organization_id = resource.awx_organization.myorg.id
15+
}
16+
```
17+
18+
*/
19+
package awx
20+
21+
import (
22+
"context"
23+
"strconv"
24+
25+
awx "github.com/denouche/goawx/client"
26+
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
27+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
28+
)
29+
30+
func dataSourceOrganizationRole() *schema.Resource {
31+
return &schema.Resource{
32+
ReadContext: dataSourceOrganizationRolesRead,
33+
Schema: map[string]*schema.Schema{
34+
"id": {
35+
Type: schema.TypeInt,
36+
Optional: true,
37+
Computed: true,
38+
},
39+
"name": {
40+
Type: schema.TypeString,
41+
Optional: true,
42+
Computed: true,
43+
},
44+
"organization_id": {
45+
Type: schema.TypeInt,
46+
Required: true,
47+
},
48+
},
49+
}
50+
}
51+
52+
func dataSourceOrganizationRolesRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
53+
var diags diag.Diagnostics
54+
client := m.(*awx.AWX)
55+
params := make(map[string]string)
56+
57+
org_id := d.Get("organization_id").(int)
58+
59+
organization, err := client.OrganizationsService.GetOrganizationsByID(org_id, params)
60+
if err != nil {
61+
return buildDiagnosticsMessage(
62+
"Get: Fail to fetch organization role",
63+
"Fail to find the organization role got: %s",
64+
err.Error(),
65+
)
66+
}
67+
68+
roleslist := []*awx.ApplyRole{
69+
organization.SummaryFields.ObjectRoles.AdhocRole,
70+
organization.SummaryFields.ObjectRoles.AdminRole,
71+
organization.SummaryFields.ObjectRoles.ApprovalRole,
72+
organization.SummaryFields.ObjectRoles.AuditorRole,
73+
organization.SummaryFields.ObjectRoles.CredentialAdminRole,
74+
organization.SummaryFields.ObjectRoles.ExecuteRole,
75+
organization.SummaryFields.ObjectRoles.InventoryAdminRole,
76+
organization.SummaryFields.ObjectRoles.JobTemplateAdminRole,
77+
organization.SummaryFields.ObjectRoles.MemberRole,
78+
organization.SummaryFields.ObjectRoles.NotificationAdminRole,
79+
organization.SummaryFields.ObjectRoles.ProjectAdminRole,
80+
organization.SummaryFields.ObjectRoles.ReadRole,
81+
organization.SummaryFields.ObjectRoles.UpdateRole,
82+
organization.SummaryFields.ObjectRoles.UseRole,
83+
organization.SummaryFields.ObjectRoles.WorkflowAdminRole,
84+
}
85+
86+
if roleID, okID := d.GetOk("id"); okID {
87+
id := roleID.(int)
88+
for _, v := range roleslist {
89+
if v != nil && id == v.ID {
90+
d = setOrganizationRoleData(d, v)
91+
return diags
92+
}
93+
}
94+
}
95+
96+
if roleName, okName := d.GetOk("name"); okName {
97+
name := roleName.(string)
98+
99+
for _, v := range roleslist {
100+
if v != nil && name == v.Name {
101+
d = setOrganizationRoleData(d, v)
102+
return diags
103+
}
104+
}
105+
}
106+
107+
return buildDiagnosticsMessage(
108+
"Failed to fetch organization role - Not Found",
109+
"The organization role was not found",
110+
)
111+
}
112+
113+
func setOrganizationRoleData(d *schema.ResourceData, r *awx.ApplyRole) *schema.ResourceData {
114+
d.Set("name", r.Name)
115+
d.SetId(strconv.Itoa(r.ID))
116+
return d
117+
}

awx/data_source_project_role.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,8 @@ func dataSourceProjectRolesRead(ctx context.Context, d *schema.ResourceData, m i
6868
roleslist := []*awx.ApplyRole{
6969
Project.SummaryFields.ObjectRoles.UseRole,
7070
Project.SummaryFields.ObjectRoles.AdminRole,
71-
Project.SummaryFields.ObjectRoles.AdhocRole,
7271
Project.SummaryFields.ObjectRoles.UpdateRole,
7372
Project.SummaryFields.ObjectRoles.ReadRole,
74-
Project.SummaryFields.ObjectRoles.ExecuteRole,
7573
}
7674

7775
if roleID, okID := d.GetOk("id"); okID {

awx/provider.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ func Provider() *schema.Provider {
6666
"awx_project": resourceProject(),
6767
"awx_schedule": resourceSchedule(),
6868
"awx_settings_ldap_team_map": resourceSettingsLDAPTeamMap(),
69+
"awx_setting": resourceSetting(),
6970
"awx_team": resourceTeam(),
7071
"awx_workflow_job_template_node_always": resourceWorkflowJobTemplateNodeAlways(),
7172
"awx_workflow_job_template_node_failure": resourceWorkflowJobTemplateNodeFailure(),
@@ -89,6 +90,7 @@ func Provider() *schema.Provider {
8990
"awx_job_template": dataSourceJobTemplate(),
9091
"awx_notification_template": dataSourceNotificationTemplate(),
9192
"awx_organization": dataSourceOrganization(),
93+
"awx_organization_role": dataSourceOrganizationRole(),
9294
"awx_organizations": dataSourceOrganizations(),
9395
"awx_project": dataSourceProject(),
9496
"awx_project_role": dataSourceProjectRole(),

awx/resource_organization.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,9 @@ func resourceOrganization() *schema.Resource {
5353
Description: "Local absolute file path containing a custom Python virtualenv to use",
5454
},
5555
},
56-
//Importer: &schema.ResourceImporter{
57-
// State: schema.ImportStatePassthrough,
58-
//},
56+
Importer: &schema.ResourceImporter{
57+
State: schema.ImportStatePassthrough,
58+
},
5959
//
6060
//Timeouts: &schema.ResourceTimeout{
6161
// Create: schema.DefaultTimeout(1 * time.Minute),

awx/resource_setting.go

Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
/*
2+
This resource configure generic AWX settings.
3+
Please note that resource deletion only delete object from terraform state and do not reset setting to his initial value.
4+
5+
See available settings list here: https://docs.ansible.com/ansible-tower/latest/html/towerapi/api_ref.html#/Settings/Settings_settings_update
6+
7+
Example Usage
8+
9+
```hcl
10+
resource "awx_setting" "social_auth_saml_technical_contact" {
11+
name = "SOCIAL_AUTH_SAML_TECHNICAL_CONTACT"
12+
value = <<EOF
13+
{
14+
"givenName": "Myorg",
15+
"emailAddress": "test@foo.com"
16+
}
17+
EOF
18+
}
19+
20+
resource "awx_setting" "social_auth_saml_sp_entity_id" {
21+
name = "SOCIAL_AUTH_SAML_SP_ENTITY_ID"
22+
value = "test"
23+
}
24+
25+
resource "awx_setting" "schedule_max_jobs" {
26+
name = "SCHEDULE_MAX_JOBS"
27+
value = 15
28+
}
29+
30+
resource "awx_setting" "remote_host_headers" {
31+
name = "REMOTE_HOST_HEADERS"
32+
value = <<EOF
33+
[
34+
"HTTP_X_FORWARDED_FOR",
35+
"REMOTE_ADDR",
36+
"REMOTE_HOST"
37+
]
38+
EOF
39+
}
40+
```
41+
42+
*/
43+
package awx
44+
45+
import (
46+
"context"
47+
"encoding/json"
48+
"time"
49+
50+
awx "github.com/denouche/goawx/client"
51+
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
52+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
53+
)
54+
55+
func resourceSetting() *schema.Resource {
56+
return &schema.Resource{
57+
CreateContext: resourceSettingUpdate,
58+
ReadContext: resourceSettingRead,
59+
DeleteContext: resourceSettingDelete,
60+
UpdateContext: resourceSettingUpdate,
61+
62+
Schema: map[string]*schema.Schema{
63+
"name": {
64+
Type: schema.TypeString,
65+
Required: true,
66+
Description: "Name of setting to modify",
67+
},
68+
"value": {
69+
Type: schema.TypeString,
70+
Required: true,
71+
Description: "Value to be modified for given setting.",
72+
},
73+
},
74+
Importer: &schema.ResourceImporter{
75+
State: schema.ImportStatePassthrough,
76+
},
77+
78+
Timeouts: &schema.ResourceTimeout{
79+
Create: schema.DefaultTimeout(1 * time.Minute),
80+
Update: schema.DefaultTimeout(1 * time.Minute),
81+
Delete: schema.DefaultTimeout(5 * time.Minute),
82+
},
83+
}
84+
}
85+
86+
type setting map[string]string
87+
88+
func resourceSettingUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
89+
client := m.(*awx.AWX)
90+
awxService := client.SettingService
91+
92+
_, err := awxService.GetSettingsBySlug("all", make(map[string]string))
93+
if err != nil {
94+
return buildDiagnosticsMessage(
95+
"Create: failed to fetch settings",
96+
"Failed to fetch setting, got: %s", err.Error(),
97+
)
98+
}
99+
100+
var map_decoded map[string]interface{}
101+
var array_decoded []interface{}
102+
var formatted_value interface{}
103+
104+
name := d.Get("name").(string)
105+
value := d.Get("value").(string)
106+
107+
// Attempt to unmarshall string into a map
108+
err = json.Unmarshal([]byte(value), &map_decoded)
109+
110+
if err != nil {
111+
// Attempt to unmarshall string into an array
112+
err = json.Unmarshal([]byte(value), &array_decoded)
113+
114+
if err != nil {
115+
formatted_value = value
116+
} else {
117+
formatted_value = array_decoded
118+
}
119+
} else {
120+
formatted_value = map_decoded
121+
}
122+
123+
payload := map[string]interface{}{
124+
name: formatted_value,
125+
}
126+
127+
_, err = awxService.UpdateSettings("all", payload, make(map[string]string))
128+
if err != nil {
129+
return buildDiagnosticsMessage(
130+
"Create: setting not created",
131+
"failed to save setting data, got: %s, %s", err.Error(), value,
132+
)
133+
}
134+
135+
d.SetId(name)
136+
return resourceSettingRead(ctx, d, m)
137+
}
138+
139+
func resourceSettingRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
140+
var diags diag.Diagnostics
141+
client := m.(*awx.AWX)
142+
awxService := client.SettingService
143+
144+
_, err := awxService.GetSettingsBySlug("all", make(map[string]string))
145+
if err != nil {
146+
return buildDiagnosticsMessage(
147+
"Unable to fetch settings",
148+
"Unable to load settings with slug all: got %s", err.Error(),
149+
)
150+
}
151+
152+
d.Set("name", d.Id())
153+
d.Set("value", d.Get("value").(string))
154+
return diags
155+
}
156+
157+
func resourceSettingDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
158+
var diags diag.Diagnostics
159+
160+
d.SetId("")
161+
return diags
162+
}

0 commit comments

Comments
 (0)