github.com/rakutentech/cli@v6.12.5-0.20151006231303-24468b65536e+incompatible/cf/commands/service/rename_service.go (about)

     1  package service
     2  
     3  import (
     4  	"github.com/cloudfoundry/cli/cf"
     5  	"github.com/cloudfoundry/cli/cf/api"
     6  	"github.com/cloudfoundry/cli/cf/command_registry"
     7  	"github.com/cloudfoundry/cli/cf/configuration/core_config"
     8  	"github.com/cloudfoundry/cli/cf/errors"
     9  	. "github.com/cloudfoundry/cli/cf/i18n"
    10  	"github.com/cloudfoundry/cli/cf/requirements"
    11  	"github.com/cloudfoundry/cli/cf/terminal"
    12  	"github.com/cloudfoundry/cli/flags"
    13  )
    14  
    15  type RenameService struct {
    16  	ui                 terminal.UI
    17  	config             core_config.Reader
    18  	serviceRepo        api.ServiceRepository
    19  	serviceInstanceReq requirements.ServiceInstanceRequirement
    20  }
    21  
    22  func init() {
    23  	command_registry.Register(&RenameService{})
    24  }
    25  
    26  func (cmd *RenameService) MetaData() command_registry.CommandMetadata {
    27  	return command_registry.CommandMetadata{
    28  		Name:        "rename-service",
    29  		Description: T("Rename a service instance"),
    30  		Usage:       T("CF_NAME rename-service SERVICE_INSTANCE NEW_SERVICE_INSTANCE"),
    31  	}
    32  }
    33  
    34  func (cmd *RenameService) Requirements(requirementsFactory requirements.Factory, fc flags.FlagContext) (reqs []requirements.Requirement, err error) {
    35  	if len(fc.Args()) != 2 {
    36  		cmd.ui.Failed(T("Incorrect Usage. Requires SERVICE_INSTANCE and NEW_SERVICE_INSTANCE as arguments\n\n") + command_registry.Commands.CommandUsage("rename-service"))
    37  	}
    38  
    39  	cmd.serviceInstanceReq = requirementsFactory.NewServiceInstanceRequirement(fc.Args()[0])
    40  
    41  	reqs = []requirements.Requirement{
    42  		requirementsFactory.NewLoginRequirement(),
    43  		requirementsFactory.NewTargetedSpaceRequirement(),
    44  		cmd.serviceInstanceReq,
    45  	}
    46  
    47  	return
    48  }
    49  
    50  func (cmd *RenameService) SetDependency(deps command_registry.Dependency, pluginCall bool) command_registry.Command {
    51  	cmd.ui = deps.Ui
    52  	cmd.config = deps.Config
    53  	cmd.serviceRepo = deps.RepoLocator.GetServiceRepository()
    54  	return cmd
    55  }
    56  
    57  func (cmd *RenameService) Execute(c flags.FlagContext) {
    58  	newName := c.Args()[1]
    59  	serviceInstance := cmd.serviceInstanceReq.GetServiceInstance()
    60  
    61  	cmd.ui.Say(T("Renaming service {{.ServiceName}} to {{.NewServiceName}} in org {{.OrgName}} / space {{.SpaceName}} as {{.CurrentUser}}...",
    62  		map[string]interface{}{
    63  			"ServiceName":    terminal.EntityNameColor(serviceInstance.Name),
    64  			"NewServiceName": terminal.EntityNameColor(newName),
    65  			"OrgName":        terminal.EntityNameColor(cmd.config.OrganizationFields().Name),
    66  			"SpaceName":      terminal.EntityNameColor(cmd.config.SpaceFields().Name),
    67  			"CurrentUser":    terminal.EntityNameColor(cmd.config.Username()),
    68  		}))
    69  	err := cmd.serviceRepo.RenameService(serviceInstance, newName)
    70  
    71  	if err != nil {
    72  		if httpError, ok := err.(errors.HttpError); ok && httpError.ErrorCode() == errors.SERVICE_INSTANCE_NAME_TAKEN {
    73  			cmd.ui.Failed(T("{{.ErrorDescription}}\nTIP: Use '{{.CFServicesCommand}}' to view all services in this org and space.",
    74  				map[string]interface{}{
    75  					"ErrorDescription":  httpError.Error(),
    76  					"CFServicesCommand": cf.Name() + " " + "services",
    77  				}))
    78  		} else {
    79  			cmd.ui.Failed(err.Error())
    80  		}
    81  	}
    82  
    83  	cmd.ui.Ok()
    84  }