github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/aws/resource_aws_api_gateway_usage_plan_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 resourceAwsApiGatewayUsagePlanKey() *schema.Resource {
    16  	return &schema.Resource{
    17  		Create: resourceAwsApiGatewayUsagePlanKeyCreate,
    18  		Read:   resourceAwsApiGatewayUsagePlanKeyRead,
    19  		Delete: resourceAwsApiGatewayUsagePlanKeyDelete,
    20  
    21  		Schema: map[string]*schema.Schema{
    22  			"key_id": {
    23  				Type:     schema.TypeString,
    24  				Required: true,
    25  				ForceNew: true,
    26  			},
    27  
    28  			"key_type": {
    29  				Type:     schema.TypeString,
    30  				Required: true,
    31  				ForceNew: true,
    32  			},
    33  
    34  			"usage_plan_id": {
    35  				Type:     schema.TypeString,
    36  				Required: true,
    37  				ForceNew: true,
    38  			},
    39  
    40  			"name": {
    41  				Type:     schema.TypeString,
    42  				Computed: true,
    43  			},
    44  
    45  			"value": {
    46  				Type:     schema.TypeString,
    47  				Computed: true,
    48  			},
    49  		},
    50  	}
    51  }
    52  
    53  func resourceAwsApiGatewayUsagePlanKeyCreate(d *schema.ResourceData, meta interface{}) error {
    54  	conn := meta.(*AWSClient).apigateway
    55  	log.Print("[DEBUG] Creating API Gateway Usage Plan Key")
    56  
    57  	params := &apigateway.CreateUsagePlanKeyInput{
    58  		KeyId:       aws.String(d.Get("key_id").(string)),
    59  		KeyType:     aws.String(d.Get("key_type").(string)),
    60  		UsagePlanId: aws.String(d.Get("usage_plan_id").(string)),
    61  	}
    62  
    63  	up, err := conn.CreateUsagePlanKey(params)
    64  	if err != nil {
    65  		return fmt.Errorf("Error creating API Gateway Usage Plan Key: %s", err)
    66  	}
    67  
    68  	d.SetId(*up.Id)
    69  
    70  	return resourceAwsApiGatewayUsagePlanKeyRead(d, meta)
    71  }
    72  
    73  func resourceAwsApiGatewayUsagePlanKeyRead(d *schema.ResourceData, meta interface{}) error {
    74  	conn := meta.(*AWSClient).apigateway
    75  	log.Printf("[DEBUG] Reading API Gateway Usage Plan Key: %s", d.Id())
    76  
    77  	up, err := conn.GetUsagePlanKey(&apigateway.GetUsagePlanKeyInput{
    78  		UsagePlanId: aws.String(d.Get("usage_plan_id").(string)),
    79  		KeyId:       aws.String(d.Get("key_id").(string)),
    80  	})
    81  	if err != nil {
    82  		if awsErr, ok := err.(awserr.Error); ok && awsErr.Code() == "NotFoundException" {
    83  			d.SetId("")
    84  			return nil
    85  		}
    86  		return err
    87  	}
    88  
    89  	d.Set("name", up.Name)
    90  	d.Set("value", up.Value)
    91  
    92  	return nil
    93  }
    94  
    95  func resourceAwsApiGatewayUsagePlanKeyDelete(d *schema.ResourceData, meta interface{}) error {
    96  	conn := meta.(*AWSClient).apigateway
    97  
    98  	log.Printf("[DEBUG] Deleting API Gateway Usage Plan Key: %s", d.Id())
    99  
   100  	return resource.Retry(5*time.Minute, func() *resource.RetryError {
   101  		_, err := conn.DeleteUsagePlanKey(&apigateway.DeleteUsagePlanKeyInput{
   102  			UsagePlanId: aws.String(d.Get("usage_plan_id").(string)),
   103  			KeyId:       aws.String(d.Get("key_id").(string)),
   104  		})
   105  
   106  		if err == nil {
   107  			return nil
   108  		}
   109  
   110  		return resource.NonRetryableError(err)
   111  	})
   112  }