예제 #1
0
파일: lb.go 프로젝트: mikebeyer/clc-cli
func deletePool(client *clc.Client) cli.Command {
	return cli.Command{
		Name:    "delete-pool",
		Aliases: []string{"dp"},
		Usage:   "delete load balancer pool",
		Flags: []cli.Flag{
			cli.StringFlag{Name: "id", Usage: "load balancer id [required]"},
			cli.StringFlag{Name: "location, l", Usage: "load balancer location [required]"},
			cli.StringFlag{Name: "pool, p", Usage: "load balancer pool id [required]"},
		},
		Before: func(c *cli.Context) error {
			if c.String("location") == "" || c.String("id") == "" || c.String("pool") == "" {
				fmt.Printf("missing required flags to get pool. [use --help to show required flags]\n")
				return fmt.Errorf("")
			}
			return nil
		},
		Action: func(c *cli.Context) {
			client, err := util.MaybeLoadConfig(c, client)
			if err != nil {
				fmt.Println(err.Error())
				return
			}
			err = client.LB.DeletePool(c.String("location"), c.String("id"), c.String("pool"))
			if err != nil {
				fmt.Printf("failed to get %s\n", c.Args().First())
				return
			}
			fmt.Printf("sucessfully deleted pool [%s] from lb [%s]\n", c.String("pool"), c.String("id"))
		},
	}
}
예제 #2
0
파일: status.go 프로젝트: mikebeyer/clc-cli
func get(client *clc.Client) cli.Command {
	return cli.Command{
		Name:    "get",
		Aliases: []string{"g"},
		Usage:   "get status",
		Before: func(c *cli.Context) error {
			if c.Args().First() == "" {
				fmt.Println("usage: get [id]")
				return errors.New("")
			}
			return nil
		},
		Action: func(c *cli.Context) {
			client, err := util.MaybeLoadConfig(c, client)
			if err != nil {
				fmt.Println(err.Error())
				return
			}
			status, err := client.Status.Get(c.Args().First())
			if err != nil {
				fmt.Printf("failed to get status of %s", c.Args().First())
				return
			}
			b, err := json.MarshalIndent(status, "", "  ")
			if err != nil {
				fmt.Printf("%s", err)
				return
			}
			fmt.Printf("%s\n", b)
		},
	}
}
예제 #3
0
파일: alert.go 프로젝트: mikebeyer/clc-cli
func delete(client *clc.Client) cli.Command {
	return cli.Command{
		Name:    "delete",
		Aliases: []string{"d"},
		Usage:   "delete alert policy",
		Before: func(c *cli.Context) error {
			if c.Args().First() == "" {
				fmt.Println("usage: delete [policy]")
				return errors.New("")
			}
			return nil
		},
		Action: func(c *cli.Context) {
			client, err := util.MaybeLoadConfig(c, client)
			if err != nil {
				fmt.Println(err.Error())
				return
			}
			err = client.Alert.Delete(c.Args().First())
			if err != nil {
				fmt.Printf("failed to get %s", c.Args().First())
				return
			}
			fmt.Printf("deleted policy %s\n", c.Args().First())
		},
	}
}
예제 #4
0
파일: server.go 프로젝트: mikebeyer/clc-cli
func get(client *clc.Client) cli.Command {
	return cli.Command{
		Name:    "get",
		Aliases: []string{"g"},
		Usage:   "get server details",
		Before: func(c *cli.Context) error {
			return util.CheckArgs(c)
		},
		Action: func(c *cli.Context) {
			client, err := util.MaybeLoadConfig(c, client)
			if err != nil {
				fmt.Println(err.Error())
				return
			}
			server, err := client.Server.Get(c.Args().First())
			if err != nil {
				fmt.Printf("failed to get %s\n", c.Args().First())
				return
			}
			b, err := json.MarshalIndent(server, "", "  ")
			if err != nil {
				fmt.Printf("%s", err)
				return
			}
			fmt.Printf("%s\n", b)
		},
	}
}
예제 #5
0
파일: server.go 프로젝트: mikebeyer/clc-cli
func deleteIP(client *clc.Client) cli.Command {
	return cli.Command{
		Name:    "delete",
		Aliases: []string{"d"},
		Usage:   "delete public ip",
		Flags: []cli.Flag{
			cli.StringFlag{Name: "name, n", Usage: "server name [required]"},
			cli.StringFlag{Name: "ip", Usage: "ip [required]"},
		},
		Before: func(c *cli.Context) error {
			return util.CheckStringFlag(c, "name", "ip")
		},
		Action: func(c *cli.Context) {
			client, err := util.MaybeLoadConfig(c, client)
			if err != nil {
				fmt.Println(err.Error())
				return
			}
			resp, err := client.Server.DeletePublicIP(c.String("name"), c.String("ip"))
			if err != nil {
				fmt.Printf("err %s\n", err)
				return
			}
			b, err := json.MarshalIndent(resp, "", "  ")
			if err != nil {
				fmt.Printf("%s", err)
				return
			}
			fmt.Printf("%s\n", b)
		},
	}
}
예제 #6
0
파일: server.go 프로젝트: mikebeyer/clc-cli
func restore(client *clc.Client) cli.Command {
	return cli.Command{
		Name:    "restore",
		Aliases: []string{"r"},
		Usage:   "restore server",
		Flags: []cli.Flag{
			cli.StringFlag{Name: "name, n", Usage: "name of server to restore [required]"},
			cli.StringFlag{Name: "group, g", Usage: "group for server to restore to [required]"},
		},
		Before: func(c *cli.Context) error {
			return util.CheckStringFlag(c, "name", "group")
		},
		Action: func(c *cli.Context) {
			client, err := util.MaybeLoadConfig(c, client)
			if err != nil {
				fmt.Println(err.Error())
				return
			}
			resp, err := client.Server.Restore(c.String("name"), c.String("group"))
			if err != nil {
				fmt.Printf("failed to restore %s", c.String("name"))
				return
			}
			b, err := json.MarshalIndent(resp, "", "  ")
			if err != nil {
				fmt.Printf("%s", err)
				return
			}
			fmt.Printf("%s\n", b)
		},
	}
}
예제 #7
0
파일: server.go 프로젝트: mikebeyer/clc-cli
func archive(client *clc.Client) cli.Command {
	return cli.Command{
		Name:    "archive",
		Aliases: []string{"a"},
		Usage:   "archive server",
		Flags: []cli.Flag{
			cli.StringSliceFlag{Name: "name, n", Usage: "name of servers to archive"},
		},
		Before: func(c *cli.Context) error {
			return util.CheckStringSliceFlag(c, "name")
		},
		Action: func(c *cli.Context) {
			client, err := util.MaybeLoadConfig(c, client)
			if err != nil {
				fmt.Println(err.Error())
				return
			}
			resp, err := client.Server.Archive(c.StringSlice("name")...)
			if err != nil {
				fmt.Printf("failed to archive %s", strings.Join(c.StringSlice("name"), ", "))
				return
			}
			b, err := json.MarshalIndent(resp, "", "  ")
			if err != nil {
				fmt.Printf("%s", err)
				return
			}
			fmt.Printf("%s\n", b)
		},
	}
}
예제 #8
0
파일: group.go 프로젝트: mikebeyer/clc-cli
func delete(client *clc.Client) cli.Command {
	return cli.Command{
		Name:    "delete",
		Aliases: []string{"d"},
		Usage:   "delete group details",
		Before: func(c *cli.Context) error {
			return util.CheckArgs(c)
		},
		Action: func(c *cli.Context) {
			client, err := util.MaybeLoadConfig(c, client)
			if err != nil {
				fmt.Println(err.Error())
				return
			}
			resp, err := client.Group.Delete(c.Args().First())
			if err != nil {
				fmt.Printf("failed to delete %s", c.Args().First())
				return
			}
			b, err := json.MarshalIndent(resp, "", "  ")
			if err != nil {
				fmt.Printf("%s", err)
				return
			}
			fmt.Printf("%s\n", b)
		},
	}
}
예제 #9
0
파일: lb.go 프로젝트: mikebeyer/clc-cli
func updatePool(client *clc.Client) cli.Command {
	return cli.Command{
		Name:    "update-pool",
		Aliases: []string{"up"},
		Usage:   "update load balancer pool",
		Flags: []cli.Flag{
			cli.StringFlag{Name: "id", Usage: "load balancer id [required]"},
			cli.StringFlag{Name: "location, l", Usage: "load balancer location [required]"},
			cli.StringFlag{Name: "pool, p", Usage: "load balancer pool id [required]"},
			cli.BoolFlag{Name: "sticky", Usage: "use stick persistence"},
			cli.BoolFlag{Name: "standard", Usage: "use standard persistence [default]"},
			cli.BoolFlag{Name: "least-connection, lc", Usage: "use least-connection load balacing"},
			cli.BoolFlag{Name: "round-robin, rr", Usage: "use round-robin load balacing [default]"},
		},
		Before: func(c *cli.Context) error {
			if c.Bool("sticky") && c.Bool("standard") {
				fmt.Println("only one of sticky and standard can be selected")
				return fmt.Errorf("")
			}

			if c.Bool("least-connection") && c.Bool("round-robin") {
				fmt.Println("only one of least-connection and round-robin can be selected")
				return fmt.Errorf("")
			}

			if c.String("id") == "" || c.String("location") == "" || c.String("pool") == "" {
				fmt.Println("missing required flags, --help for more details")
				return fmt.Errorf("")
			}
			return nil
		},
		Action: func(c *cli.Context) {
			client, err := util.MaybeLoadConfig(c, client)
			if err != nil {
				fmt.Println(err.Error())
				return
			}
			pool := lb.Pool{}
			if c.Bool("sticky") {
				pool.Persistence = lb.Sticky
			} else {
				pool.Persistence = lb.Standard
			}

			if c.Bool("least-connection") {
				pool.Method = lb.LeastConn
			} else {
				pool.Method = lb.RoundRobin
			}

			err = client.LB.UpdatePool(c.String("location"), c.String("id"), c.String("pool"), pool)
			if err != nil {
				fmt.Printf("failed to update load balancer pool [%s] in %s\n", c.String("pool"), c.String("location"))
				return
			}
			fmt.Printf("successfully updated pool [%s]\n", c.String("pool"))
		},
	}
}
예제 #10
0
파일: server.go 프로젝트: mikebeyer/clc-cli
func create(client *clc.Client) cli.Command {
	return cli.Command{
		Name:        "create",
		Aliases:     []string{"c"},
		Usage:       "create server",
		Description: "example: clc server create -n 'my cool server' -c 1 -m 1 -g [group id] -t standard --ubuntu-14",
		Flags:       append(createFlags(), templateFlags()...),
		Before: func(c *cli.Context) error {
			err := util.CheckStringFlag(c, "name", "cpu", "memory", "group")
			if err == nil {
				err = util.CheckForEitherBooleanFlag(c, "standard", "hyperscale")
			}
			return err
		},
		Action: func(c *cli.Context) {
			client, err := util.MaybeLoadConfig(c, client)
			if err != nil {
				fmt.Println(err.Error())
				return
			}
			source, err := findTemplateInContext(c)
			if err != nil {
				return
			}
			server := server.Server{
				Name:           c.String("name"),
				CPU:            c.Int("cpu"),
				MemoryGB:       c.Int("memory"),
				GroupID:        c.String("group"),
				SourceServerID: source,
				Type:           deriveServerType(c),
			}
			server.Password = c.String("password")
			server.Description = c.String("description")
			server.IPaddress = c.String("ip")
			server.IsManagedOS = c.Bool("managed")
			server.PrimaryDNS = c.String("primaryDNS")
			server.SecondaryDNS = c.String("secondaryDNS")
			server.NetworkID = c.String("network")
			server.Storagetype = deriveStorageType(c)

			resp, err := client.Server.Create(server)
			if err != nil {
				fmt.Printf("failed to create %s", server.Name)
				return
			}
			b, err := json.MarshalIndent(resp, "", "  ")
			if err != nil {
				fmt.Printf("%s", err)
				return
			}
			fmt.Printf("%s\n", b)
		},
	}
}
예제 #11
0
파일: server.go 프로젝트: mikebeyer/clc-cli
func createIP(client *clc.Client) cli.Command {
	return cli.Command{
		Name:    "add",
		Aliases: []string{"a"},
		Usage:   "add public ip to server",
		Flags: []cli.Flag{
			cli.StringFlag{Name: "name, n", Usage: "server name [required]"},
			cli.StringSliceFlag{Name: "tcp", Usage: "provide a port [8080] or a port range [8080:8082]"},
			cli.StringSliceFlag{Name: "udp", Usage: "provide a port [8080] or a port range [8080:8082]"},
			cli.StringSliceFlag{Name: "restriction, r", Usage: "provide an ip subnet to restrict to access the public ip [ex. 10.0.0.1/24 (must be cidr notation)]"},
		},
		Before: func(c *cli.Context) error {
			err := util.CheckStringFlag(c, "name")
			if err == nil {
				err = util.CheckForAnyOfStringSliceFlag(c, "tcp", "udp")
			}
			return err
		},
		Action: func(c *cli.Context) {
			client, err := util.MaybeLoadConfig(c, client)
			if err != nil {
				fmt.Println(err.Error())
				return
			}
			ports := make([]server.Port, 0)
			tcps, err := parsePort("tcp", c.StringSlice("tcp"))
			if err != nil {
				fmt.Println(err.Error())
			}
			ports = append(ports, tcps...)
			udps, err := parsePort("udp", c.StringSlice("udp"))
			if err != nil {
				fmt.Println(err.Error())
			}
			ports = append(ports, udps...)
			restrictions := make([]server.SourceRestriction, 0)
			for _, v := range c.StringSlice("restriction") {
				restrictions = append(restrictions, server.SourceRestriction{CIDR: v})
			}

			ip := server.PublicIP{Ports: ports}
			resp, err := client.Server.AddPublicIP(c.String("name"), ip)
			if err != nil {
				fmt.Printf("err %s\n", err)
				return
			}
			b, err := json.MarshalIndent(resp, "", "  ")
			if err != nil {
				fmt.Printf("%s", err)
				return
			}
			fmt.Printf("%s\n", b)
		},
	}
}
예제 #12
0
파일: lb.go 프로젝트: mikebeyer/clc-cli
func getPool(client *clc.Client) cli.Command {
	return cli.Command{
		Name:    "get-pool",
		Aliases: []string{"gp"},
		Usage:   "get load balancer pool details",
		Flags: []cli.Flag{
			cli.BoolFlag{Name: "all", Usage: "list all load balancers for location"},
			cli.StringFlag{Name: "id", Usage: "load balancer id [required]"},
			cli.StringFlag{Name: "location, l", Usage: "load balancer location [required]"},
			cli.StringFlag{Name: "pool, p", Usage: "load balancer pool id"},
		},
		Before: func(c *cli.Context) error {
			if c.String("location") == "" || c.String("id") == "" {
				fmt.Printf("missing required flags to get pool. [use --help to show required flags]\n")
				return fmt.Errorf("")
			}
			return nil
		},
		Action: func(c *cli.Context) {
			client, err := util.MaybeLoadConfig(c, client)
			if err != nil {
				fmt.Println(err.Error())
				return
			}
			if c.Bool("all") || c.String("pool") == "" {
				resp, err := client.LB.GetAllPools(c.String("location"), c.String("id"))
				if err != nil {
					fmt.Printf("failed to get %s\n", c.Args().First())
					return
				}
				b, err := json.MarshalIndent(resp, "", "  ")
				if err != nil {
					fmt.Printf("%s\n", err)
					return
				}
				fmt.Printf("%s\n", b)
				return
			}
			resp, err := client.LB.GetPool(c.String("location"), c.String("id"), c.String("pool"))
			if err != nil {
				fmt.Printf("failed to get %s\n", c.Args().First())
				return
			}
			b, err := json.MarshalIndent(resp, "", "  ")
			if err != nil {
				fmt.Printf("%s\n", err)
				return
			}
			fmt.Printf("%s\n", b)
		},
	}
}
예제 #13
0
파일: lb.go 프로젝트: mikebeyer/clc-cli
func updateNode(client *clc.Client) cli.Command {
	return cli.Command{
		Name:    "update-node",
		Aliases: []string{"un"},
		Usage:   "update load balancer node details",
		Flags: []cli.Flag{
			cli.StringFlag{Name: "id", Usage: "load balancer id [required]"},
			cli.StringFlag{Name: "location, l", Usage: "load balancer location [required]"},
			cli.StringFlag{Name: "pool, p", Usage: "load balancer pool id [required]"},
			cli.StringSliceFlag{Name: "host, h", Usage: "node hostname and port (ex. 10.10.10.10:8080)"},
		},
		Before: func(c *cli.Context) error {
			if c.String("location") == "" || c.String("id") == "" || c.String("pool") == "" {
				fmt.Printf("missing required flags to get pool. [use --help to show required flags]\n")
				return fmt.Errorf("")
			}
			return nil
		},
		Action: func(c *cli.Context) {
			client, err := util.MaybeLoadConfig(c, client)
			if err != nil {
				fmt.Println(err.Error())
				return
			}
			nodes := make([]lb.Node, len(c.StringSlice("host")))
			for i, v := range c.StringSlice("host") {
				split := strings.Split(v, ":")
				port, err := strconv.Atoi(split[1])
				if err != nil {
					fmt.Printf("failed parsing %s", v)
					return
				}
				node := lb.Node{
					IPaddress:   split[0],
					PrivatePort: port,
				}
				nodes[i] = node
			}
			err = client.LB.UpdateNodes(c.String("location"), c.String("id"), c.String("pool"), nodes...)
			if err != nil {
				fmt.Printf("failed to update nodes\n")
				return
			}
			fmt.Printf("nodes updates for pool %s\n", c.String("pool"))
		},
	}
}
예제 #14
0
파일: aa.go 프로젝트: mikebeyer/clc-cli
func get(client *clc.Client) cli.Command {
	return cli.Command{
		Name:    "get",
		Aliases: []string{"g"},
		Usage:   "get aa policy",
		Flags: []cli.Flag{
			cli.BoolFlag{Name: "all", Usage: "retrieve all policies"},
			cli.StringFlag{Name: "alias, a", Usage: "account alias"},
		},
		Action: func(c *cli.Context) {
			client, err := util.MaybeLoadConfig(c, client)
			if err != nil {
				fmt.Println(err.Error())
				return
			}

			if c.Bool("all") || c.Args().First() == "" {
				policies, err := client.AA.GetAll()
				if err != nil {
					fmt.Printf("unable to retrieve aa policies\n")
					return
				}
				b, err := json.MarshalIndent(policies, "", "  ")
				if err != nil {
					fmt.Printf("%s", err)
					return
				}
				fmt.Printf("%s\n", b)
				return
			}

			policy, err := client.AA.Get(c.Args().First())
			if err != nil {
				fmt.Printf("unable to retrieve aa policy: [%s]\n", c.Args().First())
				return
			}
			b, err := json.MarshalIndent(policy, "", "  ")
			if err != nil {
				fmt.Printf("%s", err)
				return
			}
			fmt.Printf("%s\n", b)
		},
	}
}
예제 #15
0
파일: server.go 프로젝트: mikebeyer/clc-cli
func sshServer(client *clc.Client) cli.Command {
	return cli.Command{
		Name:    "ssh",
		Aliases: []string{"s"},
		Usage:   "ssh to server",
		Before: func(c *cli.Context) error {
			return util.CheckArgs(c)
		},
		Action: func(c *cli.Context) {
			client, err := util.MaybeLoadConfig(c, client)
			if err != nil {
				fmt.Println(err.Error())
				return
			}
			server, err := client.Server.Get(c.Args().First())
			if err != nil {
				fmt.Printf("failed to shh to %s\n", c.Args().First())
				return
			}
			creds, err := client.Server.GetCredentials(c.Args().First())
			if err != nil {
				fmt.Printf("failed to shh to %s\n", c.Args().First())
				return
			}

			internal, err := getInternal(server)
			if err != nil {
				fmt.Printf("failed to shh to %s\n", c.Args().First())
				return
			}

			if err := connect(internal, creds); err != nil {
				fmt.Printf("failed to shh to %s\n", c.Args().First())
				return
			}
		},
	}
}
예제 #16
0
파일: group.go 프로젝트: mikebeyer/clc-cli
func create(client *clc.Client) cli.Command {
	return cli.Command{
		Name:    "create",
		Aliases: []string{"c"},
		Usage:   "create group",
		Flags: []cli.Flag{
			cli.StringFlag{Name: "name, n", Usage: "group name [required]"},
			cli.StringFlag{Name: "parent, p", Usage: "parent group id [required]"},
			cli.StringFlag{Name: "description, d", Usage: "group description"},
		},
		Before: func(c *cli.Context) error {
			return util.CheckStringFlag(c, "name", "parent")
		},
		Action: func(c *cli.Context) {
			client, err := util.MaybeLoadConfig(c, client)
			if err != nil {
				fmt.Println(err.Error())
				return
			}
			g := group.Group{
				Name:          c.String("name"),
				ParentGroupID: c.String("parent"),
				Description:   c.String("description"),
			}
			resp, err := client.Group.Create(g)
			if err != nil {
				fmt.Printf("failed to create %s", c.String("name"))
				return
			}
			b, err := json.MarshalIndent(resp, "", "  ")
			if err != nil {
				fmt.Printf("%s", err)
				return
			}
			fmt.Printf("%s\n", b)
		},
	}
}
예제 #17
0
파일: lb.go 프로젝트: mikebeyer/clc-cli
func create(client *clc.Client) cli.Command {
	return cli.Command{
		Name:    "create",
		Aliases: []string{"c"},
		Usage:   "create shared load balancer",
		Flags: []cli.Flag{
			cli.StringFlag{Name: "name, n", Usage: "load balancer name [required]"},
			cli.StringFlag{Name: "location, l", Usage: "load balancer location [required]"},
			cli.StringFlag{Name: "description, d", Usage: "load balancer description"},
		},
		Action: func(c *cli.Context) {
			client, err := util.MaybeLoadConfig(c, client)
			if err != nil {
				fmt.Println(err.Error())
				return
			}
			name := c.String("name")
			loc := c.String("location")
			if name == "" || loc == "" {
				fmt.Printf("missing required flags to create load balancer. [use --help to show required flags]\n")
				return
			}

			lb := lb.LoadBalancer{Name: name, Description: c.String("description")}
			resp, err := client.LB.Create(loc, lb)
			if err != nil {
				fmt.Printf("failed to create load balancer [%s] in %s\n", name, loc)
				return
			}
			b, err := json.MarshalIndent(resp, "", "  ")
			if err != nil {
				fmt.Printf("%s\n", err)
				return
			}
			fmt.Printf("%s\n", b)
		},
	}
}
예제 #18
0
파일: aa.go 프로젝트: mikebeyer/clc-cli
func create(client *clc.Client) cli.Command {
	return cli.Command{
		Name:    "create",
		Aliases: []string{"c"},
		Usage:   "create aa policy",
		Flags: []cli.Flag{
			cli.StringFlag{Name: "name, n", Usage: "policy name [required]"},
			cli.StringFlag{Name: "location, l", Usage: "policy location [required]"},
		},
		Action: func(c *cli.Context) {
			client, err := util.MaybeLoadConfig(c, client)
			if err != nil {
				fmt.Println(err.Error())
				return
			}
			name := c.String("name")
			loc := c.String("location")
			if name == "" || loc == "" {
				fmt.Printf("missing required flags to create policy. [use --help to show required flags]\n")
				return
			}

			policy, err := client.AA.Create(name, loc)
			if err != nil {
				fmt.Printf("failed to create policy %s in %s", name, loc)
				return
			}
			b, err := json.MarshalIndent(policy, "", "  ")
			if err != nil {
				fmt.Printf("%s", err)
				return
			}
			fmt.Printf("%s\n", b)
		},
	}
}
예제 #19
0
파일: lb.go 프로젝트: mikebeyer/clc-cli
func update(client *clc.Client) cli.Command {
	return cli.Command{
		Name:    "update",
		Aliases: []string{"u"},
		Usage:   "update shared load balancer",
		Flags: []cli.Flag{
			cli.StringFlag{Name: "name, n", Usage: "load balancer name [required]"},
			cli.StringFlag{Name: "id", Usage: "load balancer id [required]"},
			cli.StringFlag{Name: "location, l", Usage: "load balancer location [required]"},
			cli.StringFlag{Name: "description, d", Usage: "load balancer description [required]"},
		},
		Action: func(c *cli.Context) {
			client, err := util.MaybeLoadConfig(c, client)
			if err != nil {
				fmt.Println(err.Error())
				return
			}
			id := c.String("id")
			loc := c.String("location")
			name := c.String("name")
			desc := c.String("description")
			if id == "" || loc == "" || name == "" || desc == "" {
				fmt.Printf("missing required flags to update load balancer. [use --help to show required flags]\n")
				return
			}

			lb := lb.LoadBalancer{Name: name, Description: desc}
			err = client.LB.Update(loc, id, lb)
			if err != nil {
				fmt.Printf("failed to update load balancer [%s] in %s\n", lb.Name, loc)
				return
			}
			fmt.Printf("successfully updated lb [%s]\n", id)
		},
	}
}
예제 #20
0
파일: alert.go 프로젝트: mikebeyer/clc-cli
func create(client *clc.Client) cli.Command {
	return cli.Command{
		Name:    "create",
		Aliases: []string{"c"},
		Usage:   "create alert policy",
		Flags: []cli.Flag{
			cli.StringFlag{Name: "name, n", Usage: "policy name [required]"},
			cli.StringSliceFlag{Name: "email, e", Usage: "provide email address for alert"},
			cli.StringSliceFlag{Name: "cpu, c", Usage: "provide threshold / duration in minutes (ex. --cpu 90/10)"},
			cli.StringSliceFlag{Name: "disk, d", Usage: "provide threshold / duration in minutes (ex. --disk 90/10)"},
			cli.StringSliceFlag{Name: "memory, m", Usage: "provide threshold / duration in minutes (ex. --memory 90/10)"},
		},
		Before: func(c *cli.Context) error {
			if c.String("name") == "" {
				fmt.Printf("missing flags, --help for usage")
				return errors.New("")
			}

			return nil
		},
		Action: func(c *cli.Context) {
			client, err := util.MaybeLoadConfig(c, client)
			if err != nil {
				fmt.Println(err.Error())
				return
			}
			a := alert.Alert{
				Name: c.String("name"),
			}
			if len(c.StringSlice("email")) > 0 {
				action := alert.Action{
					Action: "email",
					Setting: alert.Setting{
						Recipients: c.StringSlice("email"),
					},
				}
				a.Actions = []alert.Action{action}
			}

			cpus, err := parseTrigger("cpu", c.StringSlice("cpu"))
			if err != nil {
				fmt.Printf(err.Error())
				return
			}
			a.Triggers = append(a.Triggers, cpus...)

			disks, err := parseTrigger("disk", c.StringSlice("disk"))
			if err != nil {
				fmt.Printf(err.Error())
				return
			}
			a.Triggers = append(a.Triggers, disks...)

			mems, err := parseTrigger("memory", c.StringSlice("memory"))
			if err != nil {
				fmt.Printf(err.Error())
				return
			}
			a.Triggers = append(a.Triggers, mems...)
			policy, err := client.Alert.Create(a)
			if err != nil {
				fmt.Printf("failed to create policy")
				return
			}

			b, err := json.MarshalIndent(policy, "", "  ")
			if err != nil {
				fmt.Printf("%s", err)
				return
			}
			fmt.Printf("%s\n", b)
		},
	}
}