forked from hashicorp/terraform-provider-aws
/
data_source_aws_api_gateway_resource.go
67 lines (58 loc) · 1.57 KB
/
data_source_aws_api_gateway_resource.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package aws
import (
"fmt"
"log"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/apigateway"
"github.com/hashicorp/terraform/helper/schema"
)
func dataSourceAwsApiGatewayResource() *schema.Resource {
return &schema.Resource{
Read: dataSourceAwsApiGatewayResourceRead,
Schema: map[string]*schema.Schema{
"rest_api_id": {
Type: schema.TypeString,
Required: true,
},
"path": {
Type: schema.TypeString,
Required: true,
},
"path_part": {
Type: schema.TypeString,
Computed: true,
},
"parent_id": {
Type: schema.TypeString,
Computed: true,
},
},
}
}
func dataSourceAwsApiGatewayResourceRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).apigateway
restApiId := d.Get("rest_api_id").(string)
target := d.Get("path").(string)
params := &apigateway.GetResourcesInput{RestApiId: aws.String(restApiId)}
var match *apigateway.Resource
log.Printf("[DEBUG] Reading API Gateway Resources: %s", params)
err := conn.GetResourcesPages(params, func(page *apigateway.GetResourcesOutput, lastPage bool) bool {
for _, resource := range page.Items {
if aws.StringValue(resource.Path) == target {
match = resource
return false
}
}
return !lastPage
})
if err != nil {
return fmt.Errorf("error describing API Gateway Resources: %s", err)
}
if match == nil {
return fmt.Errorf("no Resources with path %q found for rest api %q", target, restApiId)
}
d.SetId(*match.Id)
d.Set("path_part", match.PathPart)
d.Set("parent_id", match.ParentId)
return nil
}