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

     1  package user
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"code.cloudfoundry.org/cli/cf/actors/userprint"
     7  	"code.cloudfoundry.org/cli/cf/api"
     8  	"code.cloudfoundry.org/cli/cf/api/spaces"
     9  	"code.cloudfoundry.org/cli/cf/commandregistry"
    10  	"code.cloudfoundry.org/cli/cf/configuration/coreconfig"
    11  	"code.cloudfoundry.org/cli/cf/flags"
    12  	. "code.cloudfoundry.org/cli/cf/i18n"
    13  	"code.cloudfoundry.org/cli/cf/models"
    14  	"code.cloudfoundry.org/cli/cf/requirements"
    15  	"code.cloudfoundry.org/cli/cf/terminal"
    16  	"code.cloudfoundry.org/cli/plugin/models"
    17  )
    18  
    19  type SpaceUsers struct {
    20  	ui          terminal.UI
    21  	config      coreconfig.Reader
    22  	spaceRepo   spaces.SpaceRepository
    23  	userRepo    api.UserRepository
    24  	orgReq      requirements.OrganizationRequirement
    25  	pluginModel *[]plugin_models.GetSpaceUsers_Model
    26  	pluginCall  bool
    27  }
    28  
    29  func init() {
    30  	commandregistry.Register(&SpaceUsers{})
    31  }
    32  
    33  func (cmd *SpaceUsers) MetaData() commandregistry.CommandMetadata {
    34  	return commandregistry.CommandMetadata{
    35  		Name:        "space-users",
    36  		Description: T("Show space users by role"),
    37  		Usage: []string{
    38  			T("CF_NAME space-users ORG SPACE"),
    39  		},
    40  	}
    41  }
    42  
    43  func (cmd *SpaceUsers) Requirements(requirementsFactory requirements.Factory, fc flags.FlagContext) ([]requirements.Requirement, error) {
    44  	if len(fc.Args()) != 2 {
    45  		cmd.ui.Failed(T("Incorrect Usage. Requires arguments\n\n") + commandregistry.Commands.CommandUsage("space-users"))
    46  		return nil, fmt.Errorf("Incorrect usage: %d arguments of %d required", len(fc.Args()), 2)
    47  	}
    48  
    49  	cmd.orgReq = requirementsFactory.NewOrganizationRequirement(fc.Args()[0])
    50  
    51  	reqs := []requirements.Requirement{
    52  		requirementsFactory.NewLoginRequirement(),
    53  		cmd.orgReq,
    54  	}
    55  
    56  	return reqs, nil
    57  }
    58  
    59  func (cmd *SpaceUsers) SetDependency(deps commandregistry.Dependency, pluginCall bool) commandregistry.Command {
    60  	cmd.ui = deps.UI
    61  	cmd.config = deps.Config
    62  	cmd.userRepo = deps.RepoLocator.GetUserRepository()
    63  	cmd.spaceRepo = deps.RepoLocator.GetSpaceRepository()
    64  	cmd.pluginCall = pluginCall
    65  	cmd.pluginModel = deps.PluginModels.SpaceUsers
    66  
    67  	return cmd
    68  }
    69  
    70  func (cmd *SpaceUsers) Execute(c flags.FlagContext) error {
    71  	spaceName := c.Args()[1]
    72  	org := cmd.orgReq.GetOrganization()
    73  
    74  	space, err := cmd.spaceRepo.FindByNameInOrg(spaceName, org.GUID)
    75  	if err != nil {
    76  		return err
    77  	}
    78  
    79  	printer := cmd.printer(org, space, cmd.config.Username())
    80  	printer.PrintUsers(space.GUID, cmd.config.Username())
    81  	return nil
    82  }
    83  
    84  func (cmd *SpaceUsers) printer(org models.Organization, space models.Space, username string) userprint.UserPrinter {
    85  	var roles = []models.Role{models.RoleSpaceManager, models.RoleSpaceDeveloper, models.RoleSpaceAuditor}
    86  
    87  	if cmd.pluginCall {
    88  		return userprint.NewSpaceUsersPluginPrinter(
    89  			cmd.pluginModel,
    90  			cmd.userRepo.ListUsersInSpaceForRoleWithNoUAA,
    91  			roles,
    92  		)
    93  	}
    94  
    95  	cmd.ui.Say(T("Getting users in org {{.TargetOrg}} / space {{.TargetSpace}} as {{.CurrentUser}}",
    96  		map[string]interface{}{
    97  			"TargetOrg":   terminal.EntityNameColor(org.Name),
    98  			"TargetSpace": terminal.EntityNameColor(space.Name),
    99  			"CurrentUser": terminal.EntityNameColor(username),
   100  		}))
   101  
   102  	return &userprint.SpaceUsersUIPrinter{
   103  		UI:         cmd.ui,
   104  		UserLister: cmd.userRepo.ListUsersInSpaceForRoleWithNoUAA,
   105  		Roles:      roles,
   106  		RoleDisplayNames: map[models.Role]string{
   107  			models.RoleSpaceManager:   T("SPACE MANAGER"),
   108  			models.RoleSpaceDeveloper: T("SPACE DEVELOPER"),
   109  			models.RoleSpaceAuditor:   T("SPACE AUDITOR"),
   110  		},
   111  	}
   112  }