github.com/swisscom/cloudfoundry-cli@v7.1.0+incompatible/cf/commands/servicekey/delete_service_key.go (about)

     1  package servicekey
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"code.cloudfoundry.org/cli/cf/api"
     7  	"code.cloudfoundry.org/cli/cf/commandregistry"
     8  	"code.cloudfoundry.org/cli/cf/configuration/coreconfig"
     9  	"code.cloudfoundry.org/cli/cf/errors"
    10  	"code.cloudfoundry.org/cli/cf/flags"
    11  	"code.cloudfoundry.org/cli/cf/requirements"
    12  	"code.cloudfoundry.org/cli/cf/terminal"
    13  
    14  	. "code.cloudfoundry.org/cli/cf/i18n"
    15  )
    16  
    17  type DeleteServiceKey struct {
    18  	ui             terminal.UI
    19  	config         coreconfig.Reader
    20  	serviceRepo    api.ServiceRepository
    21  	serviceKeyRepo api.ServiceKeyRepository
    22  }
    23  
    24  func init() {
    25  	commandregistry.Register(&DeleteServiceKey{})
    26  }
    27  
    28  func (cmd *DeleteServiceKey) MetaData() commandregistry.CommandMetadata {
    29  	fs := make(map[string]flags.FlagSet)
    30  	fs["f"] = &flags.BoolFlag{ShortName: "f", Usage: T("Force deletion without confirmation")}
    31  
    32  	return commandregistry.CommandMetadata{
    33  		Name:        "delete-service-key",
    34  		ShortName:   "dsk",
    35  		Description: T("Delete a service key"),
    36  		Usage: []string{
    37  			T("CF_NAME delete-service-key SERVICE_INSTANCE SERVICE_KEY [-f]"),
    38  		},
    39  		Examples: []string{
    40  			"CF_NAME delete-service-key mydb mykey",
    41  		},
    42  		Flags: fs,
    43  	}
    44  }
    45  
    46  func (cmd *DeleteServiceKey) Requirements(requirementsFactory requirements.Factory, fc flags.FlagContext) ([]requirements.Requirement, error) {
    47  	if len(fc.Args()) != 2 {
    48  		cmd.ui.Failed(T("Incorrect Usage. Requires SERVICE_INSTANCE SERVICE_KEY as arguments\n\n") + commandregistry.Commands.CommandUsage("delete-service-key"))
    49  		return nil, fmt.Errorf("Incorrect usage: %d arguments of %d required", len(fc.Args()), 2)
    50  	}
    51  
    52  	loginRequirement := requirementsFactory.NewLoginRequirement()
    53  	targetSpaceRequirement := requirementsFactory.NewTargetedSpaceRequirement()
    54  
    55  	reqs := []requirements.Requirement{loginRequirement, targetSpaceRequirement}
    56  	return reqs, nil
    57  }
    58  
    59  func (cmd *DeleteServiceKey) SetDependency(deps commandregistry.Dependency, pluginCall bool) commandregistry.Command {
    60  	cmd.ui = deps.UI
    61  	cmd.config = deps.Config
    62  	cmd.serviceRepo = deps.RepoLocator.GetServiceRepository()
    63  	cmd.serviceKeyRepo = deps.RepoLocator.GetServiceKeyRepository()
    64  	return cmd
    65  }
    66  
    67  func (cmd *DeleteServiceKey) Execute(c flags.FlagContext) error {
    68  	serviceInstanceName := c.Args()[0]
    69  	serviceKeyName := c.Args()[1]
    70  
    71  	if !c.Bool("f") {
    72  		if !cmd.ui.ConfirmDelete(T("service key"), serviceKeyName) {
    73  			return nil
    74  		}
    75  	}
    76  
    77  	cmd.ui.Say(T("Deleting key {{.ServiceKeyName}} for service instance {{.ServiceInstanceName}} as {{.CurrentUser}}...",
    78  		map[string]interface{}{
    79  			"ServiceKeyName":      terminal.EntityNameColor(serviceKeyName),
    80  			"ServiceInstanceName": terminal.EntityNameColor(serviceInstanceName),
    81  			"CurrentUser":         terminal.EntityNameColor(cmd.config.Username()),
    82  		}))
    83  
    84  	serviceInstance, err := cmd.serviceRepo.FindInstanceByName(serviceInstanceName)
    85  	if err != nil {
    86  		cmd.ui.Ok()
    87  		cmd.ui.Warn(T("Service instance {{.ServiceInstanceName}} does not exist.",
    88  			map[string]interface{}{
    89  				"ServiceInstanceName": serviceInstanceName,
    90  			}))
    91  		return nil
    92  	}
    93  
    94  	serviceKey, err := cmd.serviceKeyRepo.GetServiceKey(serviceInstance.GUID, serviceKeyName)
    95  	if err != nil || serviceKey.Fields.GUID == "" {
    96  		switch err.(type) {
    97  		case *errors.NotAuthorizedError:
    98  			cmd.ui.Say(T("No service key {{.ServiceKeyName}} found for service instance {{.ServiceInstanceName}}",
    99  				map[string]interface{}{
   100  					"ServiceKeyName":      terminal.EntityNameColor(serviceKeyName),
   101  					"ServiceInstanceName": terminal.EntityNameColor(serviceInstanceName)}))
   102  			return nil
   103  		default:
   104  			cmd.ui.Ok()
   105  			cmd.ui.Warn(T("Service key {{.ServiceKeyName}} does not exist for service instance {{.ServiceInstanceName}}.",
   106  				map[string]interface{}{
   107  					"ServiceKeyName":      serviceKeyName,
   108  					"ServiceInstanceName": serviceInstanceName,
   109  				}))
   110  			return nil
   111  		}
   112  	}
   113  
   114  	err = cmd.serviceKeyRepo.DeleteServiceKey(serviceKey.Fields.GUID)
   115  	if err != nil {
   116  		return err
   117  	}
   118  
   119  	cmd.ui.Ok()
   120  	return nil
   121  }