/
data_source_aws_ssm_document.go
90 lines (77 loc) · 2.09 KB
/
data_source_aws_ssm_document.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package aws
import (
"fmt"
"log"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/arn"
"github.com/aws/aws-sdk-go/service/ssm"
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/helper/validation"
)
func dataSourceAwsSsmDocument() *schema.Resource {
return &schema.Resource{
Read: dataAwsSsmDocumentRead,
Schema: map[string]*schema.Schema{
"arn": {
Type: schema.TypeString,
Computed: true,
},
"content": {
Type: schema.TypeString,
Computed: true,
},
"document_format": {
Type: schema.TypeString,
Optional: true,
Default: ssm.DocumentFormatJson,
ValidateFunc: validation.StringInSlice([]string{
ssm.DocumentFormatJson,
ssm.DocumentFormatYaml,
}, false),
},
"document_type": {
Type: schema.TypeString,
Computed: true,
},
"document_version": {
Type: schema.TypeString,
Optional: true,
},
"name": {
Type: schema.TypeString,
Required: true,
},
},
}
}
func dataAwsSsmDocumentRead(d *schema.ResourceData, meta interface{}) error {
ssmconn := meta.(*AWSClient).ssmconn
name := d.Get("name").(string)
docInput := &ssm.GetDocumentInput{
Name: aws.String(name),
DocumentFormat: aws.String(d.Get("document_format").(string)),
}
if docVersion, ok := d.GetOk("document_version"); ok {
docInput.DocumentVersion = aws.String(docVersion.(string))
}
log.Printf("[DEBUG] Reading SSM Document: %s", docInput)
resp, err := ssmconn.GetDocument(docInput)
if err != nil {
return fmt.Errorf("Error reading SSM Document: %s", err)
}
d.SetId(aws.StringValue(resp.Name))
arn := arn.ARN{
Partition: meta.(*AWSClient).partition,
Service: "ssm",
Region: meta.(*AWSClient).region,
AccountID: meta.(*AWSClient).accountid,
Resource: fmt.Sprintf("document/%s", aws.StringValue(resp.Name)),
}.String()
d.Set("arn", arn)
d.Set("name", resp.Name)
d.Set("content", resp.Content)
d.Set("document_version", resp.DocumentVersion)
d.Set("document_format", resp.DocumentFormat)
d.Set("document_type", resp.DocumentType)
return nil
}