forked from hashicorp/terraform
-
Notifications
You must be signed in to change notification settings - Fork 2
/
datasource_cloudinit_config.go
182 lines (156 loc) · 4.1 KB
/
datasource_cloudinit_config.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
package template
import (
"bytes"
"compress/gzip"
"encoding/base64"
"fmt"
"io"
"mime/multipart"
"net/textproto"
"strconv"
"github.com/hashicorp/terraform/helper/hashcode"
"github.com/hashicorp/terraform/helper/schema"
)
func dataSourceCloudinitConfig() *schema.Resource {
return &schema.Resource{
Read: dataSourceCloudinitConfigRead,
Schema: map[string]*schema.Schema{
"part": &schema.Schema{
Type: schema.TypeList,
Required: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"content_type": &schema.Schema{
Type: schema.TypeString,
Optional: true,
},
"content": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
"filename": &schema.Schema{
Type: schema.TypeString,
Optional: true,
},
"merge_type": &schema.Schema{
Type: schema.TypeString,
Optional: true,
},
},
},
},
"gzip": &schema.Schema{
Type: schema.TypeBool,
Optional: true,
Default: true,
},
"base64_encode": &schema.Schema{
Type: schema.TypeBool,
Optional: true,
Default: true,
},
"rendered": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "rendered cloudinit configuration",
},
},
}
}
func dataSourceCloudinitConfigRead(d *schema.ResourceData, meta interface{}) error {
rendered, err := renderCloudinitConfig(d)
if err != nil {
return err
}
d.Set("rendered", rendered)
d.SetId(strconv.Itoa(hashcode.String(rendered)))
return nil
}
func renderCloudinitConfig(d *schema.ResourceData) (string, error) {
gzipOutput := d.Get("gzip").(bool)
base64Output := d.Get("base64_encode").(bool)
partsValue, hasParts := d.GetOk("part")
if !hasParts {
return "", fmt.Errorf("No parts found in the cloudinit resource declaration")
}
cloudInitParts := make(cloudInitParts, len(partsValue.([]interface{})))
for i, v := range partsValue.([]interface{}) {
p := v.(map[string]interface{})
part := cloudInitPart{}
if p, ok := p["content_type"]; ok {
part.ContentType = p.(string)
}
if p, ok := p["content"]; ok {
part.Content = p.(string)
}
if p, ok := p["merge_type"]; ok {
part.MergeType = p.(string)
}
if p, ok := p["filename"]; ok {
part.Filename = p.(string)
}
cloudInitParts[i] = part
}
var buffer bytes.Buffer
var err error
if gzipOutput {
gzipWriter := gzip.NewWriter(&buffer)
err = renderPartsToWriter(cloudInitParts, gzipWriter)
gzipWriter.Close()
} else {
err = renderPartsToWriter(cloudInitParts, &buffer)
}
if err != nil {
return "", err
}
output := ""
if base64Output {
output = base64.StdEncoding.EncodeToString(buffer.Bytes())
} else {
output = buffer.String()
}
return output, nil
}
func renderPartsToWriter(parts cloudInitParts, writer io.Writer) error {
mimeWriter := multipart.NewWriter(writer)
defer mimeWriter.Close()
// we need to set the boundary explictly, otherwise the boundary is random
// and this causes terraform to complain about the resource being different
if err := mimeWriter.SetBoundary("MIMEBOUNDARY"); err != nil {
return err
}
writer.Write([]byte(fmt.Sprintf("Content-Type: multipart/mixed; boundary=\"%s\"\n", mimeWriter.Boundary())))
writer.Write([]byte("MIME-Version: 1.0\r\n"))
for _, part := range parts {
header := textproto.MIMEHeader{}
if part.ContentType == "" {
header.Set("Content-Type", "text/plain")
} else {
header.Set("Content-Type", part.ContentType)
}
header.Set("MIME-Version", "1.0")
header.Set("Content-Transfer-Encoding", "7bit")
if part.Filename != "" {
header.Set("Content-Disposition", fmt.Sprintf(`attachment; filename="%s"`, part.Filename))
}
if part.MergeType != "" {
header.Set("X-Merge-Type", part.MergeType)
}
partWriter, err := mimeWriter.CreatePart(header)
if err != nil {
return err
}
_, err = partWriter.Write([]byte(part.Content))
if err != nil {
return err
}
}
return nil
}
type cloudInitPart struct {
ContentType string
MergeType string
Filename string
Content string
}
type cloudInitParts []cloudInitPart