-
Notifications
You must be signed in to change notification settings - Fork 393
/
Copy pathdata_source_datadog_user.go
177 lines (167 loc) · 5.53 KB
/
data_source_datadog_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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package datadog
import (
"context"
"time"
"github.com/DataDog/datadog-api-client-go/v2/api/datadogV2"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/terraform-providers/terraform-provider-datadog/datadog/internal/utils"
)
func dataSourceDatadogUser() *schema.Resource {
return &schema.Resource{
Description: "Use this data source to retrieve information about an existing user to use it in an other resources.",
ReadContext: dataSourceDatadogUserRead,
SchemaFunc: func() map[string]*schema.Schema {
return map[string]*schema.Schema{
"filter": {
Description: "Filter all users by the given string.",
Type: schema.TypeString,
Required: true,
},
"exact_match": {
Description: "When true, `filter` string is exact matched against the user's `email`, followed by `name` attribute.",
Type: schema.TypeBool,
Default: false,
Optional: true,
},
// Computed values
"created_at": {
Description: "The time when the user was created (RFC3339 format).",
Type: schema.TypeString,
Computed: true,
},
"disabled": {
Description: "Indicates whether the user is disabled.",
Type: schema.TypeBool,
Computed: true,
},
"email": {
Description: "Email of the user.",
Type: schema.TypeString,
Computed: true,
},
"handle": {
Description: "The user's handle.",
Type: schema.TypeString,
Computed: true,
},
"icon": {
Description: "The URL where the user's icon is located.",
Type: schema.TypeString,
Computed: true,
},
"mfa_enabled": {
Description: "Indicates whether the user has enabled MFA.",
Type: schema.TypeBool,
Computed: true,
},
"modified_at": {
Description: "The time at which the user was last updated (RFC3339 format).",
Type: schema.TypeString,
Computed: true,
},
"name": {
Description: "Name of the user.",
Type: schema.TypeString,
Computed: true,
},
"service_account": {
Description: "Indicates whether the user is a service account.",
Type: schema.TypeBool,
Computed: true,
},
"status": {
Description: "The user's status.",
Type: schema.TypeString,
Computed: true,
},
"title": {
Description: "The user's title.",
Type: schema.TypeString,
Computed: true,
},
"verified": {
Description: "Indicates whether the user is verified.",
Type: schema.TypeBool,
Computed: true,
},
}
},
}
}
func dataSourceDatadogUserRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
providerConf := meta.(*ProviderConfiguration)
apiInstances := providerConf.DatadogApiInstances
auth := providerConf.Auth
filter := d.Get("filter").(string) // string | Filter all users by the given string. Defaults to no filtering. (optional) // string | Filter on status attribute. Comma separated list, with possible values `Active`, `Pending`, and `Disabled`. Defaults to no filtering. (optional)
exactMatch := d.Get("exact_match").(bool)
optionalParams := datadogV2.ListUsersOptionalParameters{
Filter: &filter,
}
res, httpresp, err := apiInstances.GetUsersApiV2().ListUsers(auth, optionalParams)
if err != nil {
return utils.TranslateClientErrorDiag(err, httpresp, "error querying user")
}
users := res.GetData()
if len(users) > 1 && !exactMatch {
return diag.Errorf("your query returned more than one result for filter \"%s\", please try a more specific search criteria",
filter,
)
} else if len(users) == 0 {
return diag.Errorf("didn't find any user matching filter string \"%s\"", filter)
}
matchedUser := users[0]
if exactMatch {
matchCount := 0
for _, user := range users {
if user.Attributes.GetEmail() == filter {
matchedUser = user
matchCount++
continue
}
if user.Attributes.GetName() == filter {
matchedUser = user
matchCount++
continue
}
}
if matchCount > 1 {
return diag.Errorf("your query returned more than one result for filter with exact match \"%s\", please try a more specific search criteria",
filter,
)
}
if matchCount == 0 {
return diag.Errorf("didn't find any user matching filter string with exact match \"%s\"", filter)
}
}
if err := utils.CheckForUnparsed(matchedUser); err != nil {
return diag.FromErr(err)
}
d.SetId(matchedUser.GetId())
mapAttrString := map[string]func() string{
"created_at": func() string { return matchedUser.Attributes.GetCreatedAt().Format(time.RFC3339) },
"email": matchedUser.Attributes.GetEmail,
"handle": matchedUser.Attributes.GetHandle,
"icon": matchedUser.Attributes.GetIcon,
"modified_at": func() string { return matchedUser.Attributes.GetModifiedAt().Format(time.RFC3339) },
"name": matchedUser.Attributes.GetName,
"status": matchedUser.Attributes.GetStatus,
"title": matchedUser.Attributes.GetTitle,
}
for key, value := range mapAttrString {
if err := d.Set(key, value()); err != nil {
return diag.FromErr(err)
}
}
mapAttrBool := map[string]func() bool{
"disabled": matchedUser.Attributes.GetDisabled,
"mfa_enabled": matchedUser.Attributes.GetMfaEnabled,
"service_account": matchedUser.Attributes.GetServiceAccount,
}
for key, value := range mapAttrBool {
if err := d.Set(key, value()); err != nil {
return diag.FromErr(err)
}
}
return nil
}