-
Notifications
You must be signed in to change notification settings - Fork 9.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
provider/aws: Add API Gateway Client Certificate #8775
Merged
stack72
merged 1 commit into
hashicorp:master
from
MeredithCorpOSS:f-aws-api-gateway-client-cert
Sep 20, 2016
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
125 changes: 125 additions & 0 deletions
125
builtin/providers/aws/resource_aws_api_gateway_client_certificate.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
package aws | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
|
||
"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/schema" | ||
) | ||
|
||
func resourceAwsApiGatewayClientCertificate() *schema.Resource { | ||
return &schema.Resource{ | ||
Create: resourceAwsApiGatewayClientCertificateCreate, | ||
Read: resourceAwsApiGatewayClientCertificateRead, | ||
Update: resourceAwsApiGatewayClientCertificateUpdate, | ||
Delete: resourceAwsApiGatewayClientCertificateDelete, | ||
Importer: &schema.ResourceImporter{ | ||
State: schema.ImportStatePassthrough, | ||
}, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"description": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
}, | ||
"created_date": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"expiration_date": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"pem_encoded_certificate": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func resourceAwsApiGatewayClientCertificateCreate(d *schema.ResourceData, meta interface{}) error { | ||
conn := meta.(*AWSClient).apigateway | ||
|
||
input := apigateway.GenerateClientCertificateInput{} | ||
if v, ok := d.GetOk("description"); ok { | ||
input.Description = aws.String(v.(string)) | ||
} | ||
log.Printf("[DEBUG] Generating API Gateway Client Certificate: %s", input) | ||
out, err := conn.GenerateClientCertificate(&input) | ||
if err != nil { | ||
return fmt.Errorf("Failed to generate client certificate: %s", err) | ||
} | ||
|
||
d.SetId(*out.ClientCertificateId) | ||
|
||
return resourceAwsApiGatewayClientCertificateRead(d, meta) | ||
} | ||
|
||
func resourceAwsApiGatewayClientCertificateRead(d *schema.ResourceData, meta interface{}) error { | ||
conn := meta.(*AWSClient).apigateway | ||
|
||
input := apigateway.GetClientCertificateInput{ | ||
ClientCertificateId: aws.String(d.Id()), | ||
} | ||
out, err := conn.GetClientCertificate(&input) | ||
if err != nil { | ||
if awsErr, ok := err.(awserr.Error); ok && awsErr.Code() == "NotFoundException" { | ||
log.Printf("[WARN] API Gateway Client Certificate %s not found, removing", d.Id()) | ||
d.SetId("") | ||
return nil | ||
} | ||
return err | ||
} | ||
log.Printf("[DEBUG] Received API Gateway Client Certificate: %s", out) | ||
|
||
d.Set("description", out.Description) | ||
d.Set("created_date", out.CreatedDate) | ||
d.Set("expiration_date", out.ExpirationDate) | ||
d.Set("pem_encoded_certificate", out.PemEncodedCertificate) | ||
|
||
return nil | ||
} | ||
|
||
func resourceAwsApiGatewayClientCertificateUpdate(d *schema.ResourceData, meta interface{}) error { | ||
conn := meta.(*AWSClient).apigateway | ||
|
||
operations := make([]*apigateway.PatchOperation, 0) | ||
if d.HasChange("description") { | ||
operations = append(operations, &apigateway.PatchOperation{ | ||
Op: aws.String("replace"), | ||
Path: aws.String("/description"), | ||
Value: aws.String(d.Get("description").(string)), | ||
}) | ||
} | ||
|
||
input := apigateway.UpdateClientCertificateInput{ | ||
ClientCertificateId: aws.String(d.Id()), | ||
PatchOperations: operations, | ||
} | ||
|
||
log.Printf("[DEBUG] Updating API Gateway Client Certificate: %s", input) | ||
_, err := conn.UpdateClientCertificate(&input) | ||
if err != nil { | ||
return fmt.Errorf("Updating API Gateway Client Certificate failed: %s", err) | ||
} | ||
|
||
return resourceAwsApiGatewayClientCertificateRead(d, meta) | ||
} | ||
|
||
func resourceAwsApiGatewayClientCertificateDelete(d *schema.ResourceData, meta interface{}) error { | ||
conn := meta.(*AWSClient).apigateway | ||
log.Printf("[DEBUG] Deleting API Gateway Client Certificate: %s", d.Id()) | ||
input := apigateway.DeleteClientCertificateInput{ | ||
ClientCertificateId: aws.String(d.Id()), | ||
} | ||
_, err := conn.DeleteClientCertificate(&input) | ||
if err != nil { | ||
return fmt.Errorf("Deleting API Gateway Client Certificate failed: %s", err) | ||
} | ||
|
||
return nil | ||
} |
128 changes: 128 additions & 0 deletions
128
builtin/providers/aws/resource_aws_api_gateway_client_certificate_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
package aws | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"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/terraform" | ||
) | ||
|
||
func TestAccAWSAPIGatewayClientCertificate_basic(t *testing.T) { | ||
var conf apigateway.ClientCertificate | ||
|
||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
CheckDestroy: testAccCheckAWSAPIGatewayClientCertificateDestroy, | ||
Steps: []resource.TestStep{ | ||
resource.TestStep{ | ||
Config: testAccAWSAPIGatewayClientCertificateConfig_basic, | ||
Check: resource.ComposeTestCheckFunc( | ||
testAccCheckAWSAPIGatewayClientCertificateExists("aws_api_gateway_client_certificate.cow", &conf), | ||
resource.TestCheckResourceAttr("aws_api_gateway_client_certificate.cow", "description", "Hello from TF acceptance test"), | ||
), | ||
}, | ||
resource.TestStep{ | ||
Config: testAccAWSAPIGatewayClientCertificateConfig_basic_updated, | ||
Check: resource.ComposeTestCheckFunc( | ||
testAccCheckAWSAPIGatewayClientCertificateExists("aws_api_gateway_client_certificate.cow", &conf), | ||
resource.TestCheckResourceAttr("aws_api_gateway_client_certificate.cow", "description", "Hello from TF acceptance test - updated"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func TestAccAWSAPIGatewayClientCertificate_importBasic(t *testing.T) { | ||
resourceName := "aws_api_gateway_client_certificate.cow" | ||
|
||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
CheckDestroy: testAccCheckAWSAPIGatewayClientCertificateDestroy, | ||
Steps: []resource.TestStep{ | ||
resource.TestStep{ | ||
Config: testAccAWSAPIGatewayClientCertificateConfig_basic, | ||
}, | ||
|
||
resource.TestStep{ | ||
ResourceName: resourceName, | ||
ImportState: true, | ||
ImportStateVerify: true, | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func testAccCheckAWSAPIGatewayClientCertificateExists(n string, res *apigateway.ClientCertificate) resource.TestCheckFunc { | ||
return func(s *terraform.State) error { | ||
rs, ok := s.RootModule().Resources[n] | ||
if !ok { | ||
return fmt.Errorf("Not found: %s", n) | ||
} | ||
|
||
if rs.Primary.ID == "" { | ||
return fmt.Errorf("No API Gateway Client Certificate ID is set") | ||
} | ||
|
||
conn := testAccProvider.Meta().(*AWSClient).apigateway | ||
|
||
req := &apigateway.GetClientCertificateInput{ | ||
ClientCertificateId: aws.String(rs.Primary.ID), | ||
} | ||
out, err := conn.GetClientCertificate(req) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
*res = *out | ||
|
||
return nil | ||
} | ||
} | ||
|
||
func testAccCheckAWSAPIGatewayClientCertificateDestroy(s *terraform.State) error { | ||
conn := testAccProvider.Meta().(*AWSClient).apigateway | ||
|
||
for _, rs := range s.RootModule().Resources { | ||
if rs.Type != "aws_api_gateway_client_certificate" { | ||
continue | ||
} | ||
|
||
req := &apigateway.GetClientCertificateInput{ | ||
ClientCertificateId: aws.String(rs.Primary.ID), | ||
} | ||
out, err := conn.GetClientCertificate(req) | ||
if err == nil { | ||
return fmt.Errorf("API Gateway Client Certificate still exists: %s", out) | ||
} | ||
|
||
awsErr, ok := err.(awserr.Error) | ||
if !ok { | ||
return err | ||
} | ||
if awsErr.Code() != "NotFoundException" { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
return nil | ||
} | ||
|
||
const testAccAWSAPIGatewayClientCertificateConfig_basic = ` | ||
resource "aws_api_gateway_client_certificate" "cow" { | ||
description = "Hello from TF acceptance test" | ||
} | ||
` | ||
|
||
const testAccAWSAPIGatewayClientCertificateConfig_basic_updated = ` | ||
resource "aws_api_gateway_client_certificate" "cow" { | ||
description = "Hello from TF acceptance test - updated" | ||
} | ||
` |
44 changes: 44 additions & 0 deletions
44
website/source/docs/providers/aws/r/api_gateway_client_certificate.html.markdown
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
--- | ||
layout: "aws" | ||
page_title: "AWS: aws_api_gateway_client_certificate" | ||
sidebar_current: "docs-aws-resource-api-gateway-client-certificate" | ||
description: |- | ||
Provides an API Gateway Client Certificate. | ||
--- | ||
|
||
# aws\_api\_gateway\_client\_certificate | ||
|
||
Provides an API Gateway Client Certificate. | ||
|
||
## Example Usage | ||
|
||
``` | ||
resource "aws_api_gateway_client_certificate" "demo" { | ||
description = "My client certificate" | ||
} | ||
|
||
``` | ||
|
||
## Argument Reference | ||
|
||
The following arguments are supported: | ||
|
||
* `description` - (Optional) The description of the client certificate. | ||
|
||
|
||
## Attribute Reference | ||
|
||
The following attributes are exported: | ||
|
||
* `id` - The identifier of the client certificate. | ||
* `created_date` - The date when the client certificate was created. | ||
* `expiration_date` - The date when the client certificate will expire. | ||
* `pem_encoded_certificate` - The PEM-encoded public key of the client certificate. | ||
|
||
## Import | ||
|
||
API Gateway Client Certificates can be imported using the id, e.g. | ||
|
||
``` | ||
$ terraform import aws_api_gateway_client_certificate.demo ab1cqe | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we get a test for the import func?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is there, search for
TestAccAWSAPIGatewayClientCertificate_importBasic
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Got it! It's usually in a different file - maybe we need to remove that from elsewhere as it is just extra files in the repo :)