github.com/loggregator/cli@v6.33.1-0.20180224010324-82334f081791+incompatible/cf/commands/service/rename_service.go (about)

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