github.com/vmware/govmomi@v0.43.0/object/custom_fields_manager.go (about)

     1  /*
     2  Copyright (c) 2015 VMware, Inc. All Rights Reserved.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package object
    18  
    19  import (
    20  	"context"
    21  	"errors"
    22  	"strconv"
    23  
    24  	"github.com/vmware/govmomi/vim25"
    25  	"github.com/vmware/govmomi/vim25/methods"
    26  	"github.com/vmware/govmomi/vim25/mo"
    27  	"github.com/vmware/govmomi/vim25/types"
    28  )
    29  
    30  var (
    31  	ErrKeyNameNotFound = errors.New("key name not found")
    32  )
    33  
    34  type CustomFieldsManager struct {
    35  	Common
    36  }
    37  
    38  // GetCustomFieldsManager wraps NewCustomFieldsManager, returning ErrNotSupported
    39  // when the client is not connected to a vCenter instance.
    40  func GetCustomFieldsManager(c *vim25.Client) (*CustomFieldsManager, error) {
    41  	if c.ServiceContent.CustomFieldsManager == nil {
    42  		return nil, ErrNotSupported
    43  	}
    44  	return NewCustomFieldsManager(c), nil
    45  }
    46  
    47  func NewCustomFieldsManager(c *vim25.Client) *CustomFieldsManager {
    48  	m := CustomFieldsManager{
    49  		Common: NewCommon(c, *c.ServiceContent.CustomFieldsManager),
    50  	}
    51  
    52  	return &m
    53  }
    54  
    55  func (m CustomFieldsManager) Add(ctx context.Context, name string, moType string, fieldDefPolicy *types.PrivilegePolicyDef, fieldPolicy *types.PrivilegePolicyDef) (*types.CustomFieldDef, error) {
    56  	req := types.AddCustomFieldDef{
    57  		This:           m.Reference(),
    58  		Name:           name,
    59  		MoType:         moType,
    60  		FieldDefPolicy: fieldDefPolicy,
    61  		FieldPolicy:    fieldPolicy,
    62  	}
    63  
    64  	res, err := methods.AddCustomFieldDef(ctx, m.c, &req)
    65  	if err != nil {
    66  		return nil, err
    67  	}
    68  
    69  	return &res.Returnval, nil
    70  }
    71  
    72  func (m CustomFieldsManager) Remove(ctx context.Context, key int32) error {
    73  	req := types.RemoveCustomFieldDef{
    74  		This: m.Reference(),
    75  		Key:  key,
    76  	}
    77  
    78  	_, err := methods.RemoveCustomFieldDef(ctx, m.c, &req)
    79  	return err
    80  }
    81  
    82  func (m CustomFieldsManager) Rename(ctx context.Context, key int32, name string) error {
    83  	req := types.RenameCustomFieldDef{
    84  		This: m.Reference(),
    85  		Key:  key,
    86  		Name: name,
    87  	}
    88  
    89  	_, err := methods.RenameCustomFieldDef(ctx, m.c, &req)
    90  	return err
    91  }
    92  
    93  func (m CustomFieldsManager) Set(ctx context.Context, entity types.ManagedObjectReference, key int32, value string) error {
    94  	req := types.SetField{
    95  		This:   m.Reference(),
    96  		Entity: entity,
    97  		Key:    key,
    98  		Value:  value,
    99  	}
   100  
   101  	_, err := methods.SetField(ctx, m.c, &req)
   102  	return err
   103  }
   104  
   105  type CustomFieldDefList []types.CustomFieldDef
   106  
   107  func (m CustomFieldsManager) Field(ctx context.Context) (CustomFieldDefList, error) {
   108  	var fm mo.CustomFieldsManager
   109  
   110  	err := m.Properties(ctx, m.Reference(), []string{"field"}, &fm)
   111  	if err != nil {
   112  		return nil, err
   113  	}
   114  
   115  	return fm.Field, nil
   116  }
   117  
   118  func (m CustomFieldsManager) FindKey(ctx context.Context, name string) (int32, error) {
   119  	field, err := m.Field(ctx)
   120  	if err != nil {
   121  		return -1, err
   122  	}
   123  
   124  	for _, def := range field {
   125  		if def.Name == name {
   126  			return def.Key, nil
   127  		}
   128  	}
   129  
   130  	k, err := strconv.Atoi(name)
   131  	if err == nil {
   132  		// assume literal int key
   133  		return int32(k), nil
   134  	}
   135  
   136  	return -1, ErrKeyNameNotFound
   137  }
   138  
   139  func (l CustomFieldDefList) ByKey(key int32) *types.CustomFieldDef {
   140  	for _, def := range l {
   141  		if def.Key == key {
   142  			return &def
   143  		}
   144  	}
   145  	return nil
   146  }