github.com/recobe182/terraform@v0.8.5-0.20170117231232-49ab22a935b7/builtin/providers/aws/resource_aws_api_gateway_client_certificate.go (about)

     1  package aws
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  
     7  	"github.com/aws/aws-sdk-go/aws"
     8  	"github.com/aws/aws-sdk-go/aws/awserr"
     9  	"github.com/aws/aws-sdk-go/service/apigateway"
    10  	"github.com/hashicorp/terraform/helper/schema"
    11  )
    12  
    13  func resourceAwsApiGatewayClientCertificate() *schema.Resource {
    14  	return &schema.Resource{
    15  		Create: resourceAwsApiGatewayClientCertificateCreate,
    16  		Read:   resourceAwsApiGatewayClientCertificateRead,
    17  		Update: resourceAwsApiGatewayClientCertificateUpdate,
    18  		Delete: resourceAwsApiGatewayClientCertificateDelete,
    19  		Importer: &schema.ResourceImporter{
    20  			State: schema.ImportStatePassthrough,
    21  		},
    22  
    23  		Schema: map[string]*schema.Schema{
    24  			"description": {
    25  				Type:     schema.TypeString,
    26  				Optional: true,
    27  			},
    28  			"created_date": {
    29  				Type:     schema.TypeString,
    30  				Computed: true,
    31  			},
    32  			"expiration_date": {
    33  				Type:     schema.TypeString,
    34  				Computed: true,
    35  			},
    36  			"pem_encoded_certificate": {
    37  				Type:     schema.TypeString,
    38  				Computed: true,
    39  			},
    40  		},
    41  	}
    42  }
    43  
    44  func resourceAwsApiGatewayClientCertificateCreate(d *schema.ResourceData, meta interface{}) error {
    45  	conn := meta.(*AWSClient).apigateway
    46  
    47  	input := apigateway.GenerateClientCertificateInput{}
    48  	if v, ok := d.GetOk("description"); ok {
    49  		input.Description = aws.String(v.(string))
    50  	}
    51  	log.Printf("[DEBUG] Generating API Gateway Client Certificate: %s", input)
    52  	out, err := conn.GenerateClientCertificate(&input)
    53  	if err != nil {
    54  		return fmt.Errorf("Failed to generate client certificate: %s", err)
    55  	}
    56  
    57  	d.SetId(*out.ClientCertificateId)
    58  
    59  	return resourceAwsApiGatewayClientCertificateRead(d, meta)
    60  }
    61  
    62  func resourceAwsApiGatewayClientCertificateRead(d *schema.ResourceData, meta interface{}) error {
    63  	conn := meta.(*AWSClient).apigateway
    64  
    65  	input := apigateway.GetClientCertificateInput{
    66  		ClientCertificateId: aws.String(d.Id()),
    67  	}
    68  	out, err := conn.GetClientCertificate(&input)
    69  	if err != nil {
    70  		if awsErr, ok := err.(awserr.Error); ok && awsErr.Code() == "NotFoundException" {
    71  			log.Printf("[WARN] API Gateway Client Certificate %s not found, removing", d.Id())
    72  			d.SetId("")
    73  			return nil
    74  		}
    75  		return err
    76  	}
    77  	log.Printf("[DEBUG] Received API Gateway Client Certificate: %s", out)
    78  
    79  	d.Set("description", out.Description)
    80  	d.Set("created_date", out.CreatedDate)
    81  	d.Set("expiration_date", out.ExpirationDate)
    82  	d.Set("pem_encoded_certificate", out.PemEncodedCertificate)
    83  
    84  	return nil
    85  }
    86  
    87  func resourceAwsApiGatewayClientCertificateUpdate(d *schema.ResourceData, meta interface{}) error {
    88  	conn := meta.(*AWSClient).apigateway
    89  
    90  	operations := make([]*apigateway.PatchOperation, 0)
    91  	if d.HasChange("description") {
    92  		operations = append(operations, &apigateway.PatchOperation{
    93  			Op:    aws.String("replace"),
    94  			Path:  aws.String("/description"),
    95  			Value: aws.String(d.Get("description").(string)),
    96  		})
    97  	}
    98  
    99  	input := apigateway.UpdateClientCertificateInput{
   100  		ClientCertificateId: aws.String(d.Id()),
   101  		PatchOperations:     operations,
   102  	}
   103  
   104  	log.Printf("[DEBUG] Updating API Gateway Client Certificate: %s", input)
   105  	_, err := conn.UpdateClientCertificate(&input)
   106  	if err != nil {
   107  		return fmt.Errorf("Updating API Gateway Client Certificate failed: %s", err)
   108  	}
   109  
   110  	return resourceAwsApiGatewayClientCertificateRead(d, meta)
   111  }
   112  
   113  func resourceAwsApiGatewayClientCertificateDelete(d *schema.ResourceData, meta interface{}) error {
   114  	conn := meta.(*AWSClient).apigateway
   115  	log.Printf("[DEBUG] Deleting API Gateway Client Certificate: %s", d.Id())
   116  	input := apigateway.DeleteClientCertificateInput{
   117  		ClientCertificateId: aws.String(d.Id()),
   118  	}
   119  	_, err := conn.DeleteClientCertificate(&input)
   120  	if err != nil {
   121  		return fmt.Errorf("Deleting API Gateway Client Certificate failed: %s", err)
   122  	}
   123  
   124  	return nil
   125  }