-
Notifications
You must be signed in to change notification settings - Fork 6
/
data_source_zia_user_management_users.go
163 lines (154 loc) · 3.64 KB
/
data_source_zia_user_management_users.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
package zia
import (
"fmt"
"log"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
"github.com/zscaler/zscaler-sdk-go/v2/zia/services/usermanagement/users"
)
func dataSourceUserManagement() *schema.Resource {
return &schema.Resource{
Read: dataSourceUserManagementRead,
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeInt,
Optional: true,
},
"name": {
Type: schema.TypeString,
Optional: true,
},
"email": {
Type: schema.TypeString,
Computed: true,
},
"groups": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeInt,
Computed: true,
},
"name": {
Type: schema.TypeString,
Computed: true,
},
"idp_id": {
Type: schema.TypeInt,
Computed: true,
},
"comments": {
Type: schema.TypeString,
Computed: true,
},
},
},
},
"department": {
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeInt,
Computed: true,
},
"name": {
Type: schema.TypeString,
Computed: true,
},
"idp_id": {
Type: schema.TypeInt,
Computed: true,
},
"comments": {
Type: schema.TypeString,
Computed: true,
},
"deleted": {
Type: schema.TypeBool,
Computed: true,
},
},
},
},
"comments": {
Type: schema.TypeString,
Computed: true,
},
"temp_auth_email": {
Type: schema.TypeString,
Computed: true,
},
"auth_methods": {
Type: schema.TypeSet,
Optional: true,
Description: "Accepted Authentication Methods",
Elem: &schema.Schema{
Type: schema.TypeString,
ValidateFunc: validation.StringInSlice([]string{
"BASIC",
"DIGEST",
}, false),
},
},
"is_auditor": {
Type: schema.TypeString,
Computed: true,
},
"admin_user": {
Type: schema.TypeBool,
Computed: true,
},
"type": {
Type: schema.TypeString,
Computed: true,
},
},
}
}
func dataSourceUserManagementRead(d *schema.ResourceData, m interface{}) error {
zClient := m.(*Client)
var resp *users.Users
id, ok := getIntFromResourceData(d, "id")
if ok {
log.Printf("[INFO] Getting data for user id: %d\n", id)
res, err := zClient.users.Get(id)
if err != nil {
return err
}
resp = res
}
name, _ := d.Get("name").(string)
if resp == nil && name != "" {
log.Printf("[INFO] Getting data for user : %s\n", name)
res, err := zClient.users.GetUserByName(name)
if err != nil {
return err
}
resp = res
}
if resp != nil {
d.SetId(fmt.Sprintf("%d", resp.ID))
_ = d.Set("name", resp.Name)
_ = d.Set("email", resp.Email)
_ = d.Set("comments", resp.Comments)
_ = d.Set("temp_auth_email", resp.TempAuthEmail)
_ = d.Set("admin_user", resp.AdminUser)
_ = d.Set("type", resp.Type)
_ = d.Set("auth_methods", resp.AuthMethods)
if err := d.Set("department", flattenUserDepartment(resp.Department)); err != nil {
return err
}
// This function needs to be fixed, as the attributes are not application for user management
// See: https://help.zscaler.com/zia/user-management#/users-get
if err := d.Set("groups", flattenIDNameExtensions(resp.Groups)); err != nil {
return err
}
} else {
return fmt.Errorf("couldn't find any user with name '%s' or id '%d'", name, id)
}
return nil
}