Move controller module into separate package
[multicloud/k8s.git] / src / orchestrator / pkg / module / deployment_intent_groups.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         "github.com/onap/multicloud-k8s/src/orchestrator/pkg/infra/db"
22         "reflect"
23
24         pkgerrors "github.com/pkg/errors"
25 )
26
27 // DeploymentIntentGroup shall have 2 fields - MetaData and Spec
28 type DeploymentIntentGroup struct {
29         MetaData DepMetaData `json:"metadata"`
30         Spec     DepSpecData `json:"spec"`
31 }
32
33 // DepMetaData has Name, description, userdata1, userdata2
34 type DepMetaData struct {
35         Name        string `json:"name"`
36         Description string `json:"description"`
37         UserData1   string `json:"userData1"`
38         UserData2   string `json:"userData2"`
39 }
40
41 // DepSpecData has profile, version, OverrideValuesObj
42 type DepSpecData struct {
43         Profile           string           `json:"profile"`
44         Version           string           `json:"version"`
45         OverrideValuesObj []OverrideValues `json:"override-values"`
46 }
47
48 // OverrideValues has appName and ValuesObj
49 type OverrideValues struct {
50         AppName   string            `json:"app-name"`
51         ValuesObj map[string]string `json:"values"`
52 }
53
54 // Values has ImageRepository
55 // type Values struct {
56 //      ImageRepository string `json:"imageRepository"`
57 // }
58
59 // DeploymentIntentGroupManager is an interface which exposes the DeploymentIntentGroupManager functionality
60 type DeploymentIntentGroupManager interface {
61         CreateDeploymentIntentGroup(d DeploymentIntentGroup, p string, ca string, v string) (DeploymentIntentGroup, error)
62         GetDeploymentIntentGroup(di string, p string, ca string, v string) (DeploymentIntentGroup, error)
63         DeleteDeploymentIntentGroup(di string, p string, ca string, v string) error
64 }
65
66 // DeploymentIntentGroupKey consists of Name of the deployment group, project name, CompositeApp name, CompositeApp version
67 type DeploymentIntentGroupKey struct {
68         Name         string `json:"deploymentintentgroup"`
69         Project      string `json:"project"`
70         CompositeApp string `json:"compositeapp"`
71         Version      string `json:"compositeappversion"`
72 }
73
74 // We will use json marshalling to convert to string to
75 // preserve the underlying structure.
76 func (dk DeploymentIntentGroupKey) String() string {
77         out, err := json.Marshal(dk)
78         if err != nil {
79                 return ""
80         }
81         return string(out)
82 }
83
84 // DeploymentIntentGroupClient implements the DeploymentIntentGroupManager interface
85 type DeploymentIntentGroupClient struct {
86         storeName   string
87         tagMetaData string
88 }
89
90 // NewDeploymentIntentGroupClient return an instance of DeploymentIntentGroupClient which implements DeploymentIntentGroupManager
91 func NewDeploymentIntentGroupClient() *DeploymentIntentGroupClient {
92         return &DeploymentIntentGroupClient{
93                 storeName:   "orchestrator",
94                 tagMetaData: "deploymentintentgroupmetadata",
95         }
96 }
97
98 // CreateDeploymentIntentGroup creates an entry for a given  DeploymentIntentGroup in the database. Other Input parameters for it - projectName, compositeAppName, version
99 func (c *DeploymentIntentGroupClient) CreateDeploymentIntentGroup(d DeploymentIntentGroup, p string, ca string,
100         v string) (DeploymentIntentGroup, error) {
101
102         res, err := c.GetDeploymentIntentGroup(d.MetaData.Name, p, ca, v)
103         if !reflect.DeepEqual(res, DeploymentIntentGroup{}) {
104                 return DeploymentIntentGroup{}, pkgerrors.New("AppIntent already exists")
105         }
106
107         //Check if project exists
108         _, err = NewProjectClient().GetProject(p)
109         if err != nil {
110                 return DeploymentIntentGroup{}, pkgerrors.New("Unable to find the project")
111         }
112
113         //check if compositeApp exists
114         _, err = NewCompositeAppClient().GetCompositeApp(ca, v, p)
115         if err != nil {
116                 return DeploymentIntentGroup{}, pkgerrors.New("Unable to find the composite-app")
117         }
118
119         gkey := DeploymentIntentGroupKey{
120                 Name:         d.MetaData.Name,
121                 Project:      p,
122                 CompositeApp: ca,
123                 Version:      v,
124         }
125
126         err = db.DBconn.Insert(c.storeName, gkey, nil, c.tagMetaData, d)
127         if err != nil {
128                 return DeploymentIntentGroup{}, pkgerrors.Wrap(err, "Create DB entry error")
129         }
130
131         return d, nil
132 }
133
134 // GetDeploymentIntentGroup returns the DeploymentIntentGroup with a given name, project, compositeApp and version of compositeApp
135 func (c *DeploymentIntentGroupClient) GetDeploymentIntentGroup(di string, p string, ca string, v string) (DeploymentIntentGroup, error) {
136
137         key := DeploymentIntentGroupKey{
138                 Name:         di,
139                 Project:      p,
140                 CompositeApp: ca,
141                 Version:      v,
142         }
143
144         result, err := db.DBconn.Find(c.storeName, key, c.tagMetaData)
145         if err != nil {
146                 return DeploymentIntentGroup{}, pkgerrors.Wrap(err, "Get DeploymentIntentGroup error")
147         }
148
149         if result != nil {
150                 d := DeploymentIntentGroup{}
151                 err = db.DBconn.Unmarshal(result[0], &d)
152                 if err != nil {
153                         return DeploymentIntentGroup{}, pkgerrors.Wrap(err, "Unmarshalling DeploymentIntentGroup")
154                 }
155                 return d, nil
156         }
157
158         return DeploymentIntentGroup{}, pkgerrors.New("Error getting DeploymentIntentGroup")
159
160 }
161
162 // DeleteDeploymentIntentGroup deletes a DeploymentIntentGroup
163 func (c *DeploymentIntentGroupClient) DeleteDeploymentIntentGroup(di string, p string, ca string, v string) error {
164         k := DeploymentIntentGroupKey{
165                 Name:         di,
166                 Project:      p,
167                 CompositeApp: ca,
168                 Version:      v,
169         }
170
171         err := db.DBconn.Remove(c.storeName, k)
172         if err != nil {
173                 return pkgerrors.Wrap(err, "Delete DeploymentIntentGroup entry;")
174         }
175         return nil
176
177 }