-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathdata_source_zia_user_management_departments.go
74 lines (66 loc) · 1.6 KB
/
data_source_zia_user_management_departments.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
package zia
import (
"fmt"
"log"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/zscaler/zscaler-sdk-go/v2/zia/services/usermanagement/departments"
)
func dataSourceDepartmentManagement() *schema.Resource {
return &schema.Resource{
Read: dataSourceDepartmentManagementRead,
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeInt,
Computed: true,
},
"name": {
Type: schema.TypeString,
Optional: true,
},
"idp_id": {
Type: schema.TypeInt,
Computed: true,
},
"comments": {
Type: schema.TypeString,
Computed: true,
},
"deleted": {
Type: schema.TypeBool,
Computed: true,
},
},
}
}
func dataSourceDepartmentManagementRead(d *schema.ResourceData, m interface{}) error {
zClient := m.(*Client)
var resp *departments.Department
id, ok := getIntFromResourceData(d, "id")
if ok {
log.Printf("[INFO] Getting data for department id: %d\n", id)
res, err := zClient.departments.GetDepartments(id)
if err != nil {
return err
}
resp = res
}
name, _ := d.Get("name").(string)
if resp == nil && name != "" {
log.Printf("[INFO] Getting data for department : %s\n", name)
res, err := zClient.departments.GetDepartmentsByName(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("idp_id", resp.IdpID)
_ = d.Set("comments", resp.Comments)
_ = d.Set("deleted", resp.Deleted)
} else {
return fmt.Errorf("couldn't find any department with name '%s' or id '%d'", name, id)
}
return nil
}