|
| 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 apphub |
| 19 | + |
| 20 | +import ( |
| 21 | + "fmt" |
| 22 | + "log" |
| 23 | + "reflect" |
| 24 | + "time" |
| 25 | + |
| 26 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/customdiff" |
| 27 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 28 | + |
| 29 | + "github.com/hashicorp/terraform-provider-google/google/tpgresource" |
| 30 | + transport_tpg "github.com/hashicorp/terraform-provider-google/google/transport" |
| 31 | +) |
| 32 | + |
| 33 | +// Suppress all diff for the field Service Project |
| 34 | +func ServiceProjectDiffSuppress(_, _, _ string, _ *schema.ResourceData) bool { |
| 35 | + return true |
| 36 | +} |
| 37 | + |
| 38 | +func ResourceApphubServiceProjectAttachment() *schema.Resource { |
| 39 | + return &schema.Resource{ |
| 40 | + Create: resourceApphubServiceProjectAttachmentCreate, |
| 41 | + Read: resourceApphubServiceProjectAttachmentRead, |
| 42 | + Delete: resourceApphubServiceProjectAttachmentDelete, |
| 43 | + |
| 44 | + Importer: &schema.ResourceImporter{ |
| 45 | + State: resourceApphubServiceProjectAttachmentImport, |
| 46 | + }, |
| 47 | + |
| 48 | + Timeouts: &schema.ResourceTimeout{ |
| 49 | + Create: schema.DefaultTimeout(20 * time.Minute), |
| 50 | + Delete: schema.DefaultTimeout(20 * time.Minute), |
| 51 | + }, |
| 52 | + |
| 53 | + CustomizeDiff: customdiff.All( |
| 54 | + tpgresource.DefaultProviderProject, |
| 55 | + ), |
| 56 | + |
| 57 | + Schema: map[string]*schema.Schema{ |
| 58 | + "service_project_attachment_id": { |
| 59 | + Type: schema.TypeString, |
| 60 | + Required: true, |
| 61 | + ForceNew: true, |
| 62 | + Description: `Required. The service project attachment identifier must contain the project_id of the service project specified in the service_project_attachment.service_project field. Hint: "projects/{project_id}"`, |
| 63 | + }, |
| 64 | + "service_project": { |
| 65 | + Type: schema.TypeString, |
| 66 | + Optional: true, |
| 67 | + ForceNew: true, |
| 68 | + DiffSuppressFunc: ServiceProjectDiffSuppress, |
| 69 | + Description: `"Immutable. Service project name in the format: \"projects/abc\" |
| 70 | +or \"projects/123\". As input, project name with either project id or number |
| 71 | +are accepted. As output, this field will contain project number. "`, |
| 72 | + }, |
| 73 | + "create_time": { |
| 74 | + Type: schema.TypeString, |
| 75 | + Computed: true, |
| 76 | + Description: `Output only. Create time.`, |
| 77 | + }, |
| 78 | + "name": { |
| 79 | + Type: schema.TypeString, |
| 80 | + Computed: true, |
| 81 | + Description: `"Identifier. The resource name of a ServiceProjectAttachment. Format:\"projects/{host-project-id}/locations/global/serviceProjectAttachments/{service-project-id}.\" "`, |
| 82 | + }, |
| 83 | + "state": { |
| 84 | + Type: schema.TypeString, |
| 85 | + Computed: true, |
| 86 | + Description: `ServiceProjectAttachment state.`, |
| 87 | + }, |
| 88 | + "uid": { |
| 89 | + Type: schema.TypeString, |
| 90 | + Computed: true, |
| 91 | + Description: `Output only. A globally unique identifier (in UUID4 format) for the 'ServiceProjectAttachment'.`, |
| 92 | + }, |
| 93 | + "project": { |
| 94 | + Type: schema.TypeString, |
| 95 | + Optional: true, |
| 96 | + Computed: true, |
| 97 | + ForceNew: true, |
| 98 | + }, |
| 99 | + }, |
| 100 | + UseJSONNumber: true, |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +func resourceApphubServiceProjectAttachmentCreate(d *schema.ResourceData, meta interface{}) error { |
| 105 | + config := meta.(*transport_tpg.Config) |
| 106 | + userAgent, err := tpgresource.GenerateUserAgentString(d, config.UserAgent) |
| 107 | + if err != nil { |
| 108 | + return err |
| 109 | + } |
| 110 | + |
| 111 | + obj := make(map[string]interface{}) |
| 112 | + serviceProjectProp, err := expandApphubServiceProjectAttachmentServiceProject(d.Get("service_project"), d, config) |
| 113 | + if err != nil { |
| 114 | + return err |
| 115 | + } else if v, ok := d.GetOkExists("service_project"); !tpgresource.IsEmptyValue(reflect.ValueOf(serviceProjectProp)) && (ok || !reflect.DeepEqual(v, serviceProjectProp)) { |
| 116 | + obj["serviceProject"] = serviceProjectProp |
| 117 | + } |
| 118 | + |
| 119 | + url, err := tpgresource.ReplaceVars(d, config, "{{ApphubBasePath}}projects/{{project}}/locations/global/serviceProjectAttachments?serviceProjectAttachmentId={{service_project_attachment_id}}") |
| 120 | + if err != nil { |
| 121 | + return err |
| 122 | + } |
| 123 | + |
| 124 | + log.Printf("[DEBUG] Creating new ServiceProjectAttachment: %#v", obj) |
| 125 | + billingProject := "" |
| 126 | + |
| 127 | + project, err := tpgresource.GetProject(d, config) |
| 128 | + if err != nil { |
| 129 | + return fmt.Errorf("Error fetching project for ServiceProjectAttachment: %s", err) |
| 130 | + } |
| 131 | + billingProject = project |
| 132 | + |
| 133 | + // err == nil indicates that the billing_project value was found |
| 134 | + if bp, err := tpgresource.GetBillingProject(d, config); err == nil { |
| 135 | + billingProject = bp |
| 136 | + } |
| 137 | + |
| 138 | + res, err := transport_tpg.SendRequest(transport_tpg.SendRequestOptions{ |
| 139 | + Config: config, |
| 140 | + Method: "POST", |
| 141 | + Project: billingProject, |
| 142 | + RawURL: url, |
| 143 | + UserAgent: userAgent, |
| 144 | + Body: obj, |
| 145 | + Timeout: d.Timeout(schema.TimeoutCreate), |
| 146 | + }) |
| 147 | + if err != nil { |
| 148 | + return fmt.Errorf("Error creating ServiceProjectAttachment: %s", err) |
| 149 | + } |
| 150 | + |
| 151 | + // Store the ID now |
| 152 | + id, err := tpgresource.ReplaceVars(d, config, "projects/{{project}}/locations/global/serviceProjectAttachments/{{service_project_attachment_id}}") |
| 153 | + if err != nil { |
| 154 | + return fmt.Errorf("Error constructing id: %s", err) |
| 155 | + } |
| 156 | + d.SetId(id) |
| 157 | + |
| 158 | + // Use the resource in the operation response to populate |
| 159 | + // identity fields and d.Id() before read |
| 160 | + var opRes map[string]interface{} |
| 161 | + err = ApphubOperationWaitTimeWithResponse( |
| 162 | + config, res, &opRes, project, "Creating ServiceProjectAttachment", userAgent, |
| 163 | + d.Timeout(schema.TimeoutCreate)) |
| 164 | + if err != nil { |
| 165 | + // The resource didn't actually create |
| 166 | + d.SetId("") |
| 167 | + |
| 168 | + return fmt.Errorf("Error waiting to create ServiceProjectAttachment: %s", err) |
| 169 | + } |
| 170 | + |
| 171 | + if err := d.Set("name", flattenApphubServiceProjectAttachmentName(opRes["name"], d, config)); err != nil { |
| 172 | + return err |
| 173 | + } |
| 174 | + |
| 175 | + // This may have caused the ID to update - update it if so. |
| 176 | + id, err = tpgresource.ReplaceVars(d, config, "projects/{{project}}/locations/global/serviceProjectAttachments/{{service_project_attachment_id}}") |
| 177 | + if err != nil { |
| 178 | + return fmt.Errorf("Error constructing id: %s", err) |
| 179 | + } |
| 180 | + d.SetId(id) |
| 181 | + |
| 182 | + log.Printf("[DEBUG] Finished creating ServiceProjectAttachment %q: %#v", d.Id(), res) |
| 183 | + |
| 184 | + return resourceApphubServiceProjectAttachmentRead(d, meta) |
| 185 | +} |
| 186 | + |
| 187 | +func resourceApphubServiceProjectAttachmentRead(d *schema.ResourceData, meta interface{}) error { |
| 188 | + config := meta.(*transport_tpg.Config) |
| 189 | + userAgent, err := tpgresource.GenerateUserAgentString(d, config.UserAgent) |
| 190 | + if err != nil { |
| 191 | + return err |
| 192 | + } |
| 193 | + |
| 194 | + url, err := tpgresource.ReplaceVars(d, config, "{{ApphubBasePath}}projects/{{project}}/locations/global/serviceProjectAttachments/{{service_project_attachment_id}}") |
| 195 | + if err != nil { |
| 196 | + return err |
| 197 | + } |
| 198 | + |
| 199 | + billingProject := "" |
| 200 | + |
| 201 | + project, err := tpgresource.GetProject(d, config) |
| 202 | + if err != nil { |
| 203 | + return fmt.Errorf("Error fetching project for ServiceProjectAttachment: %s", err) |
| 204 | + } |
| 205 | + billingProject = project |
| 206 | + |
| 207 | + // err == nil indicates that the billing_project value was found |
| 208 | + if bp, err := tpgresource.GetBillingProject(d, config); err == nil { |
| 209 | + billingProject = bp |
| 210 | + } |
| 211 | + |
| 212 | + res, err := transport_tpg.SendRequest(transport_tpg.SendRequestOptions{ |
| 213 | + Config: config, |
| 214 | + Method: "GET", |
| 215 | + Project: billingProject, |
| 216 | + RawURL: url, |
| 217 | + UserAgent: userAgent, |
| 218 | + }) |
| 219 | + if err != nil { |
| 220 | + return transport_tpg.HandleNotFoundError(err, d, fmt.Sprintf("ApphubServiceProjectAttachment %q", d.Id())) |
| 221 | + } |
| 222 | + |
| 223 | + if err := d.Set("project", project); err != nil { |
| 224 | + return fmt.Errorf("Error reading ServiceProjectAttachment: %s", err) |
| 225 | + } |
| 226 | + |
| 227 | + if err := d.Set("name", flattenApphubServiceProjectAttachmentName(res["name"], d, config)); err != nil { |
| 228 | + return fmt.Errorf("Error reading ServiceProjectAttachment: %s", err) |
| 229 | + } |
| 230 | + if err := d.Set("service_project", flattenApphubServiceProjectAttachmentServiceProject(res["serviceProject"], d, config)); err != nil { |
| 231 | + return fmt.Errorf("Error reading ServiceProjectAttachment: %s", err) |
| 232 | + } |
| 233 | + if err := d.Set("create_time", flattenApphubServiceProjectAttachmentCreateTime(res["createTime"], d, config)); err != nil { |
| 234 | + return fmt.Errorf("Error reading ServiceProjectAttachment: %s", err) |
| 235 | + } |
| 236 | + if err := d.Set("uid", flattenApphubServiceProjectAttachmentUid(res["uid"], d, config)); err != nil { |
| 237 | + return fmt.Errorf("Error reading ServiceProjectAttachment: %s", err) |
| 238 | + } |
| 239 | + if err := d.Set("state", flattenApphubServiceProjectAttachmentState(res["state"], d, config)); err != nil { |
| 240 | + return fmt.Errorf("Error reading ServiceProjectAttachment: %s", err) |
| 241 | + } |
| 242 | + |
| 243 | + return nil |
| 244 | +} |
| 245 | + |
| 246 | +func resourceApphubServiceProjectAttachmentDelete(d *schema.ResourceData, meta interface{}) error { |
| 247 | + config := meta.(*transport_tpg.Config) |
| 248 | + userAgent, err := tpgresource.GenerateUserAgentString(d, config.UserAgent) |
| 249 | + if err != nil { |
| 250 | + return err |
| 251 | + } |
| 252 | + |
| 253 | + billingProject := "" |
| 254 | + |
| 255 | + project, err := tpgresource.GetProject(d, config) |
| 256 | + if err != nil { |
| 257 | + return fmt.Errorf("Error fetching project for ServiceProjectAttachment: %s", err) |
| 258 | + } |
| 259 | + billingProject = project |
| 260 | + |
| 261 | + url, err := tpgresource.ReplaceVars(d, config, "{{ApphubBasePath}}projects/{{project}}/locations/global/serviceProjectAttachments/{{service_project_attachment_id}}") |
| 262 | + if err != nil { |
| 263 | + return err |
| 264 | + } |
| 265 | + |
| 266 | + var obj map[string]interface{} |
| 267 | + |
| 268 | + // err == nil indicates that the billing_project value was found |
| 269 | + if bp, err := tpgresource.GetBillingProject(d, config); err == nil { |
| 270 | + billingProject = bp |
| 271 | + } |
| 272 | + |
| 273 | + log.Printf("[DEBUG] Deleting ServiceProjectAttachment %q", d.Id()) |
| 274 | + res, err := transport_tpg.SendRequest(transport_tpg.SendRequestOptions{ |
| 275 | + Config: config, |
| 276 | + Method: "DELETE", |
| 277 | + Project: billingProject, |
| 278 | + RawURL: url, |
| 279 | + UserAgent: userAgent, |
| 280 | + Body: obj, |
| 281 | + Timeout: d.Timeout(schema.TimeoutDelete), |
| 282 | + }) |
| 283 | + if err != nil { |
| 284 | + return transport_tpg.HandleNotFoundError(err, d, "ServiceProjectAttachment") |
| 285 | + } |
| 286 | + |
| 287 | + err = ApphubOperationWaitTime( |
| 288 | + config, res, project, "Deleting ServiceProjectAttachment", userAgent, |
| 289 | + d.Timeout(schema.TimeoutDelete)) |
| 290 | + |
| 291 | + if err != nil { |
| 292 | + return err |
| 293 | + } |
| 294 | + |
| 295 | + log.Printf("[DEBUG] Finished deleting ServiceProjectAttachment %q: %#v", d.Id(), res) |
| 296 | + return nil |
| 297 | +} |
| 298 | + |
| 299 | +func resourceApphubServiceProjectAttachmentImport(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) { |
| 300 | + config := meta.(*transport_tpg.Config) |
| 301 | + if err := tpgresource.ParseImportId([]string{ |
| 302 | + "^projects/(?P<project>[^/]+)/locations/global/serviceProjectAttachments/(?P<service_project_attachment_id>[^/]+)$", |
| 303 | + "^(?P<project>[^/]+)/(?P<service_project_attachment_id>[^/]+)$", |
| 304 | + "^(?P<service_project_attachment_id>[^/]+)$", |
| 305 | + }, d, config); err != nil { |
| 306 | + return nil, err |
| 307 | + } |
| 308 | + |
| 309 | + // Replace import id for the resource id |
| 310 | + id, err := tpgresource.ReplaceVars(d, config, "projects/{{project}}/locations/global/serviceProjectAttachments/{{service_project_attachment_id}}") |
| 311 | + if err != nil { |
| 312 | + return nil, fmt.Errorf("Error constructing id: %s", err) |
| 313 | + } |
| 314 | + d.SetId(id) |
| 315 | + |
| 316 | + return []*schema.ResourceData{d}, nil |
| 317 | +} |
| 318 | + |
| 319 | +func flattenApphubServiceProjectAttachmentName(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} { |
| 320 | + return v |
| 321 | +} |
| 322 | + |
| 323 | +func flattenApphubServiceProjectAttachmentServiceProject(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} { |
| 324 | + return nil |
| 325 | +} |
| 326 | + |
| 327 | +func flattenApphubServiceProjectAttachmentCreateTime(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} { |
| 328 | + return v |
| 329 | +} |
| 330 | + |
| 331 | +func flattenApphubServiceProjectAttachmentUid(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} { |
| 332 | + return v |
| 333 | +} |
| 334 | + |
| 335 | +func flattenApphubServiceProjectAttachmentState(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} { |
| 336 | + return v |
| 337 | +} |
| 338 | + |
| 339 | +func expandApphubServiceProjectAttachmentServiceProject(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) { |
| 340 | + |
| 341 | + service_project := "projects/" + d.Get("service_project_attachment_id").(string) |
| 342 | + |
| 343 | + return service_project, nil |
| 344 | +} |
0 commit comments