github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/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": { 27 Type: schema.TypeString, 28 Required: true, 29 ForceNew: true, 30 }, 31 32 "description": { 33 Type: schema.TypeString, 34 Optional: true, 35 Default: "Managed by Terraform", 36 }, 37 38 "enabled": { 39 Type: schema.TypeBool, 40 Optional: true, 41 Default: true, 42 }, 43 44 "stage_key": { 45 Type: schema.TypeSet, 46 Optional: true, 47 Deprecated: "Since the API Gateway usage plans feature was launched on August 11, 2016, usage plans are now required to associate an API key with an API stage", 48 Elem: &schema.Resource{ 49 Schema: map[string]*schema.Schema{ 50 "rest_api_id": { 51 Type: schema.TypeString, 52 Required: true, 53 }, 54 55 "stage_name": { 56 Type: schema.TypeString, 57 Required: true, 58 }, 59 }, 60 }, 61 }, 62 63 "created_date": { 64 Type: schema.TypeString, 65 Computed: true, 66 }, 67 68 "last_updated_date": { 69 Type: schema.TypeString, 70 Computed: true, 71 }, 72 73 "value": { 74 Type: schema.TypeString, 75 Optional: true, 76 Computed: true, 77 ForceNew: true, 78 Sensitive: true, 79 ValidateFunc: validateApiGatewayApiKeyValue, 80 }, 81 }, 82 } 83 } 84 85 func resourceAwsApiGatewayApiKeyCreate(d *schema.ResourceData, meta interface{}) error { 86 conn := meta.(*AWSClient).apigateway 87 log.Printf("[DEBUG] Creating API Gateway API Key") 88 89 apiKey, err := conn.CreateApiKey(&apigateway.CreateApiKeyInput{ 90 Name: aws.String(d.Get("name").(string)), 91 Description: aws.String(d.Get("description").(string)), 92 Enabled: aws.Bool(d.Get("enabled").(bool)), 93 Value: aws.String(d.Get("value").(string)), 94 StageKeys: expandApiGatewayStageKeys(d), 95 }) 96 if err != nil { 97 return fmt.Errorf("Error creating API Gateway: %s", err) 98 } 99 100 d.SetId(*apiKey.Id) 101 102 return resourceAwsApiGatewayApiKeyRead(d, meta) 103 } 104 105 func resourceAwsApiGatewayApiKeyRead(d *schema.ResourceData, meta interface{}) error { 106 conn := meta.(*AWSClient).apigateway 107 log.Printf("[DEBUG] Reading API Gateway API Key: %s", d.Id()) 108 109 apiKey, err := conn.GetApiKey(&apigateway.GetApiKeyInput{ 110 ApiKey: aws.String(d.Id()), 111 IncludeValue: aws.Bool(true), 112 }) 113 if err != nil { 114 if awsErr, ok := err.(awserr.Error); ok && awsErr.Code() == "NotFoundException" { 115 d.SetId("") 116 return nil 117 } 118 119 return err 120 } 121 122 d.Set("name", apiKey.Name) 123 d.Set("description", apiKey.Description) 124 d.Set("enabled", apiKey.Enabled) 125 d.Set("stage_key", flattenApiGatewayStageKeys(apiKey.StageKeys)) 126 d.Set("value", apiKey.Value) 127 128 if err := d.Set("created_date", apiKey.CreatedDate.Format(time.RFC3339)); err != nil { 129 log.Printf("[DEBUG] Error setting created_date: %s", err) 130 } 131 132 if err := d.Set("last_updated_date", apiKey.LastUpdatedDate.Format(time.RFC3339)); err != nil { 133 log.Printf("[DEBUG] Error setting last_updated_date: %s", err) 134 } 135 136 return nil 137 } 138 139 func resourceAwsApiGatewayApiKeyUpdateOperations(d *schema.ResourceData) []*apigateway.PatchOperation { 140 operations := make([]*apigateway.PatchOperation, 0) 141 if d.HasChange("enabled") { 142 isEnabled := "false" 143 if d.Get("enabled").(bool) { 144 isEnabled = "true" 145 } 146 operations = append(operations, &apigateway.PatchOperation{ 147 Op: aws.String("replace"), 148 Path: aws.String("/enabled"), 149 Value: aws.String(isEnabled), 150 }) 151 } 152 153 if d.HasChange("description") { 154 operations = append(operations, &apigateway.PatchOperation{ 155 Op: aws.String("replace"), 156 Path: aws.String("/description"), 157 Value: aws.String(d.Get("description").(string)), 158 }) 159 } 160 161 if d.HasChange("stage_key") { 162 operations = append(operations, expandApiGatewayStageKeyOperations(d)...) 163 } 164 return operations 165 } 166 167 func resourceAwsApiGatewayApiKeyUpdate(d *schema.ResourceData, meta interface{}) error { 168 conn := meta.(*AWSClient).apigateway 169 170 log.Printf("[DEBUG] Updating API Gateway API Key: %s", d.Id()) 171 172 _, err := conn.UpdateApiKey(&apigateway.UpdateApiKeyInput{ 173 ApiKey: aws.String(d.Id()), 174 PatchOperations: resourceAwsApiGatewayApiKeyUpdateOperations(d), 175 }) 176 if err != nil { 177 return err 178 } 179 180 return resourceAwsApiGatewayApiKeyRead(d, meta) 181 } 182 183 func resourceAwsApiGatewayApiKeyDelete(d *schema.ResourceData, meta interface{}) error { 184 conn := meta.(*AWSClient).apigateway 185 log.Printf("[DEBUG] Deleting API Gateway API Key: %s", d.Id()) 186 187 return resource.Retry(5*time.Minute, func() *resource.RetryError { 188 _, err := conn.DeleteApiKey(&apigateway.DeleteApiKeyInput{ 189 ApiKey: aws.String(d.Id()), 190 }) 191 192 if err == nil { 193 return nil 194 } 195 196 if apigatewayErr, ok := err.(awserr.Error); ok && apigatewayErr.Code() == "NotFoundException" { 197 return nil 198 } 199 200 return resource.NonRetryableError(err) 201 }) 202 }