-
Notifications
You must be signed in to change notification settings - Fork 0
/
resource_k8s_yaml.go
149 lines (124 loc) · 3.17 KB
/
resource_k8s_yaml.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
package main
import (
"errors"
"fmt"
"github.com/ephyrasoftware/terraform-provider-k8s-yaml/lib"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"os/exec"
)
func k8sYaml() *schema.Resource {
return &schema.Resource{
Create: k8sYamlCreate,
Read: k8sYamlRead,
Update: k8sYamlUpdate,
Delete: k8sYamlDelete,
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
},
"namespace": {
Type: schema.TypeString,
Optional: true,
Default: "",
},
"files": {
Type: schema.TypeList,
Optional: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"file_url": {
Type: schema.TypeString,
Optional: true,
},
"collected_content": {
Type: schema.TypeString,
Computed: true,
Optional: true,
},
},
}
}
func k8sYamlCreate(d *schema.ResourceData, m interface{}) error {
name := d.Get("name").(string)
namespace := d.Get("namespace").(string)
fileUrl := d.Get("file_url").(string)
files := extractFiles(d)
if isFilesSet(files) && isUrlSet(fileUrl) {
return errors.New("cannot specify both 'file_url' and 'files'")
}
applyFailed := false
var applyErr error
var fieldName string
var content *string
if isUrlSet(fileUrl) {
content, applyErr = lib.ApplyFromUrl(name, namespace, fileUrl)
} else if isFilesSet(files) {
content, applyErr = lib.ApplyFromFiles(name, namespace, files)
} else {
return errors.New("one of 'files' or 'file_url' must be provided")
}
if applyErr != nil {
applyFailed = true
}
err := d.Set("collected_content", content)
if err != nil {
return fmt.Errorf("error setting generated field: [%s]", fieldName)
}
if applyErr != nil && applyFailed {
if content != nil {
// The apply got far enough to give back the content, assume the apply started and a partial resource may exist.
d.SetId(name)
}
return fmt.Errorf("apply failed: [%s]", applyErr.Error())
}
d.SetId(name)
return k8sYamlRead(d, m)
}
func k8sYamlRead(d *schema.ResourceData, m interface{}) error {
name := d.Get("name").(string)
d.SetId(name)
return nil
}
func k8sYamlUpdate(d *schema.ResourceData, m interface{}) error {
return k8sYamlRead(d, m)
}
func k8sYamlDelete(d *schema.ResourceData, m interface{}) error {
name := d.Get("name").(string)
namespace := d.Get("namespace").(string)
content := d.Get("collected_content").(string)
filePath, err := lib.ContentToFile(name, content)
if err != nil {
return errors.New("failed to send content to file")
}
args := []string{"delete", "-f", filePath}
if namespace != "" {
args = append(args, "-n", namespace)
}
cmd := exec.Command("kubectl", args[:]...)
output, err := cmd.CombinedOutput()
if err != nil {
return fmt.Errorf("error removing resource: %s\n%s", err.Error(), output)
}
return nil
}
func extractFiles(d *schema.ResourceData) []string {
var files []string
for _, raw := range d.Get("files").([]interface{}) {
if raw != nil {
file := raw.(string)
if file != "" {
files = append(files, file)
}
}
}
return files
}
func isFilesSet(files []string) bool {
return len(files) != 0
}
func isUrlSet(fileUrl string) bool {
return fileUrl != ""
}