Welcome back to the third part of the three-part series describing how to use the new API Client provided in the UI Extensibility Seed Project. In this post, I demonstrate how to use the data management functionality provided by the API Client.
Retrieve Data
The API Client provides two methods to retrieve data from the vCloud API, get and getEntity. Both methods return an observable.
get
this.client
.get('api/org')
.subscribe(org => {
console.log(org);
});
getEntity
this.client
.getEntity("urn:vcloud:org:625ddb93-fc23-4145-accf-53898e3201d4")
.subscribe(org=> {
console.log(org);
});
Sync vs Async
The vCloud API provides endpoints for creating, updating and deleting records. Each of these endpoints are configured to respond either synchronously or asynchronously. To determine the expected output types, look at the Output Type specified in the User Operations, Admin Operations or Extension Operations section of the vCloud API Reference. If the expected Output Type is a TaskType, then you will use the Async version of the method. Otherwise, use the Sync version of the method.
|
Synchronous:
|
Asynchronous:
|
Regardless of whether you use the synchronous or asynchronous version of the methods listed above, the browser will always send the request in an asynchronous manner.
The Sync version of each of these methods returns an observable for the specified object type. The Async version, instead returns a TaskType object.
Task Helpers
The API Client includes a few helper methods to simplify working with tasks.
isTaskComplete
this.client.isTaskComplete(Task)
updateTask
this.client
.updateTask(Task)
.subscribe(latestTask => {
console.log(latestTask);
})
Create
The Client API provides two methods to create new records: createSync and createAsync. Both require that you create the desired dataset to send to the vCloud API. Use Input Type parameter listed in the Operations sections of the vCloud API Reference to help define that dataset.
Here we will create a new organization using the createSync method.
import { AdminOrgType, OrgSettingsType, OrgGeneralSettingsType } from "@vcd/bindings/vcloud/api/rest/schema_v1_5";
let newOrg = <AdminOrgType> {
name: 'TestOrg',
fullName: 'Test Organization',
isEnabled: false,
settings <OrgSettingsType> {
orgGeneralSettings: <OrgGeneralSettingsType> {
canPublishCatalogs: true,
canPublishExternally: true,
deployedVMQuota: 0
}
}
}
let newOrg = new AdminOrgType; newOrg.name = 'TestOrg'; newOrg.fullName = 'Test Organization'; newOrg.isEnabled = false; newOrg.settings = new OrgSettingsType; newOrg.settings.orgGeneralSettings = new OrgGeneralSettingsType; newOrg.settings.orgGeneralSettings.canPublishCatalogs = true; newOrg.settings.orgGeneralSettings.canPublishExternally = true; newOrg.settings.orgGeneralSettings.deployedVMQuota = 0;
this.client
.createSync<AdminOrgType>('api/admin/orgs', newOrg)
.subscribe(org => {
console.log(org);
});
Update
this.client
.get<AdminOrgType>('api/admin/org/1084874f-9de0-4643-b935-37bf19654e3e')
.subscribe(org => {
org.name='TestOrgUpdated';
org.fullName='Test Organization Updated';
this.client
.updateSync<AdminOrgType>(
'api/admin/org/1084874f-9de0-4643-b935-37bf19654e3e',
org)
.subscribe(updatedOrg => {
console.log(updatedOrg);
})
});
Delete
The Client API provides three methods to delete records: removeItem, deleteSync and deleteAsync.
this.client
.get<AdminOrgType>('api/admin/org/1084874f-9de0-4643-b935-37bf19654e3e')
.subscribe(org => {
this.client
.removeItem(org)
.subscribe(resp => {
console.log(resp);
})
});
this.client
.deleteSync('api/admin/org/1084874f-9de0-4643-b935-37bf19654e3e')
.subscribe(resp => {
console.log(resp)
});
Conclusion
I hope that this series has helped provide some insight into how to use the UI Extensibility API Client. The API Client already provides a lot of functionality out of the box. However, the development effort continues at a rapid pace and I expect that more is coming soon. Please download the Seed Project and start creating new and exciting vCloud Director Extensions.

