Beispiel #1
0
func TestInstance(t *testing.T) {
	c := sl.NewSoftlayerWithOptions(opts)
	f := &sl.Filter{}
	d := time.Now()
	instances, err := c.InstancesByFilter(f)
	if sl.IsNotFound(err) {
		t.Skip("dev environment has no instances created to test the API all")
	}
	if err != nil {
		t.Fatalf("InstancesByFilter(%v)=%s", f, err)
	}
	reqDur := time.Now().Sub(d)
	d = time.Now()
	xinstances, err := c.XInstancesByFilter(f)
	if sl.IsNotFound(err) {
		t.Skip("no instances found to perform filering / tag setting test")
	}
	if err != nil {
		t.Fatalf("XInstancesByFilter(%v)=%s", f, err)
	}
	xreqDur := time.Now().Sub(d)
	t.Logf("[TEST] filtering took: client-side=%s, server-side=%s", reqDur, xreqDur)
	if !reflect.DeepEqual(instances, xinstances) {
		t.Errorf("%+v != %+v", instances, xinstances)
	}
	id := instances[0].ID
	tags := instances[0].Tags.Copy()
	tags["test-tag"] = "test-value"
	err = c.InstanceSetTags(id, tags)
	if err != nil {
		t.Fatal(err)
	}
	f = &sl.Filter{
		ID: id,
		Tags: map[string]string{
			"tag-key": "",
		},
	}
	instances, err = c.InstancesByFilter(f)
	if err != nil {
		t.Fatal(err)
	}
	tags = instances[0].Tags.Copy()
	if v := tags["test-tag"]; v != "test-value" {
		t.Errorf(`want v="test-value"; got %q`, v)
	}
	delete(tags, "test-tag")
	if err = c.InstanceSetTags(id, tags); err != nil {
		t.Fatal(err)
	}
}
Beispiel #2
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
}
Beispiel #3
0
func (cmd *groupVMCache) Valid() error {
	if err := cmd.groupValues.Valid(); err != nil {
		return err
	}

	if cmd.vmcache == "" {
		return errors.New("invalid empty value provided for vm-cache flag")
	}

	if len(cmd.values) == 0 {
		return errors.New("invalid empty value provided for -users flag")
	}

	cmd.users = make(map[string]struct{}, len(cmd.values))
	for _, user := range cmd.values {
		cmd.users[strings.ToLower(user)] = struct{}{}
	}

	f, err := os.Open(cmd.vmcache)
	if err == nil {
		err = json.NewDecoder(f).Decode(&cmd.vms)

		if err != nil {
			DefaultUi.Error(fmt.Sprintf("unable to read %q: %s", cmd.vmcache, err))
		}

		f.Close()
	}

	err = cmd.verifyCache()

	if err == nil {
		return nil
	}

	DefaultUi.Info(fmt.Sprintf("building vm cache in %q due to: %s", cmd.vmcache, err))

	vlans, err := cmd.client.VlansByFilter(cmd.filter())
	if err != nil {
		return err
	}

	cmd.vms = make(map[string]*sl.Instance)

	DefaultUi.Info(fmt.Sprintf("processing %d vlans...", len(vlans)))

	for _, vlan := range vlans {
		vms, err := cmd.client.InstancesInVlan(vlan.ID)
		if sl.IsNotFound(err) {
			continue
		}
		if err != nil {
			return err
		}

		DefaultUi.Info(fmt.Sprintf("processing %d vms...", len(vms)))

		for _, vm := range vms {
			key := strings.ToLower(vm.Hostname)

			if key == "vlanguard" {
				key = fmt.Sprintf("vlanguard-%d", vm.ID)
				cmd.vms[key] = vm
				continue
			}

			if _, ok := cmd.users[key]; !ok {
				continue
			}

			if oldVM, ok := cmd.vms[key]; ok && oldVM.ID != vm.ID {
				DefaultUi.Warn(fmt.Sprintf("multiple vms for user %q: %d, %d", key, oldVM.ID, vm.ID))
				continue
			}

			cmd.vms[key] = vm
		}
	}

	DefaultUi.Info(fmt.Sprintf("fetched %d vms", len(cmd.vms)))

	if err := cmd.verifyCache(); err != nil {
		return fmt.Errorf("failed to build cache: %s", err)
	}

	f, err = os.Create(cmd.vmcache)
	if err != nil {
		DefaultUi.Error(fmt.Sprintf("unable to dump vm cache to %q: %s", cmd.vmcache, err))
		return nil
	}

	err = nonil(json.NewEncoder(f).Encode(cmd.vms), f.Sync(), f.Close())

	if err != nil {
		DefaultUi.Error(fmt.Sprintf("unable to dump vm cache to %q: %s", cmd.vmcache, nonil(err, os.Remove(f.Name()))))
	} else {
		DefaultUi.Info(fmt.Sprintf("vm-cache for %d instances dumped to %q", len(cmd.vms), cmd.vmcache))
	}

	return nil
}