-
Notifications
You must be signed in to change notification settings - Fork 4
/
datasource_directoryobject.go
111 lines (100 loc) · 2.94 KB
/
datasource_directoryobject.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
package centrify
import (
"fmt"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/helper/validation"
logger "github.com/marcozj/golang-sdk/logging"
vault "github.com/marcozj/golang-sdk/platform"
"github.com/marcozj/golang-sdk/restapi"
)
func dataSourceDirectoryObject() *schema.Resource {
return &schema.Resource{
Read: dataSourceDirectoryObjectRead,
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
Description: "Name of the directory object",
},
"object_type": {
Type: schema.TypeString,
Required: true,
Description: "Type of the directory object",
ValidateFunc: validation.StringInSlice([]string{
"User",
"Group",
}, false),
},
"system_name": {
Type: schema.TypeString,
Optional: true,
Computed: true,
Description: "UPN of the directory object",
},
"display_name": {
Type: schema.TypeString,
Optional: true,
Computed: true,
Description: "Display name of the directory object",
},
"distinguished_name": {
Type: schema.TypeString,
Optional: true,
Computed: true,
Description: "Distinguished name of the directory object",
},
"forest": {
Type: schema.TypeString,
Optional: true,
Computed: true,
Description: "Forest name of the directory object",
},
"directory_services": {
Type: schema.TypeSet,
Required: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
Description: "List of UUID of directory services",
},
},
}
}
func dataSourceDirectoryObjectRead(d *schema.ResourceData, m interface{}) error {
logger.Infof("Finding Directory Object")
client := m.(*restapi.RestClient)
object := vault.NewDirectoryObjects(client)
object.QueryName = d.Get("name").(string)
object.ObjectType = d.Get("object_type").(string)
object.DirectoryServices = flattenSchemaSetToStringSlice(d.Get("directory_services"))
err := object.Read()
if err != nil {
return fmt.Errorf("error retrieving directory services: %s", err)
}
var results []vault.DirectoryObject
// Further narrow down with Distinguished if specified
dn := d.Get("distinguished_name").(string)
if dn != "" {
for _, v := range object.DirectoryObjects {
if dn == v.DistinguishedName {
results = append(results, v)
}
}
} else {
results = object.DirectoryObjects
}
if len(results) == 0 {
return fmt.Errorf("query returns 0 object for directory object %s", object.QueryName)
}
if len(results) > 1 {
return fmt.Errorf("search directory object %s, but returns too many objects (found %d, expected 1)", object.QueryName, len(results))
}
var result = results[0]
d.SetId(result.ID)
d.Set("ID", result.ID)
d.Set("display_name", result.DisplayName)
d.Set("name", result.Name)
d.Set("forest", result.Forest)
d.Set("system_name", result.SystemName)
return nil
}