-
Notifications
You must be signed in to change notification settings - Fork 16
/
data_source_zpa_scim_group.go
104 lines (100 loc) · 2.56 KB
/
data_source_zpa_scim_group.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
package zpa
import (
"fmt"
"log"
"strconv"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/zscaler/zscaler-sdk-go/zpa/services/idpcontroller"
"github.com/zscaler/zscaler-sdk-go/zpa/services/scimgroup"
)
func dataSourceScimGroup() *schema.Resource {
return &schema.Resource{
Read: dataSourceScimGroupRead,
Schema: map[string]*schema.Schema{
"creation_time": {
Type: schema.TypeInt,
Computed: true,
},
"id": {
Type: schema.TypeString,
Optional: true,
},
"name": {
Type: schema.TypeString,
Optional: true,
},
"idp_group_id": {
Type: schema.TypeString,
Computed: true,
},
"idp_id": {
Type: schema.TypeInt,
Optional: true,
},
"idp_name": {
Type: schema.TypeString,
Optional: true,
},
"modified_time": {
Type: schema.TypeInt,
Computed: true,
},
},
}
}
func dataSourceScimGroupRead(d *schema.ResourceData, m interface{}) error {
zClient := m.(*Client)
var resp *scimgroup.ScimGroup
idpId, okidpId := d.Get("idp_id").(string)
idpName, okIdpName := d.Get("idp_name").(string)
if !okIdpName && !okidpId || idpId == "" && idpName == "" {
log.Printf("[INFO] idp name or id is required\n")
return fmt.Errorf("idp name or id is required")
}
var idpResp *idpcontroller.IdpController
// getting Idp controller by id or name
if idpId != "" {
resp, _, err := zClient.idpcontroller.Get(idpId)
if err != nil || resp == nil {
log.Printf("[INFO] couldn't find idp by id: %s\n", idpId)
return err
}
idpResp = resp
} else {
resp, _, err := zClient.idpcontroller.GetByName(idpName)
if err != nil || resp == nil {
log.Printf("[INFO] couldn't find idp by name: %s\n", idpName)
return err
}
idpResp = resp
}
// getting scim attribute header by id or name
id, ok := d.Get("id").(string)
if ok && id != "" {
res, _, err := zClient.scimgroup.Get(idpResp.ID)
if err != nil {
return err
}
resp = res
}
name, ok := d.Get("name").(string)
if id == "" && ok && name != "" {
res, _, err := zClient.scimgroup.GetByName(name, idpResp.ID)
if err != nil {
return err
}
resp = res
}
if resp != nil {
d.SetId(strconv.FormatInt(int64(resp.ID), 10))
_ = d.Set("creation_time", resp.CreationTime)
_ = d.Set("idp_group_id", resp.IdpGroupID)
_ = d.Set("idp_id", resp.IdpID)
_ = d.Set("idp_name", resp.IdpName)
_ = d.Set("modified_time", resp.ModifiedTime)
_ = d.Set("name", resp.Name)
} else {
return fmt.Errorf("no scim name '%s' & idp name '%s' OR id '%s' was found", name, idpName, id)
}
return nil
}