Skip to content

Commit

Permalink
Make cups command idempotent (#3288)
Browse files Browse the repository at this point in the history
  • Loading branch information
gururajsh authored Nov 7, 2024
1 parent ad2f7f4 commit dfe2201
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
12 changes: 11 additions & 1 deletion command/v7/create_user_provided_service_command.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package v7

import (
"code.cloudfoundry.org/cli/api/cloudcontroller/ccerror"
"code.cloudfoundry.org/cli/command"
"code.cloudfoundry.org/cli/command/flag"
"code.cloudfoundry.org/cli/resources"
Expand Down Expand Up @@ -43,7 +44,16 @@ func (cmd CreateUserProvidedServiceCommand) Execute(args []string) error {

warnings, err := cmd.Actor.CreateUserProvidedServiceInstance(serviceInstance)
cmd.UI.DisplayWarnings(warnings)
if err != nil {
switch err.(type) {
case nil:
case ccerror.ServiceInstanceNameTakenError:
cmd.UI.DisplayTextWithFlavor("Service instance {{.ServiceInstanceName}} already exists",
map[string]interface{}{
"ServiceInstanceName": serviceInstance.Name,
})
cmd.UI.DisplayOK()
return nil
default:
return err
}

Expand Down
16 changes: 16 additions & 0 deletions command/v7/create_user_provided_service_command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package v7_test
import (
"errors"

"code.cloudfoundry.org/cli/api/cloudcontroller/ccerror"
"code.cloudfoundry.org/cli/types"

"code.cloudfoundry.org/cli/actor/v7action"
Expand Down Expand Up @@ -215,5 +216,20 @@ var _ = Describe("create-user-provided-service Command", func() {
))
})
})

When("the service instance name is taken", func() {
BeforeEach(func() {
fakeActor.CreateUserProvidedServiceInstanceReturns(
nil,
ccerror.ServiceInstanceNameTakenError{},
)
})

It("succeeds, displaying warnings, 'OK' and an informative message", func() {
Expect(executeErr).NotTo(HaveOccurred())
Expect(testUI.Out).To(Say("Service instance %s already exists", fakeServiceInstanceName))
Expect(testUI.Out).To(Say("OK"))
})
})
})
})

0 comments on commit dfe2201

Please sign in to comment.