github.com/mook-as/cf-cli@v7.0.0-beta.28.0.20200120190804-b91c115fae48+incompatible/cf/commands/servicekey/service_key.go (about)

     1  package servicekey
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  
     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/requirements"
    13  	"code.cloudfoundry.org/cli/cf/terminal"
    14  
    15  	. "code.cloudfoundry.org/cli/cf/i18n"
    16  )
    17  
    18  type ServiceKey struct {
    19  	ui                         terminal.UI
    20  	config                     coreconfig.Reader
    21  	serviceRepo                api.ServiceRepository
    22  	serviceKeyRepo             api.ServiceKeyRepository
    23  	serviceInstanceRequirement requirements.ServiceInstanceRequirement
    24  }
    25  
    26  func init() {
    27  	commandregistry.Register(&ServiceKey{})
    28  }
    29  
    30  func (cmd *ServiceKey) MetaData() commandregistry.CommandMetadata {
    31  	fs := make(map[string]flags.FlagSet)
    32  	fs["guid"] = &flags.BoolFlag{Name: "guid", Usage: T("Retrieve and display the given service-key's guid.  All other output for the service is suppressed.")}
    33  
    34  	return commandregistry.CommandMetadata{
    35  		Name:        "service-key",
    36  		Description: T("Show service key info"),
    37  		Usage: []string{
    38  			T("CF_NAME service-key SERVICE_INSTANCE SERVICE_KEY"),
    39  		},
    40  		Examples: []string{
    41  			"CF_NAME service-key mydb mykey",
    42  		},
    43  		Flags: fs,
    44  	}
    45  }
    46  
    47  func (cmd *ServiceKey) Requirements(requirementsFactory requirements.Factory, fc flags.FlagContext) ([]requirements.Requirement, error) {
    48  	if len(fc.Args()) != 2 {
    49  		cmd.ui.Failed(T("Incorrect Usage. Requires SERVICE_INSTANCE SERVICE_KEY as arguments\n\n") + commandregistry.Commands.CommandUsage("service-key"))
    50  		return nil, fmt.Errorf("Incorrect usage: %d arguments of %d required", len(fc.Args()), 2)
    51  	}
    52  
    53  	loginRequirement := requirementsFactory.NewLoginRequirement()
    54  	cmd.serviceInstanceRequirement = requirementsFactory.NewServiceInstanceRequirement(fc.Args()[0])
    55  	targetSpaceRequirement := requirementsFactory.NewTargetedSpaceRequirement()
    56  
    57  	reqs := []requirements.Requirement{loginRequirement, cmd.serviceInstanceRequirement, targetSpaceRequirement}
    58  	return reqs, nil
    59  }
    60  
    61  func (cmd *ServiceKey) SetDependency(deps commandregistry.Dependency, pluginCall bool) commandregistry.Command {
    62  	cmd.ui = deps.UI
    63  	cmd.config = deps.Config
    64  	cmd.serviceRepo = deps.RepoLocator.GetServiceRepository()
    65  	cmd.serviceKeyRepo = deps.RepoLocator.GetServiceKeyRepository()
    66  	return cmd
    67  }
    68  
    69  func (cmd *ServiceKey) Execute(c flags.FlagContext) error {
    70  	serviceInstance := cmd.serviceInstanceRequirement.GetServiceInstance()
    71  	serviceKeyName := c.Args()[1]
    72  
    73  	if !c.Bool("guid") {
    74  		cmd.ui.Say(T("Getting key {{.ServiceKeyName}} for service instance {{.ServiceInstanceName}} as {{.CurrentUser}}...",
    75  			map[string]interface{}{
    76  				"ServiceKeyName":      terminal.EntityNameColor(serviceKeyName),
    77  				"ServiceInstanceName": terminal.EntityNameColor(serviceInstance.Name),
    78  				"CurrentUser":         terminal.EntityNameColor(cmd.config.Username()),
    79  			}))
    80  	}
    81  
    82  	serviceKey, err := cmd.serviceKeyRepo.GetServiceKey(serviceInstance.GUID, serviceKeyName)
    83  	if err != nil {
    84  		switch err.(type) {
    85  		case *errors.NotAuthorizedError:
    86  			cmd.ui.Say(T("No service key {{.ServiceKeyName}} found for service instance {{.ServiceInstanceName}}",
    87  				map[string]interface{}{
    88  					"ServiceKeyName":      terminal.EntityNameColor(serviceKeyName),
    89  					"ServiceInstanceName": terminal.EntityNameColor(serviceInstance.Name)}))
    90  			return nil
    91  		default:
    92  			return err
    93  		}
    94  	}
    95  
    96  	if c.Bool("guid") {
    97  		cmd.ui.Say(serviceKey.Fields.GUID)
    98  	} else {
    99  		if serviceKey.Fields.Name == "" {
   100  			return errors.New(
   101  				T("No service key {{.ServiceKeyName}} found for service instance {{.ServiceInstanceName}}",
   102  					map[string]interface{}{
   103  						"ServiceKeyName":      terminal.EntityNameColor(serviceKeyName),
   104  						"ServiceInstanceName": terminal.EntityNameColor(serviceInstance.Name)}))
   105  		}
   106  
   107  		jsonBytes, err := json.MarshalIndent(serviceKey.Credentials, "", " ")
   108  		if err != nil {
   109  			return err
   110  		}
   111  
   112  		cmd.ui.Say("")
   113  		cmd.ui.Say(string(jsonBytes))
   114  	}
   115  	return nil
   116  }