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