|
| 1 | +package cloudidentity |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + |
| 7 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 8 | + "github.com/hashicorp/terraform-provider-google/google/tpgresource" |
| 9 | + transport_tpg "github.com/hashicorp/terraform-provider-google/google/transport" |
| 10 | +) |
| 11 | + |
| 12 | +func DataSourceGoogleCloudIdentityPolicies() *schema.Resource { |
| 13 | + return &schema.Resource{ |
| 14 | + Read: dataSourceGoogleCloudIdentityPoliciesRead, |
| 15 | + Schema: map[string]*schema.Schema{ |
| 16 | + "filter": { |
| 17 | + Type: schema.TypeString, |
| 18 | + Optional: true, |
| 19 | + Description: `Filter expression for listing policies, as documented in the Cloud Identity Policy API policies.list method`, |
| 20 | + }, |
| 21 | + "policies": { |
| 22 | + Type: schema.TypeList, |
| 23 | + Computed: true, |
| 24 | + Description: `List of Cloud Identity policies that match the filter (or all policies if no filter is provided).`, |
| 25 | + Elem: &schema.Resource{ |
| 26 | + Schema: map[string]*schema.Schema{ |
| 27 | + "name": { |
| 28 | + Type: schema.TypeString, |
| 29 | + Computed: true, |
| 30 | + Description: `The resource name of the policy.`, |
| 31 | + }, |
| 32 | + "customer": { |
| 33 | + Type: schema.TypeString, |
| 34 | + Computed: true, |
| 35 | + Description: `The customer that the policy belongs to.`, |
| 36 | + }, |
| 37 | + "policy_query": { |
| 38 | + Type: schema.TypeList, |
| 39 | + Computed: true, |
| 40 | + Description: `The CEL query that defines which entities the policy applies to.`, |
| 41 | + Elem: &schema.Resource{ |
| 42 | + Schema: map[string]*schema.Schema{ |
| 43 | + "query": { |
| 44 | + Type: schema.TypeString, |
| 45 | + Computed: true, |
| 46 | + Description: "The query that defines which entities the policy applies to.", |
| 47 | + }, |
| 48 | + "group": { |
| 49 | + Type: schema.TypeString, |
| 50 | + Computed: true, |
| 51 | + Description: "The group that the policy applies to.", |
| 52 | + }, |
| 53 | + "org_unit": { |
| 54 | + Type: schema.TypeString, |
| 55 | + Computed: true, |
| 56 | + Description: "The org unit that the policy applies to.", |
| 57 | + }, |
| 58 | + "sort_order": { |
| 59 | + Type: schema.TypeFloat, |
| 60 | + Computed: true, |
| 61 | + Description: "The sort order of the policy.", |
| 62 | + }, |
| 63 | + }, |
| 64 | + }, |
| 65 | + }, |
| 66 | + "setting": { |
| 67 | + Type: schema.TypeString, |
| 68 | + Computed: true, |
| 69 | + Description: `The setting configured by this policy.`, |
| 70 | + }, |
| 71 | + "type": { |
| 72 | + Type: schema.TypeString, |
| 73 | + Computed: true, |
| 74 | + Description: `The type of the policy.`, |
| 75 | + }, |
| 76 | + }, |
| 77 | + }, |
| 78 | + }, |
| 79 | + }, |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +func dataSourceGoogleCloudIdentityPoliciesRead(d *schema.ResourceData, meta interface{}) error { |
| 84 | + config := meta.(*transport_tpg.Config) |
| 85 | + userAgent, err := tpgresource.GenerateUserAgentString(d, config.UserAgent) |
| 86 | + if err != nil { |
| 87 | + return err |
| 88 | + } |
| 89 | + |
| 90 | + policiesListCall := config.NewCloudIdentityClient(userAgent).Policies.List() |
| 91 | + |
| 92 | + if filter, ok := d.GetOk("filter"); ok { |
| 93 | + policiesListCall = policiesListCall.Filter(filter.(string)) |
| 94 | + } |
| 95 | + |
| 96 | + if config.UserProjectOverride { |
| 97 | + billingProject := "" |
| 98 | + // err may be nil - project isn't required for this resource |
| 99 | + if project, err := tpgresource.GetProject(d, config); err == nil { |
| 100 | + billingProject = project |
| 101 | + } |
| 102 | + |
| 103 | + // err == nil indicates that the billing_project value was found |
| 104 | + if bp, err := tpgresource.GetBillingProject(d, config); err == nil { |
| 105 | + billingProject = bp |
| 106 | + } |
| 107 | + |
| 108 | + if billingProject != "" { |
| 109 | + policiesListCall.Header().Set("X-Goog-User-Project", billingProject) |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + var allPolicies []map[string]interface{} |
| 114 | + |
| 115 | + for { |
| 116 | + resp, err := policiesListCall.Do() |
| 117 | + if err != nil { |
| 118 | + return transport_tpg.HandleDataSourceNotFoundError(err, d, "CloudIdentityListPolicies", "Policies") |
| 119 | + } |
| 120 | + |
| 121 | + if resp.Policies != nil { |
| 122 | + for _, p := range resp.Policies { |
| 123 | + policyMap := map[string]interface{}{ |
| 124 | + "name": p.Name, |
| 125 | + "customer": p.Customer, |
| 126 | + } |
| 127 | + |
| 128 | + if p.PolicyQuery != nil { |
| 129 | + pq := map[string]interface{}{ |
| 130 | + "query": p.PolicyQuery.Query, |
| 131 | + "group": p.PolicyQuery.Group, |
| 132 | + "org_unit": p.PolicyQuery.OrgUnit, |
| 133 | + "sort_order": p.PolicyQuery.SortOrder, |
| 134 | + } |
| 135 | + policyMap["policy_query"] = []interface{}{pq} |
| 136 | + } |
| 137 | + |
| 138 | + if p.Setting != nil { |
| 139 | + settingBytes, err := json.Marshal(p.Setting) |
| 140 | + if err != nil { |
| 141 | + return fmt.Errorf("error marshalling policy setting: %s", err) |
| 142 | + } |
| 143 | + policyMap["setting"] = string(settingBytes) |
| 144 | + } |
| 145 | + |
| 146 | + policyMap["type"] = p.Type |
| 147 | + |
| 148 | + allPolicies = append(allPolicies, policyMap) |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + if resp.NextPageToken == "" { |
| 153 | + break |
| 154 | + } |
| 155 | + |
| 156 | + policiesListCall = policiesListCall.PageToken(resp.NextPageToken) |
| 157 | + } |
| 158 | + |
| 159 | + if err := d.Set("policies", allPolicies); err != nil { |
| 160 | + return fmt.Errorf("error setting policies: %s", err) |
| 161 | + } |
| 162 | + |
| 163 | + if filter, ok := d.GetOk("filter"); ok { |
| 164 | + d.SetId(fmt.Sprintf("cloud-identity-policies-%s", filter.(string))) |
| 165 | + } else { |
| 166 | + d.SetId(fmt.Sprintf("cloud-identity-policies-%d", len(allPolicies))) |
| 167 | + } |
| 168 | + |
| 169 | + return nil |
| 170 | +} |
0 commit comments