|
| 1 | +// ---------------------------------------------------------------------------- |
| 2 | +// |
| 3 | +// *** AUTO GENERATED CODE *** AUTO GENERATED CODE *** |
| 4 | +// |
| 5 | +// ---------------------------------------------------------------------------- |
| 6 | +// |
| 7 | +// This file is automatically generated by Magic Modules and manual |
| 8 | +// changes will be clobbered when the file is regenerated. |
| 9 | +// |
| 10 | +// Please read more about how to change this file in |
| 11 | +// .github/CONTRIBUTING.md. |
| 12 | +// |
| 13 | +// ---------------------------------------------------------------------------- |
| 14 | + |
| 15 | +package google |
| 16 | + |
| 17 | +import ( |
| 18 | + "fmt" |
| 19 | + "log" |
| 20 | + "reflect" |
| 21 | + "time" |
| 22 | + |
| 23 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 24 | +) |
| 25 | + |
| 26 | +func resourceApigeeEnvgroupAttachment() *schema.Resource { |
| 27 | + return &schema.Resource{ |
| 28 | + Create: resourceApigeeEnvgroupAttachmentCreate, |
| 29 | + Read: resourceApigeeEnvgroupAttachmentRead, |
| 30 | + Delete: resourceApigeeEnvgroupAttachmentDelete, |
| 31 | + |
| 32 | + Importer: &schema.ResourceImporter{ |
| 33 | + State: resourceApigeeEnvgroupAttachmentImport, |
| 34 | + }, |
| 35 | + |
| 36 | + Timeouts: &schema.ResourceTimeout{ |
| 37 | + Create: schema.DefaultTimeout(30 * time.Minute), |
| 38 | + Delete: schema.DefaultTimeout(30 * time.Minute), |
| 39 | + }, |
| 40 | + |
| 41 | + Schema: map[string]*schema.Schema{ |
| 42 | + "envgroup_id": { |
| 43 | + Type: schema.TypeString, |
| 44 | + Required: true, |
| 45 | + ForceNew: true, |
| 46 | + Description: `The Apigee environment group associated with the Apigee environment, |
| 47 | +in the format 'organizations/{{org_name}}/envgroups/{{envgroup_name}}'.`, |
| 48 | + }, |
| 49 | + "environment": { |
| 50 | + Type: schema.TypeString, |
| 51 | + Required: true, |
| 52 | + ForceNew: true, |
| 53 | + Description: `The resource ID of the environment.`, |
| 54 | + }, |
| 55 | + "name": { |
| 56 | + Type: schema.TypeString, |
| 57 | + Computed: true, |
| 58 | + Description: `The name of the newly created attachment (output parameter).`, |
| 59 | + }, |
| 60 | + }, |
| 61 | + UseJSONNumber: true, |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +func resourceApigeeEnvgroupAttachmentCreate(d *schema.ResourceData, meta interface{}) error { |
| 66 | + config := meta.(*Config) |
| 67 | + userAgent, err := generateUserAgentString(d, config.userAgent) |
| 68 | + if err != nil { |
| 69 | + return err |
| 70 | + } |
| 71 | + |
| 72 | + obj := make(map[string]interface{}) |
| 73 | + environmentProp, err := expandApigeeEnvgroupAttachmentEnvironment(d.Get("environment"), d, config) |
| 74 | + if err != nil { |
| 75 | + return err |
| 76 | + } else if v, ok := d.GetOkExists("environment"); !isEmptyValue(reflect.ValueOf(environmentProp)) && (ok || !reflect.DeepEqual(v, environmentProp)) { |
| 77 | + obj["environment"] = environmentProp |
| 78 | + } |
| 79 | + |
| 80 | + url, err := replaceVars(d, config, "{{ApigeeBasePath}}{{envgroup_id}}/attachments") |
| 81 | + if err != nil { |
| 82 | + return err |
| 83 | + } |
| 84 | + |
| 85 | + log.Printf("[DEBUG] Creating new EnvgroupAttachment: %#v", obj) |
| 86 | + billingProject := "" |
| 87 | + |
| 88 | + // err == nil indicates that the billing_project value was found |
| 89 | + if bp, err := getBillingProject(d, config); err == nil { |
| 90 | + billingProject = bp |
| 91 | + } |
| 92 | + |
| 93 | + res, err := sendRequestWithTimeout(config, "POST", billingProject, url, userAgent, obj, d.Timeout(schema.TimeoutCreate)) |
| 94 | + if err != nil { |
| 95 | + return fmt.Errorf("Error creating EnvgroupAttachment: %s", err) |
| 96 | + } |
| 97 | + |
| 98 | + // Store the ID now |
| 99 | + id, err := replaceVars(d, config, "{{envgroup_id}}/attachments/{{name}}") |
| 100 | + if err != nil { |
| 101 | + return fmt.Errorf("Error constructing id: %s", err) |
| 102 | + } |
| 103 | + d.SetId(id) |
| 104 | + |
| 105 | + // Use the resource in the operation response to populate |
| 106 | + // identity fields and d.Id() before read |
| 107 | + var opRes map[string]interface{} |
| 108 | + err = apigeeOperationWaitTimeWithResponse( |
| 109 | + config, res, &opRes, "Creating EnvgroupAttachment", userAgent, |
| 110 | + d.Timeout(schema.TimeoutCreate)) |
| 111 | + if err != nil { |
| 112 | + // The resource didn't actually create |
| 113 | + d.SetId("") |
| 114 | + return fmt.Errorf("Error waiting to create EnvgroupAttachment: %s", err) |
| 115 | + } |
| 116 | + |
| 117 | + if err := d.Set("name", flattenApigeeEnvgroupAttachmentName(opRes["name"], d, config)); err != nil { |
| 118 | + return err |
| 119 | + } |
| 120 | + |
| 121 | + // This may have caused the ID to update - update it if so. |
| 122 | + id, err = replaceVars(d, config, "{{envgroup_id}}/attachments/{{name}}") |
| 123 | + if err != nil { |
| 124 | + return fmt.Errorf("Error constructing id: %s", err) |
| 125 | + } |
| 126 | + d.SetId(id) |
| 127 | + |
| 128 | + log.Printf("[DEBUG] Finished creating EnvgroupAttachment %q: %#v", d.Id(), res) |
| 129 | + |
| 130 | + return resourceApigeeEnvgroupAttachmentRead(d, meta) |
| 131 | +} |
| 132 | + |
| 133 | +func resourceApigeeEnvgroupAttachmentRead(d *schema.ResourceData, meta interface{}) error { |
| 134 | + config := meta.(*Config) |
| 135 | + userAgent, err := generateUserAgentString(d, config.userAgent) |
| 136 | + if err != nil { |
| 137 | + return err |
| 138 | + } |
| 139 | + |
| 140 | + url, err := replaceVars(d, config, "{{ApigeeBasePath}}{{envgroup_id}}/attachments/{{name}}") |
| 141 | + if err != nil { |
| 142 | + return err |
| 143 | + } |
| 144 | + |
| 145 | + billingProject := "" |
| 146 | + |
| 147 | + // err == nil indicates that the billing_project value was found |
| 148 | + if bp, err := getBillingProject(d, config); err == nil { |
| 149 | + billingProject = bp |
| 150 | + } |
| 151 | + |
| 152 | + res, err := sendRequest(config, "GET", billingProject, url, userAgent, nil) |
| 153 | + if err != nil { |
| 154 | + return handleNotFoundError(err, d, fmt.Sprintf("ApigeeEnvgroupAttachment %q", d.Id())) |
| 155 | + } |
| 156 | + |
| 157 | + if err := d.Set("environment", flattenApigeeEnvgroupAttachmentEnvironment(res["environment"], d, config)); err != nil { |
| 158 | + return fmt.Errorf("Error reading EnvgroupAttachment: %s", err) |
| 159 | + } |
| 160 | + if err := d.Set("name", flattenApigeeEnvgroupAttachmentName(res["name"], d, config)); err != nil { |
| 161 | + return fmt.Errorf("Error reading EnvgroupAttachment: %s", err) |
| 162 | + } |
| 163 | + |
| 164 | + return nil |
| 165 | +} |
| 166 | + |
| 167 | +func resourceApigeeEnvgroupAttachmentDelete(d *schema.ResourceData, meta interface{}) error { |
| 168 | + config := meta.(*Config) |
| 169 | + userAgent, err := generateUserAgentString(d, config.userAgent) |
| 170 | + if err != nil { |
| 171 | + return err |
| 172 | + } |
| 173 | + |
| 174 | + billingProject := "" |
| 175 | + |
| 176 | + url, err := replaceVars(d, config, "{{ApigeeBasePath}}{{envgroup_id}}/attachments/{{name}}") |
| 177 | + if err != nil { |
| 178 | + return err |
| 179 | + } |
| 180 | + |
| 181 | + var obj map[string]interface{} |
| 182 | + log.Printf("[DEBUG] Deleting EnvgroupAttachment %q", d.Id()) |
| 183 | + |
| 184 | + // err == nil indicates that the billing_project value was found |
| 185 | + if bp, err := getBillingProject(d, config); err == nil { |
| 186 | + billingProject = bp |
| 187 | + } |
| 188 | + |
| 189 | + res, err := sendRequestWithTimeout(config, "DELETE", billingProject, url, userAgent, obj, d.Timeout(schema.TimeoutDelete)) |
| 190 | + if err != nil { |
| 191 | + return handleNotFoundError(err, d, "EnvgroupAttachment") |
| 192 | + } |
| 193 | + |
| 194 | + err = apigeeOperationWaitTime( |
| 195 | + config, res, "Deleting EnvgroupAttachment", userAgent, |
| 196 | + d.Timeout(schema.TimeoutDelete)) |
| 197 | + |
| 198 | + if err != nil { |
| 199 | + return err |
| 200 | + } |
| 201 | + |
| 202 | + log.Printf("[DEBUG] Finished deleting EnvgroupAttachment %q: %#v", d.Id(), res) |
| 203 | + return nil |
| 204 | +} |
| 205 | + |
| 206 | +func resourceApigeeEnvgroupAttachmentImport(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) { |
| 207 | + config := meta.(*Config) |
| 208 | + |
| 209 | + // current import_formats cannot import fields with forward slashes in their value |
| 210 | + if err := parseImportId([]string{ |
| 211 | + "(?P<envgroup_id>.+)/attachments/(?P<name>.+)", |
| 212 | + "(?P<envgroup_id>.+)/(?P<name>.+)", |
| 213 | + }, d, config); err != nil { |
| 214 | + return nil, err |
| 215 | + } |
| 216 | + |
| 217 | + // Replace import id for the resource id |
| 218 | + id, err := replaceVars(d, config, "{{envgroup_id}}/attachments/{{name}}") |
| 219 | + if err != nil { |
| 220 | + return nil, fmt.Errorf("Error constructing id: %s", err) |
| 221 | + } |
| 222 | + d.SetId(id) |
| 223 | + |
| 224 | + return []*schema.ResourceData{d}, nil |
| 225 | +} |
| 226 | + |
| 227 | +func flattenApigeeEnvgroupAttachmentEnvironment(v interface{}, d *schema.ResourceData, config *Config) interface{} { |
| 228 | + return v |
| 229 | +} |
| 230 | + |
| 231 | +func flattenApigeeEnvgroupAttachmentName(v interface{}, d *schema.ResourceData, config *Config) interface{} { |
| 232 | + return v |
| 233 | +} |
| 234 | + |
| 235 | +func expandApigeeEnvgroupAttachmentEnvironment(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) { |
| 236 | + return v, nil |
| 237 | +} |
0 commit comments