2 * Copyright 2018 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 "k8splugin/internal/db"
25 uuid "github.com/hashicorp/go-uuid"
26 pkgerrors "github.com/pkg/errors"
29 // Profile contains the parameters needed for resource bundle (rb) profiles
30 // It implements the interface for managing the profiles
32 UUID string `json:"uuid,omitempty"`
33 RBDID string `json:"rbdid"`
34 Name string `json:"name"`
35 Namespace string `json:"namespace"`
36 KubernetesVersion string `json:"kubernetesversion"`
39 // ProfileManager is an interface exposes the resource bundle profile functionality
40 type ProfileManager interface {
41 Create(def Profile) (Profile, error)
42 List() ([]Profile, error)
43 Get(resID string) (Profile, error)
44 Help() map[string]string
45 Delete(resID string) error
46 Upload(resID string, inp []byte) error
49 // ProfileClient implements the ProfileManager
50 // It will also be used to maintain some localized state
51 type ProfileClient struct {
53 tagMeta, tagContent string
56 // NewProfileClient returns an instance of the ProfileClient
57 // which implements the ProfileManager
59 func NewProfileClient() *ProfileClient {
60 return &ProfileClient{
61 storeName: "rbprofile",
63 tagContent: "content",
67 // Help returns some information on how to create the content
68 // for the profile in the form of html formatted page
69 func (v *ProfileClient) Help() map[string]string {
70 ret := make(map[string]string)
75 // Create an entry for the resource bundle profile in the database
76 func (v *ProfileClient) Create(p Profile) (Profile, error) {
78 //Check if provided RBID is a valid resource bundle
79 _, err := NewDefinitionClient().Get(p.RBDID)
81 return Profile{}, pkgerrors.Errorf("Invalid Resource Bundle ID provided: %s", err.Error())
86 return Profile{}, pkgerrors.New("Name is required for Resource Bundle Profile")
89 // If UUID is empty, we will generate one
91 p.UUID, _ = uuid.GenerateUUID()
95 err = db.DBconn.Create(v.storeName, key, v.tagMeta, p)
97 return Profile{}, pkgerrors.Wrap(err, "Creating Profile DB Entry")
103 // List all resource entries in the database
104 func (v *ProfileClient) List() ([]Profile, error) {
105 res, err := db.DBconn.ReadAll(v.storeName, v.tagMeta)
106 if err != nil || len(res) == 0 {
107 return []Profile{}, pkgerrors.Wrap(err, "Listing Resource Bundle Profiles")
110 var retData []Profile
112 for key, value := range res {
113 //value is a byte array
116 err = db.DBconn.Unmarshal(value, &pr)
118 log.Printf("[Profile] Error Unmarshaling value for: %s", key)
121 retData = append(retData, pr)
128 // Get returns the Resource Bundle Profile for corresponding ID
129 func (v *ProfileClient) Get(id string) (Profile, error) {
130 value, err := db.DBconn.Read(v.storeName, id, v.tagMeta)
132 return Profile{}, pkgerrors.Wrap(err, "Get Resource Bundle Profile")
135 //value is a byte array
138 err = db.DBconn.Unmarshal(value, &pr)
140 return Profile{}, pkgerrors.Wrap(err, "Unmarshaling Profile Value")
145 return Profile{}, pkgerrors.New("Error getting Resource Bundle Profile")
148 // Delete the Resource Bundle Profile from database
149 func (v *ProfileClient) Delete(id string) error {
150 err := db.DBconn.Delete(v.storeName, id, v.tagMeta)
152 return pkgerrors.Wrap(err, "Delete Resource Bundle Profile")
155 err = db.DBconn.Delete(v.storeName, id, v.tagContent)
157 return pkgerrors.Wrap(err, "Delete Resource Bundle Profile Content")
163 // Upload the contents of resource bundle into database
164 func (v *ProfileClient) Upload(id string, inp []byte) error {
166 //ignore the returned data here.
169 return pkgerrors.Errorf("Invalid Profile ID provided %s", err.Error())
172 err = isTarGz(bytes.NewBuffer(inp))
174 return pkgerrors.Errorf("Error in file format %s", err.Error())
177 //Encode given byte stream to text for storage
178 encodedStr := base64.StdEncoding.EncodeToString(inp)
179 err = db.DBconn.Create(v.storeName, id, v.tagContent, encodedStr)
181 return pkgerrors.Errorf("Error uploading data to db %s", err.Error())