Skip to content

Commit

Permalink
Added code for Apigee Environments (#4553) (#8596)
Browse files Browse the repository at this point in the history
* Added code for Apigee Environment

* Added code for Apigee Environment

* Update api.yaml

* Update api.yaml

Signed-off-by: Modular Magician <[email protected]>
  • Loading branch information
modular-magician authored Mar 3, 2021
1 parent dcf0ffc commit ece9fdc
Show file tree
Hide file tree
Showing 7 changed files with 772 additions and 2 deletions.
3 changes: 3 additions & 0 deletions .changelog/4553.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:new-resource
google_apigee_environment
```
5 changes: 3 additions & 2 deletions google/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -725,9 +725,9 @@ func Provider() *schema.Provider {
return provider
}

// Generated resources: 183
// Generated resources: 184
// Generated IAM resources: 75
// Total generated resources: 258
// Total generated resources: 259
func ResourceMap() map[string]*schema.Resource {
resourceMap, _ := ResourceMapWithErrors()
return resourceMap
Expand All @@ -751,6 +751,7 @@ func ResourceMapWithErrors() (map[string]*schema.Resource, error) {
"google_active_directory_domain_trust": resourceActiveDirectoryDomainTrust(),
"google_apigee_organization": resourceApigeeOrganization(),
"google_apigee_instance": resourceApigeeInstance(),
"google_apigee_environment": resourceApigeeEnvironment(),
"google_app_engine_domain_mapping": resourceAppEngineDomainMapping(),
"google_app_engine_firewall_rule": resourceAppEngineFirewallRule(),
"google_app_engine_standard_app_version": resourceAppEngineStandardAppVersion(),
Expand Down
357 changes: 357 additions & 0 deletions google/resource_apigee_environment.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,357 @@
// ----------------------------------------------------------------------------
//
// *** 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"
"strings"
"time"

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

func resourceApigeeEnvironment() *schema.Resource {
return &schema.Resource{
Create: resourceApigeeEnvironmentCreate,
Read: resourceApigeeEnvironmentRead,
Update: resourceApigeeEnvironmentUpdate,
Delete: resourceApigeeEnvironmentDelete,

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

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

Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
Description: `The resource ID of the environment.`,
},
"org_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: `The Apigee Organization associated with the Apigee environment,
in the format 'organizations/{{org_name}}'.`,
},
"description": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Description: `Description of the environment.`,
},
"display_name": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Description: `Display name of the environment.`,
},
},
UseJSONNumber: true,
}
}

func resourceApigeeEnvironmentCreate(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 := expandApigeeEnvironmentName(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
}
displayNameProp, err := expandApigeeEnvironmentDisplayName(d.Get("display_name"), d, config)
if err != nil {
return err
} else if v, ok := d.GetOkExists("display_name"); !isEmptyValue(reflect.ValueOf(displayNameProp)) && (ok || !reflect.DeepEqual(v, displayNameProp)) {
obj["displayName"] = displayNameProp
}
descriptionProp, err := expandApigeeEnvironmentDescription(d.Get("description"), d, config)
if err != nil {
return err
} else if v, ok := d.GetOkExists("description"); !isEmptyValue(reflect.ValueOf(descriptionProp)) && (ok || !reflect.DeepEqual(v, descriptionProp)) {
obj["description"] = descriptionProp
}

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

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

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

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

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

return resourceApigeeEnvironmentRead(d, meta)
}

func resourceApigeeEnvironmentRead(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}}{{org_id}}/environments/{{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("ApigeeEnvironment %q", d.Id()))
}

if err := d.Set("name", flattenApigeeEnvironmentName(res["name"], d, config)); err != nil {
return fmt.Errorf("Error reading Environment: %s", err)
}
if err := d.Set("display_name", flattenApigeeEnvironmentDisplayName(res["displayName"], d, config)); err != nil {
return fmt.Errorf("Error reading Environment: %s", err)
}
if err := d.Set("description", flattenApigeeEnvironmentDescription(res["description"], d, config)); err != nil {
return fmt.Errorf("Error reading Environment: %s", err)
}

return nil
}

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

billingProject := ""

obj := make(map[string]interface{})
nameProp, err := expandApigeeEnvironmentName(d.Get("name"), d, config)
if err != nil {
return err
} else if v, ok := d.GetOkExists("name"); !isEmptyValue(reflect.ValueOf(v)) && (ok || !reflect.DeepEqual(v, nameProp)) {
obj["name"] = nameProp
}
displayNameProp, err := expandApigeeEnvironmentDisplayName(d.Get("display_name"), d, config)
if err != nil {
return err
} else if v, ok := d.GetOkExists("display_name"); !isEmptyValue(reflect.ValueOf(v)) && (ok || !reflect.DeepEqual(v, displayNameProp)) {
obj["displayName"] = displayNameProp
}
descriptionProp, err := expandApigeeEnvironmentDescription(d.Get("description"), d, config)
if err != nil {
return err
} else if v, ok := d.GetOkExists("description"); !isEmptyValue(reflect.ValueOf(v)) && (ok || !reflect.DeepEqual(v, descriptionProp)) {
obj["description"] = descriptionProp
}

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

log.Printf("[DEBUG] Updating Environment %q: %#v", d.Id(), obj)

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

res, err := sendRequestWithTimeout(config, "PUT", billingProject, url, userAgent, obj, d.Timeout(schema.TimeoutUpdate))

if err != nil {
return fmt.Errorf("Error updating Environment %q: %s", d.Id(), err)
} else {
log.Printf("[DEBUG] Finished updating Environment %q: %#v", d.Id(), res)
}

err = apigeeOperationWaitTime(
config, res, "Updating Environment", userAgent,
d.Timeout(schema.TimeoutUpdate))

if err != nil {
return err
}

return resourceApigeeEnvironmentRead(d, meta)
}

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

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

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

if err != nil {
return err
}

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

func resourceApigeeEnvironmentImport(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<name>.+)"}, d, config); err != nil {
return nil, err
}

nameParts := strings.Split(d.Get("name").(string), "/")
if len(nameParts) == 4 {
// `organizations/{{org_name}}/environments/{{name}}`
orgId := fmt.Sprintf("organizations/%s", nameParts[1])
if err := d.Set("org_id", orgId); err != nil {
return nil, fmt.Errorf("Error setting org_id: %s", err)
}
if err := d.Set("name", nameParts[3]); err != nil {
return nil, fmt.Errorf("Error setting name: %s", err)
}
} else if len(nameParts) == 3 {
// `organizations/{{org_name}}/{{name}}`
orgId := fmt.Sprintf("organizations/%s", nameParts[1])
if err := d.Set("org_id", orgId); err != nil {
return nil, fmt.Errorf("Error setting org_id: %s", err)
}
if err := d.Set("name", nameParts[2]); err != nil {
return nil, fmt.Errorf("Error setting name: %s", err)
}
} else {
return nil, fmt.Errorf(
"Saw %s when the name is expected to have shape %s or %s",
d.Get("name"),
"organizations/{{org_name}}/environments/{{name}}",
"organizations/{{org_name}}/{{name}}")
}

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

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

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

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

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

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

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

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

0 comments on commit ece9fdc

Please sign in to comment.