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

     1  package securitygroup
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  
     7  	"code.cloudfoundry.org/cli/cf/flags"
     8  	. "code.cloudfoundry.org/cli/cf/i18n"
     9  
    10  	"code.cloudfoundry.org/cli/cf/api/securitygroups"
    11  	"code.cloudfoundry.org/cli/cf/commandregistry"
    12  	"code.cloudfoundry.org/cli/cf/configuration/coreconfig"
    13  	"code.cloudfoundry.org/cli/cf/requirements"
    14  	"code.cloudfoundry.org/cli/cf/terminal"
    15  )
    16  
    17  type ShowSecurityGroup struct {
    18  	ui                terminal.UI
    19  	securityGroupRepo securitygroups.SecurityGroupRepo
    20  	configRepo        coreconfig.Reader
    21  }
    22  
    23  func init() {
    24  	commandregistry.Register(&ShowSecurityGroup{})
    25  }
    26  
    27  func (cmd *ShowSecurityGroup) MetaData() commandregistry.CommandMetadata {
    28  	return commandregistry.CommandMetadata{
    29  		Name:        "security-group",
    30  		Description: T("Show a single security group"),
    31  		Usage: []string{
    32  			T("CF_NAME security-group SECURITY_GROUP"),
    33  		},
    34  	}
    35  }
    36  
    37  func (cmd *ShowSecurityGroup) Requirements(requirementsFactory requirements.Factory, fc flags.FlagContext) ([]requirements.Requirement, error) {
    38  	if len(fc.Args()) != 1 {
    39  		cmd.ui.Failed(T("Incorrect Usage. Requires an argument\n\n") + commandregistry.Commands.CommandUsage("security-group"))
    40  		return nil, fmt.Errorf("Incorrect usage: %d arguments of %d required", len(fc.Args()), 1)
    41  	}
    42  
    43  	reqs := []requirements.Requirement{
    44  		requirementsFactory.NewLoginRequirement(),
    45  	}
    46  
    47  	return reqs, nil
    48  }
    49  
    50  func (cmd *ShowSecurityGroup) SetDependency(deps commandregistry.Dependency, pluginCall bool) commandregistry.Command {
    51  	cmd.ui = deps.UI
    52  	cmd.configRepo = deps.Config
    53  	cmd.securityGroupRepo = deps.RepoLocator.GetSecurityGroupRepository()
    54  	return cmd
    55  }
    56  
    57  func (cmd *ShowSecurityGroup) Execute(c flags.FlagContext) error {
    58  	name := c.Args()[0]
    59  
    60  	cmd.ui.Say(T("Getting info for security group {{.security_group}} as {{.username}}",
    61  		map[string]interface{}{
    62  			"security_group": terminal.EntityNameColor(name),
    63  			"username":       terminal.EntityNameColor(cmd.configRepo.Username()),
    64  		}))
    65  
    66  	securityGroup, err := cmd.securityGroupRepo.Read(name)
    67  	if err != nil {
    68  		return err
    69  	}
    70  
    71  	jsonEncodedBytes, err := json.MarshalIndent(securityGroup.Rules, "\t", "\t")
    72  	if err != nil {
    73  		return err
    74  	}
    75  
    76  	cmd.ui.Ok()
    77  	table := cmd.ui.Table([]string{"", ""})
    78  	table.Add(T("Name"), securityGroup.Name)
    79  	table.Add(T("Rules"), "")
    80  	err = table.Print()
    81  	if err != nil {
    82  		return err
    83  	}
    84  	cmd.ui.Say("\t" + string(jsonEncodedBytes))
    85  
    86  	cmd.ui.Say("")
    87  
    88  	if len(securityGroup.Spaces) > 0 {
    89  		table = cmd.ui.Table([]string{"", T("Organization"), T("Space")})
    90  
    91  		for index, space := range securityGroup.Spaces {
    92  			table.Add(fmt.Sprintf("#%d", index), space.Organization.Name, space.Name)
    93  		}
    94  		err = table.Print()
    95  		if err != nil {
    96  			return err
    97  		}
    98  	} else {
    99  		cmd.ui.Say(T("No spaces assigned"))
   100  	}
   101  	return nil
   102  }