forked from metalsoft-io/metalcloud-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcmd_apply.go
146 lines (118 loc) · 3.23 KB
/
cmd_apply.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
package main
import (
"flag"
"fmt"
"regexp"
"strings"
metalcloud "github.com/metalsoft-io/metal-cloud-sdk-go/v2"
"gopkg.in/yaml.v3"
)
const yamlSeparator = "\n---"
//infrastructureCmds commands affecting infrastructures
var applyCmds = []Command{
{
Description: "Apply changes from file.",
Subject: "apply",
AltSubject: "apply",
Predicate: _nilDefaultStr,
AltPredicate: _nilDefaultStr,
FlagSet: flag.NewFlagSet("apply", flag.ExitOnError),
InitFunc: func(c *Command) {
c.Arguments = map[string]interface{}{
"read_config_from_file": c.FlagSet.String("f", _nilDefaultStr, "The file "),
}
},
ExecuteFunc: applyCmd,
Endpoint: DeveloperEndpoint,
},
{
Description: "Delete changes from file.",
Subject: "delete",
AltSubject: "delete",
Predicate: _nilDefaultStr,
AltPredicate: _nilDefaultStr,
FlagSet: flag.NewFlagSet("apply", flag.ExitOnError),
InitFunc: func(c *Command) {
c.Arguments = map[string]interface{}{
"read_config_from_file": c.FlagSet.String("f", _nilDefaultStr, "The file "),
}
},
ExecuteFunc: deleteCmd,
Endpoint: DeveloperEndpoint,
},
}
func applyCmd(c *Command, client metalcloud.MetalCloudClient) (string, error) {
objects, err := readObjectsFromCommand(c, client)
if err != nil {
return "", err
}
for _, object := range objects {
err = object.CreateOrUpdate(client)
if err != nil {
return "", err
}
}
return "", nil
}
func deleteCmd(c *Command, client metalcloud.MetalCloudClient) (string, error) {
objects, err := readObjectsFromCommand(c, client)
if err != nil {
return "", err
}
for _, object := range objects {
if err != nil {
return "", err
}
err := object.Delete(client)
if err != nil {
return "", err
}
}
return "", nil
}
func readObjectsFromCommand(c *Command, client metalcloud.MetalCloudClient) ([]metalcloud.Applier, error) {
var err error
content := []byte{}
var results []metalcloud.Applier
if filePath, ok := getStringParamOk(c.Arguments["read_config_from_file"]); ok {
content, err = readInputFromFile(filePath)
} else {
return nil, fmt.Errorf("file name is required")
}
if err != nil {
return nil, err
}
if len(content) == 0 {
return nil, fmt.Errorf("Content cannot be empty")
}
objects := strings.Split(string(content), yamlSeparator)
getKind := func(object []byte) (string, error) {
re := regexp.MustCompile(`kind\s*:\s*(.+)`)
matches := re.FindAllSubmatch(object, -1)
if len(matches) > 0 {
return string(matches[0][1]), nil
}
return "", fmt.Errorf("property kind is missing")
}
for _, object := range objects {
if len(strings.Trim(object, " \n\r")) == 0 {
continue
}
bytes := []byte(object)
kind, err := getKind(bytes)
if err != nil {
return nil, err
}
kind = strings.Trim(kind, " \n\r")
newType, err := metalcloud.GetObjectByKind(kind)
if err != nil {
return nil, err
}
if err = yaml.Unmarshal(bytes, newType.Interface()); err != nil {
return nil, err
}
newObject := newType.Elem().Interface()
results = append(results, newObject.(metalcloud.Applier))
}
return results, nil
}