-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
1 parent
dcf0ffc
commit ece9fdc
Showing
7 changed files
with
772 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_apigee_environment | ||
``` |
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
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,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 | ||
} |
Oops, something went wrong.