Skip to content

Commit

Permalink
Apigee Nat Address Resource (#5018) (#10789)
Browse files Browse the repository at this point in the history
Signed-off-by: Modular Magician <magic-modules@google.com>
  • Loading branch information
modular-magician committed Dec 22, 2021
1 parent 9c199b4 commit 45106fa
Show file tree
Hide file tree
Showing 9 changed files with 558 additions and 22 deletions.
3 changes: 3 additions & 0 deletions .changelog/5018.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:new-resource
`google_apigee_nat_address`
```
5 changes: 3 additions & 2 deletions google/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -812,9 +812,9 @@ func Provider() *schema.Provider {
return provider
}

// Generated resources: 215
// Generated resources: 216
// Generated IAM resources: 93
// Total generated resources: 308
// Total generated resources: 309
func ResourceMap() map[string]*schema.Resource {
resourceMap, _ := ResourceMapWithErrors()
return resourceMap
Expand Down Expand Up @@ -845,6 +845,7 @@ func ResourceMapWithErrors() (map[string]*schema.Resource, error) {
"google_apigee_envgroup": resourceApigeeEnvgroup(),
"google_apigee_instance_attachment": resourceApigeeInstanceAttachment(),
"google_apigee_envgroup_attachment": resourceApigeeEnvgroupAttachment(),
"google_apigee_nat_address": resourceApigeeNatAddress(),
"google_app_engine_domain_mapping": resourceAppEngineDomainMapping(),
"google_app_engine_firewall_rule": resourceAppEngineFirewallRule(),
"google_app_engine_standard_app_version": resourceAppEngineStandardAppVersion(),
Expand Down
2 changes: 1 addition & 1 deletion google/resource_apigee_instance_attachment.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func resourceApigeeInstanceAttachment() *schema.Resource {
Required: true,
ForceNew: true,
Description: `The Apigee instance associated with the Apigee environment,
in the format 'organisations/{{org_name}}/instances/{{instance_name}}'.`,
in the format 'organizations/{{org_name}}/instances/{{instance_name}}'.`,
},
"name": {
Type: schema.TypeString,
Expand Down
249 changes: 249 additions & 0 deletions google/resource_apigee_nat_address.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,249 @@
// ----------------------------------------------------------------------------
//
// *** 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 google

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

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

func resourceApigeeNatAddress() *schema.Resource {
return &schema.Resource{
Create: resourceApigeeNatAddressCreate,
Read: resourceApigeeNatAddressRead,
Delete: resourceApigeeNatAddressDelete,

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

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

Schema: map[string]*schema.Schema{
"instance_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: `The Apigee instance associated with the Apigee environment,
in the format 'organizations/{{org_name}}/instances/{{instance_name}}'.`,
},
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: `Resource ID of the NAT address.`,
},
"ip_address": {
Type: schema.TypeString,
Computed: true,
Description: `The allocated NAT IP address.`,
},
"state": {
Type: schema.TypeString,
Computed: true,
Description: `State of the NAT IP address.`,
},
},
UseJSONNumber: true,
}
}

func resourceApigeeNatAddressCreate(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{})
nameProp, err := expandApigeeNatAddressName(d.Get("name"), d, config)
if err != nil {
return err
} else if v, ok := d.GetOkExists("name"); !isEmptyValue(reflect.ValueOf(nameProp)) && (ok || !reflect.DeepEqual(v, nameProp)) {
obj["name"] = nameProp
}

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

log.Printf("[DEBUG] Creating new NatAddress: %#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 NatAddress: %s", err)
}

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

if err := d.Set("name", flattenApigeeNatAddressName(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}}/natAddresses/{{name}}")
if err != nil {
return fmt.Errorf("Error constructing id: %s", err)
}
d.SetId(id)

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

return resourceApigeeNatAddressRead(d, meta)
}

func resourceApigeeNatAddressRead(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}}/natAddresses/{{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("ApigeeNatAddress %q", d.Id()))
}

if err := d.Set("name", flattenApigeeNatAddressName(res["name"], d, config)); err != nil {
return fmt.Errorf("Error reading NatAddress: %s", err)
}
if err := d.Set("ip_address", flattenApigeeNatAddressIpAddress(res["ipAddress"], d, config)); err != nil {
return fmt.Errorf("Error reading NatAddress: %s", err)
}
if err := d.Set("state", flattenApigeeNatAddressState(res["state"], d, config)); err != nil {
return fmt.Errorf("Error reading NatAddress: %s", err)
}

return nil
}

func resourceApigeeNatAddressDelete(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}}/natAddresses/{{name}}")
if err != nil {
return err
}

var obj map[string]interface{}
log.Printf("[DEBUG] Deleting NatAddress %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, "NatAddress")
}

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

if err != nil {
return err
}

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

func resourceApigeeNatAddressImport(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>.+)/natAddresses/(?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}}/natAddresses/{{name}}")
if err != nil {
return nil, fmt.Errorf("Error constructing id: %s", err)
}
d.SetId(id)

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

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

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

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

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

0 comments on commit 45106fa

Please sign in to comment.