|
| 1 | +package google |
| 2 | + |
| 3 | +import ( |
| 4 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 5 | + |
| 6 | + "fmt" |
| 7 | + |
| 8 | + transport_tpg "github.com/hashicorp/terraform-provider-google/google/transport" |
| 9 | +) |
| 10 | + |
| 11 | +var IamPolicyBaseDataSourceSchema = map[string]*schema.Schema{ |
| 12 | + "policy_data": { |
| 13 | + Type: schema.TypeString, |
| 14 | + Computed: true, |
| 15 | + }, |
| 16 | + "etag": { |
| 17 | + Type: schema.TypeString, |
| 18 | + Computed: true, |
| 19 | + }, |
| 20 | +} |
| 21 | + |
| 22 | +func DataSourceIamPolicy(parentSpecificSchema map[string]*schema.Schema, newUpdaterFunc newResourceIamUpdaterFunc, options ...func(*IamSettings)) *schema.Resource { |
| 23 | + settings := &IamSettings{} |
| 24 | + for _, o := range options { |
| 25 | + o(settings) |
| 26 | + } |
| 27 | + |
| 28 | + return &schema.Resource{ |
| 29 | + Read: DatasourceIamPolicyRead(newUpdaterFunc), |
| 30 | + // if non-empty, this will be used to send a deprecation message when the |
| 31 | + // datasource is used. |
| 32 | + DeprecationMessage: settings.DeprecationMessage, |
| 33 | + Schema: mergeSchemas(IamPolicyBaseDataSourceSchema, parentSpecificSchema), |
| 34 | + UseJSONNumber: true, |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +func DatasourceIamPolicyRead(newUpdaterFunc newResourceIamUpdaterFunc) schema.ReadFunc { |
| 39 | + return func(d *schema.ResourceData, meta interface{}) error { |
| 40 | + config := meta.(*transport_tpg.Config) |
| 41 | + |
| 42 | + updater, err := newUpdaterFunc(d, config) |
| 43 | + if err != nil { |
| 44 | + return err |
| 45 | + } |
| 46 | + |
| 47 | + policy, err := iamPolicyReadWithRetry(updater) |
| 48 | + if err != nil { |
| 49 | + return transport_tpg.HandleNotFoundError(err, d, fmt.Sprintf("Resource %q with IAM Policy", updater.DescribeResource())) |
| 50 | + } |
| 51 | + |
| 52 | + if err := d.Set("etag", policy.Etag); err != nil { |
| 53 | + return fmt.Errorf("Error setting etag: %s", err) |
| 54 | + } |
| 55 | + if err := d.Set("policy_data", marshalIamPolicy(policy)); err != nil { |
| 56 | + return fmt.Errorf("Error setting policy_data: %s", err) |
| 57 | + } |
| 58 | + d.SetId(updater.GetResourceId()) |
| 59 | + |
| 60 | + return nil |
| 61 | + } |
| 62 | +} |
0 commit comments