Skip to content

Commit 7ead800

Browse files
committed
Add aws_api_gateway_resource resource
1 parent a73721d commit 7ead800

5 files changed

Lines changed: 309 additions & 0 deletions

File tree

builtin/providers/aws/provider.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ func Provider() terraform.ResourceProvider {
115115
"aws_ami_copy": resourceAwsAmiCopy(),
116116
"aws_ami_from_instance": resourceAwsAmiFromInstance(),
117117
"aws_api_gateway_rest_api": resourceAwsApiGatewayRestApi(),
118+
"aws_api_gateway_resource": resourceAwsApiGatewayResource(),
118119
"aws_app_cookie_stickiness_policy": resourceAwsAppCookieStickinessPolicy(),
119120
"aws_autoscaling_group": resourceAwsAutoscalingGroup(),
120121
"aws_autoscaling_notification": resourceAwsAutoscalingNotification(),
Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
package aws
2+
3+
import (
4+
"fmt"
5+
"log"
6+
"time"
7+
8+
"github.com/aws/aws-sdk-go/aws"
9+
"github.com/aws/aws-sdk-go/aws/awserr"
10+
"github.com/aws/aws-sdk-go/service/apigateway"
11+
"github.com/hashicorp/terraform/helper/resource"
12+
"github.com/hashicorp/terraform/helper/schema"
13+
)
14+
15+
func resourceAwsApiGatewayResource() *schema.Resource {
16+
return &schema.Resource{
17+
Create: resourceAwsApiGatewayResourceCreate,
18+
Read: resourceAwsApiGatewayResourceRead,
19+
Update: resourceAwsApiGatewayResourceUpdate,
20+
Delete: resourceAwsApiGatewayResourceDelete,
21+
22+
Schema: map[string]*schema.Schema{
23+
"rest_api_id": &schema.Schema{
24+
Type: schema.TypeString,
25+
Required: true,
26+
ForceNew: true,
27+
},
28+
29+
"parent_id": &schema.Schema{
30+
Type: schema.TypeString,
31+
Required: true,
32+
},
33+
34+
"path_part": &schema.Schema{
35+
Type: schema.TypeString,
36+
Required: true,
37+
},
38+
39+
"path": &schema.Schema{
40+
Type: schema.TypeString,
41+
Computed: true,
42+
},
43+
},
44+
}
45+
}
46+
47+
func resourceAwsApiGatewayResourceCreate(d *schema.ResourceData, meta interface{}) error {
48+
conn := meta.(*AWSClient).apigateway
49+
log.Printf("[DEBUG] Creating API Gateway Resource for API %s", d.Get("rest_api_id").(string))
50+
51+
var err error
52+
resource, err := conn.CreateResource(&apigateway.CreateResourceInput{
53+
ParentId: aws.String(d.Get("parent_id").(string)),
54+
PathPart: aws.String(d.Get("path_part").(string)),
55+
RestApiId: aws.String(d.Get("rest_api_id").(string)),
56+
})
57+
58+
if err != nil {
59+
return fmt.Errorf("Error creating API Gateway Resource: %s", err)
60+
}
61+
62+
d.SetId(*resource.Id)
63+
d.Set("path", resource.Path)
64+
65+
return nil
66+
}
67+
68+
func resourceAwsApiGatewayResourceRead(d *schema.ResourceData, meta interface{}) error {
69+
conn := meta.(*AWSClient).apigateway
70+
71+
log.Printf("[DEBUG] Reading API Gateway Resource %s", d.Id())
72+
resource, err := conn.GetResource(&apigateway.GetResourceInput{
73+
ResourceId: aws.String(d.Id()),
74+
RestApiId: aws.String(d.Get("rest_api_id").(string)),
75+
})
76+
77+
if err != nil {
78+
return err
79+
}
80+
81+
d.Set("parent_id", resource.ParentId)
82+
d.Set("path_part", resource.PathPart)
83+
84+
return nil
85+
}
86+
87+
func resourceAwsApiGatewayResourceUpdateOperations(d *schema.ResourceData) []*apigateway.PatchOperation {
88+
operations := make([]*apigateway.PatchOperation, 0)
89+
if d.HasChange("path_part") {
90+
operations = append(operations, &apigateway.PatchOperation{
91+
Op: aws.String("replace"),
92+
Path: aws.String("/pathPart"),
93+
Value: aws.String(d.Get("path_part").(string)),
94+
})
95+
}
96+
97+
if d.HasChange("parent_id") {
98+
operations = append(operations, &apigateway.PatchOperation{
99+
Op: aws.String("replace"),
100+
Path: aws.String("/parentId"),
101+
Value: aws.String(d.Get("parent_id").(string)),
102+
})
103+
}
104+
return operations
105+
}
106+
107+
func resourceAwsApiGatewayResourceUpdate(d *schema.ResourceData, meta interface{}) error {
108+
conn := meta.(*AWSClient).apigateway
109+
110+
log.Printf("[DEBUG] Updating API Gateway Resource %s", d.Id())
111+
_, err := conn.UpdateResource(&apigateway.UpdateResourceInput{
112+
ResourceId: aws.String(d.Id()),
113+
RestApiId: aws.String(d.Get("rest_api_id").(string)),
114+
PatchOperations: resourceAwsApiGatewayResourceUpdateOperations(d),
115+
})
116+
117+
if err != nil {
118+
return err
119+
}
120+
121+
return resourceAwsApiGatewayResourceRead(d, meta)
122+
}
123+
124+
func resourceAwsApiGatewayResourceDelete(d *schema.ResourceData, meta interface{}) error {
125+
conn := meta.(*AWSClient).apigateway
126+
log.Printf("[DEBUG] Deleting API Gateway Resource: %s", d.Id())
127+
128+
return resource.Retry(5*time.Minute, func() error {
129+
log.Printf("[DEBUG] schema is %#v", d)
130+
_, err := conn.DeleteResource(&apigateway.DeleteResourceInput{
131+
ResourceId: aws.String(d.Id()),
132+
RestApiId: aws.String(d.Get("rest_api_id").(string)),
133+
})
134+
if err == nil {
135+
return nil
136+
}
137+
138+
if apigatewayErr, ok := err.(awserr.Error); ok && apigatewayErr.Code() == "NotFoundException" {
139+
return nil
140+
}
141+
142+
return resource.RetryError{Err: err}
143+
})
144+
}
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
package aws
2+
3+
import (
4+
"fmt"
5+
"testing"
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/resource"
11+
"github.com/hashicorp/terraform/terraform"
12+
)
13+
14+
func TestAccAWSAPIGatewayResource_basic(t *testing.T) {
15+
var conf apigateway.Resource
16+
17+
resource.Test(t, resource.TestCase{
18+
PreCheck: func() { testAccPreCheck(t) },
19+
Providers: testAccProviders,
20+
CheckDestroy: testAccCheckAWSAPIGatewayResourceDestroy,
21+
Steps: []resource.TestStep{
22+
resource.TestStep{
23+
Config: testAccAWSAPIGatewayResourceConfig,
24+
Check: resource.ComposeTestCheckFunc(
25+
testAccCheckAWSAPIGatewayResourceExists("aws_api_gateway_resource.test", &conf),
26+
testAccCheckAWSAPIGatewayResourceAttributes(&conf),
27+
resource.TestCheckResourceAttr(
28+
"aws_api_gateway_resource.test", "path_part", "test"),
29+
),
30+
},
31+
},
32+
})
33+
}
34+
35+
func testAccCheckAWSAPIGatewayResourceAttributes(conf *apigateway.Resource) resource.TestCheckFunc {
36+
return func(s *terraform.State) error {
37+
if *conf.Path != "/test" {
38+
return fmt.Errorf("Wrong Path: %q", conf.Path)
39+
}
40+
41+
return nil
42+
}
43+
}
44+
45+
func testAccCheckAWSAPIGatewayResourceExists(n string, res *apigateway.Resource) resource.TestCheckFunc {
46+
return func(s *terraform.State) error {
47+
rs, ok := s.RootModule().Resources[n]
48+
if !ok {
49+
return fmt.Errorf("Not found: %s", n)
50+
}
51+
52+
if rs.Primary.ID == "" {
53+
return fmt.Errorf("No API Gateway Resource ID is set")
54+
}
55+
56+
conn := testAccProvider.Meta().(*AWSClient).apigateway
57+
58+
req := &apigateway.GetResourceInput{
59+
ResourceId: aws.String(rs.Primary.ID),
60+
RestApiId: aws.String(s.RootModule().Resources["aws_api_gateway_rest_api.test"].Primary.ID),
61+
}
62+
describe, err := conn.GetResource(req)
63+
if err != nil {
64+
return err
65+
}
66+
67+
if *describe.Id != rs.Primary.ID {
68+
return fmt.Errorf("APIGateway Resource not found")
69+
}
70+
71+
*res = *describe
72+
73+
return nil
74+
}
75+
}
76+
77+
func testAccCheckAWSAPIGatewayResourceDestroy(s *terraform.State) error {
78+
conn := testAccProvider.Meta().(*AWSClient).apigateway
79+
80+
for _, rs := range s.RootModule().Resources {
81+
if rs.Type != "aws_api_gateway_resource" {
82+
continue
83+
}
84+
85+
req := &apigateway.GetResourcesInput{
86+
RestApiId: aws.String(s.RootModule().Resources["aws_api_gateway_rest_api.test"].Primary.ID),
87+
}
88+
describe, err := conn.GetResources(req)
89+
90+
if err == nil {
91+
if len(describe.Items) != 0 &&
92+
*describe.Items[0].Id == rs.Primary.ID {
93+
return fmt.Errorf("API Gateway Resource still exists")
94+
}
95+
}
96+
97+
aws2err, ok := err.(awserr.Error)
98+
if !ok {
99+
return err
100+
}
101+
if aws2err.Code() != "NotFoundException" {
102+
return err
103+
}
104+
105+
return nil
106+
}
107+
108+
return nil
109+
}
110+
111+
const testAccAWSAPIGatewayResourceConfig = `
112+
resource "aws_api_gateway_rest_api" "test" {
113+
name = "test"
114+
}
115+
116+
resource "aws_api_gateway_resource" "test" {
117+
rest_api_id = "${aws_api_gateway_rest_api.test.id}"
118+
parent_id = "${aws_api_gateway_rest_api.test.root_resource_id}"
119+
path_part = "test"
120+
}
121+
`
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
---
2+
layout: "aws"
3+
page_title: "AWS: aws_api_gateway_resource"
4+
sidebar_current: "docs-aws-resource-api-gateway-resource"
5+
description: |-
6+
Provides an API Gateway Resource.
7+
---
8+
9+
# aws\_api\_gateway\_resource
10+
11+
Provides an API Gateway REST API Resource.
12+
13+
## Example Usage
14+
15+
```
16+
resource "aws_api_gateway_rest_api" "MyDemoAPI" {
17+
name = "MyDemoAPI"
18+
description = "This is my API for demonstration purposes"
19+
}
20+
21+
resource "aws_api_gateway_resource" "MyDemoResource" {
22+
rest_api_id = "${aws_api_gateway_rest_api.MyDemoAPI.id}"
23+
parent_resource_id = "${aws_api_gateway_rest_api.MyDemoAPI.root_resource_id}"
24+
path_part = "mydemoresource"
25+
}
26+
```
27+
28+
## Argument Reference
29+
30+
The following arguments are supported:
31+
32+
* `rest_api_id` - (Required) API Gateway ID
33+
* `parent_resource_id` - (Required) Parent resource ID
34+
* `path_part` - (Required) The resource path
35+
36+
## Attributes Reference
37+
38+
The following attributes are exported:
39+
40+
* `path` - The complete path for this resource, including all parent paths

website/source/layouts/aws.erb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616
<li<%= sidebar_current("docs-aws-resource-api-gateway-rest-api") %>>
1717
<a href="/docs/providers/aws/r/api_gateway_rest_api.html">aws_api_gateway_rest_api</a>
1818
</li>
19+
<li<%= sidebar_current("docs-aws-resource-api-gateway-resource") %>>
20+
<a href="/docs/providers/aws/r/api_gateway_resource.html">aws_api_gateway_resource</a>
21+
</li>
1922
</ul>
2023
</li>
2124

0 commit comments

Comments
 (0)