예제 #1
0
파일: sshkeys.go 프로젝트: alexcb/doit
// RunKeyGet retrieves a key.
func RunKeyGet(ns string, config doit.Config, out io.Writer, args []string) error {
	client := config.GetGodoClient()

	if len(args) != 1 {
		return doit.NewMissingArgsErr(ns)
	}

	rawKey := args[0]

	var err error
	var key *godo.Key

	if i, aerr := strconv.Atoi(rawKey); aerr == nil {
		key, _, err = client.Keys.GetByID(i)
	} else {
		if len(rawKey) > 0 {
			key, _, err = client.Keys.GetByFingerprint(rawKey)
		} else {
			err = fmt.Errorf("missing key id or fingerprint")
		}
	}

	if err != nil {
		return err
	}

	return doit.DisplayOutput(key, out)
}
예제 #2
0
파일: images.go 프로젝트: alexcb/doit
// RunImagesGet retrieves an image by id or slug.
func RunImagesGet(ns string, config doit.Config, out io.Writer, args []string) error {
	client := config.GetGodoClient()

	if len(args) != 1 {
		return doit.NewMissingArgsErr(ns)
	}

	rawID := args[0]

	var image *godo.Image
	var err error

	if id, cerr := strconv.Atoi(rawID); cerr == nil {
		image, _, err = client.Images.GetByID(id)
	} else {
		if len(rawID) > 0 {
			image, _, err = client.Images.GetBySlug(rawID)
		} else {
			err = fmt.Errorf("image identifier is required")
		}
	}

	if err != nil {
		return err
	}

	return doit.DisplayOutput(image, out)
}
예제 #3
0
파일: sshkeys.go 프로젝트: alexcb/doit
// RunKeyCreate uploads a SSH key.
func RunKeyCreate(ns string, config doit.Config, out io.Writer, args []string) error {
	client := config.GetGodoClient()

	if len(args) != 1 {
		return doit.NewMissingArgsErr(ns)
	}

	name := args[0]

	publicKey, err := config.GetString(ns, doit.ArgKeyPublicKey)
	if err != nil {
		return err
	}

	kcr := &godo.KeyCreateRequest{
		Name:      name,
		PublicKey: publicKey,
	}

	r, _, err := client.Keys.Create(kcr)
	if err != nil {
		logrus.WithField("err", err).Fatal("could not create key")
	}

	return doit.DisplayOutput(r, out)
}
예제 #4
0
파일: domains.go 프로젝트: neurodrone/doit
// RunRecordList list records for a domain.
func RunRecordList(ns string, out io.Writer) error {
	client := doit.DoitConfig.GetGodoClient()
	name := doit.DoitConfig.GetString(ns, doit.ArgDomainName)

	if len(name) < 1 {
		return errors.New("domain name is missing")
	}

	f := func(opt *godo.ListOptions) ([]interface{}, *godo.Response, error) {
		list, resp, err := client.Domains.Records(name, opt)
		if err != nil {
			return nil, nil, err
		}

		si := make([]interface{}, len(list))
		for i := range list {
			si[i] = list[i]
		}

		return si, resp, err
	}

	si, err := doit.PaginateResp(f)
	if err != nil {
		return err
	}

	list := make([]godo.DomainRecord, len(si))
	for i := range si {
		list[i] = si[i].(godo.DomainRecord)
	}

	return doit.DisplayOutput(list, out)
}
예제 #5
0
파일: droplets.go 프로젝트: neurodrone/doit
// RunDropletActions returns a list of actions for a droplet.
func RunDropletActions(ns string, out io.Writer) error {
	client := doit.DoitConfig.GetGodoClient()
	id := doit.DoitConfig.GetInt(ns, doit.ArgDropletID)

	f := func(opt *godo.ListOptions) ([]interface{}, *godo.Response, error) {
		list, resp, err := client.Droplets.Actions(id, opt)
		if err != nil {
			return nil, nil, err
		}

		si := make([]interface{}, len(list))
		for i := range list {
			si[i] = list[i]
		}

		return si, resp, err
	}

	si, err := doit.PaginateResp(f)
	if err != nil {
		return err
	}

	list := make([]godo.Action, len(si))
	for i := range si {
		list[i] = si[i].(godo.Action)
	}

	return doit.DisplayOutput(list, out)
}
예제 #6
0
파일: sshkeys.go 프로젝트: alexcb/doit
// RunKeyUpdate updates a key.
func RunKeyUpdate(ns string, config doit.Config, out io.Writer, args []string) error {
	client := config.GetGodoClient()

	if len(args) != 1 {
		return doit.NewMissingArgsErr(ns)
	}

	rawKey := args[0]

	name, err := config.GetString(ns, doit.ArgKeyName)
	if err != nil {
		return err
	}

	req := &godo.KeyUpdateRequest{
		Name: name,
	}

	var key *godo.Key
	if i, aerr := strconv.Atoi(rawKey); aerr == nil {
		key, _, err = client.Keys.UpdateByID(i, req)
	} else {
		key, _, err = client.Keys.UpdateByFingerprint(rawKey, req)
	}

	if err != nil {
		return err
	}

	return doit.DisplayOutput(key, out)
}
예제 #7
0
파일: images.go 프로젝트: alexcb/doit
// RunImagesUpdate updates an image.
func RunImagesUpdate(ns string, config doit.Config, out io.Writer, args []string) error {
	client := config.GetGodoClient()

	if len(args) != 1 {
		return doit.NewMissingArgsErr(ns)
	}

	id, err := strconv.Atoi(args[0])
	if err != nil {
		return err
	}

	name, err := config.GetString(ns, doit.ArgImageName)

	req := &godo.ImageUpdateRequest{
		Name: name,
	}

	image, _, err := client.Images.Update(id, req)
	if err != nil {
		return err
	}

	return doit.DisplayOutput(image, out)
}
예제 #8
0
파일: domains.go 프로젝트: alexcb/doit
// RunDomainCreate runs domain create.
func RunDomainCreate(ns string, config doit.Config, out io.Writer, args []string) error {
	if len(args) != 1 {
		return doit.NewMissingArgsErr(ns)
	}
	domainName := args[0]

	client := config.GetGodoClient()

	ipAddress, err := config.GetString(ns, "ip-address")
	if err != nil {
		return err
	}

	req := &godo.DomainCreateRequest{
		Name:      domainName,
		IPAddress: ipAddress,
	}

	d, _, err := client.Domains.Create(req)
	if err != nil {
		return err
	}

	return doit.DisplayOutput(d, out)
}
예제 #9
0
// RunImageActionsTransfer an image.
func RunImageActionsTransfer(ns string, config doit.Config, out io.Writer, args []string) error {
	client := config.GetGodoClient()

	if len(args) != 1 {
		return doit.NewMissingArgsErr(ns)
	}

	id, err := strconv.Atoi(args[0])
	if err != nil {
		return err
	}

	region, err := config.GetString(ns, doit.ArgRegionSlug)
	if err != nil {
		return err
	}

	req := &godo.ActionRequest{
		"region": region,
	}

	action, _, err := client.ImageActions.Transfer(id, req)
	if err != nil {
		logrus.WithField("err", err).Fatal("could not transfer image")
	}

	return doit.DisplayOutput(action, out)
}
예제 #10
0
파일: sshkeys.go 프로젝트: neurodrone/doit
// RunKeyImport imports a key from a file
func RunKeyImport(ns string, out io.Writer) error {
	client := doit.DoitConfig.GetGodoClient()

	keyPath := doit.DoitConfig.GetString(ns, doit.ArgKeyPublicKeyFile)
	keyName := doit.DoitConfig.GetString(ns, doit.ArgKeyName)

	keyFile, err := ioutil.ReadFile(keyPath)
	if err != nil {
		return err
	}

	_, comment, _, _, err := ssh.ParseAuthorizedKey(keyFile)
	if err != nil {
		return err
	}

	if len(keyName) < 1 {
		keyName = comment
	}

	kcr := &godo.KeyCreateRequest{
		Name:      keyName,
		PublicKey: string(keyFile),
	}

	r, _, err := client.Keys.Create(kcr)
	if err != nil {
		return err
	}

	return doit.DisplayOutput(r, out)
}
예제 #11
0
파일: images.go 프로젝트: alexcb/doit
func listImages(ns string, config doit.Config, out io.Writer, lFn listFn) error {
	public, err := config.GetBool(ns, doit.ArgImagePublic)
	if err != nil {
		return err
	}

	fn := func(opt *godo.ListOptions) ([]interface{}, *godo.Response, error) {
		list, resp, err := lFn(opt)
		if err != nil {
			return nil, nil, err
		}

		si := []interface{}{}
		for _, i := range list {
			if (public && i.Public) || !public {
				si = append(si, i)
			}
		}

		return si, resp, err
	}

	si, err := doit.PaginateResp(fn)
	if err != nil {
		return err
	}

	list := make([]godo.Image, len(si))
	for i := range si {
		list[i] = si[i].(godo.Image)
	}

	return doit.DisplayOutput(list, out)
}
예제 #12
0
// RunFloatingIPList runs floating IP create.
func RunFloatingIPList(ns string, out io.Writer) error {
	client := doit.DoitConfig.GetGodoClient()

	f := func(opt *godo.ListOptions) ([]interface{}, *godo.Response, error) {
		list, resp, err := client.FloatingIPs.List(opt)
		if err != nil {
			return nil, nil, err
		}

		si := make([]interface{}, len(list))
		for i := range list {
			si[i] = list[i]
		}

		return si, resp, err
	}

	si, err := doit.PaginateResp(f)
	if err != nil {
		return err
	}

	list := make([]godo.FloatingIP, len(si))
	for i := range si {
		list[i] = si[i].(godo.FloatingIP)
	}

	return doit.DisplayOutput(list, out)
}
예제 #13
0
파일: domains.go 프로젝트: alexcb/doit
// RunRecordUpdate updates a domain record.
func RunRecordUpdate(ns string, config doit.Config, out io.Writer, args []string) error {
	if len(args) != 1 {
		return doit.NewMissingArgsErr(ns)
	}
	domainName := args[0]

	client := config.GetGodoClient()

	recordID, err := config.GetInt(ns, doit.ArgRecordID)
	if err != nil {
		return err
	}

	rType, err := config.GetString(ns, doit.ArgRecordType)
	if err != nil {
		return err
	}

	rName, err := config.GetString(ns, doit.ArgRecordName)
	if err != nil {
		return err
	}

	rData, err := config.GetString(ns, doit.ArgRecordData)
	if err != nil {
		return err
	}

	rPriority, err := config.GetInt(ns, doit.ArgRecordPriority)
	if err != nil {
		return err
	}

	rPort, err := config.GetInt(ns, doit.ArgRecordPort)
	if err != nil {
		return err
	}

	rWeight, err := config.GetInt(ns, doit.ArgRecordWeight)
	if err != nil {
		return err
	}

	drcr := &godo.DomainRecordEditRequest{
		Type:     rType,
		Name:     rName,
		Data:     rData,
		Priority: rPriority,
		Port:     rPort,
		Weight:   rWeight,
	}

	r, _, err := client.Domains.EditRecord(domainName, recordID, drcr)
	if err != nil {
		return err
	}

	return doit.DisplayOutput(r, out)
}
예제 #14
0
func performAction(out io.Writer, config doit.Config, fn actionFn) error {
	client := config.GetGodoClient()

	a, err := fn(client)
	if err != nil {
		return err
	}

	return doit.DisplayOutput(a, out)
}
예제 #15
0
파일: account.go 프로젝트: neurodrone/doit
// RunAccountGet runs account get.
func RunAccountGet(ns string, out io.Writer) error {
	client := doit.DoitConfig.GetGodoClient()

	a, _, err := client.Account.Get()
	if err != nil {
		return err
	}

	return doit.DisplayOutput(a, out)
}
예제 #16
0
// RunFloatingIPActionsUnassign unassigns a floating IP to a droplet.
func RunFloatingIPActionsUnassign(ns string, out io.Writer) error {
	client := doit.DoitConfig.GetGodoClient()
	ip := doit.DoitConfig.GetString(ns, doit.ArgIPAddress)

	action, _, err := client.FloatingIPActions.Unassign(ip)
	if err != nil {
		logrus.WithField("err", err).Fatal("could not unsassign IP to droplet")
	}
	return doit.DisplayOutput(action, out)
}
예제 #17
0
파일: droplets.go 프로젝트: neurodrone/doit
// RunDropletGet returns a droplet.
func RunDropletGet(ns string, out io.Writer) error {
	client := doit.DoitConfig.GetGodoClient()
	id := doit.DoitConfig.GetInt(ns, doit.ArgDropletID)

	droplet, err := getDropletByID(client, id)
	if err != nil {
		return err
	}

	return doit.DisplayOutput(droplet, out)
}
예제 #18
0
파일: droplets.go 프로젝트: neurodrone/doit
// RunDropletNeighbors returns a list of droplet neighbors.
func RunDropletNeighbors(ns string, out io.Writer) error {
	client := doit.DoitConfig.GetGodoClient()
	id := doit.DoitConfig.GetInt(ns, doit.ArgDropletID)

	list, _, err := client.Droplets.Neighbors(id)
	if err != nil {
		return err
	}

	return doit.DisplayOutput(list, out)
}
예제 #19
0
// RunFloatingIPActionsGet retrieves an action for a floating IP.
func RunFloatingIPActionsGet(ns string, out io.Writer) error {
	client := doit.DoitConfig.GetGodoClient()
	ip := doit.DoitConfig.GetString(ns, doit.ArgIPAddress)
	actionID := doit.DoitConfig.GetInt(ns, doit.ArgActionID)

	action, _, err := client.FloatingIPActions.Get(ip, actionID)
	if err != nil {
		return err
	}

	return doit.DisplayOutput(action, out)
}
예제 #20
0
// RunImageActionsGet retrieves an action for an image.
func RunImageActionsGet(ns string, out io.Writer) error {
	client := doit.DoitConfig.GetGodoClient()
	imageID := doit.DoitConfig.GetInt(ns, doit.ArgImageID)
	actionID := doit.DoitConfig.GetInt(ns, doit.ArgActionID)

	action, _, err := client.ImageActions.Get(imageID, actionID)
	if err != nil {
		return err
	}

	return doit.DisplayOutput(action, out)
}
예제 #21
0
// RunFloatingIPCreate runs floating IP create.
func RunFloatingIPCreate(ns string, out io.Writer) error {
	client := doit.DoitConfig.GetGodoClient()
	req := &godo.FloatingIPCreateRequest{
		Region:    doit.DoitConfig.GetString(ns, doit.ArgRegionSlug),
		DropletID: doit.DoitConfig.GetInt(ns, doit.ArgDropletID),
	}
	ip, _, err := client.FloatingIPs.Create(req)
	if err != nil {
		return err
	}
	return doit.DisplayOutput(ip, out)
}
예제 #22
0
// RunImageActionsTransfer an image.
func RunImageActionsTransfer(ns string, out io.Writer) error {
	client := doit.DoitConfig.GetGodoClient()
	id := doit.DoitConfig.GetInt(ns, doit.ArgImageID)
	req := &godo.ActionRequest{
		"region": doit.DoitConfig.GetString(ns, doit.ArgRegionSlug),
	}

	action, _, err := client.ImageActions.Transfer(id, req)
	if err != nil {
		logrus.WithField("err", err).Fatal("could not transfer image")
	}

	return doit.DisplayOutput(action, out)
}
예제 #23
0
파일: actions.go 프로젝트: neurodrone/doit
// RunCmdActionGet runs action get.
func RunCmdActionGet(ns string, out io.Writer) error {
	client := doit.DoitConfig.GetGodoClient()
	id := doit.DoitConfig.GetInt(ns, doit.ArgActionID)
	if id < 1 {
		return errors.New("invalid action id")
	}

	a, _, err := client.Actions.Get(id)
	if err != nil {
		return err
	}

	return doit.DisplayOutput(a, out)
}
예제 #24
0
파일: domains.go 프로젝트: neurodrone/doit
// RunDomainCreate runs domain create.
func RunDomainCreate(ns string, out io.Writer) error {
	client := doit.DoitConfig.GetGodoClient()
	req := &godo.DomainCreateRequest{
		Name:      doit.DoitConfig.GetString(ns, "domain-name"),
		IPAddress: doit.DoitConfig.GetString(ns, "ip-address"),
	}

	d, _, err := client.Domains.Create(req)
	if err != nil {
		return err
	}

	return doit.DisplayOutput(d, out)
}
예제 #25
0
파일: images.go 프로젝트: neurodrone/doit
// RunImagesUpdate updates an image.
func RunImagesUpdate(ns string, out io.Writer) error {
	client := doit.DoitConfig.GetGodoClient()
	id := doit.DoitConfig.GetInt(ns, doit.ArgImageID)

	req := &godo.ImageUpdateRequest{
		Name: doit.DoitConfig.GetString(ns, doit.ArgImageName),
	}

	image, _, err := client.Images.Update(id, req)
	if err != nil {
		return err
	}

	return doit.DisplayOutput(image, out)
}
예제 #26
0
파일: sshkeys.go 프로젝트: neurodrone/doit
// RunKeyCreate uploads a SSH key.
func RunKeyCreate(ns string, out io.Writer) error {
	client := doit.DoitConfig.GetGodoClient()

	kcr := &godo.KeyCreateRequest{
		Name:      doit.DoitConfig.GetString(ns, doit.ArgKeyName),
		PublicKey: doit.DoitConfig.GetString(ns, doit.ArgKeyPublicKey),
	}

	r, _, err := client.Keys.Create(kcr)
	if err != nil {
		logrus.WithField("err", err).Fatal("could not create key")
	}

	return doit.DisplayOutput(r, out)
}
예제 #27
0
파일: domains.go 프로젝트: neurodrone/doit
// RunDomainGet retrieves a domain by name.
func RunDomainGet(ns string, out io.Writer) error {
	client := doit.DoitConfig.GetGodoClient()
	id := doit.DoitConfig.GetString(ns, doit.ArgDomainName)

	if len(id) < 1 {
		return errors.New("invalid domain name")
	}

	d, _, err := client.Domains.Get(id)
	if err != nil {
		return err
	}

	return doit.DisplayOutput(d, out)
}
예제 #28
0
// RunFloatingIPGet retrieves a floating IP's details.
func RunFloatingIPGet(ns string, out io.Writer) error {
	client := doit.DoitConfig.GetGodoClient()
	ip := doit.DoitConfig.GetString(ns, doit.ArgIPAddress)

	if len(ip) < 1 {
		return errors.New("invalid ip address")
	}

	d, _, err := client.FloatingIPs.Get(ip)
	if err != nil {
		return err
	}

	return doit.DisplayOutput(d, out)
}
예제 #29
0
// RunFloatingIPActionsUnassign unassigns a floating IP to a droplet.
func RunFloatingIPActionsUnassign(ns string, config doit.Config, out io.Writer, args []string) error {
	if len(args) != 1 {
		return doit.NewMissingArgsErr(ns)
	}

	ip := args[0]

	client := config.GetGodoClient()

	action, _, err := client.FloatingIPActions.Unassign(ip)
	if err != nil {
		logrus.WithField("err", err).Fatal("could not unsassign IP to droplet")
	}
	return doit.DisplayOutput(action, out)
}
예제 #30
0
파일: domains.go 프로젝트: alexcb/doit
// RunDomainGet retrieves a domain by name.
func RunDomainGet(ns string, config doit.Config, out io.Writer, args []string) error {
	if len(args) != 1 {
		return doit.NewMissingArgsErr(ns)
	}
	id := args[0]

	client := config.GetGodoClient()

	if len(id) < 1 {
		return errors.New("invalid domain name")
	}

	d, _, err := client.Domains.Get(id)
	if err != nil {
		return err
	}

	return doit.DisplayOutput(d, out)
}