|
| 1 | +// Copyright (c) HashiCorp, Inc. |
| 2 | +// SPDX-License-Identifier: MPL-2.0 |
| 3 | + |
| 4 | +// ---------------------------------------------------------------------------- |
| 5 | +// |
| 6 | +// *** AUTO GENERATED CODE *** Type: MMv1 *** |
| 7 | +// |
| 8 | +// ---------------------------------------------------------------------------- |
| 9 | +// |
| 10 | +// This file is automatically generated by Magic Modules and manual |
| 11 | +// changes will be clobbered when the file is regenerated. |
| 12 | +// |
| 13 | +// Please read more about how to change this file in |
| 14 | +// .github/CONTRIBUTING.md. |
| 15 | +// |
| 16 | +// ---------------------------------------------------------------------------- |
| 17 | + |
| 18 | +package beyondcorp |
| 19 | + |
| 20 | +import ( |
| 21 | + "fmt" |
| 22 | + |
| 23 | + "github.com/hashicorp/errwrap" |
| 24 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 25 | + "google.golang.org/api/cloudresourcemanager/v1" |
| 26 | + |
| 27 | + "github.com/hashicorp/terraform-provider-google-beta/google-beta/tpgiamresource" |
| 28 | + "github.com/hashicorp/terraform-provider-google-beta/google-beta/tpgresource" |
| 29 | + transport_tpg "github.com/hashicorp/terraform-provider-google-beta/google-beta/transport" |
| 30 | +) |
| 31 | + |
| 32 | +var BeyondcorpSecurityGatewayIamSchema = map[string]*schema.Schema{ |
| 33 | + "project": { |
| 34 | + Type: schema.TypeString, |
| 35 | + Computed: true, |
| 36 | + Optional: true, |
| 37 | + ForceNew: true, |
| 38 | + }, |
| 39 | + "location": { |
| 40 | + Type: schema.TypeString, |
| 41 | + Computed: true, |
| 42 | + Optional: true, |
| 43 | + ForceNew: true, |
| 44 | + }, |
| 45 | + "security_gateway_id": { |
| 46 | + Type: schema.TypeString, |
| 47 | + Required: true, |
| 48 | + ForceNew: true, |
| 49 | + DiffSuppressFunc: tpgresource.CompareSelfLinkOrResourceName, |
| 50 | + }, |
| 51 | +} |
| 52 | + |
| 53 | +type BeyondcorpSecurityGatewayIamUpdater struct { |
| 54 | + project string |
| 55 | + location string |
| 56 | + securityGatewayId string |
| 57 | + d tpgresource.TerraformResourceData |
| 58 | + Config *transport_tpg.Config |
| 59 | +} |
| 60 | + |
| 61 | +func BeyondcorpSecurityGatewayIamUpdaterProducer(d tpgresource.TerraformResourceData, config *transport_tpg.Config) (tpgiamresource.ResourceIamUpdater, error) { |
| 62 | + values := make(map[string]string) |
| 63 | + |
| 64 | + project, _ := tpgresource.GetProject(d, config) |
| 65 | + if project != "" { |
| 66 | + if err := d.Set("project", project); err != nil { |
| 67 | + return nil, fmt.Errorf("Error setting project: %s", err) |
| 68 | + } |
| 69 | + } |
| 70 | + values["project"] = project |
| 71 | + location, _ := tpgresource.GetLocation(d, config) |
| 72 | + if location != "" { |
| 73 | + if err := d.Set("location", location); err != nil { |
| 74 | + return nil, fmt.Errorf("Error setting location: %s", err) |
| 75 | + } |
| 76 | + } |
| 77 | + values["location"] = location |
| 78 | + if v, ok := d.GetOk("security_gateway_id"); ok { |
| 79 | + values["security_gateway_id"] = v.(string) |
| 80 | + } |
| 81 | + |
| 82 | + // We may have gotten either a long or short name, so attempt to parse long name if possible |
| 83 | + m, err := tpgresource.GetImportIdQualifiers([]string{"projects/(?P<project>[^/]+)/locations/(?P<location>[^/]+)/securityGateways/(?P<security_gateway_id>[^/]+)", "(?P<project>[^/]+)/(?P<location>[^/]+)/(?P<security_gateway_id>[^/]+)", "(?P<location>[^/]+)/(?P<security_gateway_id>[^/]+)", "(?P<security_gateway_id>[^/]+)"}, d, config, d.Get("security_gateway_id").(string)) |
| 84 | + if err != nil { |
| 85 | + return nil, err |
| 86 | + } |
| 87 | + |
| 88 | + for k, v := range m { |
| 89 | + values[k] = v |
| 90 | + } |
| 91 | + |
| 92 | + u := &BeyondcorpSecurityGatewayIamUpdater{ |
| 93 | + project: values["project"], |
| 94 | + location: values["location"], |
| 95 | + securityGatewayId: values["security_gateway_id"], |
| 96 | + d: d, |
| 97 | + Config: config, |
| 98 | + } |
| 99 | + |
| 100 | + if err := d.Set("project", u.project); err != nil { |
| 101 | + return nil, fmt.Errorf("Error setting project: %s", err) |
| 102 | + } |
| 103 | + if err := d.Set("location", u.location); err != nil { |
| 104 | + return nil, fmt.Errorf("Error setting location: %s", err) |
| 105 | + } |
| 106 | + if err := d.Set("security_gateway_id", u.GetResourceId()); err != nil { |
| 107 | + return nil, fmt.Errorf("Error setting security_gateway_id: %s", err) |
| 108 | + } |
| 109 | + |
| 110 | + return u, nil |
| 111 | +} |
| 112 | + |
| 113 | +func BeyondcorpSecurityGatewayIdParseFunc(d *schema.ResourceData, config *transport_tpg.Config) error { |
| 114 | + values := make(map[string]string) |
| 115 | + |
| 116 | + project, _ := tpgresource.GetProject(d, config) |
| 117 | + if project != "" { |
| 118 | + values["project"] = project |
| 119 | + } |
| 120 | + |
| 121 | + location, _ := tpgresource.GetLocation(d, config) |
| 122 | + if location != "" { |
| 123 | + values["location"] = location |
| 124 | + } |
| 125 | + |
| 126 | + m, err := tpgresource.GetImportIdQualifiers([]string{"projects/(?P<project>[^/]+)/locations/(?P<location>[^/]+)/securityGateways/(?P<security_gateway_id>[^/]+)", "(?P<project>[^/]+)/(?P<location>[^/]+)/(?P<security_gateway_id>[^/]+)", "(?P<location>[^/]+)/(?P<security_gateway_id>[^/]+)", "(?P<security_gateway_id>[^/]+)"}, d, config, d.Id()) |
| 127 | + if err != nil { |
| 128 | + return err |
| 129 | + } |
| 130 | + |
| 131 | + for k, v := range m { |
| 132 | + values[k] = v |
| 133 | + } |
| 134 | + |
| 135 | + u := &BeyondcorpSecurityGatewayIamUpdater{ |
| 136 | + project: values["project"], |
| 137 | + location: values["location"], |
| 138 | + securityGatewayId: values["security_gateway_id"], |
| 139 | + d: d, |
| 140 | + Config: config, |
| 141 | + } |
| 142 | + if err := d.Set("security_gateway_id", u.GetResourceId()); err != nil { |
| 143 | + return fmt.Errorf("Error setting security_gateway_id: %s", err) |
| 144 | + } |
| 145 | + d.SetId(u.GetResourceId()) |
| 146 | + return nil |
| 147 | +} |
| 148 | + |
| 149 | +func (u *BeyondcorpSecurityGatewayIamUpdater) GetResourceIamPolicy() (*cloudresourcemanager.Policy, error) { |
| 150 | + url, err := u.qualifySecurityGatewayUrl("getIamPolicy") |
| 151 | + if err != nil { |
| 152 | + return nil, err |
| 153 | + } |
| 154 | + |
| 155 | + project, err := tpgresource.GetProject(u.d, u.Config) |
| 156 | + if err != nil { |
| 157 | + return nil, err |
| 158 | + } |
| 159 | + var obj map[string]interface{} |
| 160 | + url, err = transport_tpg.AddQueryParams(url, map[string]string{"options.requestedPolicyVersion": fmt.Sprintf("%d", tpgiamresource.IamPolicyVersion)}) |
| 161 | + if err != nil { |
| 162 | + return nil, err |
| 163 | + } |
| 164 | + |
| 165 | + userAgent, err := tpgresource.GenerateUserAgentString(u.d, u.Config.UserAgent) |
| 166 | + if err != nil { |
| 167 | + return nil, err |
| 168 | + } |
| 169 | + |
| 170 | + policy, err := transport_tpg.SendRequest(transport_tpg.SendRequestOptions{ |
| 171 | + Config: u.Config, |
| 172 | + Method: "GET", |
| 173 | + Project: project, |
| 174 | + RawURL: url, |
| 175 | + UserAgent: userAgent, |
| 176 | + Body: obj, |
| 177 | + }) |
| 178 | + if err != nil { |
| 179 | + return nil, errwrap.Wrapf(fmt.Sprintf("Error retrieving IAM policy for %s: {{err}}", u.DescribeResource()), err) |
| 180 | + } |
| 181 | + |
| 182 | + out := &cloudresourcemanager.Policy{} |
| 183 | + err = tpgresource.Convert(policy, out) |
| 184 | + if err != nil { |
| 185 | + return nil, errwrap.Wrapf("Cannot convert a policy to a resource manager policy: {{err}}", err) |
| 186 | + } |
| 187 | + |
| 188 | + return out, nil |
| 189 | +} |
| 190 | + |
| 191 | +func (u *BeyondcorpSecurityGatewayIamUpdater) SetResourceIamPolicy(policy *cloudresourcemanager.Policy) error { |
| 192 | + json, err := tpgresource.ConvertToMap(policy) |
| 193 | + if err != nil { |
| 194 | + return err |
| 195 | + } |
| 196 | + |
| 197 | + obj := make(map[string]interface{}) |
| 198 | + obj["policy"] = json |
| 199 | + |
| 200 | + url, err := u.qualifySecurityGatewayUrl("setIamPolicy") |
| 201 | + if err != nil { |
| 202 | + return err |
| 203 | + } |
| 204 | + project, err := tpgresource.GetProject(u.d, u.Config) |
| 205 | + if err != nil { |
| 206 | + return err |
| 207 | + } |
| 208 | + |
| 209 | + userAgent, err := tpgresource.GenerateUserAgentString(u.d, u.Config.UserAgent) |
| 210 | + if err != nil { |
| 211 | + return err |
| 212 | + } |
| 213 | + |
| 214 | + _, err = transport_tpg.SendRequest(transport_tpg.SendRequestOptions{ |
| 215 | + Config: u.Config, |
| 216 | + Method: "POST", |
| 217 | + Project: project, |
| 218 | + RawURL: url, |
| 219 | + UserAgent: userAgent, |
| 220 | + Body: obj, |
| 221 | + Timeout: u.d.Timeout(schema.TimeoutCreate), |
| 222 | + }) |
| 223 | + if err != nil { |
| 224 | + return errwrap.Wrapf(fmt.Sprintf("Error setting IAM policy for %s: {{err}}", u.DescribeResource()), err) |
| 225 | + } |
| 226 | + |
| 227 | + return nil |
| 228 | +} |
| 229 | + |
| 230 | +func (u *BeyondcorpSecurityGatewayIamUpdater) qualifySecurityGatewayUrl(methodIdentifier string) (string, error) { |
| 231 | + urlTemplate := fmt.Sprintf("{{BeyondcorpBasePath}}%s:%s", fmt.Sprintf("projects/%s/locations/%s/securityGateways/%s", u.project, u.location, u.securityGatewayId), methodIdentifier) |
| 232 | + url, err := tpgresource.ReplaceVars(u.d, u.Config, urlTemplate) |
| 233 | + if err != nil { |
| 234 | + return "", err |
| 235 | + } |
| 236 | + return url, nil |
| 237 | +} |
| 238 | + |
| 239 | +func (u *BeyondcorpSecurityGatewayIamUpdater) GetResourceId() string { |
| 240 | + return fmt.Sprintf("projects/%s/locations/%s/securityGateways/%s", u.project, u.location, u.securityGatewayId) |
| 241 | +} |
| 242 | + |
| 243 | +func (u *BeyondcorpSecurityGatewayIamUpdater) GetMutexKey() string { |
| 244 | + return fmt.Sprintf("iam-beyondcorp-securitygateway-%s", u.GetResourceId()) |
| 245 | +} |
| 246 | + |
| 247 | +func (u *BeyondcorpSecurityGatewayIamUpdater) DescribeResource() string { |
| 248 | + return fmt.Sprintf("beyondcorp securitygateway %q", u.GetResourceId()) |
| 249 | +} |
0 commit comments