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.
21 "github.com/onap/multicloud-k8s/src/orchestrator/pkg/infra/db"
24 pkgerrors "github.com/pkg/errors"
27 // DeploymentIntentGroup shall have 2 fields - MetaData and Spec
28 type DeploymentIntentGroup struct {
29 MetaData DepMetaData `json:"metadata"`
30 Spec DepSpecData `json:"spec"`
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"`
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"`
48 // OverrideValues has appName and ValuesObj
49 type OverrideValues struct {
50 AppName string `json:"app-name"`
51 ValuesObj map[string]string `json:"values"`
54 // Values has ImageRepository
55 // type Values struct {
56 // ImageRepository string `json:"imageRepository"`
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
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"`
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)
84 // DeploymentIntentGroupClient implements the DeploymentIntentGroupManager interface
85 type DeploymentIntentGroupClient struct {
90 // NewDeploymentIntentGroupClient return an instance of DeploymentIntentGroupClient which implements DeploymentIntentGroupManager
91 func NewDeploymentIntentGroupClient() *DeploymentIntentGroupClient {
92 return &DeploymentIntentGroupClient{
93 storeName: "orchestrator",
94 tagMetaData: "deploymentintentgroupmetadata",
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) {
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")
107 //Check if project exists
108 _, err = NewProjectClient().GetProject(p)
110 return DeploymentIntentGroup{}, pkgerrors.New("Unable to find the project")
113 //check if compositeApp exists
114 _, err = NewCompositeAppClient().GetCompositeApp(ca, v, p)
116 return DeploymentIntentGroup{}, pkgerrors.New("Unable to find the composite-app")
119 gkey := DeploymentIntentGroupKey{
120 Name: d.MetaData.Name,
126 err = db.DBconn.Insert(c.storeName, gkey, nil, c.tagMetaData, d)
128 return DeploymentIntentGroup{}, pkgerrors.Wrap(err, "Create DB entry error")
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) {
137 key := DeploymentIntentGroupKey{
144 result, err := db.DBconn.Find(c.storeName, key, c.tagMetaData)
146 return DeploymentIntentGroup{}, pkgerrors.Wrap(err, "Get DeploymentIntentGroup error")
150 d := DeploymentIntentGroup{}
151 err = db.DBconn.Unmarshal(result[0], &d)
153 return DeploymentIntentGroup{}, pkgerrors.Wrap(err, "Unmarshalling DeploymentIntentGroup")
158 return DeploymentIntentGroup{}, pkgerrors.New("Error getting DeploymentIntentGroup")
162 // DeleteDeploymentIntentGroup deletes a DeploymentIntentGroup
163 func (c *DeploymentIntentGroupClient) DeleteDeploymentIntentGroup(di string, p string, ca string, v string) error {
164 k := DeploymentIntentGroupKey{
171 err := db.DBconn.Remove(c.storeName, k)
173 return pkgerrors.Wrap(err, "Delete DeploymentIntentGroup entry;")