github.com/franc20/ayesa_sap@v7.0.0-beta.28.0.20200124003224-302d4d52fa6c+incompatible/command/v6/unshare_service_command.go (about) 1 package v6 2 3 import ( 4 "code.cloudfoundry.org/cli/actor/actionerror" 5 "code.cloudfoundry.org/cli/actor/sharedaction" 6 "code.cloudfoundry.org/cli/actor/v2action" 7 "code.cloudfoundry.org/cli/actor/v2v3action" 8 "code.cloudfoundry.org/cli/actor/v3action" 9 "code.cloudfoundry.org/cli/api/cloudcontroller/ccversion" 10 "code.cloudfoundry.org/cli/command" 11 "code.cloudfoundry.org/cli/command/flag" 12 "code.cloudfoundry.org/cli/command/v6/shared" 13 ) 14 15 //go:generate counterfeiter . UnshareServiceActor 16 17 type UnshareServiceActor interface { 18 UnshareServiceInstanceFromOrganizationNameAndSpaceNameByNameAndSpace(sharedToOrgName string, sharedToSpaceName string, serviceInstanceName string, currentlyTargetedSpaceGUID string) (v2v3action.Warnings, error) 19 CloudControllerV3APIVersion() string 20 } 21 22 type UnshareServiceCommand struct { 23 RequiredArgs flag.ServiceInstance `positional-args:"yes"` 24 SharedToOrgName string `short:"o" required:"false" description:"Org of the other space (Default: targeted org)"` 25 SharedToSpaceName string `short:"s" required:"true" description:"Space to unshare the service instance from"` 26 Force bool `short:"f" description:"Force unshare without confirmation"` 27 usage interface{} `usage:"CF_NAME unshare-service SERVICE_INSTANCE -s OTHER_SPACE [-o OTHER_ORG] [-f]"` 28 relatedCommands interface{} `related_commands:"delete-service, service, services, share-service, unbind-service"` 29 30 UI command.UI 31 Config command.Config 32 SharedActor command.SharedActor 33 Actor UnshareServiceActor 34 } 35 36 func (cmd *UnshareServiceCommand) Setup(config command.Config, ui command.UI) error { 37 cmd.UI = ui 38 cmd.Config = config 39 40 sharedActor := sharedaction.NewActor(config) 41 cmd.SharedActor = sharedActor 42 43 ccClientV3, uaaClientV3, err := shared.NewV3BasedClients(config, ui, true) 44 if err != nil { 45 return err 46 } 47 48 ccClientV2, uaaClientV2, err := shared.GetNewClientsAndConnectToCF(config, ui) 49 if err != nil { 50 return err 51 } 52 53 cmd.Actor = v2v3action.NewActor( 54 v2action.NewActor(ccClientV2, uaaClientV2, config), 55 v3action.NewActor(ccClientV3, config, sharedActor, uaaClientV3), 56 ) 57 58 return nil 59 } 60 61 func (cmd UnshareServiceCommand) Execute(args []string) error { 62 err := command.MinimumCCAPIVersionCheck(cmd.Actor.CloudControllerV3APIVersion(), ccversion.MinVersionShareServiceV3) 63 if err != nil { 64 return err 65 } 66 67 err = cmd.SharedActor.CheckTarget(true, true) 68 if err != nil { 69 return err 70 } 71 72 user, err := cmd.Config.CurrentUser() 73 if err != nil { 74 return err 75 } 76 77 orgName := cmd.Config.TargetedOrganization().Name 78 if cmd.SharedToOrgName != "" { 79 orgName = cmd.SharedToOrgName 80 } 81 82 if !cmd.Force { 83 cmd.UI.DisplayWarning("WARNING: Unsharing this service instance will remove any service bindings that exist in any spaces that this instance is shared into. This could cause applications to stop working.") 84 cmd.UI.DisplayNewline() 85 86 response, promptErr := cmd.UI.DisplayBoolPrompt(false, "Really unshare the service instance?", map[string]interface{}{ 87 "ServiceInstanceName": cmd.RequiredArgs.ServiceInstance, 88 }) 89 if promptErr != nil { 90 return promptErr 91 } 92 if !response { 93 cmd.UI.DisplayText("Unshare cancelled") 94 return nil 95 } 96 } 97 98 cmd.UI.DisplayTextWithFlavor("Unsharing service instance {{.ServiceInstanceName}} from org {{.OrgName}} / space {{.SpaceName}} as {{.Username}}...", map[string]interface{}{ 99 "ServiceInstanceName": cmd.RequiredArgs.ServiceInstance, 100 "OrgName": orgName, 101 "SpaceName": cmd.SharedToSpaceName, 102 "Username": user.Name, 103 }) 104 105 warnings, err := cmd.Actor.UnshareServiceInstanceFromOrganizationNameAndSpaceNameByNameAndSpace(orgName, cmd.SharedToSpaceName, cmd.RequiredArgs.ServiceInstance, cmd.Config.TargetedSpace().GUID) 106 cmd.UI.DisplayWarnings(warnings) 107 if err != nil { 108 switch err.(type) { 109 case actionerror.ServiceInstanceNotSharedToSpaceError: 110 cmd.UI.DisplayText("Service instance {{.ServiceInstanceName}} is not shared with space {{.SpaceName}} in organization {{.OrgName}}.", map[string]interface{}{ 111 "ServiceInstanceName": cmd.RequiredArgs.ServiceInstance, 112 "SpaceName": cmd.SharedToSpaceName, 113 "OrgName": orgName, 114 }) 115 default: 116 return err 117 } 118 } 119 120 cmd.UI.DisplayOK() 121 return nil 122 }