generated from hashicorp/terraform-provider-scaffolding
/
data_source_chrome_policy_schema.go
267 lines (242 loc) · 8.32 KB
/
data_source_chrome_policy_schema.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
package googleworkspace
import (
"context"
"encoding/json"
"fmt"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"google.golang.org/api/chromepolicy/v1"
)
func dataSourceChromePolicySchema() *schema.Resource {
return &schema.Resource{
Description: "Chrome Policy Schema data source in the Terraform Googleworkspace provider. Chrome Policy Schema " +
"resides under the `https://www.googleapis.com/auth/chrome.management.policy` client scope.",
ReadContext: dataSourceChromePolicySchemaRead,
Schema: map[string]*schema.Schema{
// Intentionally ignoring field 'name' https://developers.google.com/chrome/policy/reference/rest/v1/customers.policySchemas#PolicySchema
// it is a confusing field, that includes url segments the practitioner won't find useful.
// Format: name=customers/{customer}/policySchemas/{schema_namespace}
// Using the output field 'schema_name' instead as the field the practitioner specifies
"schema_name": {
Description: "The full qualified name of the policy schema",
Type: schema.TypeString,
Required: true,
},
"policy_description": {
Description: "Description about the policy schema for user consumption.",
Type: schema.TypeString,
Computed: true,
},
"additional_target_key_names": {
Description: "Additional key names that will be used to identify the target of the policy value. When specifying a policyTargetKey, each of the additional keys specified here will have to be included in the additionalTargetKeys map.",
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"key": {
Description: "Key name.",
Type: schema.TypeString,
Computed: true,
},
"key_description": {
Description: "Key description.",
Type: schema.TypeString,
Computed: true,
},
},
},
},
"definition": {
Description: "Schema definition using proto descriptor.",
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"name": {
Description: "File name, relative to root of source tree",
Type: schema.TypeString,
Computed: true,
},
"package": {
Description: "e.g. 'foo', 'foo.bar', etc.",
Type: schema.TypeString,
Computed: true,
},
"message_type": {
Description: "All top-level definitions in this file, represented as a JSON string",
Type: schema.TypeString,
Computed: true,
},
"enum_type": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Computed: true,
},
"value": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Computed: true,
},
"number": {
Type: schema.TypeInt,
Computed: true,
},
},
},
},
},
},
},
"syntax": {
Description: "The syntax of the proto file. The supported values are 'proto' and 'proto3'.",
Type: schema.TypeString,
Computed: true,
},
},
},
},
"field_descriptions": {
Description: "Detailed description of each field that is part of the schema, represented as a JSON string.",
Type: schema.TypeString,
Computed: true,
},
"access_restrictions": {
Description: "Specific access restrictions related to this policy.",
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"notices": {
Description: "Special notice messages related to setting certain values in certain fields in the schema.",
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"field": {
Description: "The field name associated with the notice.",
Type: schema.TypeString,
Computed: true,
},
"notice_value": {
Description: "The value of the field that has a notice. When setting the field to this value, the user may be required to acknowledge the notice message in order for the value to be set.",
Type: schema.TypeString,
Computed: true,
},
"notice_message": {
Description: "The notice message associate with the value of the field.",
Type: schema.TypeString,
Computed: true,
},
"acknowledgement_required": {
Description: "Whether the user needs to acknowledge the notice message before the value can be set.",
Type: schema.TypeBool,
Computed: true,
},
},
},
},
"support_uri": {
Description: "URI to related support article for this schema.",
Type: schema.TypeString,
Computed: true,
},
},
}
}
func dataSourceChromePolicySchemaRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
client := meta.(*apiClient)
chromePolicyService, diags := client.NewChromePolicyService()
if diags.HasError() {
return diags
}
chromePolicySchemasService, diags := GetChromePolicySchemasService(chromePolicyService)
if diags.HasError() {
return diags
}
policySchema, err := chromePolicySchemasService.Get(fmt.Sprintf("customers/%s/policySchemas/%s", client.Customer, d.Get("schema_name").(string))).Do()
if err != nil {
return diag.FromErr(err)
}
d.SetId(policySchema.SchemaName)
d.Set("schema_name", policySchema.SchemaName)
d.Set("policy_description", policySchema.PolicyDescription)
d.Set("support_uri", policySchema.SupportUri)
if err := d.Set("additional_target_key_names", flattenAdditionalTargetKeyNames(policySchema.AdditionalTargetKeyNames)); err != nil {
return diag.FromErr(err)
}
if err := d.Set("definition", flattenDefinition(policySchema.Definition)); err != nil {
return diag.FromErr(err)
}
// this attribute contains recursive types, so we store it as json
fieldDescriptions, _ := json.MarshalIndent(policySchema.FieldDescriptions, "", " ")
d.Set("field_descriptions", string(fieldDescriptions))
if err := d.Set("access_restrictions", policySchema.AccessRestrictions); err != nil {
return diag.FromErr(err)
}
if err := d.Set("notices", flattenNotices(policySchema.Notices)); err != nil {
return diag.FromErr(err)
}
return nil
}
func flattenNotices(ns []*chromepolicy.GoogleChromePolicyV1PolicySchemaNoticeDescription) []interface{} {
result := make([]interface{}, len(ns))
for i, n := range ns {
obj := make(map[string]interface{})
obj["field"] = n.Field
obj["notice_value"] = n.NoticeValue
obj["notice_message"] = n.NoticeMessage
obj["acknowledgement_required"] = n.AcknowledgementRequired
result[i] = obj
}
return result
}
func flattenEnumType(es []*chromepolicy.Proto2EnumDescriptorProto) []interface{} {
result := make([]interface{}, len(es))
for i, e := range es {
obj := make(map[string]interface{})
obj["name"] = e.Name
values := make([]interface{}, len(e.Value))
for j, v := range e.Value {
values[j] = map[string]interface{}{
"name": v.Name,
"number": int(v.Number),
}
}
obj["value"] = values
result[i] = obj
}
return result
}
func flattenDefinition(d *chromepolicy.Proto2FileDescriptorProto) []interface{} {
result := make([]interface{}, 1)
obj := make(map[string]interface{})
obj["name"] = d.Name
obj["package"] = d.Package
obj["syntax"] = d.Syntax
obj["enum_type"] = flattenEnumType(d.EnumType)
// this attribute contains recursive types, so we store it as json
msgType, _ := json.MarshalIndent(d.MessageType, "", " ")
obj["message_type"] = string(msgType)
result[0] = obj
return result
}
func flattenAdditionalTargetKeyNames(as []*chromepolicy.GoogleChromePolicyV1AdditionalTargetKeyName) []interface{} {
result := make([]interface{}, len(as))
for i, a := range as {
obj := make(map[string]interface{})
obj["key"] = a.Key
obj["key_description"] = a.KeyDescription
result[i] = obj
}
return result
}