generated from hashicorp/terraform-provider-scaffolding
-
Notifications
You must be signed in to change notification settings - Fork 43
/
data_source_user.go
92 lines (80 loc) · 2.39 KB
/
data_source_user.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package user
import (
"context"
"github.com/antihax/optional"
"github.com/harness/harness-go-sdk/harness/nextgen"
"github.com/harness/terraform-provider-harness/helpers"
"github.com/harness/terraform-provider-harness/internal"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func DataSourceUser() *schema.Resource {
return &schema.Resource{
Description: "Data source for retrieving the user based on the API key.",
ReadContext: dataSourceUserRead,
Schema: map[string]*schema.Schema{
"identifier": {
Description: "Unique identifier of the user.",
Type: schema.TypeString,
Computed: true,
},
"org_id": {
Description: "Organization identifier of the user.",
Type: schema.TypeString,
Optional: true,
},
"project_id": {
Description: "Project identifier of the user.",
Type: schema.TypeString,
Optional: true,
},
"name": {
Description: "Name of the user.",
Type: schema.TypeString,
Computed: true,
},
"email": {
Description: "The email of the user.",
Type: schema.TypeString,
Required: true,
},
"disabled": {
Description: "Whether or not the user account is disabled.",
Type: schema.TypeBool,
Computed: true,
},
"locked": {
Description: "Whether or not the user account is locked.",
Type: schema.TypeBool,
Computed: true,
},
"externally_managed": {
Description: "Whether or not the user account is externally managed.",
Type: schema.TypeBool,
Computed: true,
},
},
}
}
func dataSourceUserRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
c, ctx := meta.(*internal.Session).GetPlatformClientWithContext(ctx)
var email string
if attr, ok := d.GetOk("email"); ok {
email = attr.(string)
}
resp, httpResp, err := c.UserApi.GetAggregatedUsers(ctx, c.AccountId, &nextgen.UserApiGetAggregatedUsersOpts{
OrgIdentifier: helpers.BuildField(d, "org_id"),
ProjectIdentifier: helpers.BuildField(d, "project_id"),
SearchTerm: optional.NewString(email),
})
if &resp == nil || resp.Data == nil || resp.Data.Empty {
d.SetId("")
d.MarkNewResource()
return nil
}
if err != nil {
return helpers.HandleReadApiError(err, d, httpResp)
}
readUserList(d, resp.Data)
return nil
}