aboutsummaryrefslogtreecommitdiffstats
path: root/src/tools/emcoui/middle_end/app/compositeapp.go
blob: daae5b002981aabe550d2cae729e036a1b5f1c12 (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
/*
=======================================================================
Copyright (c) 2017-2020 Aarna Networks, Inc.
All rights reserved.
======================================================================
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 app

import (
	"encoding/json"
	"fmt"
)

// CompositeApp application structure
type CompositeApp struct {
	Metadata apiMetaData      `json:"metadata"`
	Spec     compositeAppSpec `json:"spec"`
}

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

// compAppHandler , This implements the orchworkflow interface
type compAppHandler struct {
	orchURL      string
	orchInstance *OrchestrationHandler
}

// CompositeAppKey is the mongo key to fetch apps in a composite app
type CompositeAppKey struct {
	Cname    string      `json:"compositeapp"`
	Project  string      `json:"project"`
	Cversion string      `json:"compositeappversion"`
	App      interface{} `json:"app"`
}

func (h *compAppHandler) getObject() (interface{}, interface{}) {
	orch := h.orchInstance
	respcode := 200
	dataRead := h.orchInstance.dataRead
	for _, compositeAppValue := range dataRead.compositeAppMap {
		compositeAppMetadata := compositeAppValue.Metadata.Metadata
		compositeAppSpec := compositeAppValue.Metadata.Spec
		h.orchURL = "http://" + orch.MiddleendConf.OrchService + "/v2/projects/" +
			orch.projectName + "/composite-apps/" + compositeAppMetadata.Name +
			"/" + compositeAppSpec.Version + "/apps"

		respcode, respdata, err := orch.apiGet(h.orchURL, orch.compositeAppName+"_getapps")
		if err != nil {
			return nil, 500
		}
		if respcode != 200 {
			return nil, respcode
		}
		fmt.Printf("Get app status %s\n", respcode)
		compositeAppValue.AppsDataArray = make(map[string]*AppsData, len(respdata))
		var appList []CompositeApp
		json.Unmarshal(respdata, &appList)
		for _, value := range appList {
			var appsDataInstance AppsData
			appName := value.Metadata.Name
			appsDataInstance.App = value
			compositeAppValue.AppsDataArray[appName] = &appsDataInstance
		}
	}
	return nil, respcode
}

func (h *compAppHandler) getAnchor() (interface{}, interface{}) {
	orch := h.orchInstance
	dataRead := h.orchInstance.dataRead
	respcode := 200
	for _, compositeAppValue := range dataRead.compositeAppMap {
		compositeAppMetadata := compositeAppValue.Metadata.Metadata
		compositeAppSpec := compositeAppValue.Metadata.Spec
		h.orchURL = "http://" + orch.MiddleendConf.OrchService + "/v2/projects/" +
			orch.projectName + "/composite-apps/" + compositeAppMetadata.Name +
			"/" + compositeAppSpec.Version

		respcode, _, err := orch.apiGet(h.orchURL, orch.compositeAppName+"_getcompositeapp")
		if err != nil {
			return nil, 500
		}
		if respcode != 200 {
			return nil, respcode
		}
		fmt.Printf("Get composite App %s\n", respcode)
		//json.Unmarshal(respdata, &dataRead.CompositeApp)
	}
	return nil, respcode
}

func (h *compAppHandler) deleteObject() interface{} {
	orch := h.orchInstance
	dataRead := h.orchInstance.dataRead
	for _, compositeAppValue := range dataRead.compositeAppMap {
		compositeAppMetadata := compositeAppValue.Metadata.Metadata
		compositeAppSpec := compositeAppValue.Metadata.Spec
		h.orchURL = "http://" + orch.MiddleendConf.OrchService + "/v2/projects/" +
			orch.projectName + "/composite-apps/" + compositeAppMetadata.Name +
			"/" + compositeAppSpec.Version
		appList := compositeAppValue.AppsDataArray
		for _, value := range appList {
			url := h.orchURL + "/apps/" + value.App.Metadata.Name
			fmt.Printf("Delete app %s\n", url)
			resp, err := orch.apiDel(url, compositeAppMetadata.Name+"_delapp")
			if err != nil {
				return err
			}
			if resp != 204 {
				return resp
			}
			fmt.Printf("Delete app status %s\n", resp)
		}
	}
	return nil
}

func (h *compAppHandler) deleteAnchor() interface{} {
	orch := h.orchInstance
	dataRead := h.orchInstance.dataRead
	for _, compositeAppValue := range dataRead.compositeAppMap {
		compositeAppMetadata := compositeAppValue.Metadata.Metadata
		compositeAppSpec := compositeAppValue.Metadata.Spec
		h.orchURL = "http://" + orch.MiddleendConf.OrchService + "/v2/projects/" +
			orch.projectName + "/composite-apps/" + compositeAppMetadata.Name +
			"/" + compositeAppSpec.Version
		fmt.Printf("Delete composite app %s\n", h.orchURL)
		resp, err := orch.apiDel(h.orchURL, compositeAppMetadata.Name+"_delcompapp")
		if err != nil {
			return err
		}
		if resp != 204 {
			return resp
		}
		fmt.Printf("Delete compapp status %s\n", resp)
	}
	return nil
}

// CreateAnchor creates the anchor point for composite applications,
// profiles, intents etc. For example Anchor for the composite application
// will create the composite application resource in the the DB, and all apps
// will get created and uploaded under this anchor point.
func (h *compAppHandler) createAnchor() interface{} {
	orch := h.orchInstance

	compAppCreate := CompositeApp{
		Metadata: apiMetaData{
			Name:        orch.compositeAppName,
			Description: orch.compositeAppDesc,
			UserData1:   "data 1",
			UserData2:   "data 2"},
		Spec: compositeAppSpec{
			Version: "v1"},
	}

	jsonLoad, _ := json.Marshal(compAppCreate)
	tem := CompositeApp{}
	json.Unmarshal(jsonLoad, &tem)
	h.orchURL = "http://" + orch.MiddleendConf.OrchService + "/v2/projects/" +
		orch.projectName + "/composite-apps"
	resp, err := orch.apiPost(jsonLoad, h.orchURL, orch.compositeAppName)
	if err != nil {
		return err
	}
	if resp != 201 {
		return resp
	}
	orch.version = "v1"
	fmt.Printf("compAppHandler resp %s\n", resp)

	return nil
}

func (h *compAppHandler) createObject() interface{} {
	orch := h.orchInstance
	for i := range orch.meta {
		fileName := orch.meta[i].Metadata.FileName
		appName := orch.meta[i].Metadata.Name
		appDesc := orch.meta[i].Metadata.Description

		// Upload the application helm chart
		fh := orch.file[fileName]
		compAppAdd := CompositeApp{
			Metadata: apiMetaData{
				Name:        appName,
				Description: appDesc,
				UserData1:   "data 1",
				UserData2:   "data2"},
		}
		url := h.orchURL + "/" + orch.compositeAppName + "/" + orch.version + "/apps"

		jsonLoad, _ := json.Marshal(compAppAdd)

		status, err := orch.apiPostMultipart(jsonLoad, fh, url, appName, fileName)
		if err != nil {
			return err
		}
		if status != 201 {
			return status
		}
		fmt.Printf("Composite app %s createObject status %s\n", appName, status)
	}

	return nil
}

func createCompositeapp(I orchWorkflow) interface{} {
	// 1. Create the Anchor point
	err := I.createAnchor()
	if err != nil {
		return err
	}
	// 2. Create the Objects
	err = I.createObject()
	if err != nil {
		return err
	}
	return nil
}

func delCompositeapp(I orchWorkflow) interface{} {
	// 1. Delete the object
	err := I.deleteObject()
	if err != nil {
		return err
	}
	// 2. Delete the Anchor
	err = I.deleteAnchor()
	if err != nil {
		return err
	}
	return nil
}