aboutsummaryrefslogtreecommitdiffstats
path: root/src/tools/emcoui/middle_end/app/profile.go
blob: fff43cdc3814a37bc87e971dbe63e2e61ab3df8a (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
/*
=======================================================================
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"
	"log"
)

// ProfileData captures per app profile
type ProfileData struct {
	Name        string            `json:"profileName"`
	AppProfiles map[string]string `json:"appProfile"`
}

// ProfileMeta is metadta for the profile APIs
type ProfileMeta struct {
	Metadata apiMetaData `json:"metadata"`
	Spec     ProfileSpec `json:"spec"`
}

// ProfileSpec is the spec for the profile APIs
type ProfileSpec struct {
	AppName string `json:"app-name"`
}

// ProfileHandler This implements the orchworkflow interface
type ProfileHandler struct {
	orchURL      string
	orchInstance *OrchestrationHandler
	response     struct {
		payload map[string][]byte
		status  map[string]string
	}
}

func (h *ProfileHandler) getObject() (interface{}, interface{}) {
	orch := h.orchInstance
	dataRead := h.orchInstance.dataRead
	retcode := 200
	for _, compositeAppValue := range dataRead.compositeAppMap {
		var profileList []ProfileMeta
		compositeAppMetadata := compositeAppValue.Metadata.Metadata
		compositeAppSpec := compositeAppValue.Metadata.Spec
		h.orchURL = "http://" + orch.MiddleendConf.OrchService + "/v2/projects/" +
			orch.projectName + "/composite-apps/" + compositeAppMetadata.Name +
			"/" + compositeAppSpec.Version + "/composite-profiles"
		for profileName, profileValue := range compositeAppValue.ProfileDataArray {
			url := h.orchURL + "/" + profileName + "/profiles"
			retcode, respval, err := orch.apiGet(url, compositeAppMetadata.Name+"_getprofiles")
			fmt.Printf("Get app profiles status %d\n", retcode)
			if err != nil {
				fmt.Printf("Failed to read profile %s\n", profileName)
				return nil, 500
			}
			if retcode != 200 {
				fmt.Printf("Failed to read profile %s\n", profileName)
				return nil, retcode
			}
			json.Unmarshal(respval, &profileList)
			profileValue.AppProfiles = make([]ProfileMeta, len(profileList))
			for appProfileIndex, appProfile := range profileList {
				profileValue.AppProfiles[appProfileIndex] = appProfile
			}
		}
	}
	return nil, retcode
}

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

		respcode, respdata, err := orch.apiGet(h.orchURL, compositeAppMetadata.Name+"_getcprofile")
		if err != nil {
			fmt.Printf("Failed to get composite profiles\n")
			return nil, 500
		}
		if respcode != 200 {
			fmt.Printf("composite profile GET status %d\n", respcode)
			return nil, respcode
		}
		json.Unmarshal(respdata, &profilemetaList)
		compositeAppValue.ProfileDataArray = make(map[string]*ProfilesData, len(profilemetaList))
		for _, value := range profilemetaList {
			ProfilesDataInstance := ProfilesData{}
			ProfilesDataInstance.Profile = value
			compositeAppValue.ProfileDataArray[value.Metadata.Name] = &ProfilesDataInstance
		}
	}
	return nil, respcode
}

func (h *ProfileHandler) 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 + "/composite-profiles/"
		for profileName, profileValue := range compositeAppValue.ProfileDataArray {
			for _, appProfileValue := range profileValue.AppProfiles {
				url := h.orchURL + profileName + "/profiles/" + appProfileValue.Metadata.Name

				fmt.Printf("Delete app profiles %s\n", url)
				resp, err := orch.apiDel(url, compositeAppMetadata.Name+"_delappProfiles")
				if err != nil {
					return err
				}
				if resp != 204 {
					return resp
				}
				fmt.Printf("Delete profiles status %s\n", resp)
			}
		}
	}
	return nil
}

func (h *ProfileHandler) 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 + "/composite-profiles/"

		for profileName, _ := range compositeAppValue.ProfileDataArray {
			url := h.orchURL + profileName
			fmt.Printf("Delete profile %s\n", url)
			resp, err := orch.apiDel(url, compositeAppMetadata.Name+"_delProfile")
			if err != nil {
				return err
			}
			if resp != 204 {
				return resp
			}
			fmt.Printf("Delete profile status %s\n", resp)
		}
	}
	return nil
}

func (h *ProfileHandler) createAnchor() interface{} {
	orch := h.orchInstance

	profileCreate := ProfileMeta{
		Metadata: apiMetaData{
			Name:        orch.compositeAppName + "_profile",
			Description: "Profile created from middleend",
			UserData1:   "data 1",
			UserData2:   "data2"},
	}
	jsonLoad, _ := json.Marshal(profileCreate)
	h.orchURL = "http://" + orch.MiddleendConf.OrchService + "/v2/projects/" +
		orch.projectName + "/composite-apps"
	url := h.orchURL + "/" + orch.compositeAppName + "/" + "v1" + "/composite-profiles"
	resp, err := orch.apiPost(jsonLoad, url, orch.compositeAppName+"_profile")
	if err != nil {
		return err
	}
	if resp != 201 {
		return resp
	}
	fmt.Printf("ProfileHandler resp %s\n", resp)

	return nil
}

func (h *ProfileHandler) createObject() interface{} {
	orch := h.orchInstance

	for i := range orch.meta {
		fileName := orch.meta[i].ProfileMetadata.FileName
		appName := orch.meta[i].Metadata.Name
		profileName := orch.meta[i].Metadata.Name + "_profile"

		// Upload the application helm chart
		fh := orch.file[fileName]
		profileAdd := ProfileMeta{
			Metadata: apiMetaData{
				Name:        profileName,
				Description: "NA",
				UserData1:   "data 1",
				UserData2:   "data2"},
			Spec: ProfileSpec{
				AppName: appName},
		}
		compositeProfilename := orch.compositeAppName + "_profile"

		url := h.orchURL + "/" + orch.compositeAppName + "/" + "v1" + "/" +
			"composite-profiles" + "/" + compositeProfilename + "/profiles"
		jsonLoad, _ := json.Marshal(profileAdd)
		status, err := orch.apiPostMultipart(jsonLoad, fh, url, profileName, fileName)
		if err != nil {
			log.Fatalln(err)
		}
		if status != 201 {
			return status
		}
		fmt.Printf("CompositeProfile Profile  %s status %s %s\n", profileName, status, url)
	}

	return nil
}

func createProfile(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 delProfileData(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
}