|
| 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 | + "github.com/hashicorp/terraform-plugin-sdk/v2/diag" |
| 26 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 27 | + awx "github.com/mrcrilly/goawx/client" |
| 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 | +} |
0 commit comments