/
data_source_okta_user_type.go
56 lines (51 loc) · 1.41 KB
/
data_source_okta_user_type.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package okta
import (
"context"
"strings"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/okta/terraform-provider-okta/sdk"
)
func dataSourceUserType() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceUserTypeRead,
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
Description: "Name of user type to retrieve.",
},
"display_name": {
Type: schema.TypeString,
Computed: true,
Description: "Display name of user type.",
},
"description": {
Type: schema.TypeString,
Computed: true,
Description: "Description of user type.",
},
},
Description: "Get a user type from Okta.",
}
}
func dataSourceUserTypeRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
userTypes, _, err := getOktaClientFromMetadata(m).UserType.ListUserTypes(ctx)
if err != nil {
return diag.Errorf("failed to list user types: %v", err)
}
name := d.Get("name").(string)
var userType *sdk.UserType
for _, ut := range userTypes {
if strings.EqualFold(name, ut.Name) {
userType = ut
}
}
if userType == nil {
return diag.Errorf("user type '%s' does not exist", name)
}
d.SetId(userType.Id)
_ = d.Set("display_name", userType.DisplayName)
_ = d.Set("description", userType.Description)
return nil
}