Skip to content

Commit

Permalink
Merge pull request #3287 from terraform-providers/f-apig-doc-version
Browse files Browse the repository at this point in the history
New Resource: aws_api_gateway_documentation_version
  • Loading branch information
radeksimko authored Feb 9, 2018
2 parents 37de617 + eef28b1 commit 82d266f
Show file tree
Hide file tree
Showing 5 changed files with 480 additions and 0 deletions.
1 change: 1 addition & 0 deletions aws/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
137 changes: 137 additions & 0 deletions aws/resource_aws_api_gateway_documentation_version.go
Original file line number Diff line number Diff line change
@@ -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
}
Loading

0 comments on commit 82d266f

Please sign in to comment.