github.com/recobe182/terraform@v0.8.5-0.20170117231232-49ab22a935b7/builtin/providers/aws/resource_aws_api_gateway_base_path_mapping.go (about) 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 const emptyBasePathMappingValue = "(none)" 16 17 func resourceAwsApiGatewayBasePathMapping() *schema.Resource { 18 return &schema.Resource{ 19 Create: resourceAwsApiGatewayBasePathMappingCreate, 20 Read: resourceAwsApiGatewayBasePathMappingRead, 21 Delete: resourceAwsApiGatewayBasePathMappingDelete, 22 23 Schema: map[string]*schema.Schema{ 24 "api_id": { 25 Type: schema.TypeString, 26 Required: true, 27 ForceNew: true, 28 }, 29 "base_path": { 30 Type: schema.TypeString, 31 Optional: true, 32 ForceNew: true, 33 }, 34 "stage_name": { 35 Type: schema.TypeString, 36 Optional: true, 37 ForceNew: true, 38 }, 39 "domain_name": { 40 Type: schema.TypeString, 41 Required: true, 42 ForceNew: true, 43 }, 44 }, 45 } 46 } 47 48 func resourceAwsApiGatewayBasePathMappingCreate(d *schema.ResourceData, meta interface{}) error { 49 conn := meta.(*AWSClient).apigateway 50 51 err := resource.Retry(30*time.Second, func() *resource.RetryError { 52 _, err := conn.CreateBasePathMapping(&apigateway.CreateBasePathMappingInput{ 53 RestApiId: aws.String(d.Get("api_id").(string)), 54 DomainName: aws.String(d.Get("domain_name").(string)), 55 BasePath: aws.String(d.Get("base_path").(string)), 56 Stage: aws.String(d.Get("stage_name").(string)), 57 }) 58 59 if err != nil { 60 if err, ok := err.(awserr.Error); ok && err.Code() != "BadRequestException" { 61 return resource.NonRetryableError(err) 62 } 63 64 return resource.RetryableError( 65 fmt.Errorf("Error creating Gateway base path mapping: %s", err), 66 ) 67 } 68 69 return nil 70 }) 71 72 if err != nil { 73 return fmt.Errorf("Error creating Gateway base path mapping: %s", err) 74 } 75 76 id := fmt.Sprintf("%s/%s", d.Get("domain_name").(string), d.Get("base_path").(string)) 77 d.SetId(id) 78 79 return resourceAwsApiGatewayBasePathMappingRead(d, meta) 80 } 81 82 func resourceAwsApiGatewayBasePathMappingRead(d *schema.ResourceData, meta interface{}) error { 83 conn := meta.(*AWSClient).apigateway 84 85 domainName := d.Get("domain_name").(string) 86 basePath := d.Get("base_path").(string) 87 88 if domainName == "" { 89 return nil 90 } 91 92 if basePath == "" { 93 basePath = emptyBasePathMappingValue 94 } 95 96 mapping, err := conn.GetBasePathMapping(&apigateway.GetBasePathMappingInput{ 97 DomainName: aws.String(domainName), 98 BasePath: aws.String(basePath), 99 }) 100 if err != nil { 101 if err, ok := err.(awserr.Error); ok && err.Code() == "NotFoundException" { 102 log.Printf("[WARN] API gateway base path mapping %s has vanished\n", d.Id()) 103 d.SetId("") 104 return nil 105 } 106 107 return fmt.Errorf("Error reading Gateway base path mapping: %s", err) 108 } 109 110 mappingBasePath := *mapping.BasePath 111 112 if mappingBasePath == emptyBasePathMappingValue { 113 mappingBasePath = "" 114 } 115 116 d.Set("base_path", mappingBasePath) 117 d.Set("api_id", mapping.RestApiId) 118 d.Set("stage_name", mapping.Stage) 119 120 return nil 121 } 122 123 func resourceAwsApiGatewayBasePathMappingDelete(d *schema.ResourceData, meta interface{}) error { 124 conn := meta.(*AWSClient).apigateway 125 126 basePath := d.Get("base_path").(string) 127 128 if basePath == "" { 129 basePath = emptyBasePathMappingValue 130 } 131 132 _, err := conn.DeleteBasePathMapping(&apigateway.DeleteBasePathMappingInput{ 133 DomainName: aws.String(d.Get("domain_name").(string)), 134 BasePath: aws.String(basePath), 135 }) 136 137 if err != nil { 138 if err, ok := err.(awserr.Error); ok && err.Code() == "NotFoundException" { 139 return nil 140 } 141 142 return err 143 } 144 145 return nil 146 }