diff --git a/aws/provider.go b/aws/provider.go index 057409a680a..8df64770c1e 100644 --- a/aws/provider.go +++ b/aws/provider.go @@ -252,6 +252,7 @@ func Provider() terraform.ResourceProvider { "aws_api_gateway_client_certificate": resourceAwsApiGatewayClientCertificate(), "aws_api_gateway_deployment": resourceAwsApiGatewayDeployment(), "aws_api_gateway_documentation_part": resourceAwsApiGatewayDocumentationPart(), + "aws_api_gateway_documentation_version": resourceAwsApiGatewayDocumentationVersion(), "aws_api_gateway_domain_name": resourceAwsApiGatewayDomainName(), "aws_api_gateway_gateway_response": resourceAwsApiGatewayGatewayResponse(), "aws_api_gateway_integration": resourceAwsApiGatewayIntegration(), diff --git a/aws/resource_aws_api_gateway_documentation_version.go b/aws/resource_aws_api_gateway_documentation_version.go new file mode 100644 index 00000000000..6f0280744a5 --- /dev/null +++ b/aws/resource_aws_api_gateway_documentation_version.go @@ -0,0 +1,137 @@ +package aws + +import ( + "fmt" + "log" + "strings" + + "github.com/aws/aws-sdk-go/aws" + "github.com/aws/aws-sdk-go/service/apigateway" + "github.com/hashicorp/terraform/helper/schema" +) + +func resourceAwsApiGatewayDocumentationVersion() *schema.Resource { + return &schema.Resource{ + Create: resourceAwsApiGatewayDocumentationVersionCreate, + Read: resourceAwsApiGatewayDocumentationVersionRead, + Update: resourceAwsApiGatewayDocumentationVersionUpdate, + Delete: resourceAwsApiGatewayDocumentationVersionDelete, + + Importer: &schema.ResourceImporter{ + State: schema.ImportStatePassthrough, + }, + + Schema: map[string]*schema.Schema{ + "version": { + Type: schema.TypeString, + Required: true, + ForceNew: true, + }, + "rest_api_id": { + Type: schema.TypeString, + Required: true, + ForceNew: true, + }, + "description": { + Type: schema.TypeString, + Optional: true, + }, + }, + } +} + +func resourceAwsApiGatewayDocumentationVersionCreate(d *schema.ResourceData, meta interface{}) error { + conn := meta.(*AWSClient).apigateway + + restApiId := d.Get("rest_api_id").(string) + + params := &apigateway.CreateDocumentationVersionInput{ + DocumentationVersion: aws.String(d.Get("version").(string)), + RestApiId: aws.String(restApiId), + } + if v, ok := d.GetOk("description"); ok { + params.Description = aws.String(v.(string)) + } + + log.Printf("[DEBUG] Creating API Gateway Documentation Version: %s", params) + + version, err := conn.CreateDocumentationVersion(params) + if err != nil { + return fmt.Errorf("Error creating API Gateway Documentation Version: %s", err) + } + + d.SetId(restApiId + "/" + *version.Version) + + return resourceAwsApiGatewayDocumentationVersionRead(d, meta) +} + +func resourceAwsApiGatewayDocumentationVersionRead(d *schema.ResourceData, meta interface{}) error { + conn := meta.(*AWSClient).apigateway + log.Printf("[DEBUG] Reading API Gateway Documentation Version %s", d.Id()) + + apiId, docVersion, err := decodeApiGatewayDocumentationVersionId(d.Id()) + if err != nil { + return err + } + + version, err := conn.GetDocumentationVersion(&apigateway.GetDocumentationVersionInput{ + DocumentationVersion: aws.String(docVersion), + RestApiId: aws.String(apiId), + }) + if err != nil { + if isAWSErr(err, apigateway.ErrCodeNotFoundException, "") { + log.Printf("[WARN] API Gateway Documentation Version (%s) not found, removing from state", d.Id()) + d.SetId("") + return nil + } + return err + } + + d.Set("rest_api_id", apiId) + d.Set("description", version.Description) + d.Set("version", version.Version) + + return nil +} + +func resourceAwsApiGatewayDocumentationVersionUpdate(d *schema.ResourceData, meta interface{}) error { + conn := meta.(*AWSClient).apigateway + log.Printf("[DEBUG] Updating API Gateway Documentation Version %s", d.Id()) + + _, err := conn.UpdateDocumentationVersion(&apigateway.UpdateDocumentationVersionInput{ + DocumentationVersion: aws.String(d.Get("version").(string)), + RestApiId: aws.String(d.Get("rest_api_id").(string)), + PatchOperations: []*apigateway.PatchOperation{ + { + Op: aws.String(apigateway.OpReplace), + Path: aws.String("/description"), + Value: aws.String(d.Get("description").(string)), + }, + }, + }) + if err != nil { + return err + } + log.Printf("[DEBUG] Updated API Gateway Documentation Version %s", d.Id()) + + return resourceAwsApiGatewayDocumentationVersionRead(d, meta) +} + +func resourceAwsApiGatewayDocumentationVersionDelete(d *schema.ResourceData, meta interface{}) error { + conn := meta.(*AWSClient).apigateway + log.Printf("[DEBUG] Deleting API Gateway Documentation Version: %s", d.Id()) + + _, err := conn.DeleteDocumentationVersion(&apigateway.DeleteDocumentationVersionInput{ + DocumentationVersion: aws.String(d.Get("version").(string)), + RestApiId: aws.String(d.Get("rest_api_id").(string)), + }) + return err +} + +func decodeApiGatewayDocumentationVersionId(id string) (string, string, error) { + parts := strings.Split(id, "/") + if len(parts) != 2 { + return "", "", fmt.Errorf("Expected ID in the form of REST-API-ID/VERSION, given: %q", id) + } + return parts[0], parts[1], nil +} diff --git a/aws/resource_aws_api_gateway_documentation_version_test.go b/aws/resource_aws_api_gateway_documentation_version_test.go new file mode 100644 index 00000000000..0e32f4a5715 --- /dev/null +++ b/aws/resource_aws_api_gateway_documentation_version_test.go @@ -0,0 +1,285 @@ +package aws + +import ( + "fmt" + "testing" + + "github.com/aws/aws-sdk-go/aws" + "github.com/aws/aws-sdk-go/service/apigateway" + "github.com/hashicorp/terraform/helper/acctest" + "github.com/hashicorp/terraform/helper/resource" + "github.com/hashicorp/terraform/terraform" +) + +func TestAccAWSAPIGatewayDocumentationVersion_basic(t *testing.T) { + var conf apigateway.DocumentationVersion + + rString := acctest.RandString(8) + version := fmt.Sprintf("tf_acc_version_%s", rString) + apiName := fmt.Sprintf("tf_acc_api_doc_version_basic_%s", rString) + + resourceName := "aws_api_gateway_documentation_version.test" + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckAWSAPIGatewayDocumentationVersionDestroy, + Steps: []resource.TestStep{ + resource.TestStep{ + Config: testAccAWSAPIGatewayDocumentationVersionBasicConfig(version, apiName), + Check: resource.ComposeTestCheckFunc( + testAccCheckAWSAPIGatewayDocumentationVersionExists(resourceName, &conf), + resource.TestCheckResourceAttr(resourceName, "version", version), + resource.TestCheckResourceAttrSet(resourceName, "rest_api_id"), + ), + }, + }, + }) +} + +func TestAccAWSAPIGatewayDocumentationVersion_allFields(t *testing.T) { + var conf apigateway.DocumentationVersion + + rString := acctest.RandString(8) + version := fmt.Sprintf("tf_acc_version_%s", rString) + apiName := fmt.Sprintf("tf_acc_api_doc_version_method_%s", rString) + stageName := fmt.Sprintf("tf_acc_stage_%s", rString) + description := fmt.Sprintf("Tf Acc Test description %s", rString) + uDescription := fmt.Sprintf("Tf Acc Test description updated %s", rString) + + resourceName := "aws_api_gateway_documentation_version.test" + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckAWSAPIGatewayDocumentationVersionDestroy, + Steps: []resource.TestStep{ + resource.TestStep{ + Config: testAccAWSAPIGatewayDocumentationVersionAllFieldsConfig(version, apiName, stageName, description), + Check: resource.ComposeTestCheckFunc( + testAccCheckAWSAPIGatewayDocumentationVersionExists(resourceName, &conf), + resource.TestCheckResourceAttr(resourceName, "version", version), + resource.TestCheckResourceAttr(resourceName, "description", description), + resource.TestCheckResourceAttrSet(resourceName, "rest_api_id"), + ), + }, + resource.TestStep{ + Config: testAccAWSAPIGatewayDocumentationVersionAllFieldsConfig(version, apiName, stageName, uDescription), + Check: resource.ComposeTestCheckFunc( + testAccCheckAWSAPIGatewayDocumentationVersionExists(resourceName, &conf), + resource.TestCheckResourceAttr(resourceName, "version", version), + resource.TestCheckResourceAttr(resourceName, "description", uDescription), + resource.TestCheckResourceAttrSet(resourceName, "rest_api_id"), + ), + }, + }, + }) +} + +func TestAccAWSAPIGatewayDocumentationVersion_importBasic(t *testing.T) { + rString := acctest.RandString(8) + version := fmt.Sprintf("tf_acc_version_import_%s", rString) + apiName := fmt.Sprintf("tf_acc_api_doc_version_import_%s", rString) + + resourceName := "aws_api_gateway_documentation_version.test" + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckAWSAPIGatewayDocumentationVersionDestroy, + Steps: []resource.TestStep{ + resource.TestStep{ + Config: testAccAWSAPIGatewayDocumentationVersionBasicConfig(version, apiName), + }, + resource.TestStep{ + ResourceName: resourceName, + ImportState: true, + ImportStateVerify: true, + }, + }, + }) +} + +func TestAccAWSAPIGatewayDocumentationVersion_importAllFields(t *testing.T) { + rString := acctest.RandString(8) + version := fmt.Sprintf("tf_acc_version_import_af_%s", rString) + apiName := fmt.Sprintf("tf_acc_api_doc_version_import_af_%s", rString) + stageName := fmt.Sprintf("tf_acc_stage_%s", rString) + description := fmt.Sprintf("Tf Acc Test description %s", rString) + + resourceName := "aws_api_gateway_documentation_version.test" + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckAWSAPIGatewayDocumentationVersionDestroy, + Steps: []resource.TestStep{ + resource.TestStep{ + Config: testAccAWSAPIGatewayDocumentationVersionAllFieldsConfig(version, apiName, stageName, description), + }, + resource.TestStep{ + ResourceName: resourceName, + ImportState: true, + ImportStateVerify: true, + }, + }, + }) +} + +func testAccCheckAWSAPIGatewayDocumentationVersionExists(n string, res *apigateway.DocumentationVersion) 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 Documentation Version ID is set") + } + + conn := testAccProvider.Meta().(*AWSClient).apigateway + + apiId, version, err := decodeApiGatewayDocumentationVersionId(rs.Primary.ID) + if err != nil { + return err + } + + req := &apigateway.GetDocumentationVersionInput{ + DocumentationVersion: aws.String(version), + RestApiId: aws.String(apiId), + } + docVersion, err := conn.GetDocumentationVersion(req) + if err != nil { + return err + } + + *res = *docVersion + + return nil + } +} + +func testAccCheckAWSAPIGatewayDocumentationVersionDestroy(s *terraform.State) error { + conn := testAccProvider.Meta().(*AWSClient).apigateway + + for _, rs := range s.RootModule().Resources { + if rs.Type != "aws_api_gateway_documentation_version" { + continue + } + + version, apiId, err := decodeApiGatewayDocumentationVersionId(rs.Primary.ID) + if err != nil { + return err + } + + req := &apigateway.GetDocumentationVersionInput{ + DocumentationVersion: aws.String(version), + RestApiId: aws.String(apiId), + } + _, err = conn.GetDocumentationVersion(req) + if err != nil { + if isAWSErr(err, apigateway.ErrCodeNotFoundException, "") { + return nil + } + return err + } + + return fmt.Errorf("API Gateway Documentation Version %q still exists.", rs.Primary.ID) + } + return nil +} + +func testAccAWSAPIGatewayDocumentationVersionBasicConfig(version, apiName string) string { + return fmt.Sprintf(` +resource "aws_api_gateway_documentation_version" "test" { + version = "%s" + rest_api_id = "${aws_api_gateway_rest_api.test.id}" + depends_on = ["aws_api_gateway_documentation_part.test"] +} + +resource "aws_api_gateway_documentation_part" "test" { + location { + type = "API" + } + properties = "{\"description\":\"Terraform Acceptance Test\"}" + rest_api_id = "${aws_api_gateway_rest_api.test.id}" +} + +resource "aws_api_gateway_rest_api" "test" { + name = "%s" +} +`, version, apiName) +} + +func testAccAWSAPIGatewayDocumentationVersionAllFieldsConfig(version, apiName, stageName, description string) string { + return fmt.Sprintf(` +resource "aws_api_gateway_documentation_version" "test" { + version = "%s" + rest_api_id = "${aws_api_gateway_rest_api.test.id}" + description = "%s" + depends_on = ["aws_api_gateway_documentation_part.test"] +} + +resource "aws_api_gateway_documentation_part" "test" { + location { + type = "API" + } + properties = "{\"description\":\"Terraform Acceptance Test\"}" + rest_api_id = "${aws_api_gateway_rest_api.test.id}" +} + +resource "aws_api_gateway_resource" "test" { + rest_api_id = "${aws_api_gateway_rest_api.test.id}" + parent_id = "${aws_api_gateway_rest_api.test.root_resource_id}" + path_part = "test" +} + +resource "aws_api_gateway_method" "test" { + rest_api_id = "${aws_api_gateway_rest_api.test.id}" + resource_id = "${aws_api_gateway_resource.test.id}" + http_method = "GET" + authorization = "NONE" +} + +resource "aws_api_gateway_method_response" "error" { + rest_api_id = "${aws_api_gateway_rest_api.test.id}" + resource_id = "${aws_api_gateway_resource.test.id}" + http_method = "${aws_api_gateway_method.test.http_method}" + status_code = "400" +} + +resource "aws_api_gateway_integration" "test" { + rest_api_id = "${aws_api_gateway_rest_api.test.id}" + resource_id = "${aws_api_gateway_resource.test.id}" + http_method = "${aws_api_gateway_method.test.http_method}" + + type = "HTTP" + uri = "https://www.google.co.uk" + integration_http_method = "GET" +} + +resource "aws_api_gateway_integration_response" "test" { + rest_api_id = "${aws_api_gateway_rest_api.test.id}" + resource_id = "${aws_api_gateway_resource.test.id}" + http_method = "${aws_api_gateway_integration.test.http_method}" + status_code = "${aws_api_gateway_method_response.error.status_code}" +} + +resource "aws_api_gateway_deployment" "test" { + rest_api_id = "${aws_api_gateway_rest_api.test.id}" + stage_name = "first" + depends_on = ["aws_api_gateway_integration_response.test"] +} + +resource "aws_api_gateway_stage" "test" { + stage_name = "%s" + rest_api_id = "${aws_api_gateway_rest_api.test.id}" + deployment_id = "${aws_api_gateway_deployment.test.id}" + documentation_version = "${aws_api_gateway_documentation_version.test.version}" +} + +resource "aws_api_gateway_rest_api" "test" { + name = "%s" +} +`, version, description, stageName, apiName) +} diff --git a/website/aws.erb b/website/aws.erb index ad34ee55463..844711ecd52 100644 --- a/website/aws.erb +++ b/website/aws.erb @@ -275,6 +275,9 @@