You can create a local content library programmatically by using the vSphere Automation API. The API allows you to populate the content library with OVF and vApp templates. You can use these templates to deploy virtual machines or vApps in your virtual environment.

Prerequisites

Required privileges:
  • Content library.Create local library on the vCenter Server instance where you want to create the library.
  • Datastore.Allocate space on the destination datastore.

Procedure

  1. Access the LocalLibrary service that provides support for creating local content libraries.
  2. Create a StorageBacking instance and define the storage location.
  3. (Optional) Create a SecurityPolicies instance to define the security policy rules for the library. For vSphere 7.0 Update3, you can only define rules for an OVF and OVA templates in the library by using the OVF_STRICT_VERIFICATION security rule. When the OVF security policy is configured for a local content library, an OVF or OVA template can be synchronized or imported to the library only after its certificate is validated against a trusted certificate authority.
  4. Create a LibraryModel instance and set the properties of the new local library.
    If you want to apply a security policy on the local library, you must use the setSecurityPolicyId(java.lang.String securityPolicyId) method or the security_policy_id parameter of the LibraryModel instance.
  5. Call the create function on the LocalLibrary object and pass the LibraryModel as a parameter.

Results

A local content library is created on the vCenter Server instance and you can edit its contents.

What to do next

You maintain the contents of the local library by managing its library items. See How To Use Library Items and Content Library Support for OVF and OVA Packages. You can also share the library content by publishing the local library. See Publish an Existing Content Library.
Java
This example is based on the code in the LibraryCrud.java sample file.

This example uses the steps that are described in the Create a Local Content Library procedure.

Note: For a complete and up-to-date version of the Java sample code, see the vsphere-automation-sdk-java VMware repository at GitHub.
...

// Create a StorageBacking instance to back the library content on the local file system.
     StorageBacking libraryBacking = new StorageBacking();
     libraryBacking.setType(Type.OTHER);
     libraryBacking.setStorageUri(URI.create("file:///tmp"));
     libraryModel.setStorageBackings(Collections.singletonList(libraryBacking));

// Create a LibraryModel that represents a local library. 
     LibraryModel libraryModel = new LibraryModel();
     libraryModel.setType(LibraryModel.LibraryType.LOCAL);
     libraryModel.setName("AcmeLibrary");

// Access the LocalLibrary service by using the  endpoint.
     LocalLibrary localLibraryService = this.vapiAuthHelper.getStubFactory().createStub(LocalLibrary.class, sessionStubconfig);

// Call the create method of the LocalLibrary service passing as an
// argument the LibraryModel instance.
     String libraryId = localLibraryService.create(UUID.randomUUID().toString(), libraryMod
Python
This example creates a local library with name AcmeLibrary , which is stored on the local file system where vCenter Server runs.

This example uses the steps that are described in the Create a Local Content Library procedure.

Note: For related code samples, see the vsphere-automation-sdk-python VMware repository at GitHub.
...
 
# 1 - Create a storage backing instance on a local file system.
library_backing = library_client.StorageBacking()
library_backing.type = library_client.StorageBacking.Type.OTHER
library_backing.storage_uri = ’file:///tmp’
 
# 2 - Create a Library model to specify properties of the new library.
library_model = content_client.LibraryModel()
library_model.type = content_client.LibraryModel.LibraryType.LOCAL
library_model.name = ’AcmeLibrary’
library_model.storage_backings = [library_backing]
 
# 3 - Call the create() method, passing the library model as a parameter.
idem_token = str(uuid.uuid4())
local_library_stub = content_client.LocalLibrary(my_stub_config)
library_id = local_library_stub.create(create_spec=library_model,
                                       client_token=idem_token)