【问题标题】:Custom attribute to VM ware Vsphere using Java SDK使用 Java SDK 的 VM ware Vsphere 的自定义属性
【发布时间】:2018-11-08 19:06:22
【问题描述】:

我正在尝试使用 VMware JAVA SDK 向 VMware vSphere 添加一个新的自定义属性,但它会引发“InvalidType”异常。

com.vmware.vim25.InvalidType
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at java.lang.Class.newInstance(Unknown Source)
at com.vmware.vim25.ws.XmlGen.fromXml(XmlGen.java:205)
at com.vmware.vim25.ws.XmlGen.parseSoapFault(XmlGen.java:82)
at com.vmware.vim25.ws.WSClient.invoke(WSClient.java:134)
at com.vmware.vim25.ws.VimStub.addCustomFieldDef(VimStub.java:284)
at com.vmware.vim25.mo.CustomFieldsManager.addCustomFieldDef(CustomFieldsManager.java:57)
at vmwaretest.Connect.setSelectedVM(Connect.java:85)
at vmwaretest.Connect.main(Connect.java:29)

代码

ServiceInstance si = null;
    try {
        si = Initialisation(url, user, password);
    } catch (RemoteException e) {

        e.printStackTrace();
    } catch (MalformedURLException e) {

        e.printStackTrace();
    }

    ServerConnection serverConnection = si.getServerConnection();
    ManagedObjectReference  mor = si.getMOR();

    CustomFieldsManager cfm = new CustomFieldsManager(serverConnection, mor);
    try {
        cfm.addCustomFieldDef("type", null, null, null);
    } catch (DuplicateName e) {
        e.printStackTrace();
    } catch (InvalidPrivilege e) {
        e.printStackTrace();
    } catch (RuntimeFault e) {
        e.printStackTrace();
    } catch (RemoteException e) {
        e.printStackTrace();
    }

与 vSphere 的连接工作正常,因为我能够检索或更改已经存在的自定义属性,但无法创建新属性。

上面的代码有什么问题?或者有没有其他方法可以使用 vmware Java SDK 添加新的自定义属性。

CustomFieldsManager 文档https://pubs.vmware.com/vsphere-6-5/index.jsp?topic=%2Fcom.vmware.wssdk.pg.doc%2FPG_Appx_Privileges_Reference.23.2.html

【问题讨论】:

    标签: vmware vsphere vmware-sdk


    【解决方案1】:

    下面是我定义的 Tag 类。自定义属性的键对所有 VMware 资源都是全局的,并且仅对每个资源的值进行更改。 因此,如果您要向 VM 或主机添加新的自定义属性键,则需要全局添加键,然后更改该键的值以用于该特定资源。

    以下是工作 java 示例。

    public class Tag{
           String key;
           String value;
        }
    
    public void addTags(ServiceInstance serviceInstance, VirtualMachine virtualMachine, List<Tag> tags) throws CloudServiceException, CloudParameterException{
            if(EmptyUtil.isNotEmpty(tags)){
                CustomFieldsManager customFieldsManager = serviceInstance.getCustomFieldsManager();
                CloudValidate.resourceNotNull(customFieldsManager, "Custom Fields cannot be set for VM as CustomFieldsManager instance could not be obtained.");
                CustomFieldDef[] customFieldDefs = customFieldsManager.getField();
                Set<String> existingTags = getExistingTags(customFieldDefs);
                for(Tag tag : tags){
                    if(!existingTags.contains(tag.getKey())){
                        updateTagsKey(customFieldsManager, tag.getKey());
                    }
                    updateTagsValue(virtualMachine, tag);
                }
            }
        }
    
        private void updateTagsKey(CustomFieldsManager customFieldsManager, String key){
            try {
                customFieldsManager.addCustomFieldDef(key, Constants.VMWARE_CUSTOMATTRIBUTE_TYPE_VM, null, null);
            }catch (DuplicateName dname){
                logger.warn("Custom attribute : {} already exists.", key);
            }catch(Exception exception) {
                logger.warn("Failed to add custom field due to : {}.", exception.getMessage());
            }
        }
    
        private void updateTagsValue(VirtualMachine virtualMachine, Tag tag){
            try{
                logger.info("Adding {} for Virtua Machine {}", tag.toString(), virtualMachine.getName());
                virtualMachine.setCustomValue(tag.getKey(), tag.getValue());
            }catch(Exception exception) {
                logger.warn("Failed to set custom attribute on VM due to  : {}.", exception.getMessage());
            }
        }
    
        private Set<String> getExistingTags(CustomFieldDef[] customFieldDefs){
            Set<String> existingTags = new HashSet<>();
            if(EmptyUtil.isNotEmpty(customFieldDefs)){
                for(CustomFieldDef customFieldDef : customFieldDefs){
                    existingTags.add(customFieldDef.getName());
                }
                logger.debug("Existing Custom Fields from Custom Fields Manager : {}", Arrays.toString(existingTags.toArray()));
            }
            return existingTags;
        }
    

    【讨论】:

      猜你喜欢
      • 2020-11-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多