This repository has been archived by the owner on Sep 12, 2020. It is now read-only.
forked from yandex-cloud/terraform-provider-yandex
-
Notifications
You must be signed in to change notification settings - Fork 6
/
data_source_yandex_iam_policy.go
87 lines (75 loc) · 2.06 KB
/
data_source_yandex_iam_policy.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
package yandex
import (
"encoding/json"
"strconv"
"github.com/hashicorp/terraform-plugin-sdk/helper/hashcode"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
)
// dataSourceYandexIamPolicy returns a *schema.Resource that allows a customer
// to express a Yandex.Cloud IAM policy in a data resource. This is an example
// of how the schema would be used in a config:
//
// data "yandex_iam_policy" "admin" {
// binding {
// role = "roles/viewer"
// members = [
// "userAccount:some_user_id",
// ]
// }
// }
var iamBinding = &schema.Schema{
Type: schema.TypeSet,
Required: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"role": {
Type: schema.TypeString,
Required: true,
},
"members": {
Type: schema.TypeSet,
Required: true,
Elem: &schema.Schema{
Type: schema.TypeString,
ValidateFunc: validateIamMember,
},
Set: schema.HashString,
},
},
},
}
func dataSourceYandexIAMPolicy() *schema.Resource {
return &schema.Resource{
Schema: map[string]*schema.Schema{
"binding": iamBinding,
"policy_data": {
Type: schema.TypeString,
Computed: true,
},
},
Read: dataSourceYandexIAMPolicyRead,
}
}
func dataSourceYandexIAMPolicyRead(d *schema.ResourceData, meta interface{}) error {
var policy Policy
// The schema supports multiple binding{} blocks
bset := d.Get("binding").(*schema.Set)
// Convert each config binding into a access.AccessBinding
for _, v := range bset.List() {
binding := v.(map[string]interface{})
role := binding["role"].(string)
for _, member := range convertStringSet(binding["members"].(*schema.Set)) {
policy.Bindings = append(policy.Bindings, roleMemberToAccessBinding(role, member))
}
}
// Marshal Policy to JSON suitable for storing in state
jsonPolicy, err := json.Marshal(&policy)
if err != nil {
// should never happen if the above code is correct
return err
}
stringPolicy := string(jsonPolicy)
d.Set("policy_data", stringPolicy)
d.SetId(strconv.Itoa(hashcode.String(stringPolicy)))
return nil
}