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 governing 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 // 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"`
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"`
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"`
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)
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
72 // CompositeProfileClient implements the Manager
73 // It will also be used to maintain some localized state
74 type CompositeProfileClient struct {
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",
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) {
92 res, err := c.GetCompositeProfile(cpf.Metadata.Name, p, ca, v)
93 if res != (CompositeProfile{}) {
94 return CompositeProfile{}, pkgerrors.New("CompositeProfile already exists")
97 //Check if project exists
98 _, err = NewProjectClient().GetProject(p)
100 return CompositeProfile{}, pkgerrors.New("Unable to find the project")
103 // check if compositeApp exists
104 _, err = NewCompositeAppClient().GetCompositeApp(ca, v, p)
106 return CompositeProfile{}, pkgerrors.New("Unable to find the composite-app")
109 cProfkey := CompositeProfileKey{
110 Name: cpf.Metadata.Name,
116 err = db.DBconn.Insert(c.storeName, cProfkey, nil, c.tagMeta, cpf)
118 return CompositeProfile{}, pkgerrors.Wrap(err, "Create DB entry error")
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{
133 result, err := db.DBconn.Find(c.storeName, key, c.tagMeta)
135 return CompositeProfile{}, pkgerrors.Wrap(err, "Get Composite Profile error")
139 cProf := CompositeProfile{}
140 err = db.DBconn.Unmarshal(result[0], &cProf)
142 return CompositeProfile{}, pkgerrors.Wrap(err, "Unmarshalling CompositeProfile")
147 return CompositeProfile{}, pkgerrors.New("Error getting CompositeProfile")
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{
159 values, err := db.DBconn.Find(c.storeName, key, c.tagMeta)
161 return []CompositeProfile{}, pkgerrors.Wrap(err, "Get Composite Profiles error")
164 var resp []CompositeProfile
166 for _, value := range values {
167 cp := CompositeProfile{}
168 err = db.DBconn.Unmarshal(value, &cp)
170 return []CompositeProfile{}, pkgerrors.Wrap(err, "Get Composite Profiles unmarshalling error")
172 resp = append(resp, cp)
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{
187 err := db.DBconn.Remove(c.storeName, key)
189 return pkgerrors.Wrap(err, "Delete CompositeProfile entry;")