Skip to content

Commit

Permalink
Add validation to prevent use of creationOnly flavor
Browse files Browse the repository at this point in the history
  • Loading branch information
wpjunior committed Sep 6, 2021
1 parent deefc7e commit 67654bc
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 15 deletions.
4 changes: 4 additions & 0 deletions api/v1alpha1/rpaasflavor_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ type RpaasFlavorSpec struct {
// every service instance. Default flavors cannot be listed on RpaasFlavorList.
// +optional
Default bool `json:"default,omitempty"`

// CreationOnly defines if the flavor could be used only in the moment of creation of instance
// +optional
CreationOnly bool `json:"creationOnly,omitempty"`
}

// +kubebuilder:object:root=true
Expand Down
33 changes: 19 additions & 14 deletions internal/pkg/rpaas/k8s.go
Original file line number Diff line number Diff line change
Expand Up @@ -756,19 +756,14 @@ func (m *k8sRpaasManager) getFlavors(ctx context.Context) ([]v1alpha1.RpaasFlavo
return flavorList.Items, nil
}

func (m *k8sRpaasManager) isFlavorAvailable(ctx context.Context, name string) bool {
flavors, err := m.getFlavors(ctx)
if err != nil {
return false
}

for _, f := range flavors {
if f.Name == name {
return true
func (m *k8sRpaasManager) selectFlavor(ctx context.Context, flavors []v1alpha1.RpaasFlavor, name string) *v1alpha1.RpaasFlavor {
for i := range flavors {
if flavors[i].Name == name {
return &flavors[i]
}
}

return false
return nil
}

func (m *k8sRpaasManager) CreateExtraFiles(ctx context.Context, instanceName string, files ...File) error {
Expand Down Expand Up @@ -1322,28 +1317,38 @@ func (m *k8sRpaasManager) validateCreate(ctx context.Context, args CreateArgs) e
return ConflictError{Msg: fmt.Sprintf("rpaas instance named %q already exists", args.Name)}
}

if err = m.validateFlavors(ctx, args.Flavors()); err != nil {
if err = m.validateFlavors(ctx, args.Flavors(), true); err != nil {
return err
}

return nil
}

func (m *k8sRpaasManager) validateUpdateInstanceArgs(ctx context.Context, args UpdateInstanceArgs) error {
if err := m.validateFlavors(ctx, args.Flavors()); err != nil {
if err := m.validateFlavors(ctx, args.Flavors(), false); err != nil {
return err
}

return nil
}

func (m *k8sRpaasManager) validateFlavors(ctx context.Context, flavors []string) error {
func (m *k8sRpaasManager) validateFlavors(ctx context.Context, flavors []string, creation bool) error {
encountered := map[string]bool{}
allFlavors, err := m.getFlavors(ctx)
if err != nil {
return err
}
for _, f := range flavors {
if !m.isFlavorAvailable(ctx, f) {
flavorObj := m.selectFlavor(ctx, allFlavors, f)

if flavorObj == nil {
return ValidationError{Msg: fmt.Sprintf("flavor %q not found", f)}
}

if flavorObj.Spec.CreationOnly && !creation {
return ValidationError{Msg: fmt.Sprintf("flavor %q can used only in the creation of instance", f)}
}

if _, duplicated := encountered[f]; duplicated {
return ValidationError{Msg: fmt.Sprintf("flavor %q only can be set once", f)}
}
Expand Down
30 changes: 29 additions & 1 deletion internal/pkg/rpaas/k8s_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3636,7 +3636,18 @@ func Test_k8sRpaasManager_UpdateInstance(t *testing.T) {
},
}

resources := []runtime.Object{instance1, plan1, plan2}
creationOnlyFlavor := &v1alpha1.RpaasFlavor{
ObjectMeta: metav1.ObjectMeta{
Name: "feature-create-only",
Namespace: getServiceName(),
},
Spec: v1alpha1.RpaasFlavorSpec{
CreationOnly: true,
Description: "aaaaa",
},
}

resources := []runtime.Object{instance1, plan1, plan2, creationOnlyFlavor}

tests := []struct {
name string
Expand All @@ -3657,6 +3668,23 @@ func Test_k8sRpaasManager_UpdateInstance(t *testing.T) {
}, err)
},
},
{
name: "when tries to add creationOnly flavor",
instance: "instance1",
args: UpdateInstanceArgs{
Description: "Another description",
Plan: "plan2",
Tags: []string{"flavor:feature-create-only"},
Team: "team-two",
Parameters: map[string]interface{}{
"lb-name": "my-instance.example",
},
},
assertion: func(t *testing.T, err error, instance *v1alpha1.RpaasInstance) {
require.Error(t, err)
assert.Equal(t, `flavor "feature-create-only" can used only in the creation of instance`, err.Error())
},
},
{
name: "when successfully updating an instance",
instance: "instance1",
Expand Down

0 comments on commit 67654bc

Please sign in to comment.