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

     1  package securitygroup
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"code.cloudfoundry.org/cli/cf/api/organizations"
     7  	"code.cloudfoundry.org/cli/cf/api/securitygroups"
     8  	sgbinder "code.cloudfoundry.org/cli/cf/api/securitygroups/spaces"
     9  	"code.cloudfoundry.org/cli/cf/api/spaces"
    10  	"code.cloudfoundry.org/cli/cf/commandregistry"
    11  	"code.cloudfoundry.org/cli/cf/configuration/coreconfig"
    12  	"code.cloudfoundry.org/cli/cf/flags"
    13  	. "code.cloudfoundry.org/cli/cf/i18n"
    14  	"code.cloudfoundry.org/cli/cf/models"
    15  	"code.cloudfoundry.org/cli/cf/requirements"
    16  	"code.cloudfoundry.org/cli/cf/terminal"
    17  )
    18  
    19  type BindSecurityGroup struct {
    20  	ui                terminal.UI
    21  	configRepo        coreconfig.Reader
    22  	orgRepo           organizations.OrganizationRepository
    23  	spaceRepo         spaces.SpaceRepository
    24  	securityGroupRepo securitygroups.SecurityGroupRepo
    25  	spaceBinder       sgbinder.SecurityGroupSpaceBinder
    26  }
    27  
    28  func init() {
    29  	commandregistry.Register(&BindSecurityGroup{})
    30  }
    31  
    32  func (cmd *BindSecurityGroup) MetaData() commandregistry.CommandMetadata {
    33  	primaryUsage := T("CF_NAME bind-security-group SECURITY_GROUP ORG [SPACE]")
    34  	tipUsage := T("TIP: Changes will not apply to existing running applications until they are restarted.")
    35  	return commandregistry.CommandMetadata{
    36  		Name:        "bind-security-group",
    37  		Description: T("Bind a security group to a particular space, or all existing spaces of an org"),
    38  		Usage: []string{
    39  			primaryUsage,
    40  			"\n\n",
    41  			tipUsage,
    42  		},
    43  	}
    44  }
    45  
    46  func (cmd *BindSecurityGroup) Requirements(requirementsFactory requirements.Factory, fc flags.FlagContext) ([]requirements.Requirement, error) {
    47  	if len(fc.Args()) != 2 && len(fc.Args()) != 3 {
    48  		cmd.ui.Failed(T("Incorrect Usage. Requires SECURITY_GROUP and ORG, optional SPACE as arguments\n\n") + commandregistry.Commands.CommandUsage("bind-security-group"))
    49  		return nil, fmt.Errorf("Incorrect usage: %d arguments of %d required", len(fc.Args()), 3)
    50  	}
    51  
    52  	reqs := []requirements.Requirement{}
    53  	reqs = append(reqs, requirementsFactory.NewLoginRequirement())
    54  	return reqs, nil
    55  }
    56  
    57  func (cmd *BindSecurityGroup) SetDependency(deps commandregistry.Dependency, pluginCall bool) commandregistry.Command {
    58  	cmd.ui = deps.UI
    59  	cmd.configRepo = deps.Config
    60  	cmd.spaceRepo = deps.RepoLocator.GetSpaceRepository()
    61  	cmd.orgRepo = deps.RepoLocator.GetOrganizationRepository()
    62  	cmd.securityGroupRepo = deps.RepoLocator.GetSecurityGroupRepository()
    63  	cmd.spaceBinder = deps.RepoLocator.GetSecurityGroupSpaceBinder()
    64  	return cmd
    65  }
    66  
    67  func (cmd *BindSecurityGroup) Execute(context flags.FlagContext) error {
    68  	securityGroupName := context.Args()[0]
    69  	orgName := context.Args()[1]
    70  
    71  	securityGroup, err := cmd.securityGroupRepo.Read(securityGroupName)
    72  	if err != nil {
    73  		return err
    74  	}
    75  
    76  	org, err := cmd.orgRepo.FindByName(orgName)
    77  	if err != nil {
    78  		return err
    79  	}
    80  
    81  	spaces := []models.Space{}
    82  	if len(context.Args()) > 2 {
    83  		var space models.Space
    84  		space, err = cmd.spaceRepo.FindByNameInOrg(context.Args()[2], org.GUID)
    85  		if err != nil {
    86  			return err
    87  		}
    88  
    89  		spaces = append(spaces, space)
    90  	} else {
    91  		err = cmd.spaceRepo.ListSpacesFromOrg(org.GUID, func(space models.Space) bool {
    92  			spaces = append(spaces, space)
    93  			return true
    94  		})
    95  		if err != nil {
    96  			return err
    97  		}
    98  	}
    99  
   100  	for _, space := range spaces {
   101  		cmd.ui.Say(T("Assigning security group {{.security_group}} to space {{.space}} in org {{.organization}} as {{.username}}...",
   102  			map[string]interface{}{
   103  				"security_group": terminal.EntityNameColor(securityGroupName),
   104  				"space":          terminal.EntityNameColor(space.Name),
   105  				"organization":   terminal.EntityNameColor(orgName),
   106  				"username":       terminal.EntityNameColor(cmd.configRepo.Username()),
   107  			}))
   108  		err = cmd.spaceBinder.BindSpace(securityGroup.GUID, space.GUID)
   109  		if err != nil {
   110  			return err
   111  		}
   112  		cmd.ui.Ok()
   113  		cmd.ui.Say("")
   114  	}
   115  
   116  	cmd.ui.Say(T("TIP: Changes will not apply to existing running applications until they are restarted."))
   117  	return nil
   118  }