return
}
- ret, err := h.client.CreateOrUpdate(p, false)
+ ret, err := h.client.CreateOrUpdate(r.Context(), p, false)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
return
}
- err = h.client.Upload(rbName, rbVersion, prName, inpBytes)
+ err = h.client.Upload(r.Context(), rbName, rbVersion, prName, inpBytes)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
rbVersion := vars["rbversion"]
prName := vars["prname"]
- ret, err := h.client.Get(rbName, rbVersion, prName)
+ ret, err := h.client.Get(r.Context(), rbName, rbVersion, prName)
if err != nil {
// Separate "Not found" from generic DB errors
if strings.Contains(err.Error(), "Error finding") {
rbVersion := vars["rbversion"]
prName := vars["prname"]
- ret, err := h.client.Get(rbName, rbVersion, prName)
+ ret, err := h.client.Get(r.Context(), rbName, rbVersion, prName)
if err != nil {
// Separate "Not found" from generic DB errors
if strings.Contains(err.Error(), "Error finding") {
p.RBVersion = ret.RBVersion
p.RBName = ret.RBName
- ret, err = h.client.CreateOrUpdate(p, true)
+ ret, err = h.client.CreateOrUpdate(r.Context(), p, true)
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
rbName := vars["rbname"]
rbVersion := vars["rbversion"]
- ret, err := h.client.List(rbName, rbVersion)
+ ret, err := h.client.List(r.Context(), rbName, rbVersion)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
rbVersion := vars["rbversion"]
prName := vars["prname"]
- err := h.client.Delete(rbName, rbVersion, prName)
+ err := h.client.Delete(r.Context(), rbName, rbVersion, prName)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
import (
"bytes"
+ "context"
"encoding/json"
"io"
"net/http"
pkgerrors "github.com/pkg/errors"
)
-//Creating an embedded interface via anonymous variable
-//This allows us to make mockDB satisfy the DatabaseConnection
-//interface even if we are not implementing all the methods in it
+// Creating an embedded interface via anonymous variable
+// This allows us to make mockDB satisfy the DatabaseConnection
+// interface even if we are not implementing all the methods in it
type mockRBProfile struct {
rb.ProfileManager
// Items and err will be used to customize each test
Err error
}
-func (m *mockRBProfile) CreateOrUpdate(inp rb.Profile, update bool) (rb.Profile, error) {
+func (m *mockRBProfile) CreateOrUpdate(ctx context.Context, inp rb.Profile, update bool) (rb.Profile, error) {
if m.Err != nil {
return rb.Profile{}, m.Err
}
return m.Items[0], nil
}
-func (m *mockRBProfile) Get(rbname, rbversion, prname string) (rb.Profile, error) {
+func (m *mockRBProfile) Get(ctx context.Context, rbname, rbversion, prname string) (rb.Profile, error) {
if m.Err != nil {
return rb.Profile{}, m.Err
}
return m.Items[0], nil
}
-func (m *mockRBProfile) List(rbname, rbversion string) ([]rb.Profile, error) {
+func (m *mockRBProfile) List(ctx context.Context, rbname, rbversion string) ([]rb.Profile, error) {
if m.Err != nil {
return []rb.Profile{}, m.Err
}
return m.Items, nil
}
-func (m *mockRBProfile) Delete(rbname, rbversion, prname string) error {
+func (m *mockRBProfile) Delete(ctx context.Context, rbname, rbversion, prname string) error {
return m.Err
}
-func (m *mockRBProfile) Upload(rbname, rbversion, prname string, inp []byte) error {
+func (m *mockRBProfile) Upload(ctx context.Context, rbname, rbversion, prname string, inp []byte) error {
return m.Err
}
var resTemplates []helm.KubernetesResourceTemplate
profileClient := rb.NewProfileClient()
- profile, err := profileClient.Get(rbName, rbVersion, profileName)
+ profile, err := profileClient.Get(context.TODO(), rbName, rbVersion, profileName)
if err != nil {
return configResourceList{}, pkgerrors.Wrap(err, "Reading Profile Data")
}
//Download and process the profile first
//If everything seems okay, then download the config templates
- prYamlClient, err := profileClient.GetYamlClient(rbName, rbVersion, profileName)
+ prYamlClient, err := profileClient.GetYamlClient(context.TODO(), rbName, rbVersion, profileName)
if err != nil {
return configResourceList{}, pkgerrors.Wrap(err, "Processing Profile Manifest")
}
}
//Check if profile exists
- profile, err := rb.NewProfileClient().Get(i.RBName, i.RBVersion, i.ProfileName)
+ profile, err := rb.NewProfileClient().Get(ctx, i.RBName, i.RBVersion, i.ProfileName)
if err != nil {
return InstanceResponse{}, pkgerrors.New("Unable to find Profile to create instance")
}
overrideValues = append(overrideValues, "k8s-rb-instance-id="+finalId)
//Execute the kubernetes create command
- sortedTemplates, crdList, hookList, releaseName, err := rb.NewProfileClient().Resolve(i.RBName, i.RBVersion, i.ProfileName, overrideValues, i.ReleaseName)
+ sortedTemplates, crdList, hookList, releaseName, err := rb.NewProfileClient().Resolve(ctx, i.RBName, i.RBVersion, i.ProfileName, overrideValues, i.ReleaseName)
if err != nil {
namegenerator.Release(ctx, generatedId)
return InstanceResponse{}, pkgerrors.Wrap(err, "Error resolving helm charts")
}
//Check if profile exists
- profile, err := rb.NewProfileClient().Get(i.RBName, i.RBVersion, i.ProfileName)
+ profile, err := rb.NewProfileClient().Get(ctx, i.RBName, i.RBVersion, i.ProfileName)
if err != nil {
return InstanceResponse{}, pkgerrors.New("Unable to find Profile to create instance")
}
overrideValues = append(overrideValues, "k8s-rb-instance-id="+id)
//Execute the kubernetes create command
- sortedTemplates, crdList, hookList, releaseName, err := rb.NewProfileClient().Resolve(i.RBName, i.RBVersion, i.ProfileName, overrideValues, i.ReleaseName)
+ sortedTemplates, crdList, hookList, releaseName, err := rb.NewProfileClient().Resolve(ctx, i.RBName, i.RBVersion, i.ProfileName, overrideValues, i.ReleaseName)
if err != nil {
return InstanceResponse{}, pkgerrors.Wrap(err, "Error resolving helm charts")
}
return InstanceStatus{}, pkgerrors.Wrap(err, "Getting CloudRegion Information")
}
req := resResp.Request
- profile, err := rb.NewProfileClient().Get(req.RBName, req.RBVersion, req.ProfileName)
+ profile, err := rb.NewProfileClient().Get(ctx, req.RBName, req.RBVersion, req.ProfileName)
if err != nil {
return InstanceStatus{}, pkgerrors.New("Unable to find Profile instance status")
}
ID: id,
}
log.Printf(" Resolving template for release %s", instance.Request.ReleaseName)
- _, _, hookList, _, _ := rb.NewProfileClient().Resolve(instance.Request.RBName, instance.Request.RBVersion, instance.Request.ProfileName, overrideValues, instance.Request.ReleaseName)
+ _, _, hookList, _, _ := rb.NewProfileClient().Resolve(ctx, instance.Request.RBName, instance.Request.RBVersion, instance.Request.ProfileName, overrideValues, instance.Request.ReleaseName)
instance.Hooks = hookList
err = db.DBconn.Update(ctx, v.storeName, key, v.tagInst, instance)
if err != nil {
// Create Status Subscription
func (iss *InstanceStatusSubClient) Create(ctx context.Context, instanceId string, subDetails SubscriptionRequest) (StatusSubscription, error) {
-
+ ctx, span := tracer.Start(ctx, "InstanceStatusSubClient.Create")
+ defer span.End()
_, err := iss.Get(ctx, instanceId, subDetails.Name)
if err == nil {
return StatusSubscription{}, pkgerrors.New("Subscription already exists")
sub.NotifyMetadata = make(map[string]interface{})
}
- err = iss.refreshWatchers(instanceId, subDetails.Name)
+ err = iss.refreshWatchers(ctx, instanceId, subDetails.Name)
if err != nil {
return sub, pkgerrors.Wrap(err, "Creating Status Subscription DB Entry")
}
// Get Status subscription
func (iss *InstanceStatusSubClient) Get(ctx context.Context, instanceId, subId string) (StatusSubscription, error) {
+ ctx, span := tracer.Start(ctx, "InstanceStatusSubClient.Get")
+ defer span.End()
lock, _, _ := getSubscriptionData(instanceId)
// Acquire Mutex
lock.Lock()
sub.NotifyMetadata = make(map[string]interface{})
}
- err = iss.refreshWatchers(instanceId, subDetails.Name)
+ err = iss.refreshWatchers(ctx, instanceId, subDetails.Name)
if err != nil {
return sub, pkgerrors.Wrap(err, "Updating Status Subscription DB Entry")
}
}
for _, sub := range subList {
- err = iss.refreshWatchers(instance.ID, sub.Name)
+ err = iss.refreshWatchers(ctx, instance.ID, sub.Name)
if err != nil {
log.Error("Error on refreshing watchers", log.Fields{
"error": err.Error(),
}()
}
-func (iss *InstanceStatusSubClient) refreshWatchers(instanceId, subId string) error {
+func (iss *InstanceStatusSubClient) refreshWatchers(ctx context.Context, instanceId, subId string) error {
log.Info("REFRESH WATCHERS", log.Fields{
"instance": instanceId,
"subscription": subId,
})
v := NewInstanceClient()
k8sClient := KubernetesClient{}
- ctx := context.TODO()
instance, err := v.Get(ctx, instanceId)
if err != nil {
return pkgerrors.Wrap(err, "Cannot get instance for notify thread")
}
- profile, err := rb.NewProfileClient().Get(instance.Request.RBName, instance.Request.RBVersion,
+ profile, err := rb.NewProfileClient().Get(ctx, instance.Request.RBName, instance.Request.RBVersion,
instance.Request.ProfileName)
if err != nil {
return pkgerrors.Wrap(err, "Unable to find Profile instance status")
// Create a default profile automatically
prc := NewProfileClient()
- pr, err := prc.CreateOrUpdate(Profile{
+ pr, err := prc.CreateOrUpdate(ctx, Profile{
RBName: def.RBName,
RBVersion: def.RBVersion,
ProfileName: "default",
return Definition{}, pkgerrors.Wrap(err, "Creating Default Profile")
}
- err = prc.Upload(pr.RBName, pr.RBVersion, pr.ProfileName, prc.getEmptyProfile())
+ err = prc.Upload(ctx, pr.RBName, pr.RBVersion, pr.ProfileName, prc.getEmptyProfile())
if err != nil {
logutils.Error("Upload Empty Profile", logutils.Fields{
"error": err,
//Delete the default profile as well
prc := NewProfileClient()
- err = prc.Delete(name, version, "default")
+ err = prc.Delete(ctx, name, version, "default")
if err != nil {
logutils.Error("Delete Default Profile", logutils.Fields{
"error": err,
// ProfileManager is an interface exposes the resource bundle profile functionality
type ProfileManager interface {
- CreateOrUpdate(def Profile, update bool) (Profile, error)
- Get(rbName, rbVersion, prName string) (Profile, error)
- List(rbName, rbVersion string) ([]Profile, error)
- Delete(rbName, rbVersion, prName string) error
- Upload(rbName, rbVersion, prName string, inp []byte) error
+ CreateOrUpdate(ctx context.Context, def Profile, update bool) (Profile, error)
+ Get(ctx context.Context, rbName, rbVersion, prName string) (Profile, error)
+ List(ctx context.Context, rbName, rbVersion string) ([]Profile, error)
+ Delete(ctx context.Context, rbName, rbVersion, prName string) error
+ Upload(ctx context.Context, rbName, rbVersion, prName string, inp []byte) error
}
type ProfileKey struct {
}
// CreateOrUpdate an entry for the resource bundle profile in the database
-func (v *ProfileClient) CreateOrUpdate(p Profile, update bool) (Profile, error) {
+func (v *ProfileClient) CreateOrUpdate(ctx context.Context, p Profile, update bool) (Profile, error) {
// Name is required
if p.ProfileName == "" {
}
//Check if profile already exists
- _, err := v.Get(p.RBName, p.RBVersion, p.ProfileName)
+ _, err := v.Get(ctx, p.RBName, p.RBVersion, p.ProfileName)
if err == nil && !update {
return Profile{}, pkgerrors.New("Profile already exists for this Definition")
}
return Profile{}, pkgerrors.New("Profile does not exists for this Definition")
}
//Check if provided resource bundle information is valid
- _, err = NewDefinitionClient().Get(context.TODO(), p.RBName, p.RBVersion)
+ _, err = NewDefinitionClient().Get(ctx, p.RBName, p.RBVersion)
if err != nil {
return Profile{}, pkgerrors.Errorf("Invalid Resource Bundle ID provided: %s", err.Error())
}
}
if update {
- err = db.DBconn.Update(context.TODO(), v.storeName, key, v.tagMeta, p)
+ err = db.DBconn.Update(ctx, v.storeName, key, v.tagMeta, p)
if err != nil {
return Profile{}, pkgerrors.Wrap(err, "Updating Profile DB Entry")
}
} else {
- err = db.DBconn.Create(context.TODO(), v.storeName, key, v.tagMeta, p)
+ err = db.DBconn.Create(ctx, v.storeName, key, v.tagMeta, p)
if err != nil {
return Profile{}, pkgerrors.Wrap(err, "Creating Profile DB Entry")
}
}
// Get returns the Resource Bundle Profile for corresponding ID
-func (v *ProfileClient) Get(rbName, rbVersion, prName string) (Profile, error) {
+func (v *ProfileClient) Get(ctx context.Context, rbName, rbVersion, prName string) (Profile, error) {
key := ProfileKey{
RBName: rbName,
RBVersion: rbVersion,
ProfileName: prName,
}
- value, err := db.DBconn.Read(context.TODO(), v.storeName, key, v.tagMeta)
+ value, err := db.DBconn.Read(ctx, v.storeName, key, v.tagMeta)
if err != nil {
return Profile{}, pkgerrors.Wrap(err, "Get Resource Bundle Profile")
}
}
// List returns the Resource Bundle Profile for corresponding ID
-func (v *ProfileClient) List(rbName, rbVersion string) ([]Profile, error) {
+func (v *ProfileClient) List(ctx context.Context, rbName, rbVersion string) ([]Profile, error) {
//Get all profiles
- dbres, err := db.DBconn.ReadAll(context.TODO(), v.storeName, v.tagMeta)
+ dbres, err := db.DBconn.ReadAll(ctx, v.storeName, v.tagMeta)
if err != nil || len(dbres) == 0 {
return []Profile{}, pkgerrors.Wrap(err, "No Profiles Found")
}
}
// Delete the Resource Bundle Profile from database
-func (v *ProfileClient) Delete(rbName, rbVersion, prName string) error {
+func (v *ProfileClient) Delete(ctx context.Context, rbName, rbVersion, prName string) error {
key := ProfileKey{
RBName: rbName,
RBVersion: rbVersion,
ProfileName: prName,
}
- err := db.DBconn.Delete(context.TODO(), v.storeName, key, v.tagMeta)
+ err := db.DBconn.Delete(ctx, v.storeName, key, v.tagMeta)
if err != nil {
return pkgerrors.Wrap(err, "Delete Resource Bundle Profile")
}
- err = db.DBconn.Delete(context.TODO(), v.storeName, key, v.tagContent)
+ err = db.DBconn.Delete(ctx, v.storeName, key, v.tagContent)
if err != nil {
return pkgerrors.Wrap(err, "Delete Resource Bundle Profile Content")
}
}
// Upload the contents of resource bundle into database
-func (v *ProfileClient) Upload(rbName, rbVersion, prName string, inp []byte) error {
+func (v *ProfileClient) Upload(ctx context.Context, rbName, rbVersion, prName string, inp []byte) error {
//ignore the returned data here.
- _, err := v.Get(rbName, rbVersion, prName)
+ _, err := v.Get(ctx, rbName, rbVersion, prName)
if err != nil {
return pkgerrors.Errorf("Invalid Profile Name provided %s", err.Error())
}
}
//Encode given byte stream to text for storage
encodedStr := base64.StdEncoding.EncodeToString(inp)
- err = db.DBconn.Create(context.TODO(), v.storeName, key, v.tagContent, encodedStr)
+ err = db.DBconn.Create(ctx, v.storeName, key, v.tagContent, encodedStr)
if err != nil {
return pkgerrors.Errorf("Error uploading data to db %s", err.Error())
}
// Download the contents of the resource bundle profile from DB
// Returns a byte array of the contents which is used by the
// ExtractTarBall code to create the folder structure on disk
-func (v *ProfileClient) Download(rbName, rbVersion, prName string) ([]byte, error) {
+func (v *ProfileClient) Download(ctx context.Context, rbName, rbVersion, prName string) ([]byte, error) {
//ignore the returned data here
//Check if id is valid
- _, err := v.Get(rbName, rbVersion, prName)
+ _, err := v.Get(ctx, rbName, rbVersion, prName)
if err != nil {
return nil, pkgerrors.Errorf("Invalid Profile Name provided: %s", err.Error())
}
RBVersion: rbVersion,
ProfileName: prName,
}
- value, err := db.DBconn.Read(context.TODO(), v.storeName, key, v.tagContent)
+ value, err := db.DBconn.Read(ctx, v.storeName, key, v.tagContent)
if err != nil {
return nil, pkgerrors.Wrap(err, "Get Resource Bundle Profile content")
}
}
// GetYamlClient GEt Yaml Files client for profile
-func (v *ProfileClient) GetYamlClient(rbName string, rbVersion string, profileName string) (ProfileYamlClient, error) {
- prData, err := v.Download(rbName, rbVersion, profileName)
+func (v *ProfileClient) GetYamlClient(ctx context.Context, rbName string, rbVersion string, profileName string) (ProfileYamlClient, error) {
+ prData, err := v.Download(ctx, rbName, rbVersion, profileName)
if err != nil {
return ProfileYamlClient{}, pkgerrors.Wrap(err, "Downloading Profile")
}
// Resolve returns the path where the helm chart merged with
// configuration overrides resides and final ReleaseName picked for instantiation
-func (v *ProfileClient) Resolve(rbName string, rbVersion string,
+func (v *ProfileClient) Resolve(ctx context.Context, rbName string, rbVersion string,
profileName string, values []string, overrideReleaseName string) ([]helm.KubernetesResourceTemplate, []helm.KubernetesResourceTemplate, []*helm.Hook, string, error) {
var sortedTemplates []helm.KubernetesResourceTemplate
//Download and process the profile first
//If everything seems okay, then download the definition
- prYamlClient, err := v.GetYamlClient(rbName, rbVersion, profileName)
+ prYamlClient, err := v.GetYamlClient(ctx, rbName, rbVersion, profileName)
if err != nil {
return sortedTemplates, crdList, hookList, finalReleaseName, pkgerrors.Wrap(err, "Processing Profile Manifest")
}
definitionClient := NewDefinitionClient()
- definition, err := definitionClient.Get(context.TODO(), rbName, rbVersion)
+ definition, err := definitionClient.Get(ctx, rbName, rbVersion)
if err != nil {
return sortedTemplates, crdList, hookList, finalReleaseName, pkgerrors.Wrap(err, "Getting Definition Metadata")
}
- defData, err := definitionClient.Download(context.TODO(), rbName, rbVersion)
+ defData, err := definitionClient.Download(ctx, rbName, rbVersion)
if err != nil {
return sortedTemplates, crdList, hookList, finalReleaseName, pkgerrors.Wrap(err, "Downloading Definition")
}
}
//Get the definition ID and download its contents
- profile, err := v.Get(rbName, rbVersion, profileName)
+ profile, err := v.Get(ctx, rbName, rbVersion, profileName)
if err != nil {
return sortedTemplates, crdList, hookList, finalReleaseName, pkgerrors.Wrap(err, "Getting Profile")
}
import (
"bytes"
+ "context"
"os"
"reflect"
"sort"
t.Run(testCase.label, func(t *testing.T) {
db.DBconn = testCase.mockdb
impl := NewProfileClient()
- got, err := impl.CreateOrUpdate(testCase.inp, false)
+ got, err := impl.CreateOrUpdate(context.TODO(), testCase.inp, false)
if err != nil {
if testCase.expectedError == "" {
t.Fatalf("Create returned an unexpected error %s", err)
t.Run(testCase.label, func(t *testing.T) {
db.DBconn = testCase.mockdb
impl := NewProfileClient()
- got, err := impl.Get(testCase.rbname, testCase.rbversion, testCase.prname)
+ got, err := impl.Get(context.TODO(), testCase.rbname, testCase.rbversion, testCase.prname)
if err != nil {
if testCase.expectedError == "" {
t.Fatalf("Get returned an unexpected error %s", err)
t.Run(testCase.label, func(t *testing.T) {
db.DBconn = testCase.mockdb
impl := NewProfileClient()
- got, err := impl.List(testCase.rbdef, testCase.version)
+ got, err := impl.List(context.TODO(), testCase.rbdef, testCase.version)
if err != nil {
if testCase.expectedError == "" {
t.Fatalf("List returned an unexpected error %s", err)
t.Run(testCase.label, func(t *testing.T) {
db.DBconn = testCase.mockdb
impl := NewProfileClient()
- err := impl.Delete(testCase.rbname, testCase.rbversion, testCase.prname)
+ err := impl.Delete(context.TODO(), testCase.rbname, testCase.rbversion, testCase.prname)
if err != nil {
if testCase.expectedError == "" {
t.Fatalf("Delete returned an unexpected error %s", err)
t.Run(testCase.label, func(t *testing.T) {
db.DBconn = testCase.mockdb
impl := NewProfileClient()
- err := impl.Upload(testCase.rbname, testCase.rbversion, testCase.prname, testCase.content)
+ err := impl.Upload(context.TODO(), testCase.rbname, testCase.rbversion, testCase.prname, testCase.content)
if err != nil {
if testCase.expectedError == "" {
t.Errorf("Upload returned an unexpected error %s", err)
t.Run(testCase.label, func(t *testing.T) {
db.DBconn = testCase.mockdb
impl := NewProfileClient()
- data, err := impl.Download(testCase.rbname, testCase.rbversion, testCase.prname)
+ data, err := impl.Download(context.TODO(), testCase.rbname, testCase.rbversion, testCase.prname)
if err != nil {
if testCase.expectedError == "" {
t.Errorf("Download returned an unexpected error %s", err)
t.Run(testCase.label, func(t *testing.T) {
db.DBconn = testCase.mockdb
impl := NewProfileClient()
- data, _, _, releaseName, err := impl.Resolve(testCase.rbname,
+ data, _, _, releaseName, err := impl.Resolve(context.TODO(), testCase.rbname,
testCase.rbversion, testCase.prname, []string{}, testCase.releaseName)
defer cleanup(data)
if err != nil {