2016-08-10 06:44:53 +02:00
|
|
|
package google
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"strconv"
|
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/helper/hashcode"
|
|
|
|
"github.com/hashicorp/terraform/helper/schema"
|
|
|
|
"google.golang.org/api/cloudresourcemanager/v1"
|
|
|
|
)
|
|
|
|
|
2016-08-21 09:25:00 +02:00
|
|
|
// dataSourceGoogleIamPolicy returns a *schema.Resource that allows a customer
|
|
|
|
// to express a Google Cloud IAM policy in a data resource. This is an example
|
|
|
|
// of how the schema would be used in a config:
|
|
|
|
//
|
|
|
|
// data "google_iam_policy" "admin" {
|
|
|
|
// binding {
|
|
|
|
// role = "roles/storage.objectViewer"
|
|
|
|
// members = [
|
|
|
|
// "user:evanbrown@google.com",
|
|
|
|
// ]
|
|
|
|
// }
|
|
|
|
// }
|
2016-08-10 06:44:53 +02:00
|
|
|
func dataSourceGoogleIamPolicy() *schema.Resource {
|
|
|
|
return &schema.Resource{
|
|
|
|
Read: dataSourceGoogleIamPolicyRead,
|
|
|
|
Schema: map[string]*schema.Schema{
|
|
|
|
"binding": {
|
|
|
|
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},
|
|
|
|
Set: schema.HashString,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"policy": {
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Computed: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-21 09:25:00 +02:00
|
|
|
// dataSourceGoogleIamPolicyRead reads a data source from config and writes it
|
|
|
|
// to state.
|
2016-08-10 06:44:53 +02:00
|
|
|
func dataSourceGoogleIamPolicyRead(d *schema.ResourceData, meta interface{}) error {
|
2016-08-21 09:25:00 +02:00
|
|
|
var policy cloudresourcemanager.Policy
|
2016-08-10 06:44:53 +02:00
|
|
|
var bindings []*cloudresourcemanager.Binding
|
|
|
|
|
2016-08-21 09:25:00 +02:00
|
|
|
// The schema supports multiple binding{} blocks
|
|
|
|
bset := d.Get("binding").(*schema.Set)
|
2016-08-10 06:44:53 +02:00
|
|
|
|
2016-08-21 09:25:00 +02:00
|
|
|
// All binding{} blocks will be converted and stored in an array
|
|
|
|
bindings = make([]*cloudresourcemanager.Binding, bset.Len())
|
|
|
|
policy.Bindings = bindings
|
|
|
|
|
|
|
|
// Convert each config binding into a cloudresourcemanager.Binding
|
|
|
|
for i, v := range bset.List() {
|
|
|
|
binding := v.(map[string]interface{})
|
|
|
|
policy.Bindings[i] = &cloudresourcemanager.Binding{
|
|
|
|
Role: binding["role"].(string),
|
|
|
|
Members: dataSourceGoogleIamPolicyMembers(binding["members"].(*schema.Set)),
|
2016-08-10 06:44:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-21 09:25:00 +02:00
|
|
|
// Marshal cloudresourcemanager.Policy to JSON suitable for storing in state
|
|
|
|
pjson, err := json.Marshal(&policy)
|
2016-08-10 06:44:53 +02:00
|
|
|
if err != nil {
|
|
|
|
// should never happen if the above code is correct
|
|
|
|
return err
|
|
|
|
}
|
2016-08-21 09:25:00 +02:00
|
|
|
pstring := string(pjson)
|
2016-08-10 06:44:53 +02:00
|
|
|
|
2016-08-21 09:25:00 +02:00
|
|
|
d.Set("policy", pstring)
|
|
|
|
d.SetId(strconv.Itoa(hashcode.String(pstring)))
|
2016-08-10 06:44:53 +02:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2016-08-21 09:25:00 +02:00
|
|
|
|
|
|
|
// dataSourceGoogleIamPolicyMembers converts a set of members in a binding
|
|
|
|
// (a member is a principal, usually an e-mail address) into an array of
|
|
|
|
// string.
|
|
|
|
func dataSourceGoogleIamPolicyMembers(d *schema.Set) []string {
|
|
|
|
var members []string
|
|
|
|
members = make([]string, d.Len())
|
|
|
|
|
|
|
|
for i, v := range d.List() {
|
|
|
|
members[i] = v.(string)
|
|
|
|
}
|
|
|
|
return members
|
|
|
|
}
|