aboutsummaryrefslogtreecommitdiffstats
path: root/src/orchestrator/pkg/module/generic_placement_intent.go
blob: 8e739fc8fd61d4d1b8e56cccf39d9a2f578d34a1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/*
 * Copyright 2020 Intel Corporation, Inc
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package module

import (
	"encoding/json"
	"github.com/onap/multicloud-k8s/src/orchestrator/pkg/infra/db"

	pkgerrors "github.com/pkg/errors"
)

// GenericPlacementIntent shall have 2 fields - metadata and spec
type GenericPlacementIntent struct {
	MetaData GenIntentMetaData `json:"metadata"`
	Spec     GenIntentSpecData `json:"spec"`
}

// GenIntentMetaData has name, description, userdata1, userdata2
type GenIntentMetaData struct {
	Name        string `json:"name"`
	Description string `json:"description"`
	UserData1   string `json:"userData1"`
	UserData2   string `json:"userData2"`
}

// GenIntentSpecData has logical-cloud-name
type GenIntentSpecData struct {
	LogicalCloud string `json:"logical-cloud"`
}

// GenericPlacementIntentManager is an interface which exposes the GenericPlacementIntentManager functionality
type GenericPlacementIntentManager interface {
	CreateGenericPlacementIntent(g GenericPlacementIntent, p string, ca string,
		v string) (GenericPlacementIntent, error)
	GetGenericPlacementIntent(intentName string, projectName string,
		compositeAppName string, version string) (GenericPlacementIntent, error)
	DeleteGenericPlacementIntent(intentName string, projectName string,
		compositeAppName string, version string) error
}

// GenericPlacementIntentKey is used as the primary key
type GenericPlacementIntentKey struct {
	Name         string `json:"name"`
	Project      string `json:"project"`
	CompositeApp string `json:"compositeapp"`
	Version      string `json:"version"`
}

// We will use json marshalling to convert to string to
// preserve the underlying structure.
func (gk GenericPlacementIntentKey) String() string {
	out, err := json.Marshal(gk)
	if err != nil {
		return ""
	}
	return string(out)
}

// GenericPlacementIntentClient implements the GenericPlacementIntentManager interface
type GenericPlacementIntentClient struct {
	storeName   string
	tagMetaData string
}

// NewGenericPlacementIntentClient return an instance of GenericPlacementIntentClient which implements GenericPlacementIntentManager
func NewGenericPlacementIntentClient() *GenericPlacementIntentClient {
	return &GenericPlacementIntentClient{
		storeName:   "orchestrator",
		tagMetaData: "genericplacementintent",
	}
}

// CreateGenericPlacementIntent creates an entry for GenericPlacementIntent in the database. Other Input parameters for it - projectName, compositeAppName, version
func (c *GenericPlacementIntentClient) CreateGenericPlacementIntent(g GenericPlacementIntent, p string, ca string,
	v string) (GenericPlacementIntent, error) {

	// check if the genericPlacement already exists.
	res, err := c.GetGenericPlacementIntent(g.MetaData.Name, p, ca, v)
	if res != (GenericPlacementIntent{}) {
		return GenericPlacementIntent{}, pkgerrors.New("Intent already exists")
	}

	//Check if project exists
	_, err = NewProjectClient().GetProject(p)
	if err != nil {
		return GenericPlacementIntent{}, pkgerrors.New("Unable to find the project")
	}

	// check if compositeApp exists
	_, err = NewCompositeAppClient().GetCompositeApp(ca, v, p)
	if err != nil {
		return GenericPlacementIntent{}, pkgerrors.New("Unable to find the composite-app")
	}

	gkey := GenericPlacementIntentKey{
		Name:         g.MetaData.Name,
		Project:      p,
		CompositeApp: ca,
		Version:      v,
	}

	err = db.DBconn.Insert(c.storeName, gkey, nil, c.tagMetaData, g)
	if err != nil {
		return GenericPlacementIntent{}, pkgerrors.Wrap(err, "Create DB entry error")
	}

	return g, nil
}

// GetGenericPlacementIntent shall take arguments - name of the intent, name of the project, name of the composite app and version of the composite app. It shall return the genericPlacementIntent if its present.
func (c *GenericPlacementIntentClient) GetGenericPlacementIntent(i string, p string, ca string, v string) (GenericPlacementIntent, error) {
	key := GenericPlacementIntentKey{
		Name:         i,
		Project:      p,
		CompositeApp: ca,
		Version:      v,
	}

	result, err := db.DBconn.Find(c.storeName, key, c.tagMetaData)
	if err != nil {
		return GenericPlacementIntent{}, pkgerrors.Wrap(err, "Get Intent error")
	}

	if result != nil {
		g := GenericPlacementIntent{}
		err = db.DBconn.Unmarshal(result[0], &g)
		if err != nil {
			return GenericPlacementIntent{}, pkgerrors.Wrap(err, "Unmarshalling GenericPlacement Intent")
		}
		return g, nil
	}

	return GenericPlacementIntent{}, pkgerrors.New("Error getting GenericPlacementIntent")

}

// DeleteGenericPlacementIntent the intent from the database
func (c *GenericPlacementIntentClient) DeleteGenericPlacementIntent(i string, p string, ca string, v string) error {
	key := GenericPlacementIntentKey{
		Name:         i,
		Project:      p,
		CompositeApp: ca,
		Version:      v,
	}

	err := db.DBconn.Remove(c.storeName, key)
	if err != nil {
		return pkgerrors.Wrap(err, "Delete Project entry;")
	}
	return nil
}