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