add python compatibility module
[multicloud/k8s.git] / src / orchestrator / pkg / module / composite_profile.go
1 /*
2  * Copyright 2020 Intel Corporation, Inc
3  *
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
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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 governing permissions and
14  * limitations under the License.
15  */
16
17 package module
18
19 import (
20         "encoding/json"
21
22         "github.com/onap/multicloud-k8s/src/orchestrator/pkg/infra/db"
23
24         pkgerrors "github.com/pkg/errors"
25 )
26
27 // CompositeProfile contains the parameters needed for CompositeProfiles
28 // It implements the interface for managing the CompositeProfiles
29 type CompositeProfile struct {
30         Metadata CompositeProfileMetadata `json:"metadata"`
31 }
32
33 // CompositeProfileMetadata contains the metadata for CompositeProfiles
34 type CompositeProfileMetadata struct {
35         Name        string `json:"name"`
36         Description string `json:"description"`
37         UserData1   string `json:"userData1"`
38         UserData2   string `json:"userData2"`
39 }
40
41 // CompositeProfileKey is the key structure that is used in the database
42 type CompositeProfileKey struct {
43         Name         string `json:"compositeprofile"`
44         Project      string `json:"project"`
45         CompositeApp string `json:"compositeapp"`
46         Version      string `json:"compositeappversion"`
47 }
48
49 // We will use json marshalling to convert to string to
50 // preserve the underlying structure.
51 func (cpk CompositeProfileKey) String() string {
52         out, err := json.Marshal(cpk)
53         if err != nil {
54                 return ""
55         }
56
57         return string(out)
58 }
59
60 // CompositeProfileManager exposes the CompositeProfile functionality
61 type CompositeProfileManager interface {
62         CreateCompositeProfile(cpf CompositeProfile, p string, ca string,
63                 v string) (CompositeProfile, error)
64         GetCompositeProfile(compositeProfileName string, projectName string,
65                 compositeAppName string, version string) (CompositeProfile, error)
66         GetCompositeProfiles(projectName string, compositeAppName string,
67                 version string) ([]CompositeProfile, error)
68         DeleteCompositeProfile(compositeProfileName string, projectName string,
69                 compositeAppName string, version string) error
70 }
71
72 // CompositeProfileClient implements the Manager
73 // It will also be used to maintain some localized state
74 type CompositeProfileClient struct {
75         storeName string
76         tagMeta   string
77 }
78
79 // NewCompositeProfileClient returns an instance of the CompositeProfileClient
80 // which implements the Manager
81 func NewCompositeProfileClient() *CompositeProfileClient {
82         return &CompositeProfileClient{
83                 storeName: "orchestrator",
84                 tagMeta:   "compositeprofilemetadata",
85         }
86 }
87
88 // CreateCompositeProfile creates an entry for CompositeProfile in the database. Other Input parameters for it - projectName, compositeAppName, version
89 func (c *CompositeProfileClient) CreateCompositeProfile(cpf CompositeProfile, p string, ca string,
90         v string) (CompositeProfile, error) {
91
92         res, err := c.GetCompositeProfile(cpf.Metadata.Name, p, ca, v)
93         if res != (CompositeProfile{}) {
94                 return CompositeProfile{}, pkgerrors.New("CompositeProfile already exists")
95         }
96
97         //Check if project exists
98         _, err = NewProjectClient().GetProject(p)
99         if err != nil {
100                 return CompositeProfile{}, pkgerrors.New("Unable to find the project")
101         }
102
103         // check if compositeApp exists
104         _, err = NewCompositeAppClient().GetCompositeApp(ca, v, p)
105         if err != nil {
106                 return CompositeProfile{}, pkgerrors.New("Unable to find the composite-app")
107         }
108
109         cProfkey := CompositeProfileKey{
110                 Name:         cpf.Metadata.Name,
111                 Project:      p,
112                 CompositeApp: ca,
113                 Version:      v,
114         }
115
116         err = db.DBconn.Insert(c.storeName, cProfkey, nil, c.tagMeta, cpf)
117         if err != nil {
118                 return CompositeProfile{}, pkgerrors.Wrap(err, "Create DB entry error")
119         }
120
121         return cpf, nil
122 }
123
124 // GetCompositeProfile shall take arguments - name of the composite profile, name of the project, name of the composite app and version of the composite app. It shall return the CompositeProfile if its present.
125 func (c *CompositeProfileClient) GetCompositeProfile(cpf string, p string, ca string, v string) (CompositeProfile, error) {
126         key := CompositeProfileKey{
127                 Name:         cpf,
128                 Project:      p,
129                 CompositeApp: ca,
130                 Version:      v,
131         }
132
133         result, err := db.DBconn.Find(c.storeName, key, c.tagMeta)
134         if err != nil {
135                 return CompositeProfile{}, pkgerrors.Wrap(err, "Get Composite Profile error")
136         }
137
138         if result != nil {
139                 cProf := CompositeProfile{}
140                 err = db.DBconn.Unmarshal(result[0], &cProf)
141                 if err != nil {
142                         return CompositeProfile{}, pkgerrors.Wrap(err, "Unmarshalling CompositeProfile")
143                 }
144                 return cProf, nil
145         }
146
147         return CompositeProfile{}, pkgerrors.New("Error getting CompositeProfile")
148 }
149
150 // GetCompositeProfiles shall take arguments - name of the project, name of the composite profile and version of the composite app. It shall return an array of CompositeProfile.
151 func (c *CompositeProfileClient) GetCompositeProfiles(p string, ca string, v string) ([]CompositeProfile, error) {
152         key := CompositeProfileKey{
153                 Name:         "",
154                 Project:      p,
155                 CompositeApp: ca,
156                 Version:      v,
157         }
158
159         values, err := db.DBconn.Find(c.storeName, key, c.tagMeta)
160         if err != nil {
161                 return []CompositeProfile{}, pkgerrors.Wrap(err, "Get Composite Profiles error")
162         }
163
164         var resp []CompositeProfile
165
166         for _, value := range values {
167                 cp := CompositeProfile{}
168                 err = db.DBconn.Unmarshal(value, &cp)
169                 if err != nil {
170                         return []CompositeProfile{}, pkgerrors.Wrap(err, "Get Composite Profiles unmarshalling error")
171                 }
172                 resp = append(resp, cp)
173         }
174
175         return resp, nil
176 }
177
178 // DeleteCompositeProfile deletes the compsiteApp profile from the database
179 func (c *CompositeProfileClient) DeleteCompositeProfile(cpf string, p string, ca string, v string) error {
180         key := CompositeProfileKey{
181                 Name:         cpf,
182                 Project:      p,
183                 CompositeApp: ca,
184                 Version:      v,
185         }
186
187         err := db.DBconn.Remove(c.storeName, key)
188         if err != nil {
189                 return pkgerrors.Wrap(err, "Delete CompositeProfile entry;")
190         }
191         return nil
192 }