Beispiel #1
0
func (cmd *rename) Run(f *flag.FlagSet) error {
	if f.NArg() != 2 {
		return flag.ErrHelp
	}

	ctx := context.TODO()

	c, err := cmd.Client()
	if err != nil {
		return err
	}

	m, err := object.GetCustomFieldsManager(c)
	if err != nil {
		return err
	}

	key, err := m.FindKey(ctx, f.Arg(0))
	if err != nil {
		return err
	}

	name := f.Arg(1)

	return m.Rename(ctx, key, name)
}
Beispiel #2
0
func (cmd *ls) Run(f *flag.FlagSet) error {
	ctx := context.TODO()

	c, err := cmd.Client()
	if err != nil {
		return err
	}

	m, err := object.GetCustomFieldsManager(c)
	if err != nil {
		return err
	}

	field, err := m.Field(ctx)
	if err != nil {
		return err
	}

	tw := tabwriter.NewWriter(os.Stdout, 2, 0, 2, ' ', 0)

	for _, def := range field {
		fmt.Fprintf(tw, "%d\t%s\n", def.Key, def.Name)
	}

	return tw.Flush()
}
Beispiel #3
0
func (cmd *add) Run(f *flag.FlagSet) error {
	if f.NArg() != 1 {
		return flag.ErrHelp
	}

	ctx := context.TODO()

	c, err := cmd.Client()
	if err != nil {
		return err
	}

	m, err := object.GetCustomFieldsManager(c)
	if err != nil {
		return err
	}

	name := f.Arg(0)

	def, err := m.Add(ctx, name, "", nil, nil)
	if err != nil {
		return err
	}

	fmt.Printf("%d\n", def.Key)

	return nil
}
Beispiel #4
0
func (m *Manager) CustomField(managedObject types.ManagedObjectReference, managedType string, key string, value string) error {
	mgr, err := object.GetCustomFieldsManager(m.session.Client.Client)
	if err != nil {
		return err
	}

	if managedType == "" {
		_, err = mgr.Add(m.ctx, key, managedObject.Type, nil, nil)
	} else {
		_, err = mgr.Add(m.ctx, key, managedType, nil, nil)
	}

	if err != nil && !m.isDuplicateName(err) {
		return err
	}

	ID, err := mgr.FindKey(m.ctx, key)
	if err != nil {
		return err
	}

	err = mgr.Set(m.ctx, managedObject, ID, value)
	if err != nil {
		return err
	}
	return nil
}
Beispiel #5
0
func (cmd *rm) Run(f *flag.FlagSet) error {
	ctx := context.TODO()

	c, err := cmd.Client()
	if err != nil {
		return err
	}

	m, err := object.GetCustomFieldsManager(c)
	if err != nil {
		return err
	}

	for _, name := range f.Args() {
		key, err := m.FindKey(ctx, name)
		if err != nil {
			return err
		}

		if err := m.Remove(ctx, key); err != nil {
			return err
		}
	}

	return nil
}
Beispiel #6
0
func (cmd *set) Run(f *flag.FlagSet) error {
	if f.NArg() < 3 {
		return flag.ErrHelp
	}

	ctx := context.TODO()

	finder, err := cmd.Finder()
	if err != nil {
		return err
	}

	c, err := cmd.Client()
	if err != nil {
		return err
	}

	m, err := object.GetCustomFieldsManager(c)
	if err != nil {
		return err
	}

	var objs []list.Element

	args := f.Args()

	key, err := m.FindKey(ctx, args[0])
	if err != nil {
		return err
	}

	val := args[1]

	for _, arg := range args[2:] {
		es, err := finder.ManagedObjectList(ctx, arg)
		if err != nil {
			return err
		}

		objs = append(objs, es...)
	}

	for _, ref := range objs {
		err := m.Set(ctx, ref.Object.Reference(), key, val)
		if err != nil {
			return err
		}
	}

	return nil
}
Beispiel #7
0
func (cmd *set) Run(f *flag.FlagSet) error {
	if f.NArg() < 3 {
		return flag.ErrHelp
	}

	ctx := context.TODO()

	c, err := cmd.Client()
	if err != nil {
		return err
	}

	m, err := object.GetCustomFieldsManager(c)
	if err != nil {
		return err
	}

	args := f.Args()

	key, err := m.FindKey(ctx, args[0])
	if err != nil {
		return err
	}

	val := args[1]

	objs, err := cmd.ManagedObjects(ctx, args[2:])
	if err != nil {
		return err
	}

	for _, ref := range objs {
		err := m.Set(ctx, ref, key, val)
		if err != nil {
			return err
		}
	}

	return nil
}