summaryrefslogtreecommitdiffstats
path: root/src/tools/emcoctl/cmd/utils.go
blob: d266a00f94a145c6e464eaa5917d3f1278d23087 (plain)
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
/*Copyright © 2020 Intel Corp

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package cmd

import (
	"bytes"
	"encoding/json"
	"fmt"
	"io/ioutil"
	neturl "net/url"
	"os"
	"strings"

	"github.com/go-resty/resty/v2"
	"github.com/mitchellh/mapstructure"
	pkgerrors "github.com/pkg/errors"
	"gopkg.in/yaml.v3"
)

var inputFiles []string
var valuesFiles []string

type ResourceContext struct {
	Anchor string `json:"anchor" yaml:"anchor"`
}

type Metadata struct {
	Name        string `yaml:"name" json:"name"`
	Description string `yaml:"description,omitempty" json:"description,omitempty"`
	UserData1   string `yaml:"userData1,omitempty" json:"userData1,omitempty"`
	UserData2   string `yaml:"userData2,omitempty" json:"userData2,omitempty"`
}

type emcoRes struct {
	Version string                 `yaml:"version" json:"version"`
	Context ResourceContext        `yaml:"resourceContext" json:"resourceContext"`
	Meta    Metadata               `yaml:"metadata" json:"metadata"`
	Spec    map[string]interface{} `yaml:"spec,omitempty" json:"spec,omitempty"`
	File    string                 `yaml:"file,omitempty" json:"file,omitempty"`
	Label   string                 `yaml:"label-name,omitempty" json:"label-name,omitempty"`
}

type emcoBody struct {
	Meta  Metadata               `json:"metadata,omitempty"`
	Label string                 `json:"label-name,omitempty"`
	Spec  map[string]interface{} `json:"spec,omitempty"`
}

type emcoCompositeAppSpec struct {
	Version string `json: "version"`
}

type Resources struct {
	anchor string
	body   []byte
	file   string
}

// RestyClient to use with CLI
type RestyClient struct {
	client *resty.Client
}

var Client RestyClient

// NewRestClient returns a rest client
func NewRestClient() RestyClient {
	// Create a Resty Client
	Client.client = resty.New()
	// Bearer Auth Token for all request
	// Client.client.SetAuthToken()
	// Registering global Error object structure for JSON/XML request
	//Client.client.SetError(&Error{})
	return Client
}

// readResources reads all the resources in the file provided
func readResources() []Resources {
	// TODO: Remove Assumption only one file
	// Open file and Parse to get all resources
	var resources []Resources
	f, err := os.Open(inputFiles[0])
	defer f.Close()
	if err != nil {
		fmt.Printf("Error %s reading file %s\n", err, inputFiles[0])
		return []Resources{}
	}
	if len(valuesFiles) > 0 {
		//Apply template
	}

	dec := yaml.NewDecoder(f)
	// Iterate through all resources in the file
	for {
		var doc emcoRes
		if dec.Decode(&doc) != nil {
			break
		}
		body := &emcoBody{Meta: doc.Meta, Spec: doc.Spec, Label: doc.Label}
		jsonBody, err := json.Marshal(body)
		if err != nil {
			return []Resources{}
		}
		var res Resources
		if doc.File != "" {
			res = Resources{anchor: doc.Context.Anchor, body: jsonBody, file: doc.File}
		} else {
			res = Resources{anchor: doc.Context.Anchor, body: jsonBody}
		}
		resources = append(resources, res)
	}
	return resources
}

//RestClientPost to post to server no multipart
func (r RestyClient) RestClientPost(anchor string, body []byte) error {

	url, err := GetURL(anchor)
	if err != nil {
		return err
	}

	// POST JSON string
	resp, err := r.client.R().
		SetHeader("Content-Type", "application/json").
		SetBody(body).
		Post(url)
	if err != nil {
		fmt.Println(err)
		return err
	}
	fmt.Println("URL:", anchor, "Response Code:", resp.StatusCode(), "Response:", resp)
	if resp.StatusCode() >= 201 && resp.StatusCode() <= 299 {
		return nil
	}
	return pkgerrors.Errorf("Server Post Error")
}

//RestClientMultipartPost to post to server with multipart
func (r RestyClient) RestClientMultipartPost(anchor string, body []byte, file string) error {
	url, err := GetURL(anchor)
	if err != nil {
		return err
	}

	// Read file for multipart
	f, err := ioutil.ReadFile(file)
	if err != nil {
		fmt.Printf("Error %s reading file %s\n", err, file)
		return err
	}

	// Multipart Post
	formParams := neturl.Values{}
	formParams.Add("metadata", string(body))
	resp, err := r.client.R().
		SetFileReader("file", "filename", bytes.NewReader(f)).
		SetFormDataFromValues(formParams).
		Post(url)

	if err != nil {
		fmt.Println(err)
		return err
	}
	fmt.Println("URL:", anchor, "Response Code:", resp.StatusCode(), "Response:", resp)
	if resp.StatusCode() >= 201 && resp.StatusCode() <= 299 {
		return nil
	}
	return pkgerrors.Errorf("Server Multipart Post Error")
}

// RestClientGetAnchor returns get data from anchor
func (r RestyClient) RestClientGetAnchor(anchor string) error {
	url, err := GetURL(anchor)
	if err != nil {
		return err
	}
	s := strings.Split(anchor, "/")
	if len(s) >= 3 {
		a := s[len(s)-2]
		// Determine if multipart
		if a == "apps" || a == "profiles" || a == "clusters" {
			// Supports only getting metadata
			resp, err := r.client.R().
				SetHeader("Accept", "application/json").
				Get(url)
			if err != nil {
				fmt.Println(err)
				return err
			}
			fmt.Println("URL:", anchor, "Response Code:", resp.StatusCode(), "Response:", resp)
			return nil
		}
	}
	resp, err := r.client.R().
		Get(url)
	if err != nil {
		fmt.Println(err)
		return err
	}
	fmt.Println("URL:", anchor, "Response Code:", resp.StatusCode(), "Response:", resp)
	return nil
}

// RestClientGet gets resource
func (r RestyClient) RestClientGet(anchor string, body []byte) error {
	if anchor == "" {
		return pkgerrors.Errorf("Anchor can't be empty")
	}
	s := strings.Split(anchor, "/")
	a := s[len(s)-1]
	if a == "instantiate" || a == "apply" || a == "approve" || a == "terminate" {
		// No get for these
		return nil
	}
	var e emcoBody
	err := json.Unmarshal(body, &e)
	if err != nil {
		fmt.Println(err)
		return err
	}
	if e.Meta.Name != "" {
		name := e.Meta.Name
		anchor = anchor + "/" + name
		if a == "composite-apps" {
			var cav emcoCompositeAppSpec
			err := mapstructure.Decode(e.Spec, &cav)
			if err != nil {
				fmt.Println("mapstruct error")
				return err
			}
			anchor = anchor + "/" + cav.Version
		}
	} else if e.Label != "" {
		anchor = anchor + "/" + e.Label
	}

	return r.RestClientGetAnchor(anchor)
}

// RestClientDeleteAnchor returns all resource in the input file
func (r RestyClient) RestClientDeleteAnchor(anchor string) error {
	url, err := GetURL(anchor)
	if err != nil {
		return err
	}
	resp, err := r.client.R().Delete(url)
	if err != nil {
		fmt.Println(err)
		return err
	}
	fmt.Println("URL:", anchor, "Response Code:", resp.StatusCode(), "Response:", resp)
	return nil
}

// RestClientDelete calls rest delete command
func (r RestyClient) RestClientDelete(anchor string, body []byte) error {

	s := strings.Split(anchor, "/")
	a := s[len(s)-1]
	if a == "instantiate" {
		// Change instantiate to destroy
		s[len(s)-1] = "terminate"
		anchor = strings.Join(s[:], "/")
		return r.RestClientPost(anchor, []byte{})
	} else if a == "apply" {
		// Change apply to terminate
		s[len(s)-1] = "terminate"
		anchor = strings.Join(s[:], "/")
		return r.RestClientPost(anchor, []byte{})
	} else if a == "approve" || a == "status" {
		// Approve and status  doesn't have delete
		return nil
	}
	var e emcoBody
	err := json.Unmarshal(body, &e)
	if err != nil {
		fmt.Println(err)
		return err
	}
	if e.Meta.Name != "" {
		s := strings.Split(anchor, "/")
		a := s[len(s)-1]
		name := e.Meta.Name
		anchor = anchor + "/" + name
		if a == "composite-apps" {
			var cav emcoCompositeAppSpec
			err := mapstructure.Decode(e.Spec, &cav)
			if err != nil {
				fmt.Println("mapstruct error")
				return err
			}
			anchor = anchor + "/" + cav.Version
		}
	} else if e.Label != "" {
		anchor = anchor + "/" + e.Label
	}
	return r.RestClientDeleteAnchor(anchor)
}

// GetURL reads the configuration file to get URL
func GetURL(anchor string) (string, error) {
	var baseUrl string
	s := strings.Split(anchor, "/")
	if len(s) < 1 {
		return "", fmt.Errorf("Invalid Anchor")
	}

	switch s[0] {
	case "cluster-providers":
		if len(s) >= 5 && (s[4] == "networks" || s[4] == "provider-networks" || s[4] == "apply" || s[4] == "terminate") {
			baseUrl = GetNcmURL()
		} else {
			baseUrl = GetClmURL()
		}
	case "controllers":
		baseUrl = GetOrchestratorURL()
	case "projects":
		if len(s) >= 3 && s[2] == "logical-clouds" {
			baseUrl = GetDcmURL()
			break
		}
		if len(s) >= 6 && s[5] == "network-controller-intent" {
			baseUrl = GetOvnactionURL()
			break
		}
		// All other paths go to Orchestrator
		baseUrl = GetOrchestratorURL()
	default:
		return "", fmt.Errorf("Invalid Anchor")
	}
	fmt.Printf(baseUrl)
	return (baseUrl + "/" + anchor), nil
}