github.com/minamijoyo/terraform@v0.7.8-0.20161029001309-18b3736ba44b/builtin/providers/aws/resource_aws_api_gateway_api_key.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 func resourceAwsApiGatewayApiKey() *schema.Resource { 16 return &schema.Resource{ 17 Create: resourceAwsApiGatewayApiKeyCreate, 18 Read: resourceAwsApiGatewayApiKeyRead, 19 Update: resourceAwsApiGatewayApiKeyUpdate, 20 Delete: resourceAwsApiGatewayApiKeyDelete, 21 Importer: &schema.ResourceImporter{ 22 State: schema.ImportStatePassthrough, 23 }, 24 25 Schema: map[string]*schema.Schema{ 26 "name": &schema.Schema{ 27 Type: schema.TypeString, 28 Required: true, 29 ForceNew: true, 30 }, 31 32 "description": &schema.Schema{ 33 Type: schema.TypeString, 34 Optional: true, 35 Default: "Managed by Terraform", 36 }, 37 38 "enabled": &schema.Schema{ 39 Type: schema.TypeBool, 40 Optional: true, 41 Default: true, 42 }, 43 44 "stage_key": &schema.Schema{ 45 Type: schema.TypeSet, 46 Optional: true, 47 Elem: &schema.Resource{ 48 Schema: map[string]*schema.Schema{ 49 "rest_api_id": &schema.Schema{ 50 Type: schema.TypeString, 51 Required: true, 52 }, 53 54 "stage_name": &schema.Schema{ 55 Type: schema.TypeString, 56 Required: true, 57 }, 58 }, 59 }, 60 }, 61 }, 62 } 63 } 64 65 func resourceAwsApiGatewayApiKeyCreate(d *schema.ResourceData, meta interface{}) error { 66 conn := meta.(*AWSClient).apigateway 67 log.Printf("[DEBUG] Creating API Gateway API Key") 68 69 apiKey, err := conn.CreateApiKey(&apigateway.CreateApiKeyInput{ 70 Name: aws.String(d.Get("name").(string)), 71 Description: aws.String(d.Get("description").(string)), 72 Enabled: aws.Bool(d.Get("enabled").(bool)), 73 StageKeys: expandApiGatewayStageKeys(d), 74 }) 75 if err != nil { 76 return fmt.Errorf("Error creating API Gateway: %s", err) 77 } 78 79 d.SetId(*apiKey.Id) 80 81 return resourceAwsApiGatewayApiKeyRead(d, meta) 82 } 83 84 func resourceAwsApiGatewayApiKeyRead(d *schema.ResourceData, meta interface{}) error { 85 conn := meta.(*AWSClient).apigateway 86 log.Printf("[DEBUG] Reading API Gateway API Key: %s", d.Id()) 87 88 apiKey, err := conn.GetApiKey(&apigateway.GetApiKeyInput{ 89 ApiKey: aws.String(d.Id()), 90 }) 91 if err != nil { 92 if awsErr, ok := err.(awserr.Error); ok && awsErr.Code() == "NotFoundException" { 93 d.SetId("") 94 return nil 95 } 96 97 return err 98 } 99 100 d.Set("name", apiKey.Name) 101 d.Set("description", apiKey.Description) 102 d.Set("enabled", apiKey.Enabled) 103 d.Set("stage_key", flattenApiGatewayStageKeys(apiKey.StageKeys)) 104 105 return nil 106 } 107 108 func resourceAwsApiGatewayApiKeyUpdateOperations(d *schema.ResourceData) []*apigateway.PatchOperation { 109 operations := make([]*apigateway.PatchOperation, 0) 110 if d.HasChange("enabled") { 111 isEnabled := "false" 112 if d.Get("enabled").(bool) { 113 isEnabled = "true" 114 } 115 operations = append(operations, &apigateway.PatchOperation{ 116 Op: aws.String("replace"), 117 Path: aws.String("/enabled"), 118 Value: aws.String(isEnabled), 119 }) 120 } 121 122 if d.HasChange("description") { 123 operations = append(operations, &apigateway.PatchOperation{ 124 Op: aws.String("replace"), 125 Path: aws.String("/description"), 126 Value: aws.String(d.Get("description").(string)), 127 }) 128 } 129 130 if d.HasChange("stage_key") { 131 operations = append(operations, expandApiGatewayStageKeyOperations(d)...) 132 } 133 return operations 134 } 135 136 func resourceAwsApiGatewayApiKeyUpdate(d *schema.ResourceData, meta interface{}) error { 137 conn := meta.(*AWSClient).apigateway 138 139 log.Printf("[DEBUG] Updating API Gateway API Key: %s", d.Id()) 140 141 _, err := conn.UpdateApiKey(&apigateway.UpdateApiKeyInput{ 142 ApiKey: aws.String(d.Id()), 143 PatchOperations: resourceAwsApiGatewayApiKeyUpdateOperations(d), 144 }) 145 if err != nil { 146 return err 147 } 148 149 return resourceAwsApiGatewayApiKeyRead(d, meta) 150 } 151 152 func resourceAwsApiGatewayApiKeyDelete(d *schema.ResourceData, meta interface{}) error { 153 conn := meta.(*AWSClient).apigateway 154 log.Printf("[DEBUG] Deleting API Gateway API Key: %s", d.Id()) 155 156 return resource.Retry(5*time.Minute, func() *resource.RetryError { 157 _, err := conn.DeleteApiKey(&apigateway.DeleteApiKeyInput{ 158 ApiKey: aws.String(d.Id()), 159 }) 160 161 if err == nil { 162 return nil 163 } 164 165 if apigatewayErr, ok := err.(awserr.Error); ok && apigatewayErr.Code() == "NotFoundException" { 166 return nil 167 } 168 169 return resource.NonRetryableError(err) 170 }) 171 }