Example #1
0
func (cmd *sshkeyRm) Run(context.Context) error {
	var ids []int
	if cmd.id != 0 {
		ids = append(ids, cmd.id)
	}
	if cmd.pem != "" {
		key, err := sl.ParseKey(cmd.pem)
		if err != nil {
			return err
		}
		f := &sl.Filter{
			Fingerprint: key.Fingerprint,
		}
		keys, err := client.KeysByFilter(f)
		if err != nil && !sl.IsNotFound(err) {
			return err
		}
		for _, key := range keys {
			ids = append(ids, key.ID)
		}
	}
	if len(ids) == 0 {
		return errors.New("no key found to remove")
	}
	for _, id := range ids {
		if err := client.DeleteKey(id); err != nil {
			return err
		}
		fmt.Println("Removed", id)
	}
	return nil
}
Example #2
0
func TestKeys(t *testing.T) {
	pem := os.Getenv("KLOUD_USER_PRIVATEKEY")
	if pem == "" {
		t.Skip("skipping, KLOUD_USER_PRIVATEKEY is empty")
	}
	c := sl.NewSoftlayerWithOptions(opts)
	key, err := sl.ParseKey(pem)
	if err != nil {
		t.Fatalf("NewKey(%q)=%s", pem, err)
	}
	key.Label = fmt.Sprintf("test-%s-%d", key.Label, time.Now().UnixNano())
	newKey, err := c.CreateKey(key)
	if err != nil {
		t.Fatalf("CreateKey(%+v)=%s", key, err)
	}
	defer func() {
		if err := c.DeleteKey(newKey.ID); err != nil {
			t.Error(err)
		}
	}()
	if newKey.ID == 0 {
		t.Error("want key.ID != 0")
	}
	if newKey.Fingerprint != key.Fingerprint {
		t.Errorf("want fingerprint=%q; got %q", key.Fingerprint, newKey.Fingerprint)
	}
	if newKey.CreateDate.IsZero() {
		t.Errorf("want %v to be actual date", newKey.CreateDate)
	}
	f := &sl.Filter{
		Label: key.Label,
	}
	d := time.Now()
	keys, err := c.KeysByFilter(f)
	if err != nil {
		t.Fatal(err)
	}
	reqDur := time.Now().Sub(d)
	d = time.Now()
	xkeys, err := c.XKeysByFilter(f)
	if err != nil {
		t.Fatal(err)
	}
	xreqDur := time.Now().Sub(d)
	t.Logf("[TEST] filtering took: client-side=%s, server-side=%s", reqDur, xreqDur)
	if len(keys) != 1 {
		t.Errorf("want len(keys)=1; got %d", len(keys))
	}
	if len(xkeys) != 1 {
		t.Errorf("want len(xkeys)=1; got %d", len(keys))
	}
	if !reflect.DeepEqual(keys[0], newKey) {
		t.Errorf("want key=%+v; got %+v", newKey, keys[0])
	}
	if !reflect.DeepEqual(xkeys[0], newKey) {
		t.Errorf("want key=%+v; got %+v", newKey, xkeys[0])
	}
}
Example #3
0
func (cmd *sshkeyList) Run(context.Context) error {
	f := &sl.Filter{
		Label: cmd.label,
		User:  cmd.user,
	}
	if cmd.pem != "" {
		key, err := sl.ParseKey(cmd.pem)
		if err != nil {
			return err
		}
		f.Fingerprint = key.Fingerprint
	}
	keys, err := client.KeysByFilter(f)
	if err != nil {
		return err
	}
	printKeys(keys...)
	return nil
}
Example #4
0
func (cmd *sshkeyAdd) Run(context.Context) error {
	key, err := sl.ParseKey(cmd.pem)
	if err != nil {
		return err
	}
	if cmd.label != "" {
		key.Label = cmd.label
	}
	if cmd.user != "" {
		key.User = cmd.user
	}
	if cmd.tags != "" {
		key.Tags = newTags(strings.Split(cmd.tags, ","))
	}
	key, err = client.CreateKey(key)
	if err != nil {
		return err
	}
	printKeys(key)
	return nil
}