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