Skip to content

Commit 64a5e25

Browse files
Adding Data Source for okta_admin_role_custom (#2704)
1 parent 7ac4158 commit 64a5e25

8 files changed

Lines changed: 1007 additions & 1 deletion

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ Pull requests with new resources and data sources must include:
129129

130130
- Signed [Okta Individual Contributor License Agreement](https://developer.okta.com/cla/) emailed to `CLA@okta.com`
131131
- Implemented with the [Terraform Plugin Framework](https://developer.hashicorp.com/terraform/plugin/framework) (not [Terraform Plugin SDKv2](https://developer.hashicorp.com/terraform/plugin/sdkv2) )
132-
- Make Okta API calls with the [okta-sdk-golang v3](https://github.com/okta/okta-sdk-golang) client
132+
- Make Okta API calls with the [okta-sdk-golang v6](https://github.com/okta/okta-sdk-golang) client
133133
- Include [Terraform Plugin Acceptance Tests](https://developer.hashicorp.com/terraform/plugin/sdkv2/testing/acceptance-tests)
134134

135135
Please see the [contribution guidelines](.github/CONTRIBUTING.md) for additional
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
---
2+
page_title: "Data Source: okta_admin_role_custom"
3+
description: |-
4+
Get a custom admin role from Okta.
5+
---
6+
7+
# Data Source: okta_admin_role_custom
8+
9+
Use this data source to retrieve a custom admin role from Okta.
10+
11+
## Example Usage
12+
13+
```terraform
14+
data "okta_admin_role_custom" "example" {
15+
id = "<role_id>"
16+
}
17+
```
18+
19+
<!-- schema generated by tfplugindocs -->
20+
## Schema
21+
22+
### Required
23+
24+
- `id` (String) The unique identifier for the custom role. Accepts the role ID or label.
25+
26+
### Read-Only
27+
28+
- `label` (String) The name given to the custom role.
29+
- `description` (String) A human-readable description of the custom role.
30+
- `permissions` (Set of String) The permissions that the custom role grants.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
resource "okta_admin_role_custom" "test" {
2+
label = "testAcc_replace_with_uuid"
3+
description = "test custom role"
4+
permissions = ["okta.apps.assignment.manage"]
5+
}
6+
7+
data "okta_admin_role_custom" "test" {
8+
id = okta_admin_role_custom.test.id
9+
}
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
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+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package idaas_test
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
8+
"github.com/okta/terraform-provider-okta/okta/acctest"
9+
"github.com/okta/terraform-provider-okta/okta/resources"
10+
)
11+
12+
func TestAccDataSourceOktaAdminRoleCustom_read(t *testing.T) {
13+
datasourceName := fmt.Sprintf("data.%s.test", resources.OktaIDaaSAdminRoleCustom)
14+
15+
mgr := newFixtureManager("data-sources", resources.OktaIDaaSAdminRoleCustom, t.Name())
16+
config := mgr.GetFixtures("datasource.tf", t)
17+
18+
acctest.OktaResourceTest(t, resource.TestCase{
19+
PreCheck: acctest.AccPreCheck(t),
20+
ErrorCheck: testAccErrorChecks(t),
21+
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactoriesForTestAcc(t),
22+
Steps: []resource.TestStep{
23+
{
24+
Config: config,
25+
Check: resource.ComposeTestCheckFunc(
26+
resource.TestCheckResourceAttrSet(datasourceName, "id"),
27+
resource.TestCheckResourceAttr(datasourceName, "label", acctest.BuildResourceName(mgr.Seed)),
28+
resource.TestCheckResourceAttr(datasourceName, "description", "test custom role"),
29+
resource.TestCheckResourceAttr(datasourceName, "permissions.#", "1"),
30+
resource.TestCheckResourceAttr(datasourceName, "permissions.0", "okta.apps.assignment.manage"),
31+
),
32+
},
33+
},
34+
})
35+
}

okta/services/idaas/idaas.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ func FWProviderDataSources() []func() datasource.DataSource {
155155
newAppFederatedClaimDataSource,
156156
newPushGroupDataSource,
157157
newPushGroupsDataSource,
158+
newAdminRoleCustomDataSource,
158159
}
159160
}
160161

0 commit comments

Comments
 (0)