github.com/vtorhonen/terraform@v0.9.0-beta2.0.20170307220345-5d894e4ffda7/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 Elem: &schema.Resource{ 48 Schema: map[string]*schema.Schema{ 49 "rest_api_id": { 50 Type: schema.TypeString, 51 Required: true, 52 }, 53 54 "stage_name": { 55 Type: schema.TypeString, 56 Required: true, 57 }, 58 }, 59 }, 60 }, 61 62 "created_date": { 63 Type: schema.TypeString, 64 Computed: true, 65 }, 66 67 "last_updated_date": { 68 Type: schema.TypeString, 69 Computed: true, 70 }, 71 }, 72 } 73 } 74 75 func resourceAwsApiGatewayApiKeyCreate(d *schema.ResourceData, meta interface{}) error { 76 conn := meta.(*AWSClient).apigateway 77 log.Printf("[DEBUG] Creating API Gateway API Key") 78 79 apiKey, err := conn.CreateApiKey(&apigateway.CreateApiKeyInput{ 80 Name: aws.String(d.Get("name").(string)), 81 Description: aws.String(d.Get("description").(string)), 82 Enabled: aws.Bool(d.Get("enabled").(bool)), 83 StageKeys: expandApiGatewayStageKeys(d), 84 }) 85 if err != nil { 86 return fmt.Errorf("Error creating API Gateway: %s", err) 87 } 88 89 d.SetId(*apiKey.Id) 90 91 return resourceAwsApiGatewayApiKeyRead(d, meta) 92 } 93 94 func resourceAwsApiGatewayApiKeyRead(d *schema.ResourceData, meta interface{}) error { 95 conn := meta.(*AWSClient).apigateway 96 log.Printf("[DEBUG] Reading API Gateway API Key: %s", d.Id()) 97 98 apiKey, err := conn.GetApiKey(&apigateway.GetApiKeyInput{ 99 ApiKey: aws.String(d.Id()), 100 }) 101 if err != nil { 102 if awsErr, ok := err.(awserr.Error); ok && awsErr.Code() == "NotFoundException" { 103 d.SetId("") 104 return nil 105 } 106 107 return err 108 } 109 110 d.Set("name", apiKey.Name) 111 d.Set("description", apiKey.Description) 112 d.Set("enabled", apiKey.Enabled) 113 d.Set("stage_key", flattenApiGatewayStageKeys(apiKey.StageKeys)) 114 115 if err := d.Set("created_date", apiKey.CreatedDate.Format(time.RFC3339)); err != nil { 116 log.Printf("[DEBUG] Error setting created_date: %s", err) 117 } 118 119 if err := d.Set("last_updated_date", apiKey.LastUpdatedDate.Format(time.RFC3339)); err != nil { 120 log.Printf("[DEBUG] Error setting last_updated_date: %s", err) 121 } 122 123 return nil 124 } 125 126 func resourceAwsApiGatewayApiKeyUpdateOperations(d *schema.ResourceData) []*apigateway.PatchOperation { 127 operations := make([]*apigateway.PatchOperation, 0) 128 if d.HasChange("enabled") { 129 isEnabled := "false" 130 if d.Get("enabled").(bool) { 131 isEnabled = "true" 132 } 133 operations = append(operations, &apigateway.PatchOperation{ 134 Op: aws.String("replace"), 135 Path: aws.String("/enabled"), 136 Value: aws.String(isEnabled), 137 }) 138 } 139 140 if d.HasChange("description") { 141 operations = append(operations, &apigateway.PatchOperation{ 142 Op: aws.String("replace"), 143 Path: aws.String("/description"), 144 Value: aws.String(d.Get("description").(string)), 145 }) 146 } 147 148 if d.HasChange("stage_key") { 149 operations = append(operations, expandApiGatewayStageKeyOperations(d)...) 150 } 151 return operations 152 } 153 154 func resourceAwsApiGatewayApiKeyUpdate(d *schema.ResourceData, meta interface{}) error { 155 conn := meta.(*AWSClient).apigateway 156 157 log.Printf("[DEBUG] Updating API Gateway API Key: %s", d.Id()) 158 159 _, err := conn.UpdateApiKey(&apigateway.UpdateApiKeyInput{ 160 ApiKey: aws.String(d.Id()), 161 PatchOperations: resourceAwsApiGatewayApiKeyUpdateOperations(d), 162 }) 163 if err != nil { 164 return err 165 } 166 167 return resourceAwsApiGatewayApiKeyRead(d, meta) 168 } 169 170 func resourceAwsApiGatewayApiKeyDelete(d *schema.ResourceData, meta interface{}) error { 171 conn := meta.(*AWSClient).apigateway 172 log.Printf("[DEBUG] Deleting API Gateway API Key: %s", d.Id()) 173 174 return resource.Retry(5*time.Minute, func() *resource.RetryError { 175 _, err := conn.DeleteApiKey(&apigateway.DeleteApiKeyInput{ 176 ApiKey: aws.String(d.Id()), 177 }) 178 179 if err == nil { 180 return nil 181 } 182 183 if apigatewayErr, ok := err.(awserr.Error); ok && apigatewayErr.Code() == "NotFoundException" { 184 return nil 185 } 186 187 return resource.NonRetryableError(err) 188 }) 189 }