|
| 1 | +package aws |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "log" |
| 6 | + "time" |
| 7 | + |
| 8 | + "github.com/aws/aws-sdk-go/aws" |
| 9 | + "github.com/aws/aws-sdk-go/aws/awserr" |
| 10 | + "github.com/aws/aws-sdk-go/service/apigateway" |
| 11 | + "github.com/hashicorp/terraform/helper/resource" |
| 12 | + "github.com/hashicorp/terraform/helper/schema" |
| 13 | +) |
| 14 | + |
| 15 | +func resourceAwsApiGatewayResource() *schema.Resource { |
| 16 | + return &schema.Resource{ |
| 17 | + Create: resourceAwsApiGatewayResourceCreate, |
| 18 | + Read: resourceAwsApiGatewayResourceRead, |
| 19 | + Update: resourceAwsApiGatewayResourceUpdate, |
| 20 | + Delete: resourceAwsApiGatewayResourceDelete, |
| 21 | + |
| 22 | + Schema: map[string]*schema.Schema{ |
| 23 | + "rest_api_id": &schema.Schema{ |
| 24 | + Type: schema.TypeString, |
| 25 | + Required: true, |
| 26 | + ForceNew: true, |
| 27 | + }, |
| 28 | + |
| 29 | + "parent_id": &schema.Schema{ |
| 30 | + Type: schema.TypeString, |
| 31 | + Required: true, |
| 32 | + }, |
| 33 | + |
| 34 | + "path_part": &schema.Schema{ |
| 35 | + Type: schema.TypeString, |
| 36 | + Required: true, |
| 37 | + }, |
| 38 | + |
| 39 | + "path": &schema.Schema{ |
| 40 | + Type: schema.TypeString, |
| 41 | + Computed: true, |
| 42 | + }, |
| 43 | + }, |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +func resourceAwsApiGatewayResourceCreate(d *schema.ResourceData, meta interface{}) error { |
| 48 | + conn := meta.(*AWSClient).apigateway |
| 49 | + log.Printf("[DEBUG] Creating API Gateway Resource for API %s", d.Get("rest_api_id").(string)) |
| 50 | + |
| 51 | + var err error |
| 52 | + resource, err := conn.CreateResource(&apigateway.CreateResourceInput{ |
| 53 | + ParentId: aws.String(d.Get("parent_id").(string)), |
| 54 | + PathPart: aws.String(d.Get("path_part").(string)), |
| 55 | + RestApiId: aws.String(d.Get("rest_api_id").(string)), |
| 56 | + }) |
| 57 | + |
| 58 | + if err != nil { |
| 59 | + return fmt.Errorf("Error creating API Gateway Resource: %s", err) |
| 60 | + } |
| 61 | + |
| 62 | + d.SetId(*resource.Id) |
| 63 | + d.Set("path", resource.Path) |
| 64 | + |
| 65 | + return nil |
| 66 | +} |
| 67 | + |
| 68 | +func resourceAwsApiGatewayResourceRead(d *schema.ResourceData, meta interface{}) error { |
| 69 | + conn := meta.(*AWSClient).apigateway |
| 70 | + |
| 71 | + log.Printf("[DEBUG] Reading API Gateway Resource %s", d.Id()) |
| 72 | + resource, err := conn.GetResource(&apigateway.GetResourceInput{ |
| 73 | + ResourceId: aws.String(d.Id()), |
| 74 | + RestApiId: aws.String(d.Get("rest_api_id").(string)), |
| 75 | + }) |
| 76 | + |
| 77 | + if err != nil { |
| 78 | + return err |
| 79 | + } |
| 80 | + |
| 81 | + d.Set("parent_id", resource.ParentId) |
| 82 | + d.Set("path_part", resource.PathPart) |
| 83 | + |
| 84 | + return nil |
| 85 | +} |
| 86 | + |
| 87 | +func resourceAwsApiGatewayResourceUpdateOperations(d *schema.ResourceData) []*apigateway.PatchOperation { |
| 88 | + operations := make([]*apigateway.PatchOperation, 0) |
| 89 | + if d.HasChange("path_part") { |
| 90 | + operations = append(operations, &apigateway.PatchOperation{ |
| 91 | + Op: aws.String("replace"), |
| 92 | + Path: aws.String("/pathPart"), |
| 93 | + Value: aws.String(d.Get("path_part").(string)), |
| 94 | + }) |
| 95 | + } |
| 96 | + |
| 97 | + if d.HasChange("parent_id") { |
| 98 | + operations = append(operations, &apigateway.PatchOperation{ |
| 99 | + Op: aws.String("replace"), |
| 100 | + Path: aws.String("/parentId"), |
| 101 | + Value: aws.String(d.Get("parent_id").(string)), |
| 102 | + }) |
| 103 | + } |
| 104 | + return operations |
| 105 | +} |
| 106 | + |
| 107 | +func resourceAwsApiGatewayResourceUpdate(d *schema.ResourceData, meta interface{}) error { |
| 108 | + conn := meta.(*AWSClient).apigateway |
| 109 | + |
| 110 | + log.Printf("[DEBUG] Updating API Gateway Resource %s", d.Id()) |
| 111 | + _, err := conn.UpdateResource(&apigateway.UpdateResourceInput{ |
| 112 | + ResourceId: aws.String(d.Id()), |
| 113 | + RestApiId: aws.String(d.Get("rest_api_id").(string)), |
| 114 | + PatchOperations: resourceAwsApiGatewayResourceUpdateOperations(d), |
| 115 | + }) |
| 116 | + |
| 117 | + if err != nil { |
| 118 | + return err |
| 119 | + } |
| 120 | + |
| 121 | + return resourceAwsApiGatewayResourceRead(d, meta) |
| 122 | +} |
| 123 | + |
| 124 | +func resourceAwsApiGatewayResourceDelete(d *schema.ResourceData, meta interface{}) error { |
| 125 | + conn := meta.(*AWSClient).apigateway |
| 126 | + log.Printf("[DEBUG] Deleting API Gateway Resource: %s", d.Id()) |
| 127 | + |
| 128 | + return resource.Retry(5*time.Minute, func() error { |
| 129 | + log.Printf("[DEBUG] schema is %#v", d) |
| 130 | + _, err := conn.DeleteResource(&apigateway.DeleteResourceInput{ |
| 131 | + ResourceId: aws.String(d.Id()), |
| 132 | + RestApiId: aws.String(d.Get("rest_api_id").(string)), |
| 133 | + }) |
| 134 | + if err == nil { |
| 135 | + return nil |
| 136 | + } |
| 137 | + |
| 138 | + if apigatewayErr, ok := err.(awserr.Error); ok && apigatewayErr.Code() == "NotFoundException" { |
| 139 | + return nil |
| 140 | + } |
| 141 | + |
| 142 | + return resource.RetryError{Err: err} |
| 143 | + }) |
| 144 | +} |
0 commit comments