github.com/orange-cloudfoundry/cli@v7.1.0+incompatible/command/v6/share_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 . ShareServiceActor
    16  
    17  type ShareServiceActor interface {
    18  	ShareServiceInstanceToSpaceNameByNameAndSpaceAndOrganizationName(sharedToSpaceName string, serviceInstanceName string, sourceSpaceGUID string, sharedToOrgName string) (v2v3action.Warnings, error)
    19  	ShareServiceInstanceToSpaceNameByNameAndSpaceAndOrganization(sharedToSpaceName string, serviceInstanceName string, sourceSpaceGUID string, sharedToOrgGUID string) (v2v3action.Warnings, error)
    20  	CloudControllerV3APIVersion() string
    21  }
    22  
    23  type ShareServiceCommand struct {
    24  	RequiredArgs    flag.ServiceInstance `positional-args:"yes"`
    25  	OrgName         string               `short:"o" required:"false" description:"Org of the other space (Default: targeted org)"`
    26  	SpaceName       string               `short:"s" required:"true" description:"Space to share the service instance into"`
    27  	usage           interface{}          `usage:"CF_NAME share-service SERVICE_INSTANCE -s OTHER_SPACE [-o OTHER_ORG]"`
    28  	relatedCommands interface{}          `related_commands:"bind-service, service, services, unshare-service"`
    29  
    30  	UI          command.UI
    31  	Config      command.Config
    32  	SharedActor command.SharedActor
    33  	Actor       ShareServiceActor
    34  }
    35  
    36  func (cmd *ShareServiceCommand) 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 ShareServiceCommand) Execute(args []string) error {
    62  	err := command.MinimumCCAPIVersionCheck(cmd.Actor.CloudControllerV3APIVersion(), ccversion.MinSupportedV3ClientVersion)
    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.OrgName != "" {
    79  		orgName = cmd.OrgName
    80  	}
    81  
    82  	cmd.UI.DisplayTextWithFlavor("Sharing service instance {{.ServiceInstanceName}} into org {{.OrgName}} / space {{.SpaceName}} as {{.Username}}...", map[string]interface{}{
    83  		"ServiceInstanceName": cmd.RequiredArgs.ServiceInstance,
    84  		"OrgName":             orgName,
    85  		"SpaceName":           cmd.SpaceName,
    86  		"Username":            user.Name,
    87  	})
    88  
    89  	var warnings v2v3action.Warnings
    90  
    91  	if cmd.OrgName != "" {
    92  		warnings, err = cmd.Actor.ShareServiceInstanceToSpaceNameByNameAndSpaceAndOrganizationName(cmd.SpaceName, cmd.RequiredArgs.ServiceInstance, cmd.Config.TargetedSpace().GUID, cmd.OrgName)
    93  	} else {
    94  		warnings, err = cmd.Actor.ShareServiceInstanceToSpaceNameByNameAndSpaceAndOrganization(cmd.SpaceName, cmd.RequiredArgs.ServiceInstance, cmd.Config.TargetedSpace().GUID, cmd.Config.TargetedOrganization().GUID)
    95  	}
    96  
    97  	cmd.UI.DisplayWarnings(warnings)
    98  	if err != nil {
    99  		switch err.(type) {
   100  		case actionerror.ServiceInstanceAlreadySharedError:
   101  			cmd.UI.DisplayText("Service instance {{.ServiceInstanceName}} is already shared with that space.", map[string]interface{}{
   102  				"ServiceInstanceName": cmd.RequiredArgs.ServiceInstance,
   103  			})
   104  		default:
   105  			return err
   106  		}
   107  	}
   108  
   109  	cmd.UI.DisplayOK()
   110  	return nil
   111  }