forked from hashicorp/terraform-provider-aws
/
data_source_aws_cloudformation_export.go
58 lines (53 loc) · 1.45 KB
/
data_source_aws_cloudformation_export.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
package aws
import (
"fmt"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/cloudformation"
"github.com/hashicorp/terraform/helper/schema"
)
func dataSourceAwsCloudFormationExport() *schema.Resource {
return &schema.Resource{
Read: dataSourceAwsCloudFormationExportRead,
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
},
"value": {
Type: schema.TypeString,
Computed: true,
},
"exporting_stack_id": {
Type: schema.TypeString,
Computed: true,
},
},
}
}
func dataSourceAwsCloudFormationExportRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).cfconn
var value string
name := d.Get("name").(string)
region := meta.(*AWSClient).region
d.SetId(fmt.Sprintf("cloudformation-exports-%s-%s", region, name))
input := &cloudformation.ListExportsInput{}
err := conn.ListExportsPages(input,
func(page *cloudformation.ListExportsOutput, lastPage bool) bool {
for _, e := range page.Exports {
if name == aws.StringValue(e.Name) {
value = aws.StringValue(e.Value)
d.Set("value", e.Value)
d.Set("exporting_stack_id", e.ExportingStackId)
return false
}
}
return !lastPage
})
if err != nil {
return fmt.Errorf("Failed listing CloudFormation exports: %s", err)
}
if "" == value {
return fmt.Errorf("%s was not found in CloudFormation Exports for region %s", name, region)
}
return nil
}