diff options
author | Lukasz Rajewski <lukasz.rajewski@orange.com> | 2021-10-15 12:11:44 +0200 |
---|---|---|
committer | Lukasz Rajewski <lukasz.rajewski@orange.com> | 2021-10-15 12:12:44 +0200 |
commit | b2f51225bc78212682fd087a4ef8a67c51a94188 (patch) | |
tree | 6984ad7ca8a6af18658957d4fa6be9d029d11111 /src/k8splugin/internal/rb | |
parent | 7e265aa8286a5e77ddc63fe2f9e20c776792e2f1 (diff) |
Expose Update Handlers
Expose Update Handlers for Definition, Profile and Config Tmpl
Issue-ID: MULTICLOUD-1410
Signed-off-by: Lukasz Rajewski <lukasz.rajewski@orange.com>
Change-Id: Ibe6fe05458f2af28f3e1ca14a54492a4bae19362
Diffstat (limited to 'src/k8splugin/internal/rb')
-rw-r--r-- | src/k8splugin/internal/rb/config_template.go | 24 | ||||
-rw-r--r-- | src/k8splugin/internal/rb/definition.go | 25 | ||||
-rw-r--r-- | src/k8splugin/internal/rb/definition_test.go | 3 | ||||
-rw-r--r-- | src/k8splugin/internal/rb/profile.go | 25 | ||||
-rw-r--r-- | src/k8splugin/internal/rb/profile_test.go | 2 |
5 files changed, 60 insertions, 19 deletions
diff --git a/src/k8splugin/internal/rb/config_template.go b/src/k8splugin/internal/rb/config_template.go index b84b6461..97fe0fb4 100644 --- a/src/k8splugin/internal/rb/config_template.go +++ b/src/k8splugin/internal/rb/config_template.go @@ -41,7 +41,7 @@ type ConfigTemplate struct { // ConfigTemplateManager is an interface exposes the resource bundle ConfigTemplate functionality type ConfigTemplateManager interface { - Create(rbName, rbVersion string, p ConfigTemplate) error + CreateOrUpdate(rbName, rbVersion string, p ConfigTemplate, update bool) error Get(rbName, rbVersion, templateName string) (ConfigTemplate, error) List(rbName, rbVersion string) ([]ConfigTemplate, error) Delete(rbName, rbVersion, templateName string) error @@ -84,8 +84,8 @@ func NewConfigTemplateClient() *ConfigTemplateClient { } } -// Create an entry for the resource bundle ConfigTemplate in the database -func (v *ConfigTemplateClient) Create(rbName, rbVersion string, p ConfigTemplate) error { +// CreateOrUpdate an entry for the resource bundle ConfigTemplate in the database +func (v *ConfigTemplateClient) CreateOrUpdate(rbName, rbVersion string, p ConfigTemplate, update bool) error { log.Printf("[ConfigiTemplate]: create %s", rbName) // Name is required @@ -95,9 +95,12 @@ func (v *ConfigTemplateClient) Create(rbName, rbVersion string, p ConfigTemplate //Check if ConfigTemplate already exists _, err := v.Get(rbName, rbVersion, p.TemplateName) - if err == nil { + if err == nil && !update { return pkgerrors.New(" ConfigTemplate already exists for this Definition") } + if err != nil && update { + return pkgerrors.New(" ConfigTemplate does not exist for this Definition") + } //Check if provided resource bundle information is valid _, err = NewDefinitionClient().Get(rbName, rbVersion) @@ -111,9 +114,16 @@ func (v *ConfigTemplateClient) Create(rbName, rbVersion string, p ConfigTemplate TemplateName: p.TemplateName, } - err = db.DBconn.Create(v.storeName, key, v.tagMeta, p) - if err != nil { - return pkgerrors.Wrap(err, "Creating ConfigTemplate DB Entry") + if update { + err = db.DBconn.Update(v.storeName, key, v.tagMeta, p) + if err != nil { + return pkgerrors.Wrap(err, "Updating ConfigTemplate DB Entry") + } + } else { + err = db.DBconn.Create(v.storeName, key, v.tagMeta, p) + if err != nil { + return pkgerrors.Wrap(err, "Creating ConfigTemplate DB Entry") + } } return nil diff --git a/src/k8splugin/internal/rb/definition.go b/src/k8splugin/internal/rb/definition.go index 73ea44da..aa76afaa 100644 --- a/src/k8splugin/internal/rb/definition.go +++ b/src/k8splugin/internal/rb/definition.go @@ -61,6 +61,7 @@ func (dk DefinitionKey) String() string { // DefinitionManager is an interface exposes the resource bundle definition functionality type DefinitionManager interface { Create(def Definition) (Definition, error) + Update(def Definition) (Definition, error) List(name string) ([]Definition, error) Get(name string, version string) (Definition, error) Delete(name string, version string) error @@ -104,13 +105,13 @@ func (v *DefinitionClient) Create(def Definition) (Definition, error) { // Create a default profile automatically prc := NewProfileClient() - pr, err := prc.Create(Profile{ + pr, err := prc.CreateOrUpdate(Profile{ RBName: def.RBName, RBVersion: def.RBVersion, ProfileName: "default", Namespace: "default", ReleaseName: "default", - }) + }, false) if err != nil { logutils.Error("Create Default Profile", logutils.Fields{ @@ -139,6 +140,26 @@ func (v *DefinitionClient) Create(def Definition) (Definition, error) { return def, nil } +// Update an entry for the resource in the database` +func (v *DefinitionClient) Update(def Definition) (Definition, error) { + + //Construct composite key consisting of name and version + key := DefinitionKey{RBName: def.RBName, RBVersion: def.RBVersion} + + //Check if this definition already exists + _, err := v.Get(def.RBName, def.RBVersion) + if err != nil { + return Definition{}, pkgerrors.New("Definition does not exists") + } + + err = db.DBconn.Update(v.storeName, key, v.tagMeta, def) + if err != nil { + return Definition{}, pkgerrors.Wrap(err, "Updating DB Entry") + } + + return def, nil +} + // List all resource entry's versions in the database func (v *DefinitionClient) List(name string) ([]Definition, error) { res, err := db.DBconn.ReadAll(v.storeName, v.tagMeta) diff --git a/src/k8splugin/internal/rb/definition_test.go b/src/k8splugin/internal/rb/definition_test.go index 0140b459..42fb5374 100644 --- a/src/k8splugin/internal/rb/definition_test.go +++ b/src/k8splugin/internal/rb/definition_test.go @@ -18,12 +18,13 @@ package rb import ( "bytes" - "github.com/onap/multicloud-k8s/src/k8splugin/internal/db" "reflect" "sort" "strings" "testing" + "github.com/onap/multicloud-k8s/src/k8splugin/internal/db" + pkgerrors "github.com/pkg/errors" ) diff --git a/src/k8splugin/internal/rb/profile.go b/src/k8splugin/internal/rb/profile.go index 78023e59..77398580 100644 --- a/src/k8splugin/internal/rb/profile.go +++ b/src/k8splugin/internal/rb/profile.go @@ -46,7 +46,7 @@ type Profile struct { // ProfileManager is an interface exposes the resource bundle profile functionality type ProfileManager interface { - Create(def Profile) (Profile, error) + 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 @@ -89,8 +89,8 @@ func NewProfileClient() *ProfileClient { } } -// Create an entry for the resource bundle profile in the database -func (v *ProfileClient) Create(p Profile) (Profile, error) { +// CreateOrUpdate an entry for the resource bundle profile in the database +func (v *ProfileClient) CreateOrUpdate(p Profile, update bool) (Profile, error) { // Name is required if p.ProfileName == "" { @@ -99,10 +99,12 @@ func (v *ProfileClient) Create(p Profile) (Profile, error) { //Check if profile already exists _, err := v.Get(p.RBName, p.RBVersion, p.ProfileName) - if err == nil { + if err == nil && !update { return Profile{}, pkgerrors.New("Profile already exists for this Definition") } - + if err != nil && update { + return Profile{}, pkgerrors.New("Profile does not exists for this Definition") + } //Check if provided resource bundle information is valid _, err = NewDefinitionClient().Get(p.RBName, p.RBVersion) if err != nil { @@ -120,9 +122,16 @@ func (v *ProfileClient) Create(p Profile) (Profile, error) { ProfileName: p.ProfileName, } - err = db.DBconn.Create(v.storeName, key, v.tagMeta, p) - if err != nil { - return Profile{}, pkgerrors.Wrap(err, "Creating Profile DB Entry") + if update { + err = db.DBconn.Update(v.storeName, key, v.tagMeta, p) + if err != nil { + return Profile{}, pkgerrors.Wrap(err, "Updating Profile DB Entry") + } + } else { + err = db.DBconn.Create(v.storeName, key, v.tagMeta, p) + if err != nil { + return Profile{}, pkgerrors.Wrap(err, "Creating Profile DB Entry") + } } return p, nil diff --git a/src/k8splugin/internal/rb/profile_test.go b/src/k8splugin/internal/rb/profile_test.go index 2a9dc4fd..e52897ce 100644 --- a/src/k8splugin/internal/rb/profile_test.go +++ b/src/k8splugin/internal/rb/profile_test.go @@ -105,7 +105,7 @@ func TestCreateProfile(t *testing.T) { t.Run(testCase.label, func(t *testing.T) { db.DBconn = testCase.mockdb impl := NewProfileClient() - got, err := impl.Create(testCase.inp) + got, err := impl.CreateOrUpdate(testCase.inp, false) if err != nil { if testCase.expectedError == "" { t.Fatalf("Create returned an unexpected error %s", err) |