Skip to content

Commit

Permalink
odo remove binding
Browse files Browse the repository at this point in the history
Signed-off-by: Parthvi Vala <[email protected]>
  • Loading branch information
valaparthvi committed Jun 2, 2022
1 parent f1b2c72 commit e04e4cf
Show file tree
Hide file tree
Showing 8 changed files with 314 additions and 53 deletions.
44 changes: 44 additions & 0 deletions pkg/binding/binding.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,13 @@ package binding

import (
"fmt"
"path/filepath"
"strings"

"github.com/devfile/api/v2/pkg/apis/workspaces/v1alpha2"
"github.com/devfile/library/pkg/devfile/parser"
"github.com/devfile/library/pkg/devfile/parser/data/v2/common"
devfilefs "github.com/devfile/library/pkg/testingutil/filesystem"
sboApi "github.com/redhat-developer/service-binding-operator/apis/binding/v1alpha1"
"gopkg.in/yaml.v2"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
Expand Down Expand Up @@ -146,3 +151,42 @@ func (o *BindingClient) GetServiceInstances() (map[string]unstructured.Unstructu

return bindableObjectMap, nil
}

// ValidateRemoveBinding validates if the command has adequate arguments/flags
func (o *BindingClient) ValidateRemoveBinding(flags map[string]string) error {
if flags[backendpkg.FLAG_NAME] == "" {
return fmt.Errorf("you must specify the service binding name with --%s flag", backendpkg.FLAG_NAME)
}
return nil
}

// RemoveBinding removes the binding from devfile
func (o *BindingClient) RemoveBinding(servicebindingName string, obj parser.DevfileObj) (parser.DevfileObj, error) {
var componentName string
var options []string
components, err := obj.Data.GetComponents(common.DevfileOptions{
ComponentOptions: common.ComponentOptions{ComponentType: v1alpha2.KubernetesComponentType},
})
if err != nil {
return obj, err
}
for _, component := range components {
unstructuredObj, err := libdevfile.GetK8sComponentAsUnstructured(obj, component.Name, filepath.Dir(obj.Ctx.GetAbsPath()), devfilefs.DefaultFs{})
if err != nil {
continue
}
if unstructuredObj.GetKind() == kclient.ServiceBindingKind {
options = append(options, unstructuredObj.GetName())
if unstructuredObj.GetName() == servicebindingName {
componentName = component.Name
break
}
}
}

err = obj.Data.DeleteComponent(componentName)
if err != nil {
err = fmt.Errorf("Failed to remove Service Binding %q from the the devfile. Available Service Bindings: %s", servicebindingName, strings.Join(options, ", "))
}
return obj, err
}
5 changes: 5 additions & 0 deletions pkg/binding/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,9 @@ type Client interface {
AddBinding(bindingName string, bindAsFiles bool, unstructuredService unstructured.Unstructured, obj parser.DevfileObj, componentContext string) (parser.DevfileObj, error)
// GetServiceInstances returns a map of bindable instance name with its unstructured.Unstructured object, and an error
GetServiceInstances() (map[string]unstructured.Unstructured, error)

// ValidateRemoveBinding validates if the command has adequate arguments/flags
ValidateRemoveBinding(flags map[string]string) error
// RemoveBinding removes the binding from devfile
RemoveBinding(bindingName string, obj parser.DevfileObj) (parser.DevfileObj, error)
}
130 changes: 79 additions & 51 deletions pkg/binding/mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions pkg/odo/cli/add/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ import (
"github.com/redhat-developer/odo/pkg/odo/util"
)

// RecommendedCommandName is the recommended create command name
// RecommendedCommandName is the recommended add command name
const RecommendedCommandName = "add"

// NewCmdDelete implements the delete odo command
// NewCmdAdd implements the odo add command
func NewCmdAdd(name, fullName string) *cobra.Command {
var createCmd = &cobra.Command{
Use: name,
Expand Down
2 changes: 2 additions & 0 deletions pkg/odo/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"github.com/redhat-developer/odo/pkg/odo/cli/preference"
"github.com/redhat-developer/odo/pkg/odo/cli/project"
"github.com/redhat-developer/odo/pkg/odo/cli/registry"
"github.com/redhat-developer/odo/pkg/odo/cli/remove"
"github.com/redhat-developer/odo/pkg/odo/cli/set"
"github.com/redhat-developer/odo/pkg/odo/cli/telemetry"
"github.com/redhat-developer/odo/pkg/odo/cli/utils"
Expand Down Expand Up @@ -177,6 +178,7 @@ func odoRootCmd(name, fullName string) *cobra.Command {
_init.NewCmdInit(_init.RecommendedCommandName, util.GetFullName(fullName, _init.RecommendedCommandName)),
_delete.NewCmdDelete(_delete.RecommendedCommandName, util.GetFullName(fullName, _delete.RecommendedCommandName)),
add.NewCmdAdd(add.RecommendedCommandName, util.GetFullName(fullName, add.RecommendedCommandName)),
remove.NewCmdRemove(remove.RecommendedCommandName, util.GetFullName(fullName, remove.RecommendedCommandName)),
dev.NewCmdDev(dev.RecommendedCommandName, util.GetFullName(fullName, dev.RecommendedCommandName)),
alizer.NewCmdAlizer(alizer.RecommendedCommandName, util.GetFullName(fullName, alizer.RecommendedCommandName)),
describe.NewCmdDescribe(describe.RecommendedCommandName, util.GetFullName(fullName, describe.RecommendedCommandName)),
Expand Down
Loading

0 comments on commit e04e4cf

Please sign in to comment.