|
| 1 | +package accesscontextmanager |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "slices" |
| 6 | + "strings" |
| 7 | + |
| 8 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 9 | + "github.com/hashicorp/terraform-provider-google/google/tpgresource" |
| 10 | + transport_tpg "github.com/hashicorp/terraform-provider-google/google/transport" |
| 11 | +) |
| 12 | + |
| 13 | +func DataSourceAccessContextManagerAccessPolicy() *schema.Resource { |
| 14 | + return &schema.Resource{ |
| 15 | + Read: dataSourceAccessContextManagerAccessPolicyRead, |
| 16 | + Schema: map[string]*schema.Schema{ |
| 17 | + "parent": { |
| 18 | + Type: schema.TypeString, |
| 19 | + Required: true, |
| 20 | + ForceNew: true, |
| 21 | + }, |
| 22 | + "scopes": { |
| 23 | + Type: schema.TypeList, |
| 24 | + Elem: &schema.Schema{ |
| 25 | + Type: schema.TypeString, |
| 26 | + }, |
| 27 | + Optional: true, |
| 28 | + }, |
| 29 | + "title": { |
| 30 | + Type: schema.TypeString, |
| 31 | + Computed: true, |
| 32 | + }, |
| 33 | + "name": { |
| 34 | + Type: schema.TypeString, |
| 35 | + Computed: true, |
| 36 | + }, |
| 37 | + }, |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +func dataSourceAccessContextManagerAccessPolicyRead(d *schema.ResourceData, meta interface{}) error { |
| 42 | + config := meta.(*transport_tpg.Config) |
| 43 | + userAgent, err := tpgresource.GenerateUserAgentString(d, config.UserAgent) |
| 44 | + if err != nil { |
| 45 | + return err |
| 46 | + } |
| 47 | + |
| 48 | + url, err := tpgresource.ReplaceVars(d, config, "{{AccessContextManagerBasePath}}accessPolicies?parent={{parent}}") |
| 49 | + if err != nil { |
| 50 | + return err |
| 51 | + } |
| 52 | + |
| 53 | + billingProject := "" |
| 54 | + |
| 55 | + // err == nil indicates that the billing_project value was found |
| 56 | + if bp, err := tpgresource.GetBillingProject(d, config); err == nil { |
| 57 | + billingProject = bp |
| 58 | + } |
| 59 | + |
| 60 | + res, err := transport_tpg.SendRequest(transport_tpg.SendRequestOptions{ |
| 61 | + Config: config, |
| 62 | + Method: "GET", |
| 63 | + Project: billingProject, |
| 64 | + RawURL: url, |
| 65 | + UserAgent: userAgent, |
| 66 | + }) |
| 67 | + |
| 68 | + if err != nil { |
| 69 | + return transport_tpg.HandleDataSourceNotFoundError(err, d, fmt.Sprintf("AccessContextManagerAccessPolicy %q", d.Id()), url) |
| 70 | + } |
| 71 | + |
| 72 | + if res == nil { |
| 73 | + return fmt.Errorf("Error fetching policies: %s", err) |
| 74 | + } |
| 75 | + |
| 76 | + policies, err := parse_policies_response(res) |
| 77 | + if err != nil { |
| 78 | + fmt.Errorf("Error parsing list policies response: %s", err) |
| 79 | + } |
| 80 | + |
| 81 | + // Find the matching policy in the list of policies response. Both the parent and scopes |
| 82 | + // should match |
| 83 | + for _, fetched_policy := range policies { |
| 84 | + scopes_match := compare_scopes(d.Get("scopes").([]interface{}), fetched_policy.Scopes) |
| 85 | + if fetched_policy.Parent == d.Get("parent").(string) && scopes_match { |
| 86 | + name_without_prefix := strings.Split(fetched_policy.Name, "accessPolicies/")[1] |
| 87 | + d.SetId(name_without_prefix) |
| 88 | + if err := d.Set("name", name_without_prefix); err != nil { |
| 89 | + return fmt.Errorf("Error setting policy name: %s", err) |
| 90 | + } |
| 91 | + |
| 92 | + if err := d.Set("title", fetched_policy.Title); err != nil { |
| 93 | + return fmt.Errorf("Error setting policy title: %s", err) |
| 94 | + } |
| 95 | + |
| 96 | + return nil |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + return nil |
| 101 | +} |
| 102 | + |
| 103 | +func parse_policies_response(res map[string]interface{}) ([]AccessPolicy, error) { |
| 104 | + var policies []AccessPolicy |
| 105 | + for _, res_policy := range res["accessPolicies"].([]interface{}) { |
| 106 | + parsed_policy := &AccessPolicy{} |
| 107 | + |
| 108 | + err := tpgresource.Convert(res_policy, parsed_policy) |
| 109 | + if err != nil { |
| 110 | + return nil, err |
| 111 | + } |
| 112 | + |
| 113 | + policies = append(policies, *parsed_policy) |
| 114 | + } |
| 115 | + return policies, nil |
| 116 | +} |
| 117 | + |
| 118 | +func compare_scopes(config_scopes []interface{}, policy_scopes []string) bool { |
| 119 | + // converts []interface{} to []string |
| 120 | + var config_scopes_slice []string |
| 121 | + for _, scope := range config_scopes { |
| 122 | + config_scopes_slice = append(config_scopes_slice, scope.(string)) |
| 123 | + } |
| 124 | + |
| 125 | + return slices.Equal(config_scopes_slice, policy_scopes) |
| 126 | +} |
| 127 | + |
| 128 | +type AccessPolicy struct { |
| 129 | + Name string `json:"name"` |
| 130 | + Title string `json:"title"` |
| 131 | + Parent string `json:"parent"` |
| 132 | + Scopes []string `json:"scopes"` |
| 133 | +} |
0 commit comments