|
| 1 | +// ---------------------------------------------------------------------------- |
| 2 | +// |
| 3 | +// *** AUTO GENERATED CODE *** Type: MMv1 *** |
| 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 | + "strings" |
| 22 | + "time" |
| 23 | + |
| 24 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 25 | +) |
| 26 | + |
| 27 | +func resourceApigeeEndpointAttachment() *schema.Resource { |
| 28 | + return &schema.Resource{ |
| 29 | + Create: resourceApigeeEndpointAttachmentCreate, |
| 30 | + Read: resourceApigeeEndpointAttachmentRead, |
| 31 | + Delete: resourceApigeeEndpointAttachmentDelete, |
| 32 | + |
| 33 | + Importer: &schema.ResourceImporter{ |
| 34 | + State: resourceApigeeEndpointAttachmentImport, |
| 35 | + }, |
| 36 | + |
| 37 | + Timeouts: &schema.ResourceTimeout{ |
| 38 | + Create: schema.DefaultTimeout(30 * time.Minute), |
| 39 | + Delete: schema.DefaultTimeout(30 * time.Minute), |
| 40 | + }, |
| 41 | + |
| 42 | + Schema: map[string]*schema.Schema{ |
| 43 | + "endpoint_attachment_id": { |
| 44 | + Type: schema.TypeString, |
| 45 | + Required: true, |
| 46 | + ForceNew: true, |
| 47 | + Description: `ID of the endpoint attachment.`, |
| 48 | + }, |
| 49 | + "location": { |
| 50 | + Type: schema.TypeString, |
| 51 | + Required: true, |
| 52 | + ForceNew: true, |
| 53 | + Description: `Location of the endpoint attachment.`, |
| 54 | + }, |
| 55 | + "org_id": { |
| 56 | + Type: schema.TypeString, |
| 57 | + Required: true, |
| 58 | + ForceNew: true, |
| 59 | + Description: `The Apigee Organization associated with the Apigee instance, |
| 60 | +in the format 'organizations/{{org_name}}'.`, |
| 61 | + }, |
| 62 | + "service_attachment": { |
| 63 | + Type: schema.TypeString, |
| 64 | + Required: true, |
| 65 | + ForceNew: true, |
| 66 | + Description: `Format: projects/*/regions/*/serviceAttachments/*`, |
| 67 | + }, |
| 68 | + "host": { |
| 69 | + Type: schema.TypeString, |
| 70 | + Computed: true, |
| 71 | + Description: `Host that can be used in either HTTP Target Endpoint directly, or as the host in Target Server.`, |
| 72 | + }, |
| 73 | + "name": { |
| 74 | + Type: schema.TypeString, |
| 75 | + Computed: true, |
| 76 | + Description: `Name of the Endpoint Attachment in the following format: |
| 77 | +organizations/{organization}/endpointAttachments/{endpointAttachment}.`, |
| 78 | + }, |
| 79 | + }, |
| 80 | + UseJSONNumber: true, |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +func resourceApigeeEndpointAttachmentCreate(d *schema.ResourceData, meta interface{}) error { |
| 85 | + config := meta.(*Config) |
| 86 | + userAgent, err := generateUserAgentString(d, config.userAgent) |
| 87 | + if err != nil { |
| 88 | + return err |
| 89 | + } |
| 90 | + |
| 91 | + obj := make(map[string]interface{}) |
| 92 | + locationProp, err := expandApigeeEndpointAttachmentLocation(d.Get("location"), d, config) |
| 93 | + if err != nil { |
| 94 | + return err |
| 95 | + } else if v, ok := d.GetOkExists("location"); !isEmptyValue(reflect.ValueOf(locationProp)) && (ok || !reflect.DeepEqual(v, locationProp)) { |
| 96 | + obj["location"] = locationProp |
| 97 | + } |
| 98 | + serviceAttachmentProp, err := expandApigeeEndpointAttachmentServiceAttachment(d.Get("service_attachment"), d, config) |
| 99 | + if err != nil { |
| 100 | + return err |
| 101 | + } else if v, ok := d.GetOkExists("service_attachment"); !isEmptyValue(reflect.ValueOf(serviceAttachmentProp)) && (ok || !reflect.DeepEqual(v, serviceAttachmentProp)) { |
| 102 | + obj["serviceAttachment"] = serviceAttachmentProp |
| 103 | + } |
| 104 | + |
| 105 | + url, err := replaceVars(d, config, "{{ApigeeBasePath}}{{org_id}}/endpointAttachments?endpointAttachmentId={{endpoint_attachment_id}}") |
| 106 | + if err != nil { |
| 107 | + return err |
| 108 | + } |
| 109 | + |
| 110 | + log.Printf("[DEBUG] Creating new EndpointAttachment: %#v", obj) |
| 111 | + billingProject := "" |
| 112 | + |
| 113 | + // err == nil indicates that the billing_project value was found |
| 114 | + if bp, err := getBillingProject(d, config); err == nil { |
| 115 | + billingProject = bp |
| 116 | + } |
| 117 | + |
| 118 | + res, err := sendRequestWithTimeout(config, "POST", billingProject, url, userAgent, obj, d.Timeout(schema.TimeoutCreate)) |
| 119 | + if err != nil { |
| 120 | + return fmt.Errorf("Error creating EndpointAttachment: %s", err) |
| 121 | + } |
| 122 | + |
| 123 | + // Store the ID now |
| 124 | + id, err := replaceVars(d, config, "{{org_id}}/endpointAttachments/{{endpoint_attachment_id}}") |
| 125 | + if err != nil { |
| 126 | + return fmt.Errorf("Error constructing id: %s", err) |
| 127 | + } |
| 128 | + d.SetId(id) |
| 129 | + |
| 130 | + // Use the resource in the operation response to populate |
| 131 | + // identity fields and d.Id() before read |
| 132 | + var opRes map[string]interface{} |
| 133 | + err = apigeeOperationWaitTimeWithResponse( |
| 134 | + config, res, &opRes, "Creating EndpointAttachment", userAgent, |
| 135 | + d.Timeout(schema.TimeoutCreate)) |
| 136 | + if err != nil { |
| 137 | + // The resource didn't actually create |
| 138 | + d.SetId("") |
| 139 | + return fmt.Errorf("Error waiting to create EndpointAttachment: %s", err) |
| 140 | + } |
| 141 | + |
| 142 | + if err := d.Set("name", flattenApigeeEndpointAttachmentName(opRes["name"], d, config)); err != nil { |
| 143 | + return err |
| 144 | + } |
| 145 | + |
| 146 | + // This may have caused the ID to update - update it if so. |
| 147 | + id, err = replaceVars(d, config, "{{org_id}}/endpointAttachments/{{endpoint_attachment_id}}") |
| 148 | + if err != nil { |
| 149 | + return fmt.Errorf("Error constructing id: %s", err) |
| 150 | + } |
| 151 | + d.SetId(id) |
| 152 | + |
| 153 | + log.Printf("[DEBUG] Finished creating EndpointAttachment %q: %#v", d.Id(), res) |
| 154 | + |
| 155 | + return resourceApigeeEndpointAttachmentRead(d, meta) |
| 156 | +} |
| 157 | + |
| 158 | +func resourceApigeeEndpointAttachmentRead(d *schema.ResourceData, meta interface{}) error { |
| 159 | + config := meta.(*Config) |
| 160 | + userAgent, err := generateUserAgentString(d, config.userAgent) |
| 161 | + if err != nil { |
| 162 | + return err |
| 163 | + } |
| 164 | + |
| 165 | + url, err := replaceVars(d, config, "{{ApigeeBasePath}}{{org_id}}/endpointAttachments/{{endpoint_attachment_id}}") |
| 166 | + if err != nil { |
| 167 | + return err |
| 168 | + } |
| 169 | + |
| 170 | + billingProject := "" |
| 171 | + |
| 172 | + // err == nil indicates that the billing_project value was found |
| 173 | + if bp, err := getBillingProject(d, config); err == nil { |
| 174 | + billingProject = bp |
| 175 | + } |
| 176 | + |
| 177 | + res, err := sendRequest(config, "GET", billingProject, url, userAgent, nil) |
| 178 | + if err != nil { |
| 179 | + return handleNotFoundError(err, d, fmt.Sprintf("ApigeeEndpointAttachment %q", d.Id())) |
| 180 | + } |
| 181 | + |
| 182 | + if err := d.Set("name", flattenApigeeEndpointAttachmentName(res["name"], d, config)); err != nil { |
| 183 | + return fmt.Errorf("Error reading EndpointAttachment: %s", err) |
| 184 | + } |
| 185 | + if err := d.Set("location", flattenApigeeEndpointAttachmentLocation(res["location"], d, config)); err != nil { |
| 186 | + return fmt.Errorf("Error reading EndpointAttachment: %s", err) |
| 187 | + } |
| 188 | + if err := d.Set("host", flattenApigeeEndpointAttachmentHost(res["host"], d, config)); err != nil { |
| 189 | + return fmt.Errorf("Error reading EndpointAttachment: %s", err) |
| 190 | + } |
| 191 | + if err := d.Set("service_attachment", flattenApigeeEndpointAttachmentServiceAttachment(res["serviceAttachment"], d, config)); err != nil { |
| 192 | + return fmt.Errorf("Error reading EndpointAttachment: %s", err) |
| 193 | + } |
| 194 | + |
| 195 | + return nil |
| 196 | +} |
| 197 | + |
| 198 | +func resourceApigeeEndpointAttachmentDelete(d *schema.ResourceData, meta interface{}) error { |
| 199 | + config := meta.(*Config) |
| 200 | + userAgent, err := generateUserAgentString(d, config.userAgent) |
| 201 | + if err != nil { |
| 202 | + return err |
| 203 | + } |
| 204 | + |
| 205 | + billingProject := "" |
| 206 | + |
| 207 | + url, err := replaceVars(d, config, "{{ApigeeBasePath}}{{org_id}}/endpointAttachments/{{endpoint_attachment_id}}") |
| 208 | + if err != nil { |
| 209 | + return err |
| 210 | + } |
| 211 | + |
| 212 | + var obj map[string]interface{} |
| 213 | + log.Printf("[DEBUG] Deleting EndpointAttachment %q", d.Id()) |
| 214 | + |
| 215 | + // err == nil indicates that the billing_project value was found |
| 216 | + if bp, err := getBillingProject(d, config); err == nil { |
| 217 | + billingProject = bp |
| 218 | + } |
| 219 | + |
| 220 | + res, err := sendRequestWithTimeout(config, "DELETE", billingProject, url, userAgent, obj, d.Timeout(schema.TimeoutDelete)) |
| 221 | + if err != nil { |
| 222 | + return handleNotFoundError(err, d, "EndpointAttachment") |
| 223 | + } |
| 224 | + |
| 225 | + err = apigeeOperationWaitTime( |
| 226 | + config, res, "Deleting EndpointAttachment", userAgent, |
| 227 | + d.Timeout(schema.TimeoutDelete)) |
| 228 | + |
| 229 | + if err != nil { |
| 230 | + return err |
| 231 | + } |
| 232 | + |
| 233 | + log.Printf("[DEBUG] Finished deleting EndpointAttachment %q: %#v", d.Id(), res) |
| 234 | + return nil |
| 235 | +} |
| 236 | + |
| 237 | +func resourceApigeeEndpointAttachmentImport(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) { |
| 238 | + config := meta.(*Config) |
| 239 | + |
| 240 | + // current import_formats cannot import fields with forward slashes in their value |
| 241 | + if err := parseImportId([]string{"(?P<name>.+)"}, d, config); err != nil { |
| 242 | + return nil, err |
| 243 | + } |
| 244 | + |
| 245 | + nameParts := strings.Split(d.Get("name").(string), "/") |
| 246 | + if len(nameParts) == 4 { |
| 247 | + // `organizations/{{org_name}}/endpointAttachment/{{endpoint_attachment_id}}` |
| 248 | + orgId := fmt.Sprintf("organizations/%s", nameParts[1]) |
| 249 | + if err := d.Set("org_id", orgId); err != nil { |
| 250 | + return nil, fmt.Errorf("Error setting org_id: %s", err) |
| 251 | + } |
| 252 | + if err := d.Set("endpoint_attachment_id", nameParts[3]); err != nil { |
| 253 | + return nil, fmt.Errorf("Error setting endpoint_attachment_id: %s", err) |
| 254 | + } |
| 255 | + } else { |
| 256 | + return nil, fmt.Errorf( |
| 257 | + "Saw %s when the name is expected to have shape %s", |
| 258 | + d.Get("name"), |
| 259 | + "organizations/{{org_name}}/environments/{{name}}") |
| 260 | + } |
| 261 | + |
| 262 | + // Replace import id for the resource id |
| 263 | + id, err := replaceVars(d, config, "{{name}}") |
| 264 | + if err != nil { |
| 265 | + return nil, fmt.Errorf("Error constructing id: %s", err) |
| 266 | + } |
| 267 | + d.SetId(id) |
| 268 | + |
| 269 | + return []*schema.ResourceData{d}, nil |
| 270 | +} |
| 271 | + |
| 272 | +func flattenApigeeEndpointAttachmentName(v interface{}, d *schema.ResourceData, config *Config) interface{} { |
| 273 | + return v |
| 274 | +} |
| 275 | + |
| 276 | +func flattenApigeeEndpointAttachmentLocation(v interface{}, d *schema.ResourceData, config *Config) interface{} { |
| 277 | + return v |
| 278 | +} |
| 279 | + |
| 280 | +func flattenApigeeEndpointAttachmentHost(v interface{}, d *schema.ResourceData, config *Config) interface{} { |
| 281 | + return v |
| 282 | +} |
| 283 | + |
| 284 | +func flattenApigeeEndpointAttachmentServiceAttachment(v interface{}, d *schema.ResourceData, config *Config) interface{} { |
| 285 | + return v |
| 286 | +} |
| 287 | + |
| 288 | +func expandApigeeEndpointAttachmentLocation(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) { |
| 289 | + return v, nil |
| 290 | +} |
| 291 | + |
| 292 | +func expandApigeeEndpointAttachmentServiceAttachment(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) { |
| 293 | + return v, nil |
| 294 | +} |
0 commit comments