package aws import ( "fmt" "log" "time" "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/aws/awserr" "github.com/aws/aws-sdk-go/service/apigateway" "github.com/hashicorp/terraform/helper/resource" "github.com/hashicorp/terraform/helper/schema" ) func resourceAwsApiGatewayApiKey() *schema.Resource { return &schema.Resource{ Create: resourceAwsApiGatewayApiKeyCreate, Read: resourceAwsApiGatewayApiKeyRead, Update: resourceAwsApiGatewayApiKeyUpdate, Delete: resourceAwsApiGatewayApiKeyDelete, Schema: map[string]*schema.Schema{ "name": &schema.Schema{ Type: schema.TypeString, Required: true, ForceNew: true, }, "description": &schema.Schema{ Type: schema.TypeString, Optional: true, Default: "Managed by Terraform", }, "enabled": &schema.Schema{ Type: schema.TypeBool, Optional: true, Default: true, }, "stage_key": &schema.Schema{ Type: schema.TypeSet, Optional: true, Elem: &schema.Resource{ Schema: map[string]*schema.Schema{ "rest_api_id": &schema.Schema{ Type: schema.TypeString, Required: true, }, "stage_name": &schema.Schema{ Type: schema.TypeString, Required: true, }, }, }, }, }, } } func resourceAwsApiGatewayApiKeyCreate(d *schema.ResourceData, meta interface{}) error { conn := meta.(*AWSClient).apigateway log.Printf("[DEBUG] Creating API Gateway API Key") apiKey, err := conn.CreateApiKey(&apigateway.CreateApiKeyInput{ Name: aws.String(d.Get("name").(string)), Description: aws.String(d.Get("description").(string)), Enabled: aws.Bool(d.Get("enabled").(bool)), StageKeys: expandApiGatewayStageKeys(d), }) if err != nil { return fmt.Errorf("Error creating API Gateway: %s", err) } d.SetId(*apiKey.Id) return resourceAwsApiGatewayApiKeyRead(d, meta) } func resourceAwsApiGatewayApiKeyRead(d *schema.ResourceData, meta interface{}) error { conn := meta.(*AWSClient).apigateway log.Printf("[DEBUG] Reading API Gateway API Key: %s", d.Id()) apiKey, err := conn.GetApiKey(&apigateway.GetApiKeyInput{ ApiKey: aws.String(d.Id()), }) if err != nil { if awsErr, ok := err.(awserr.Error); ok && awsErr.Code() == "NotFoundException" { d.SetId("") return nil } return err } d.Set("name", apiKey.Name) d.Set("description", apiKey.Description) d.Set("enabled", apiKey.Enabled) return nil } func resourceAwsApiGatewayApiKeyUpdateOperations(d *schema.ResourceData) []*apigateway.PatchOperation { operations := make([]*apigateway.PatchOperation, 0) if d.HasChange("enabled") { isEnabled := "false" if d.Get("enabled").(bool) { isEnabled = "true" } operations = append(operations, &apigateway.PatchOperation{ Op: aws.String("replace"), Path: aws.String("/enabled"), Value: aws.String(isEnabled), }) } if d.HasChange("description") { operations = append(operations, &apigateway.PatchOperation{ Op: aws.String("replace"), Path: aws.String("/description"), Value: aws.String(d.Get("description").(string)), }) } if d.HasChange("stage_key") { operations = append(operations, expandApiGatewayStageKeyOperations(d)...) } return operations } func resourceAwsApiGatewayApiKeyUpdate(d *schema.ResourceData, meta interface{}) error { conn := meta.(*AWSClient).apigateway log.Printf("[DEBUG] Updating API Gateway API Key: %s", d.Id()) _, err := conn.UpdateApiKey(&apigateway.UpdateApiKeyInput{ ApiKey: aws.String(d.Id()), PatchOperations: resourceAwsApiGatewayApiKeyUpdateOperations(d), }) if err != nil { return err } return resourceAwsApiGatewayApiKeyRead(d, meta) } func resourceAwsApiGatewayApiKeyDelete(d *schema.ResourceData, meta interface{}) error { conn := meta.(*AWSClient).apigateway log.Printf("[DEBUG] Deleting API Gateway API Key: %s", d.Id()) return resource.Retry(5*time.Minute, func() *resource.RetryError { _, err := conn.DeleteApiKey(&apigateway.DeleteApiKeyInput{ ApiKey: aws.String(d.Id()), }) if err == nil { return nil } if apigatewayErr, ok := err.(awserr.Error); ok && apigatewayErr.Code() == "NotFoundException" { return nil } return resource.NonRetryableError(err) }) }