Skip to content

Commit

Permalink
New Resource: aws_api_gateway_documentation_part
Browse files Browse the repository at this point in the history
  • Loading branch information
radeksimko committed Jan 8, 2018
1 parent b221c37 commit 16d2836
Show file tree
Hide file tree
Showing 5 changed files with 510 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 @@ -247,6 +247,7 @@ func Provider() terraform.ResourceProvider {
"aws_api_gateway_base_path_mapping": resourceAwsApiGatewayBasePathMapping(),
"aws_api_gateway_client_certificate": resourceAwsApiGatewayClientCertificate(),
"aws_api_gateway_deployment": resourceAwsApiGatewayDeployment(),
"aws_api_gateway_documentation_part": resourceAwsApiGatewayDocumentationPart(),
"aws_api_gateway_domain_name": resourceAwsApiGatewayDomainName(),
"aws_api_gateway_gateway_response": resourceAwsApiGatewayGatewayResponse(),
"aws_api_gateway_integration": resourceAwsApiGatewayIntegration(),
Expand Down
202 changes: 202 additions & 0 deletions aws/resource_aws_api_gateway_documentation_part.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,202 @@
package aws

import (
"log"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/apigateway"
"github.com/hashicorp/terraform/helper/schema"
)

func resourceAwsApiGatewayDocumentationPart() *schema.Resource {
return &schema.Resource{
Create: resourceAwsApiGatewayDocumentationPartCreate,
Read: resourceAwsApiGatewayDocumentationPartRead,
Update: resourceAwsApiGatewayDocumentationPartUpdate,
Delete: resourceAwsApiGatewayDocumentationPartDelete,

Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},

Schema: map[string]*schema.Schema{
"location": {
Type: schema.TypeList,
Required: true,
ForceNew: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"method": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},
"name": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},
"path": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},
"status_code": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},
"type": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
},
},
},
"properties": {
Type: schema.TypeString,
Required: true,
},
"rest_api_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
},
}
}

func resourceAwsApiGatewayDocumentationPartCreate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).apigateway

out, err := conn.CreateDocumentationPart(&apigateway.CreateDocumentationPartInput{
Location: expandApiGatewayDocumentationPartLocation(d.Get("location").([]interface{})),
Properties: aws.String(d.Get("properties").(string)),
RestApiId: aws.String(d.Get("rest_api_id").(string)),
})
if err != nil {
return err
}
d.SetId(*out.Id)

return nil
}

func resourceAwsApiGatewayDocumentationPartRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).apigateway

log.Printf("[INFO] Reading API Gateway Documentation Part %s", d.Id())
docPart, err := conn.GetDocumentationPart(&apigateway.GetDocumentationPartInput{
DocumentationPartId: aws.String(d.Id()),
RestApiId: aws.String(d.Get("rest_api_id").(string)),
})
if err != nil {
if isAWSErr(err, "NotFoundException", "") {
log.Printf("[WARN] API Gateway Documentation Part (%s) not found, removing from state", d.Id())
d.SetId("")
return nil
}
return err
}

log.Printf("[DEBUG] Received API Gateway Documentation Part: %s", docPart)

d.Set("location", flattenApiGatewayDocumentationPartLocation(docPart.Location))
d.Set("properties", docPart.Properties)

return nil
}

func resourceAwsApiGatewayDocumentationPartUpdate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).apigateway

input := apigateway.UpdateDocumentationPartInput{
DocumentationPartId: aws.String(d.Id()),
RestApiId: aws.String(d.Get("rest_api_id").(string)),
}
operations := make([]*apigateway.PatchOperation, 0)

if d.HasChange("properties") {
properties := d.Get("properties").(string)
operations = append(operations, &apigateway.PatchOperation{
Op: aws.String("replace"),
Path: aws.String("/properties"),
Value: aws.String(properties),
})
}

input.PatchOperations = operations

log.Printf("[INFO] Updating API Gateway Documentation Part: %s", input)

out, err := conn.UpdateDocumentationPart(&input)
if err != nil {
return err
}

log.Printf("[DEBUG] API Gateway Documentation Part updated: %s", out)

return resourceAwsApiGatewayDocumentationPartRead(d, meta)
}

func resourceAwsApiGatewayDocumentationPartDelete(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).apigateway

_, err := conn.DeleteDocumentationPart(&apigateway.DeleteDocumentationPartInput{
DocumentationPartId: aws.String(d.Id()),
RestApiId: aws.String(d.Get("rest_api_id").(string)),
})
if err != nil {
return err
}

return nil
}

func expandApiGatewayDocumentationPartLocation(l []interface{}) *apigateway.DocumentationPartLocation {
if len(l) == 0 {
return nil
}
loc := l[0].(map[string]interface{})
out := &apigateway.DocumentationPartLocation{
Type: aws.String(loc["type"].(string)),
}
if v, ok := loc["method"]; ok {
out.Method = aws.String(v.(string))
}
if v, ok := loc["name"]; ok {
out.Name = aws.String(v.(string))
}
if v, ok := loc["path"]; ok {
out.Path = aws.String(v.(string))
}
if v, ok := loc["status_code"]; ok {
out.StatusCode = aws.String(v.(string))
}
return out
}

func flattenApiGatewayDocumentationPartLocation(l *apigateway.DocumentationPartLocation) []interface{} {
if l == nil {
return []interface{}{}
}

m := make(map[string]interface{}, 0)
m["type"] = *l.Type
if l.Method != nil {
m["method"] = *l.Method
}
if l.Name != nil {
m["name"] = *l.Name
}
if l.Path != nil {
m["path"] = *l.Path
}
if l.StatusCode != nil {
m["status_code"] = *l.StatusCode
}

return []interface{}{m}
}
Loading

0 comments on commit 16d2836

Please sign in to comment.