forked from hashicorp/terraform
-
Notifications
You must be signed in to change notification settings - Fork 0
/
resource_ignition_file.go
189 lines (169 loc) · 4.03 KB
/
resource_ignition_file.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
183
184
185
186
187
188
189
package ignition
import (
"encoding/base64"
"fmt"
"github.com/coreos/ignition/config/types"
"github.com/hashicorp/terraform/helper/schema"
)
func resourceFile() *schema.Resource {
return &schema.Resource{
Create: resourceFileCreate,
Delete: resourceFileDelete,
Exists: resourceFileExists,
Read: resourceFileRead,
Schema: map[string]*schema.Schema{
"filesystem": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"path": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"content": &schema.Schema{
Type: schema.TypeList,
Optional: true,
ForceNew: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"mime": &schema.Schema{
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Default: "text/plain",
},
"content": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
},
},
},
"source": &schema.Schema{
Type: schema.TypeList,
Optional: true,
ForceNew: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"source": &schema.Schema{
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},
"compression": &schema.Schema{
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},
"verification": &schema.Schema{
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},
},
},
},
"mode": &schema.Schema{
Type: schema.TypeInt,
Optional: true,
ForceNew: true,
},
"uid": &schema.Schema{
Type: schema.TypeInt,
Optional: true,
ForceNew: true,
},
"gid": &schema.Schema{
Type: schema.TypeInt,
Optional: true,
ForceNew: true,
},
},
}
}
func resourceFileCreate(d *schema.ResourceData, meta interface{}) error {
id, err := buildFile(d, meta.(*cache))
if err != nil {
return err
}
d.SetId(id)
return nil
}
func resourceFileDelete(d *schema.ResourceData, meta interface{}) error {
d.SetId("")
return nil
}
func resourceFileExists(d *schema.ResourceData, meta interface{}) (bool, error) {
id, err := buildFile(d, meta.(*cache))
if err != nil {
return false, err
}
return id == d.Id(), nil
}
func resourceFileRead(d *schema.ResourceData, metacontent interface{}) error {
return nil
}
func buildFile(d *schema.ResourceData, c *cache) (string, error) {
_, hasContent := d.GetOk("content")
_, hasSource := d.GetOk("source")
if hasContent && hasSource {
return "", fmt.Errorf("content and source options are incompatible")
}
if !hasContent && !hasSource {
return "", fmt.Errorf("content or source options must be present")
}
var compression types.Compression
var source types.Url
var hash *types.Hash
var err error
if hasContent {
source, err = encodeDataURL(
d.Get("content.0.mime").(string),
d.Get("content.0.content").(string),
)
if err != nil {
return "", err
}
}
if hasSource {
source, err = buildURL(d.Get("source.0.source").(string))
if err != nil {
return "", err
}
compression = types.Compression(d.Get("source.0.compression").(string))
h, err := buildHash(d.Get("source.0.verification").(string))
if err != nil {
return "", err
}
hash = &h
}
return c.addFile(&types.File{
Filesystem: d.Get("filesystem").(string),
Path: types.Path(d.Get("path").(string)),
Contents: types.FileContents{
Compression: compression,
Source: source,
Verification: types.Verification{
Hash: hash,
},
},
User: types.FileUser{
Id: d.Get("uid").(int),
},
Group: types.FileGroup{
Id: d.Get("gid").(int),
},
Mode: types.FileMode(d.Get("mode").(int)),
}), nil
}
func encodeDataURL(mime, content string) (types.Url, error) {
base64 := base64.StdEncoding.EncodeToString([]byte(content))
return buildURL(
fmt.Sprintf("data:%s;charset=utf-8;base64,%s", mime, base64),
)
}