|
| 1 | +package idaas |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + |
| 6 | + "github.com/hashicorp/terraform-plugin-framework/datasource" |
| 7 | + "github.com/hashicorp/terraform-plugin-framework/datasource/schema" |
| 8 | + "github.com/hashicorp/terraform-plugin-framework/types" |
| 9 | + "github.com/okta/terraform-provider-okta/okta/config" |
| 10 | +) |
| 11 | + |
| 12 | +var _ datasource.DataSource = &adminRoleCustomDataSource{} |
| 13 | + |
| 14 | +func newAdminRoleCustomDataSource() datasource.DataSource { |
| 15 | + return &adminRoleCustomDataSource{} |
| 16 | +} |
| 17 | + |
| 18 | +type adminRoleCustomDataSource struct { |
| 19 | + config *config.Config |
| 20 | +} |
| 21 | + |
| 22 | +type adminRoleCustomDataSourceModel struct { |
| 23 | + Id types.String `tfsdk:"id"` |
| 24 | + Label types.String `tfsdk:"label"` |
| 25 | + Description types.String `tfsdk:"description"` |
| 26 | + Permissions types.Set `tfsdk:"permissions"` |
| 27 | +} |
| 28 | + |
| 29 | +func (d *adminRoleCustomDataSource) Metadata(ctx context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) { |
| 30 | + resp.TypeName = req.ProviderTypeName + "_admin_role_custom" |
| 31 | +} |
| 32 | + |
| 33 | +func (d *adminRoleCustomDataSource) Configure(_ context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) { |
| 34 | + d.config = dataSourceConfiguration(req, resp) |
| 35 | +} |
| 36 | + |
| 37 | +func (d *adminRoleCustomDataSource) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) { |
| 38 | + resp.Schema = schema.Schema{ |
| 39 | + Description: "Retrieves a custom admin role from Okta.", |
| 40 | + Attributes: map[string]schema.Attribute{ |
| 41 | + "id": schema.StringAttribute{ |
| 42 | + Required: true, |
| 43 | + Description: "The unique identifier for the custom role. Accepts the role ID or label.", |
| 44 | + }, |
| 45 | + "label": schema.StringAttribute{ |
| 46 | + Computed: true, |
| 47 | + Description: "Unique label for the role.", |
| 48 | + }, |
| 49 | + "description": schema.StringAttribute{ |
| 50 | + Computed: true, |
| 51 | + Description: "Description of the role.", |
| 52 | + }, |
| 53 | + "permissions": schema.SetAttribute{ |
| 54 | + Computed: true, |
| 55 | + ElementType: types.StringType, |
| 56 | + Description: "Array of permissions that the role grants.", |
| 57 | + }, |
| 58 | + }, |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +func (d *adminRoleCustomDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) { |
| 63 | + var data adminRoleCustomDataSourceModel |
| 64 | + |
| 65 | + resp.Diagnostics.Append(req.Config.Get(ctx, &data)...) |
| 66 | + if resp.Diagnostics.HasError() { |
| 67 | + return |
| 68 | + } |
| 69 | + |
| 70 | + client := d.config.OktaIDaaSClient.OktaSDKSupplementClient() |
| 71 | + |
| 72 | + role, _, err := client.GetCustomRole(ctx, data.Id.ValueString()) |
| 73 | + if err != nil { |
| 74 | + resp.Diagnostics.AddError( |
| 75 | + "Error reading custom admin role", |
| 76 | + "Could not read custom admin role, unexpected error: "+err.Error(), |
| 77 | + ) |
| 78 | + return |
| 79 | + } |
| 80 | + |
| 81 | + data.Id = types.StringValue(role.Id) |
| 82 | + data.Label = types.StringValue(role.Label) |
| 83 | + data.Description = types.StringValue(role.Description) |
| 84 | + |
| 85 | + perms, _, err := client.ListCustomRolePermissions(ctx, role.Id) |
| 86 | + if err != nil { |
| 87 | + resp.Diagnostics.AddError( |
| 88 | + "Error reading custom admin role permissions", |
| 89 | + "Could not read permissions for custom admin role, unexpected error: "+err.Error(), |
| 90 | + ) |
| 91 | + return |
| 92 | + } |
| 93 | + |
| 94 | + permValues := make([]string, len(perms.Permissions)) |
| 95 | + for i, p := range perms.Permissions { |
| 96 | + permValues[i] = p.Label |
| 97 | + } |
| 98 | + |
| 99 | + permSet, diags := types.SetValueFrom(ctx, types.StringType, permValues) |
| 100 | + resp.Diagnostics.Append(diags...) |
| 101 | + if resp.Diagnostics.HasError() { |
| 102 | + return |
| 103 | + } |
| 104 | + data.Permissions = permSet |
| 105 | + |
| 106 | + resp.Diagnostics.Append(resp.State.Set(ctx, &data)...) |
| 107 | +} |
0 commit comments