Example #1
0
func (cmd *UpdateSecurityGroup) Execute(context flags.FlagContext) error {
	name := context.Args()[0]
	securityGroup, err := cmd.securityGroupRepo.Read(name)
	if err != nil {
		return err
	}

	pathToJSONFile := context.Args()[1]
	rules, err := json.ParseJSONArray(pathToJSONFile)
	if err != nil {
		return err
	}

	cmd.ui.Say(T("Updating security group {{.security_group}} as {{.username}}",
		map[string]interface{}{
			"security_group": terminal.EntityNameColor(name),
			"username":       terminal.EntityNameColor(cmd.configRepo.Username()),
		}))
	err = cmd.securityGroupRepo.Update(securityGroup.GUID, rules)
	if err != nil {
		return err
	}

	cmd.ui.Ok()
	cmd.ui.Say("\n\n")
	cmd.ui.Say(T("TIP: Changes will not apply to existing running applications until they are restarted."))
	return nil
}
Example #2
0
func (cmd *CreateSecurityGroup) Execute(context flags.FlagContext) error {
	name := context.Args()[0]
	pathToJSONFile := context.Args()[1]
	rules, err := json.ParseJSONArray(pathToJSONFile)
	if err != nil {
		return errors.New(T(`Incorrect json format: file: {{.JSONFile}}
		
Valid json file example:
[
  {
    "protocol": "tcp",
    "destination": "10.244.1.18",
    "ports": "3306"
  }
]`, map[string]interface{}{"JSONFile": pathToJSONFile}))
	}

	cmd.ui.Say(T("Creating security group {{.security_group}} as {{.username}}",
		map[string]interface{}{
			"security_group": terminal.EntityNameColor(name),
			"username":       terminal.EntityNameColor(cmd.configRepo.Username()),
		}))

	err = cmd.securityGroupRepo.Create(name, rules)

	httpErr, ok := err.(errors.HTTPError)
	if ok && httpErr.ErrorCode() == errors.SecurityGroupNameTaken {
		cmd.ui.Ok()
		cmd.ui.Warn(T("Security group {{.security_group}} {{.error_message}}",
			map[string]interface{}{
				"security_group": terminal.EntityNameColor(name),
				"error_message":  terminal.WarningColor(T("already exists")),
			}))
		return nil
	}

	if err != nil {
		return err
	}

	cmd.ui.Ok()
	return nil
}
Example #3
0
		var tmpFile *os.File

		Context("when everything is proper", func() {
			BeforeEach(func() {
				tmpFile, _ = ioutil.TempFile("", "WONDERFULFILEWHOSENAMEISHARDTOREADBUTCONTAINSVALIDJSON")
				filename = tmpFile.Name()
				ioutil.WriteFile(filename, []byte("[{\"akey\": \"avalue\"}]"), 0644)
			})

			AfterEach(func() {
				tmpFile.Close()
				os.Remove(tmpFile.Name())
			})

			It("converts a json file into an unmarshalled slice of string->string map objects", func() {
				stringMaps, err := json.ParseJSONArray(filename)
				Expect(err).To(BeNil())
				Expect(stringMaps[0]["akey"]).To(Equal("avalue"))
			})
		})

		Context("when the JSON is invalid", func() {
			BeforeEach(func() {
				tmpFile, _ = ioutil.TempFile("", "TERRIBLEFILECONTAININGINVALIDJSONWHICHMAKESEVERYTHINGTERRIBLEANDSTILLHASANAMETHATSHARDTOREAD")
				filename = tmpFile.Name()
				ioutil.WriteFile(filename, []byte("SCARY NOISES}"), 0644)
			})

			AfterEach(func() {
				tmpFile.Close()
				os.Remove(tmpFile.Name())