forked from hashicorp/terraform-provider-google
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add new resource for Service Project for Apphub (hashicorp#10048)
FIXES hashicorp#17405 [upstream:ab2122202ab62d620cbbbc8928078b0782146994] Signed-off-by: Modular Magician <[email protected]>
- Loading branch information
1 parent
d5be932
commit bde13f4
Showing
6 changed files
with
818 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
```release-note:new-resource | ||
google_apphub_service_project_attachment | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
344 changes: 344 additions & 0 deletions
344
google/services/apphub/resource_apphub_service_project_attachment.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,344 @@ | ||
// Copyright (c) HashiCorp, Inc. | ||
// SPDX-License-Identifier: MPL-2.0 | ||
|
||
// ---------------------------------------------------------------------------- | ||
// | ||
// *** AUTO GENERATED CODE *** Type: MMv1 *** | ||
// | ||
// ---------------------------------------------------------------------------- | ||
// | ||
// This file is automatically generated by Magic Modules and manual | ||
// changes will be clobbered when the file is regenerated. | ||
// | ||
// Please read more about how to change this file in | ||
// .github/CONTRIBUTING.md. | ||
// | ||
// ---------------------------------------------------------------------------- | ||
|
||
package apphub | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"reflect" | ||
"time" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/customdiff" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
|
||
"github.com/hashicorp/terraform-provider-google/google/tpgresource" | ||
transport_tpg "github.com/hashicorp/terraform-provider-google/google/transport" | ||
) | ||
|
||
// Suppress all diff for the field Service Project | ||
func ServiceProjectDiffSuppress(_, _, _ string, _ *schema.ResourceData) bool { | ||
return true | ||
} | ||
|
||
func ResourceApphubServiceProjectAttachment() *schema.Resource { | ||
return &schema.Resource{ | ||
Create: resourceApphubServiceProjectAttachmentCreate, | ||
Read: resourceApphubServiceProjectAttachmentRead, | ||
Delete: resourceApphubServiceProjectAttachmentDelete, | ||
|
||
Importer: &schema.ResourceImporter{ | ||
State: resourceApphubServiceProjectAttachmentImport, | ||
}, | ||
|
||
Timeouts: &schema.ResourceTimeout{ | ||
Create: schema.DefaultTimeout(20 * time.Minute), | ||
Delete: schema.DefaultTimeout(20 * time.Minute), | ||
}, | ||
|
||
CustomizeDiff: customdiff.All( | ||
tpgresource.DefaultProviderProject, | ||
), | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"service_project_attachment_id": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
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}"`, | ||
}, | ||
"service_project": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
ForceNew: true, | ||
DiffSuppressFunc: ServiceProjectDiffSuppress, | ||
Description: `"Immutable. Service project name in the format: \"projects/abc\" | ||
or \"projects/123\". As input, project name with either project id or number | ||
are accepted. As output, this field will contain project number. "`, | ||
}, | ||
"create_time": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
Description: `Output only. Create time.`, | ||
}, | ||
"name": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
Description: `"Identifier. The resource name of a ServiceProjectAttachment. Format:\"projects/{host-project-id}/locations/global/serviceProjectAttachments/{service-project-id}.\" "`, | ||
}, | ||
"state": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
Description: `ServiceProjectAttachment state.`, | ||
}, | ||
"uid": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
Description: `Output only. A globally unique identifier (in UUID4 format) for the 'ServiceProjectAttachment'.`, | ||
}, | ||
"project": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
Computed: true, | ||
ForceNew: true, | ||
}, | ||
}, | ||
UseJSONNumber: true, | ||
} | ||
} | ||
|
||
func resourceApphubServiceProjectAttachmentCreate(d *schema.ResourceData, meta interface{}) error { | ||
config := meta.(*transport_tpg.Config) | ||
userAgent, err := tpgresource.GenerateUserAgentString(d, config.UserAgent) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
obj := make(map[string]interface{}) | ||
serviceProjectProp, err := expandApphubServiceProjectAttachmentServiceProject(d.Get("service_project"), d, config) | ||
if err != nil { | ||
return err | ||
} else if v, ok := d.GetOkExists("service_project"); !tpgresource.IsEmptyValue(reflect.ValueOf(serviceProjectProp)) && (ok || !reflect.DeepEqual(v, serviceProjectProp)) { | ||
obj["serviceProject"] = serviceProjectProp | ||
} | ||
|
||
url, err := tpgresource.ReplaceVars(d, config, "{{ApphubBasePath}}projects/{{project}}/locations/global/serviceProjectAttachments?serviceProjectAttachmentId={{service_project_attachment_id}}") | ||
if err != nil { | ||
return err | ||
} | ||
|
||
log.Printf("[DEBUG] Creating new ServiceProjectAttachment: %#v", obj) | ||
billingProject := "" | ||
|
||
project, err := tpgresource.GetProject(d, config) | ||
if err != nil { | ||
return fmt.Errorf("Error fetching project for ServiceProjectAttachment: %s", err) | ||
} | ||
billingProject = project | ||
|
||
// err == nil indicates that the billing_project value was found | ||
if bp, err := tpgresource.GetBillingProject(d, config); err == nil { | ||
billingProject = bp | ||
} | ||
|
||
res, err := transport_tpg.SendRequest(transport_tpg.SendRequestOptions{ | ||
Config: config, | ||
Method: "POST", | ||
Project: billingProject, | ||
RawURL: url, | ||
UserAgent: userAgent, | ||
Body: obj, | ||
Timeout: d.Timeout(schema.TimeoutCreate), | ||
}) | ||
if err != nil { | ||
return fmt.Errorf("Error creating ServiceProjectAttachment: %s", err) | ||
} | ||
|
||
// Store the ID now | ||
id, err := tpgresource.ReplaceVars(d, config, "projects/{{project}}/locations/global/serviceProjectAttachments/{{service_project_attachment_id}}") | ||
if err != nil { | ||
return fmt.Errorf("Error constructing id: %s", err) | ||
} | ||
d.SetId(id) | ||
|
||
// Use the resource in the operation response to populate | ||
// identity fields and d.Id() before read | ||
var opRes map[string]interface{} | ||
err = ApphubOperationWaitTimeWithResponse( | ||
config, res, &opRes, project, "Creating ServiceProjectAttachment", userAgent, | ||
d.Timeout(schema.TimeoutCreate)) | ||
if err != nil { | ||
// The resource didn't actually create | ||
d.SetId("") | ||
|
||
return fmt.Errorf("Error waiting to create ServiceProjectAttachment: %s", err) | ||
} | ||
|
||
if err := d.Set("name", flattenApphubServiceProjectAttachmentName(opRes["name"], d, config)); err != nil { | ||
return err | ||
} | ||
|
||
// This may have caused the ID to update - update it if so. | ||
id, err = tpgresource.ReplaceVars(d, config, "projects/{{project}}/locations/global/serviceProjectAttachments/{{service_project_attachment_id}}") | ||
if err != nil { | ||
return fmt.Errorf("Error constructing id: %s", err) | ||
} | ||
d.SetId(id) | ||
|
||
log.Printf("[DEBUG] Finished creating ServiceProjectAttachment %q: %#v", d.Id(), res) | ||
|
||
return resourceApphubServiceProjectAttachmentRead(d, meta) | ||
} | ||
|
||
func resourceApphubServiceProjectAttachmentRead(d *schema.ResourceData, meta interface{}) error { | ||
config := meta.(*transport_tpg.Config) | ||
userAgent, err := tpgresource.GenerateUserAgentString(d, config.UserAgent) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
url, err := tpgresource.ReplaceVars(d, config, "{{ApphubBasePath}}projects/{{project}}/locations/global/serviceProjectAttachments/{{service_project_attachment_id}}") | ||
if err != nil { | ||
return err | ||
} | ||
|
||
billingProject := "" | ||
|
||
project, err := tpgresource.GetProject(d, config) | ||
if err != nil { | ||
return fmt.Errorf("Error fetching project for ServiceProjectAttachment: %s", err) | ||
} | ||
billingProject = project | ||
|
||
// err == nil indicates that the billing_project value was found | ||
if bp, err := tpgresource.GetBillingProject(d, config); err == nil { | ||
billingProject = bp | ||
} | ||
|
||
res, err := transport_tpg.SendRequest(transport_tpg.SendRequestOptions{ | ||
Config: config, | ||
Method: "GET", | ||
Project: billingProject, | ||
RawURL: url, | ||
UserAgent: userAgent, | ||
}) | ||
if err != nil { | ||
return transport_tpg.HandleNotFoundError(err, d, fmt.Sprintf("ApphubServiceProjectAttachment %q", d.Id())) | ||
} | ||
|
||
if err := d.Set("project", project); err != nil { | ||
return fmt.Errorf("Error reading ServiceProjectAttachment: %s", err) | ||
} | ||
|
||
if err := d.Set("name", flattenApphubServiceProjectAttachmentName(res["name"], d, config)); err != nil { | ||
return fmt.Errorf("Error reading ServiceProjectAttachment: %s", err) | ||
} | ||
if err := d.Set("service_project", flattenApphubServiceProjectAttachmentServiceProject(res["serviceProject"], d, config)); err != nil { | ||
return fmt.Errorf("Error reading ServiceProjectAttachment: %s", err) | ||
} | ||
if err := d.Set("create_time", flattenApphubServiceProjectAttachmentCreateTime(res["createTime"], d, config)); err != nil { | ||
return fmt.Errorf("Error reading ServiceProjectAttachment: %s", err) | ||
} | ||
if err := d.Set("uid", flattenApphubServiceProjectAttachmentUid(res["uid"], d, config)); err != nil { | ||
return fmt.Errorf("Error reading ServiceProjectAttachment: %s", err) | ||
} | ||
if err := d.Set("state", flattenApphubServiceProjectAttachmentState(res["state"], d, config)); err != nil { | ||
return fmt.Errorf("Error reading ServiceProjectAttachment: %s", err) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func resourceApphubServiceProjectAttachmentDelete(d *schema.ResourceData, meta interface{}) error { | ||
config := meta.(*transport_tpg.Config) | ||
userAgent, err := tpgresource.GenerateUserAgentString(d, config.UserAgent) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
billingProject := "" | ||
|
||
project, err := tpgresource.GetProject(d, config) | ||
if err != nil { | ||
return fmt.Errorf("Error fetching project for ServiceProjectAttachment: %s", err) | ||
} | ||
billingProject = project | ||
|
||
url, err := tpgresource.ReplaceVars(d, config, "{{ApphubBasePath}}projects/{{project}}/locations/global/serviceProjectAttachments/{{service_project_attachment_id}}") | ||
if err != nil { | ||
return err | ||
} | ||
|
||
var obj map[string]interface{} | ||
|
||
// err == nil indicates that the billing_project value was found | ||
if bp, err := tpgresource.GetBillingProject(d, config); err == nil { | ||
billingProject = bp | ||
} | ||
|
||
log.Printf("[DEBUG] Deleting ServiceProjectAttachment %q", d.Id()) | ||
res, err := transport_tpg.SendRequest(transport_tpg.SendRequestOptions{ | ||
Config: config, | ||
Method: "DELETE", | ||
Project: billingProject, | ||
RawURL: url, | ||
UserAgent: userAgent, | ||
Body: obj, | ||
Timeout: d.Timeout(schema.TimeoutDelete), | ||
}) | ||
if err != nil { | ||
return transport_tpg.HandleNotFoundError(err, d, "ServiceProjectAttachment") | ||
} | ||
|
||
err = ApphubOperationWaitTime( | ||
config, res, project, "Deleting ServiceProjectAttachment", userAgent, | ||
d.Timeout(schema.TimeoutDelete)) | ||
|
||
if err != nil { | ||
return err | ||
} | ||
|
||
log.Printf("[DEBUG] Finished deleting ServiceProjectAttachment %q: %#v", d.Id(), res) | ||
return nil | ||
} | ||
|
||
func resourceApphubServiceProjectAttachmentImport(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) { | ||
config := meta.(*transport_tpg.Config) | ||
if err := tpgresource.ParseImportId([]string{ | ||
"^projects/(?P<project>[^/]+)/locations/global/serviceProjectAttachments/(?P<service_project_attachment_id>[^/]+)$", | ||
"^(?P<project>[^/]+)/(?P<service_project_attachment_id>[^/]+)$", | ||
"^(?P<service_project_attachment_id>[^/]+)$", | ||
}, d, config); err != nil { | ||
return nil, err | ||
} | ||
|
||
// Replace import id for the resource id | ||
id, err := tpgresource.ReplaceVars(d, config, "projects/{{project}}/locations/global/serviceProjectAttachments/{{service_project_attachment_id}}") | ||
if err != nil { | ||
return nil, fmt.Errorf("Error constructing id: %s", err) | ||
} | ||
d.SetId(id) | ||
|
||
return []*schema.ResourceData{d}, nil | ||
} | ||
|
||
func flattenApphubServiceProjectAttachmentName(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} { | ||
return v | ||
} | ||
|
||
func flattenApphubServiceProjectAttachmentServiceProject(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} { | ||
return nil | ||
} | ||
|
||
func flattenApphubServiceProjectAttachmentCreateTime(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} { | ||
return v | ||
} | ||
|
||
func flattenApphubServiceProjectAttachmentUid(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} { | ||
return v | ||
} | ||
|
||
func flattenApphubServiceProjectAttachmentState(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} { | ||
return v | ||
} | ||
|
||
func expandApphubServiceProjectAttachmentServiceProject(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) { | ||
|
||
service_project := "projects/" + d.Get("service_project_attachment_id").(string) | ||
|
||
return service_project, nil | ||
} |
Oops, something went wrong.