-
Notifications
You must be signed in to change notification settings - Fork 987
[v8] Add "--strategy" parameter to "bind-service" command #3654
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: v8
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| package flag | ||
|
|
||
| import ( | ||
| "strings" | ||
|
|
||
| "code.cloudfoundry.org/cli/v8/resources" | ||
| flags "github.com/jessevdk/go-flags" | ||
| ) | ||
|
|
||
| type ServiceBindingStrategy struct { | ||
| Strategy resources.BindingStrategyType | ||
| IsSet bool | ||
| } | ||
|
|
||
| func (ServiceBindingStrategy) Complete(prefix string) []flags.Completion { | ||
| return completions([]string{"single", "multiple"}, prefix, false) | ||
| } | ||
|
|
||
| func (h *ServiceBindingStrategy) UnmarshalFlag(val string) error { | ||
| valLower := strings.ToLower(val) | ||
| switch valLower { | ||
| case "single", "multiple": | ||
| h.Strategy = resources.BindingStrategyType(valLower) | ||
| h.IsSet = true | ||
| default: | ||
| return &flags.Error{ | ||
| Type: flags.ErrRequired, | ||
| Message: `STRATEGY must be "single" or "multiple"`, | ||
| } | ||
| } | ||
| return nil | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| package flag_test | ||
|
|
||
| import ( | ||
| . "code.cloudfoundry.org/cli/v8/command/flag" | ||
| "code.cloudfoundry.org/cli/v8/resources" | ||
| flags "github.com/jessevdk/go-flags" | ||
| . "github.com/onsi/ginkgo/v2" | ||
| . "github.com/onsi/gomega" | ||
| ) | ||
|
|
||
| var _ = Describe("ServiceBindingStrategy", func() { | ||
| var sbs ServiceBindingStrategy | ||
|
|
||
| Describe("Complete", func() { | ||
| DescribeTable("returns list of completions", | ||
| func(prefix string, matches []flags.Completion) { | ||
| completions := sbs.Complete(prefix) | ||
| Expect(completions).To(Equal(matches)) | ||
| }, | ||
| Entry("returns 'single' when passed 's'", "s", | ||
| []flags.Completion{{Item: "single"}}), | ||
| Entry("returns 'single' when passed 'S'", "S", | ||
| []flags.Completion{{Item: "single"}}), | ||
| Entry("returns 'multiple' when passed 'm'", "m", | ||
| []flags.Completion{{Item: "multiple"}}), | ||
| Entry("returns 'multiple' when passed 'M'", "M", | ||
| []flags.Completion{{Item: "multiple"}}), | ||
| Entry("returns 'single' and 'multiple' when passed ''", "", | ||
| []flags.Completion{{Item: "single"}, {Item: "multiple"}}), | ||
| ) | ||
| }) | ||
|
|
||
| Describe("UnmarshalFlag", func() { | ||
| BeforeEach(func() { | ||
| sbs = ServiceBindingStrategy{} | ||
| }) | ||
|
|
||
| When("unmarshal has not been called", func() { | ||
| It("is marked as not set", func() { | ||
| Expect(sbs.IsSet).To(BeFalse()) | ||
| }) | ||
| }) | ||
|
|
||
| DescribeTable("downcases and sets strategy", | ||
| func(input string, expected resources.BindingStrategyType) { | ||
| err := sbs.UnmarshalFlag(input) | ||
| Expect(err).ToNot(HaveOccurred()) | ||
| Expect(sbs.Strategy).To(Equal(expected)) | ||
| Expect(sbs.IsSet).To(BeTrue()) | ||
| }, | ||
| Entry("sets 'single' when passed 'single'", "single", resources.SingleBindingStrategy), | ||
| Entry("sets 'single' when passed 'sInGlE'", "sInGlE", resources.SingleBindingStrategy), | ||
| Entry("sets 'multiple' when passed 'multiple'", "multiple", resources.MultipleBindingStrategy), | ||
| Entry("sets 'multiple' when passed 'MuLtIpLe'", "MuLtIpLe", resources.MultipleBindingStrategy), | ||
| ) | ||
|
|
||
| When("passed anything else", func() { | ||
| It("returns an error", func() { | ||
| err := sbs.UnmarshalFlag("banana") | ||
| Expect(err).To(MatchError(&flags.Error{ | ||
| Type: flags.ErrRequired, | ||
| Message: `STRATEGY must be "single" or "multiple"`, | ||
| })) | ||
| Expect(sbs.Strategy).To(BeEmpty()) | ||
| }) | ||
| }) | ||
| }) | ||
| }) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,6 +3,8 @@ package v7 | |
| import ( | ||
| "code.cloudfoundry.org/cli/v8/actor/actionerror" | ||
| "code.cloudfoundry.org/cli/v8/actor/v7action" | ||
| "code.cloudfoundry.org/cli/v8/api/cloudcontroller/ccversion" | ||
| "code.cloudfoundry.org/cli/v8/command" | ||
| "code.cloudfoundry.org/cli/v8/command/flag" | ||
| "code.cloudfoundry.org/cli/v8/command/v7/shared" | ||
| "code.cloudfoundry.org/cli/v8/types" | ||
|
|
@@ -11,18 +13,26 @@ import ( | |
| type BindServiceCommand struct { | ||
| BaseCommand | ||
|
|
||
| RequiredArgs flag.BindServiceArgs `positional-args:"yes"` | ||
| BindingName flag.BindingName `long:"binding-name" description:"Name to expose service instance to app process with (Default: service instance name)"` | ||
| ParametersAsJSON flag.JSONOrFileWithValidation `short:"c" description:"Valid JSON object containing service-specific configuration parameters, provided either in-line or in a file. For a list of supported configuration parameters, see documentation for the particular service offering."` | ||
| Wait bool `short:"w" long:"wait" description:"Wait for the operation to complete"` | ||
| relatedCommands interface{} `related_commands:"services"` | ||
| RequiredArgs flag.BindServiceArgs `positional-args:"yes"` | ||
| BindingName flag.BindingName `long:"binding-name" description:"Name to expose service instance to app process with (Default: service instance name)"` | ||
| ParametersAsJSON flag.JSONOrFileWithValidation `short:"c" description:"Valid JSON object containing service-specific configuration parameters, provided either in-line or in a file. For a list of supported configuration parameters, see documentation for the particular service offering."` | ||
| ServiceBindingStrategy flag.ServiceBindingStrategy `long:"strategy" description:"Service binding strategy. Valid values are 'single' (default) and 'multiple'."` | ||
| Wait bool `short:"w" long:"wait" description:"Wait for the operation to complete"` | ||
| relatedCommands interface{} `related_commands:"services"` | ||
| } | ||
|
|
||
| func (cmd BindServiceCommand) Execute(args []string) error { | ||
| if err := cmd.SharedActor.CheckTarget(true, true); err != nil { | ||
| return err | ||
| } | ||
|
|
||
| if cmd.ServiceBindingStrategy.IsSet { | ||
| err := command.MinimumCCAPIVersionCheck(cmd.Config.APIVersion(), ccversion.MinVersionServiceBindingStrategy, "--strategy") | ||
| if err != nil { | ||
| return err | ||
| } | ||
| } | ||
|
|
||
| if err := cmd.displayIntro(); err != nil { | ||
| return err | ||
| } | ||
|
|
@@ -33,6 +43,7 @@ func (cmd BindServiceCommand) Execute(args []string) error { | |
| AppName: cmd.RequiredArgs.AppName, | ||
| BindingName: cmd.BindingName.Value, | ||
| Parameters: types.OptionalObject(cmd.ParametersAsJSON), | ||
| Strategy: cmd.ServiceBindingStrategy.Strategy, | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: May be consider adding a unit test case in
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added test.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Important: Based on the RFC document it seems like this is a new functionality added in CAPI. If that is the case, there has to be a minimum CAPI version required when using this feature right? Please let me know if I am misunderstanding this but if that is the case, we will need to add a conditional check in the implementation and may be in the API call so that this works with the older version of the CAPI as well as throw meaningful error message when used with older version of CAPI. There are many places we have done this but one of the example you can see here: https://github.com/cloudfoundry/cli/blob/main/command/v7/buildpacks_command.go#L32-L37
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, minimum required ccng version is 3.205.0. I've added a check and a test! |
||
| }) | ||
| cmd.UI.DisplayWarnings(warnings) | ||
|
|
||
|
|
@@ -82,7 +93,12 @@ Example of valid JSON object: | |
|
|
||
| Optionally provide a binding name for the association between an app and a service instance: | ||
|
|
||
| CF_NAME bind-service APP_NAME SERVICE_INSTANCE --binding-name BINDING_NAME` | ||
| CF_NAME bind-service APP_NAME SERVICE_INSTANCE --binding-name BINDING_NAME | ||
|
|
||
| Optionally provide the binding strategy type. Valid options are 'single' (default) and 'multiple'. The 'multiple' strategy allows multiple bindings between the same app and service instance. | ||
| This is useful for credential rotation scenarios. | ||
|
|
||
| CF_NAME bind-service APP_NAME SERVICE_INSTANCE --strategy multiple` | ||
|
Comment on lines
+96
to
+101
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we will need to update the
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Updated test. |
||
| } | ||
|
|
||
| func (cmd BindServiceCommand) Examples() string { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Considering we added this new flag, we will need to update the
helpintegration test here:https://github.com/cloudfoundry/cli/blob/v8/integration/v7/isolated/bind_service_command_test.go#L59-L62
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Enhanced integration test.