Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Apigee instance attachment resource #8795

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .changelog/4619.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:new-resource
`google_apigee_instance_attachment`
```
5 changes: 3 additions & 2 deletions google/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -727,9 +727,9 @@ func Provider() *schema.Provider {
return provider
}

// Generated resources: 186
// Generated resources: 187
// Generated IAM resources: 81
// Total generated resources: 267
// Total generated resources: 268
func ResourceMap() map[string]*schema.Resource {
resourceMap, _ := ResourceMapWithErrors()
return resourceMap
Expand All @@ -755,6 +755,7 @@ func ResourceMapWithErrors() (map[string]*schema.Resource, error) {
"google_apigee_instance": resourceApigeeInstance(),
"google_apigee_environment": resourceApigeeEnvironment(),
"google_apigee_envgroup": resourceApigeeEnvgroup(),
"google_apigee_instance_attachment": resourceApigeeInstanceAttachment(),
"google_app_engine_domain_mapping": resourceAppEngineDomainMapping(),
"google_app_engine_firewall_rule": resourceAppEngineFirewallRule(),
"google_app_engine_standard_app_version": resourceAppEngineStandardAppVersion(),
Expand Down
3 changes: 1 addition & 2 deletions google/resource_apigee_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,7 @@ Use the following format: 'projects/([^/]+)/locations/([^/]+)/keyRings/([^/]+)/c
Optional: true,
ForceNew: true,
ValidateFunc: validation.StringInSlice([]string{"SLASH_16", "SLASH_20", ""}, false),
Description: `The size of the CIDR block range that will be reserved by the instance. Default value: "SLASH_16" Possible values: ["SLASH_16", "SLASH_20"]`,
Default: "SLASH_16",
Description: `The size of the CIDR block range that will be reserved by the instance. Possible values: ["SLASH_16", "SLASH_20"]`,
},
"host": {
Type: schema.TypeString,
Expand Down
237 changes: 237 additions & 0 deletions google/resource_apigee_instance_attachment.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,237 @@
// ----------------------------------------------------------------------------
//
// *** AUTO GENERATED CODE *** AUTO GENERATED CODE ***
//
// ----------------------------------------------------------------------------
//
// 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 google

import (
"fmt"
"log"
"reflect"
"time"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

func resourceApigeeInstanceAttachment() *schema.Resource {
return &schema.Resource{
Create: resourceApigeeInstanceAttachmentCreate,
Read: resourceApigeeInstanceAttachmentRead,
Delete: resourceApigeeInstanceAttachmentDelete,

Importer: &schema.ResourceImporter{
State: resourceApigeeInstanceAttachmentImport,
},

Timeouts: &schema.ResourceTimeout{
Create: schema.DefaultTimeout(30 * time.Minute),
Delete: schema.DefaultTimeout(30 * time.Minute),
},

Schema: map[string]*schema.Schema{
"environment": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: `The resource ID of the environment.`,
},
"instance_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: `The Apigee instance associated with the Apigee environment,
in the format 'organisations/{{org_name}}/instances/{{instance_name}}'.`,
},
"name": {
Type: schema.TypeString,
Computed: true,
Description: `The name of the newly created attachment (output parameter).`,
},
},
UseJSONNumber: true,
}
}

func resourceApigeeInstanceAttachmentCreate(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
userAgent, err := generateUserAgentString(d, config.userAgent)
if err != nil {
return err
}

obj := make(map[string]interface{})
environmentProp, err := expandApigeeInstanceAttachmentEnvironment(d.Get("environment"), d, config)
if err != nil {
return err
} else if v, ok := d.GetOkExists("environment"); !isEmptyValue(reflect.ValueOf(environmentProp)) && (ok || !reflect.DeepEqual(v, environmentProp)) {
obj["environment"] = environmentProp
}

url, err := replaceVars(d, config, "{{ApigeeBasePath}}{{instance_id}}/attachments")
if err != nil {
return err
}

log.Printf("[DEBUG] Creating new InstanceAttachment: %#v", obj)
billingProject := ""

// err == nil indicates that the billing_project value was found
if bp, err := getBillingProject(d, config); err == nil {
billingProject = bp
}

res, err := sendRequestWithTimeout(config, "POST", billingProject, url, userAgent, obj, d.Timeout(schema.TimeoutCreate))
if err != nil {
return fmt.Errorf("Error creating InstanceAttachment: %s", err)
}

// Store the ID now
id, err := replaceVars(d, config, "{{instance_id}}/attachments/{{name}}")
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 = apigeeOperationWaitTimeWithResponse(
config, res, &opRes, "Creating InstanceAttachment", userAgent,
d.Timeout(schema.TimeoutCreate))
if err != nil {
// The resource didn't actually create
d.SetId("")
return fmt.Errorf("Error waiting to create InstanceAttachment: %s", err)
}

if err := d.Set("name", flattenApigeeInstanceAttachmentName(opRes["name"], d, config)); err != nil {
return err
}

// This may have caused the ID to update - update it if so.
id, err = replaceVars(d, config, "{{instance_id}}/attachments/{{name}}")
if err != nil {
return fmt.Errorf("Error constructing id: %s", err)
}
d.SetId(id)

log.Printf("[DEBUG] Finished creating InstanceAttachment %q: %#v", d.Id(), res)

return resourceApigeeInstanceAttachmentRead(d, meta)
}

func resourceApigeeInstanceAttachmentRead(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
userAgent, err := generateUserAgentString(d, config.userAgent)
if err != nil {
return err
}

url, err := replaceVars(d, config, "{{ApigeeBasePath}}{{instance_id}}/attachments/{{name}}")
if err != nil {
return err
}

billingProject := ""

// err == nil indicates that the billing_project value was found
if bp, err := getBillingProject(d, config); err == nil {
billingProject = bp
}

res, err := sendRequest(config, "GET", billingProject, url, userAgent, nil)
if err != nil {
return handleNotFoundError(err, d, fmt.Sprintf("ApigeeInstanceAttachment %q", d.Id()))
}

if err := d.Set("environment", flattenApigeeInstanceAttachmentEnvironment(res["environment"], d, config)); err != nil {
return fmt.Errorf("Error reading InstanceAttachment: %s", err)
}
if err := d.Set("name", flattenApigeeInstanceAttachmentName(res["name"], d, config)); err != nil {
return fmt.Errorf("Error reading InstanceAttachment: %s", err)
}

return nil
}

func resourceApigeeInstanceAttachmentDelete(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
userAgent, err := generateUserAgentString(d, config.userAgent)
if err != nil {
return err
}

billingProject := ""

url, err := replaceVars(d, config, "{{ApigeeBasePath}}{{instance_id}}/attachments/{{name}}")
if err != nil {
return err
}

var obj map[string]interface{}
log.Printf("[DEBUG] Deleting InstanceAttachment %q", d.Id())

// err == nil indicates that the billing_project value was found
if bp, err := getBillingProject(d, config); err == nil {
billingProject = bp
}

res, err := sendRequestWithTimeout(config, "DELETE", billingProject, url, userAgent, obj, d.Timeout(schema.TimeoutDelete))
if err != nil {
return handleNotFoundError(err, d, "InstanceAttachment")
}

err = apigeeOperationWaitTime(
config, res, "Deleting InstanceAttachment", userAgent,
d.Timeout(schema.TimeoutDelete))

if err != nil {
return err
}

log.Printf("[DEBUG] Finished deleting InstanceAttachment %q: %#v", d.Id(), res)
return nil
}

func resourceApigeeInstanceAttachmentImport(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
config := meta.(*Config)

// current import_formats cannot import fields with forward slashes in their value
if err := parseImportId([]string{
"(?P<instance_id>.+)/attachments/(?P<name>.+)",
"(?P<instance_id>.+)/(?P<name>.+)",
}, d, config); err != nil {
return nil, err
}

// Replace import id for the resource id
id, err := replaceVars(d, config, "{{instance_id}}/attachments/{{name}}")
if err != nil {
return nil, fmt.Errorf("Error constructing id: %s", err)
}
d.SetId(id)

return []*schema.ResourceData{d}, nil
}

func flattenApigeeInstanceAttachmentEnvironment(v interface{}, d *schema.ResourceData, config *Config) interface{} {
return v
}

func flattenApigeeInstanceAttachmentName(v interface{}, d *schema.ResourceData, config *Config) interface{} {
return v
}

func expandApigeeInstanceAttachmentEnvironment(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
return v, nil
}
Loading