This repository has been archived by the owner on Jun 27, 2021. It is now read-only.
/
data_user_attributes.go
126 lines (112 loc) · 3.52 KB
/
data_user_attributes.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
package gsuite
import (
"encoding/json"
"fmt"
"strconv"
"github.com/hashicorp/terraform-plugin-sdk/helper/hashcode"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
)
// userAttrMappings is a mapping of top level keys in a gsuite_user_attributes
// data source to a struct defining type of of value expected as input and
// whether or not the value is a list. See also userAttrMapping.schema() which
// constructs the underlying *schema.Schema at runtime.
var userAttrMappings = map[string]*userAttrMapping{
"string": {schema.TypeString, false},
"strings": {schema.TypeString, true},
"bool": {schema.TypeBool, false},
"bools": {schema.TypeBool, true},
"integer": {schema.TypeInt, false},
"integers": {schema.TypeInt, true},
"double": {schema.TypeFloat, false},
"doubles": {schema.TypeFloat, true},
"date": {schema.TypeString, false},
"dates": {schema.TypeString, true},
"email": {schema.TypeString, false},
"emails": {schema.TypeString, true},
"phone": {schema.TypeString, false},
"phones": {schema.TypeString, true},
}
type userAttrMapping struct {
valueType schema.ValueType
list bool
}
func (s *userAttrMapping) schema() *schema.Schema {
value := &schema.Schema{Required: true, Type: s.valueType}
if s.list {
value.Type = schema.TypeList
value.Elem = &schema.Schema{Type: s.valueType}
}
return &schema.Schema{
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
},
"value": value,
},
},
}
}
func dataUserAttributes() *schema.Resource {
resource := &schema.Resource{
Read: dataUserAttributesRead,
Schema: map[string]*schema.Schema{
"json": {
Type: schema.TypeString,
Computed: true,
},
},
}
for name, mapping := range userAttrMappings {
resource.Schema[name] = mapping.schema()
}
return resource
}
type entry struct {
Type string `json:"type"`
Value interface{} `json:"value"`
}
// MarshalJSON converts the interface value to a reasonable string
// representation. Without this some types of values (basically anything not a string)
// ends up without quotes around it. While Google's directory SDK does allow you
// to pass in some types of values without quotes other types, like floats, require
// strings. Placing quotes around *all* types of values provides the most
// consistent behavior overall.
func (e *entry) MarshalJSON() ([]byte, error) {
return []byte(fmt.Sprintf(`{"type": "%s", "value":"%v"}`, e.Type, e.Value)), nil
}
func dataUserAttributesRead(d *schema.ResourceData, _ interface{}) error {
customAttributes := map[string]interface{}{}
for name, mapping := range userAttrMappings {
if mapping.list {
if statements, ok := d.GetOk(name); ok {
for _, statement := range statements.(*schema.Set).List() {
stmt := statement.(map[string]interface{})
var values []*entry
for _, value := range stmt["value"].([]interface{}) {
values = append(values, &entry{Type: "work", Value: value})
}
customAttributes[stmt["name"].(string)] = values
}
}
continue
}
if statements, ok := d.GetOk(name); ok {
for _, statement := range statements.(*schema.Set).List() {
stmt := statement.(map[string]interface{})
customAttributes[stmt["name"].(string)] = stmt["value"]
}
}
}
out, err := json.Marshal(customAttributes)
if err != nil {
return err
}
outString := string(out)
d.SetId(strconv.Itoa(hashcode.String(outString)))
d.Set("json", outString)
return nil
}