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

     1  package securitygroup
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"code.cloudfoundry.org/cli/cf/flags"
     7  	. "code.cloudfoundry.org/cli/cf/i18n"
     8  
     9  	"code.cloudfoundry.org/cli/cf/api/securitygroups"
    10  	"code.cloudfoundry.org/cli/cf/commandregistry"
    11  	"code.cloudfoundry.org/cli/cf/configuration/coreconfig"
    12  	"code.cloudfoundry.org/cli/cf/requirements"
    13  	"code.cloudfoundry.org/cli/cf/terminal"
    14  	"code.cloudfoundry.org/cli/cf/util/json"
    15  )
    16  
    17  type UpdateSecurityGroup struct {
    18  	ui                terminal.UI
    19  	securityGroupRepo securitygroups.SecurityGroupRepo
    20  	configRepo        coreconfig.Reader
    21  }
    22  
    23  func init() {
    24  	commandregistry.Register(&UpdateSecurityGroup{})
    25  }
    26  
    27  func (cmd *UpdateSecurityGroup) MetaData() commandregistry.CommandMetadata {
    28  	primaryUsage := T("CF_NAME update-security-group SECURITY_GROUP PATH_TO_JSON_RULES_FILE")
    29  	secondaryUsage := T("   The provided path can be an absolute or relative path to a file.\n   It should have a single array with JSON objects inside describing the rules.")
    30  	tipUsage := T("TIP: Changes will not apply to existing running applications until they are restarted.")
    31  	return commandregistry.CommandMetadata{
    32  		Name:        "update-security-group",
    33  		Description: T("Update a security group"),
    34  		Usage: []string{
    35  			primaryUsage,
    36  			"\n\n",
    37  			secondaryUsage,
    38  			"\n\n",
    39  			tipUsage,
    40  		},
    41  	}
    42  }
    43  
    44  func (cmd *UpdateSecurityGroup) Requirements(requirementsFactory requirements.Factory, fc flags.FlagContext) ([]requirements.Requirement, error) {
    45  	if len(fc.Args()) != 2 {
    46  		cmd.ui.Failed(T("Incorrect Usage. Requires SECURITY_GROUP and PATH_TO_JSON_RULES_FILE as arguments\n\n") + commandregistry.Commands.CommandUsage("update-security-group"))
    47  		return nil, fmt.Errorf("Incorrect usage: %d arguments of %d required", len(fc.Args()), 2)
    48  	}
    49  
    50  	reqs := []requirements.Requirement{requirementsFactory.NewLoginRequirement()}
    51  	return reqs, nil
    52  }
    53  
    54  func (cmd *UpdateSecurityGroup) SetDependency(deps commandregistry.Dependency, pluginCall bool) commandregistry.Command {
    55  	cmd.ui = deps.UI
    56  	cmd.configRepo = deps.Config
    57  	cmd.securityGroupRepo = deps.RepoLocator.GetSecurityGroupRepository()
    58  	return cmd
    59  }
    60  
    61  func (cmd *UpdateSecurityGroup) Execute(context flags.FlagContext) error {
    62  	name := context.Args()[0]
    63  	securityGroup, err := cmd.securityGroupRepo.Read(name)
    64  	if err != nil {
    65  		return err
    66  	}
    67  
    68  	pathToJSONFile := context.Args()[1]
    69  	rules, err := json.ParseJSONArray(pathToJSONFile)
    70  	if err != nil {
    71  		return err
    72  	}
    73  
    74  	cmd.ui.Say(T("Updating security group {{.security_group}} as {{.username}}",
    75  		map[string]interface{}{
    76  			"security_group": terminal.EntityNameColor(name),
    77  			"username":       terminal.EntityNameColor(cmd.configRepo.Username()),
    78  		}))
    79  	err = cmd.securityGroupRepo.Update(securityGroup.GUID, rules)
    80  	if err != nil {
    81  		return err
    82  	}
    83  
    84  	cmd.ui.Ok()
    85  	cmd.ui.Say("\n\n")
    86  	cmd.ui.Say(T("TIP: Changes will not apply to existing running applications until they are restarted."))
    87  	return nil
    88  }