2 * Copyright 2020 Intel Corporation, Inc
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governinog permissions and
14 * limitations under the License.
22 "github.com/onap/multicloud-k8s/src/orchestrator/pkg/infra/db"
24 pkgerrors "github.com/pkg/errors"
27 // App contains metadata for Apps
29 Metadata AppMetaData `json:"metadata"`
32 //AppMetaData contains the parameters needed for Apps
33 type AppMetaData struct {
34 Name string `json:"name"`
35 Description string `json:"description"`
36 UserData1 string `json:"userData1"`
37 UserData2 string `json:"userData2"`
40 //AppContent contains fileContent
41 type AppContent struct {
45 // AppKey is the key structure that is used in the database
47 App string `json:"app"`
48 Project string `json:"project"`
49 CompositeApp string `json:"compositeapp"`
50 CompositeAppVersion string `json:"compositeappversion"`
53 // We will use json marshalling to convert to string to
54 // preserve the underlying structure.
55 func (aK AppKey) String() string {
56 out, err := json.Marshal(aK)
63 // AppManager is an interface exposes the App functionality
64 type AppManager interface {
65 CreateApp(a App, ac AppContent, p string, cN string, cV string) (App, error)
66 GetApp(name string, p string, cN string, cV string) (App, error)
67 GetAppContent(name string, p string, cN string, cV string) (AppContent, error)
68 GetApps(p string, cN string, cV string) ([]App, error)
69 DeleteApp(name string, p string, cN string, cV string) error
72 // AppClient implements the AppManager
73 // It will also be used to maintain some localized state
74 type AppClient struct {
76 tagMeta, tagContent string
79 // NewAppClient returns an instance of the AppClient
80 // which implements the AppManager
81 func NewAppClient() *AppClient {
83 storeName: "orchestrator",
84 tagMeta: "appmetadata",
85 tagContent: "appcontent",
89 // CreateApp creates a new collection based on the App
90 func (v *AppClient) CreateApp(a App, ac AppContent, p string, cN string, cV string) (App, error) {
92 //Construct the composite key to select the entry
97 CompositeAppVersion: cV,
100 //Check if this App already exists
101 _, err := v.GetApp(a.Metadata.Name, p, cN, cV)
103 return App{}, pkgerrors.New("App already exists")
106 //Check if Project exists
107 _, err = NewProjectClient().GetProject(p)
109 return App{}, pkgerrors.New("Unable to find the project")
112 //check if CompositeApp with version exists
113 _, err = NewCompositeAppClient().GetCompositeApp(cN, cV, p)
115 return App{}, pkgerrors.New("Unable to find the composite app with version")
118 err = db.DBconn.Insert(v.storeName, key, nil, v.tagMeta, a)
120 return App{}, pkgerrors.Wrap(err, "Creating DB Entry")
123 err = db.DBconn.Insert(v.storeName, key, nil, v.tagContent, ac)
125 return App{}, pkgerrors.Wrap(err, "Creating DB Entry")
131 // GetApp returns the App for corresponding name
132 func (v *AppClient) GetApp(name string, p string, cN string, cV string) (App, error) {
134 //Construct the composite key to select the entry
139 CompositeAppVersion: cV,
141 value, err := db.DBconn.Find(v.storeName, key, v.tagMeta)
143 return App{}, pkgerrors.Wrap(err, "Get app")
146 //value is a byte array
149 err = db.DBconn.Unmarshal(value[0], &app)
151 return App{}, pkgerrors.Wrap(err, "Unmarshaling Value")
156 return App{}, pkgerrors.New("Error getting app")
159 // GetAppContent returns content for corresponding app
160 func (v *AppClient) GetAppContent(name string, p string, cN string, cV string) (AppContent, error) {
162 //Construct the composite key to select the entry
167 CompositeAppVersion: cV,
169 value, err := db.DBconn.Find(v.storeName, key, v.tagContent)
171 return AppContent{}, pkgerrors.Wrap(err, "Get app content")
174 //value is a byte array
177 err = db.DBconn.Unmarshal(value[0], &ac)
179 return AppContent{}, pkgerrors.Wrap(err, "Unmarshaling Value")
184 return AppContent{}, pkgerrors.New("Error getting app content")
187 // GetApps returns all Apps for given composite App
188 func (v *AppClient) GetApps(project, compositeApp, compositeAppVersion string) ([]App, error) {
193 CompositeApp: compositeApp,
194 CompositeAppVersion: compositeAppVersion,
198 values, err := db.DBconn.Find(v.storeName, key, v.tagMeta)
200 return []App{}, pkgerrors.Wrap(err, "Get Apps")
203 for _, value := range values {
205 err = db.DBconn.Unmarshal(value, &a)
207 return []App{}, pkgerrors.Wrap(err, "Unmarshaling Value")
209 resp = append(resp, a)
215 // DeleteApp deletes the App from database
216 func (v *AppClient) DeleteApp(name string, p string, cN string, cV string) error {
218 //Construct the composite key to select the entry
223 CompositeAppVersion: cV,
225 err := db.DBconn.Remove(v.storeName, key)
227 return pkgerrors.Wrap(err, "Delete App Entry;")