Vcenter configuration manager installation and getting started guide (274 pages)
Summary of Contents for VMware VCENTER CHARGEBACK 1.5 - API
Page 1
Chargeback API Programming Guide vCenter Chargeback 1.5 This document supports the version of each product listed and supports all subsequent versions until the document is replaced by a new edition. To check for more recent editions of this document, see http://www.vmware.com/support/pubs. EN-000404-00...
Page 2
VMware products are covered by one or more patents listed at http://www.vmware.com/go/patents. VMware is a registered trademark or trademark of VMware, Inc. in the United States and/or other jurisdictions. All other marks and names mentioned herein may be trademarks of their respective companies.
Add a Fixed Cost 19 Modify a Fixed Cost Value 20 Generate a Report 21 Using vCenter Chargeback with a Billing System 23 Requirements for Code Examples 23 JAR Files 23 CommonUtil Class 24 FakeSSLCertificateSocketFactory Class 25 Add Cost Models 26 Add a Billing Policy 27 Retrieve List of Hierarchies 28 Get Details of a Hierarchy 29 Add Report Schedule for Hierarchy 30 Get Report Schedule by Hierarchy Name 33 Reschedule a Report 35 Delete Report Schedule 36 Get List of Archived Reports for a Hierarchy 37 Get a Report as XML 38 Index 41 VMware, Inc.
Page 4
Chargeback API Programming Guide VMware, Inc.
About This Book The vCenter Chargeback API Programming Guide provides information on how to use vCenter Chargeback APIs. Intended Audience This book is intended for anyone who develop applications to work with vCenter Chargeback. VMware Technical Publications Glossary VMware Technical Publications provides a glossary of terms that might be unfamiliar to you. For definitions of terms as they are used in VMware technical documentation go to http://www.vmware.com/support/pubs. Document Feedback VMware welcomes your suggestions for improving our documentation. If you have comments, send your feedback to docfeedback@vmware.com. vCenter Chargeback Documentation The vCenter Chargeback documentation comprises the following guides: vCenter Chargeback User’s Guide vCenter Chargeback API Programming Guide vCenter Chargeback API Reference Guide Technical Support and Education Resources The following sections describe the technical support resources available to you. To access the current version of this book and other books, go to http://www.vmware.com/support/pubs. Online and Telephone Support To use online support to submit technical support requests, view your product and contract information, and register your products, go to http://www.vmware.com/support.
Page 6
Chargeback API Programming Guide VMware Professional Services VMware Education Services courses offer extensive hands‐on labs, case study examples, and course materials designed to be used as on‐the‐job reference tools. Courses are available onsite, in the classroom, and live online. For onsite pilot programs and implementation best practices, VMware Consulting Services provides offerings to help you assess, plan, build, and manage your virtual environment. To access information about education classes, certification programs, and consulting services, go to http://www.vmware.com/services. VMware, Inc.
Chargeback APIs This chapter includes the following topics: “What Is vCenter Chargeback?” on page 7 “REST Architecture” on page 7 “Chargeback API Syntax” on page 9 “API Versioning” on page 10 What Is vCenter Chargeback? vCenter Chargeback is a cost reporting solution for environments virtualized using vSphere. This Web‐based application interacts with the vCenter Database to retrieve usage information, calculates the cost by using the defined Chargeback formulas, and generates reports. vCenter Chargeback runs on an Apache Tomcat server instance. The user interacts with the vCenter Chargeback application through a load balancer (Apache HTTP Server). vCenter Chargeback connects to a Chargeback database that stores information such as the defined hierarchies, cost model, users, roles, and so on. The application also interacts with the vCenter Server and vCenter Database through a Data Collector. The Data Collector uses VIM APIs to communicate with the vCenter Server and JDBC to communicate with the vCenter Database. vCenter Chargeback APIs provide an interface for application developers to programmatically use the functionality of vCenter Chargeback. For more information about Chargeback and its capabilities, see the vCenter Chargeback User’s Guide. REST Architecture vCenter Chargeback APIs implement the Representational State Transfer (REST) architecture. REST‐based APIs help you send HTTP requests for resources over the network and receive responses. VMware, Inc.
1‐1 shows how each of these request types maps to a standard CRUD operation. Table 1-1. Request Type Mapping Request Type CRUD Operation POST CREATE READ UPDATE/CREATE DELETE DELETE Along with the HTTP requests, you can pass request parameters by using XMLs. An example request XML for the Login API is provided here. <?xml version="1.0" encoding="UTF-8" standalone="yes"?> <Request xmlns="http://www.vmware.com/vcenter/chargeback/1.5.0"> <Users> <User> <Name>admin</Name> <Password>ttt</Password> <Type>local</Type> </User> </Users> </Request> Responses When an API task is successful, the value of the status parameter in the response XML is set to success as shown in the following example. <?xml version="1.0" encoding="UTF-8"?> <Response status="success" isValidLicense="true"...
An API response starts with this element. status Denotes whether API is successfully executed. IsValidLicense Indicates the status of the license. Value can be true or false. For an example request XML, see “Requests.” For an example response XML, see “Responses.” Chargeback API Syntax Each vCenter Chargeback API has the following syntax: <HTTP_request_method> <Base_Url>/<API_signature> For example, GET https://123.123.123.123/vCenter-CB/api/hierarchies Table 1‐4 describes the components of vCenter Chargeback API syntax. Table 1-4. API Syntax Components Syntax Component Description HTTP_request_method PUT, POST, GET, or DELETE Base_Url The URL of the Chargeback host. The base URL for vCenter Chargeback APIs is: https://<Chargeback server IP>/vCenter-CB/api For example, https://123.123.123.123/vCenter-CB/api API_signature The URL path for a Chargeback API. For example, /hierarchies retrieves the hierarchies added to the Chargeback server running on 123.123.123.123. version (Optional) API version. If you need to use Chargeback server 1.0.1, specify the version as 1.0.1. If you do not specify the version, the API version will be considered as 1.5.0. VMware, Inc.
Chargeback API Programming Guide If you want to use vCenter Chargeback 1.0.1 APIs, you need to do the following tasks: In the API URL, specify version=1.0.1. For example, <HTTP_request_method> <Base_Url>/<API_signature>?version=1.0.1 In the request XML, update the request element with the following tag: <Request xmlns="http://www.vmware.com/vcenter/chargeback/1.5.0"> API Versioning Every vCenter Chargeback 1.5.0 API request and response includes target namespace to denote the API version. For example, the following request XML sends the version with which it is working. <?xml version="1.0" encoding="UTF-8"?> <Request xmlns="http://www.vmware.com/vcenter/chargeback/1.5.0"> <Hierarchies> <Hierarchy> <Name>Test_Hierarchy</Name> <Description>Test Hierarchy</Description> </Hierarchy> </Hierarchies> </Request> The request states that vCenter Chargeback API version 1.5.0 is being called. If the request is for vCenter Chargeback server version 1.0.1, the call fails. If the request is for vCenter Chargeback server 1.5.0, the call succeeds and gets a response similar to the following. <?xml version="1.0" encoding="UTF-8"?> <Response xmlns="http://www.vmware.com/vcenter/chargeback/1.5.0"...
“Requirements for Code Examples” on page 11 “Log In to vCenter Chargeback Server” on page 14 “Add vCenter Server Information” on page 15 “Add a Custom Chargeback Hierarchy” on page 16 “Add a vCenter Server Entity to the Chargeback Hierarchy” on page 17 “Add a Fixed Cost” on page 19 “Modify a Fixed Cost Value” on page 20 “Generate a Report” on page 21 Requirements for Code Examples This chapter provides code examples to explain how you can call the APIs. To run these code examples, you require the following JAR and helper class files. JAR Files The code examples need the following JAR files. commons-httpclient-3.1.jar commons-logging-1.1.1.jar jdom.jar You must place these JAR files in the classpath. VMware, Inc.
* @param is input stream that has the XML content * @return jDOM document representation for the XML * @throws IOException * @throws JDOMException private static Document getXMLDocument(InputStream is) throws IOException, JDOMException { Document xml = null; SAXBuilder builder = new SAXBuilder(); xml = builder.build(is); return xml; VMware, Inc.
Socket createSocket(String s, int i, InetAddress inetaddress, int j, HttpConnectionParams httpconnectionparams) throws IOException, UnknownHostException, ConnectTimeoutException { return sslContext.getSocketFactory().createSocket(s, i, inetaddress, j); @Override public Socket createSocket(Socket socket, String s, int i, boolean flag) throws IOException, UnknownHostException { return sslContext.getSocketFactory().createSocket(socket, s, i, flag); VMware, Inc.
In the request XML for this API, you can specify the following login details. User type User name Password LDAP server ID or LDAP server name The following is an example request XML file. <?xml version="1.0" encoding="UTF-8"?> <Request xmlns="http://www.vmware.com/vcenter/chargeback/1.5.0"> <Users> <User> <Type>userType</Type> <Name>administrator</Name> <Password>vmware</Password> <!-- For LDap User you can either provide LdapServer id or ldap server name --> <LdapServer id=”1”/> </User> </Users> <!-- For Ldap Users -->...
vCenter Server Username vCenter Server Password PluginRegistered StatsSync Datasource URL Data Source Name Data Source User Name Data Source Password Data Source Type DataSourceAuthType The following is an example request XML. <?xml version="1.0" encoding="UTF-8"?> <Request xmlns="http://www.vmware.com/vcenter/chargeback/1.5.0"> <VCenterServers> <VCenterServer> <Url>101.102.107.21:443</Url> <Name>vc1</Name> <Description>vc1</Description> <UserName>administrator</UserName> <Password>xxxx</Password> <PluginRegistered>true</PluginRegistered> <StatsSync>true</StatsSync> <DataSourceUrl> 10.112.107.21\sqlexp_vim VMware, Inc.
System.out.println(bodyString); try { post = new PostMethod(uri); post.setRequestBody(bodyString); client.executeMethod(post); System.out.println(post.getResponseBodyAsString()); } finally { if (post != null) { post.releaseConnection(); Add a Custom Chargeback Hierarchy Use the Add a Chargeback Hierarchy API to create a hierarchy with the given name and description. To add a custom Chargeback hierarchy Call the API by using the following syntax. <HTTP_request_method> <Base_URL>/hierarchy For example, you can define a call like this: POST https://123.123.123.123/vCenter-CB/api/hierarchy In the request XML, specify a name and a description for the hierarchy. VMware, Inc.
Chapter 2 Understanding the Workflow The following is an example request XML. <?xml version="1.0" encoding="UTF-8"?> <Request xmlns="http://www.vmware.com/vcenter/chargeback/1.5.0"> <Hierarchies> <Hierarchy> <Name>Test_Hierarchy</Name> <Description>Test Hierarchy</Description> </Hierarchy> </Hierarchies> </Request> If successful, the API returns details of the new hierarchy. The following is an example program that calls the API. * This method is to add a vCenter-ChargeBack hierarchy in * vCenter-ChargeBack * @param requestFilePath * @param baseURL * @throws IOException...
Page 18
String uri = "https://" + baseURL + "/vCenter-CB/api/hierarchy/" + hierachyId + "/entity/" + CBEntityId; HttpClient client = new HttpClient(); System.out.println(uri); System.out.println(bodyString); try { post = new PostMethod(uri); post.setQueryString(parameters); post.setRequestBody(bodyString); client.executeMethod(post); System.out.println(post.getResponseBodyAsString()); } finally { if (post != null) { post.releaseConnection(); VMware, Inc.
Add a Fixed Cost A fixed cost is a definite cost that can be charged on an entity. Using the Add Fixed Cost API, you can create fixed costs for entities. To add a fixed cost Call the API using the following syntax. <HTTP_request_method> <Base_URL>/fixedCost For example, you can define a call like this: POST https://123.123.123.123/vCenter-CB/api/fixedCost In the request XML, specify a name and a description for the fixed cost. The following is an example request XML. <?xml version="1.0" encoding="UTF-8"?> <Request xmlns="http://www.vmware.com/vcenter/chargeback/1.5.0"> <FixedCosts> <FixedCost> <Name>Fixed Cost 1</Name> <Description>Fixed Cost 1 description</Description> <Currency id="1"/> </FixedCost> </FixedCosts> </Request> If the task is successful, the API returns an XML file that provides details of the new fixed cost. The following is an example program that calls the API.
Chargeback API Programming Guide Modify a Fixed Cost Value Using the Modify Fixed Cost API, you can update the ID, value, and duration for a fixed cost. To modify a fixed cost value Call the API by using the following syntax. <HTTP_request_method> <Base_URL>/fixedCost/{fixedCostId}/values For example, you can define a call like this: PUT https://123.123.123.123/vCenter-CB/api/fixedCost/{fixedCostId}/values In the request XML, specify a name and a description for the fixed cost. The following is an example request XML. <?xml version="1.0" encoding="UTF-8"?> <Request xmlns="http://www.vmware.com/vcenter/chargeback/1.5.0"> <FixedCosts> <FixedCost id="1"> <Values> <Value> <Cost>3.1415</Cost> <Duration id="1"/> </Value> </Values> </FixedCost> </FixedCosts>...
“Retrieve List of Hierarchies” on page 28 “Get Details of a Hierarchy” on page 29 “Add Report Schedule for Hierarchy” on page 30 “Get Report Schedule by Hierarchy Name” on page 33 “Reschedule a Report” on page 35 “Delete Report Schedule” on page 36 “Get List of Archived Reports for a Hierarchy” on page 37 “Get a Report as XML” on page 38 Requirements for Code Examples This chapter provides code examples to explain how you can call the APIs. To run these code examples, you require the following JAR and helper class files. JAR Files The code examples need the following JAR files. commons-httpclient-3.1.jar commons-logging-1.1.1.jar jdom.jar You must place these JAR files in the classpath. VMware, Inc.
* @param is input stream that has the XML content * @return jDOM document representation for the XML * @throws IOException * @throws JDOMException private static Document getXMLDocument(InputStream is) throws IOException, JDOMException { Document xml = null; SAXBuilder builder = new SAXBuilder(); xml = builder.build(is); return xml; VMware, Inc.
Socket createSocket(String s, int i, InetAddress inetaddress, int j, HttpConnectionParams httpconnectionparams) throws IOException, UnknownHostException, ConnectTimeoutException { return sslContext.getSocketFactory().createSocket(s, i, inetaddress, j); @Override public Socket createSocket(Socket socket, String s, int i, boolean flag) throws IOException, UnknownHostException { return sslContext.getSocketFactory().createSocket(socket, s, i, flag); VMware, Inc.
To add a cost model Call the Add Cost Model API by specifying the following URL in your program. POST https://<ipaddress>/vCenter-CB/api/costModel In the request XML, specify the following information. Name: Specify Networking as the name of the cost model. Description: Provide a brief description for the Networking cost model. Currency ID: (Optional) Specify the ID of the currency to be set for this cost model. If not specified, the currency ID will automatically be set to US Dollars (USD) or the global currency ID that was set during Chargeback server upgrade. For a list of currencies supported by vCenter Chargeback, see the Appendix of the vCenter Chargeback API Reference Guide. The following is an example request XML. <?xml version="1.0" encoding="UTF-8"?> <Request xmlns="http://www.vmware.com/vcenter/chargeback/1.5.0"> <CostModels> <CostModel> <Name>Networking</Name> <Description>Networking Cost Model</Description> <Currency id="1"/> </CostModel> </CostModels> </Request> Add the following cost models. Allocation Pool Reservation Pool ...
Description: A brief description of the billing policy. Expression: The expression defines attribute values that identify the computing resource units to be considered for cost calculation. For a list of computing resources and their associated attribute values, see the Appendix of the vCenter Chargeback API Reference Guide. For a computing resource the expression can be the maximum of available attributes. It can also account for the fixed costs, linked clones and the state of the virtual machine. The following is an example request XML. <?xml version="1.0" encoding="UTF-8"?> <Request xmlns="http://www.vmware.com/vcenter/chargeback/1.5.0"> <BillingPolicy> <Name>New Billing Policy </Name> <Description>New Flexible Billing Policy Expression</Description> <Expression>cpu=max(usage, reservation);memory=max(usage,reservation);rest=usage; </Expression> </BillingPolicy> </Request> The following is an example program that calls the API. This program assumes that the request XML is populated with the required information. * This method is to add a billing policy in vCenter-ChargeBack...
Add Report Schedule for Hierarchy You can schedule reports to be generated at regular intervals. Add a report schedule for the hierarchy by using the Report Schedule API. To add a report schedule for a hierarchy Call the Report Schedule API by using the following URL in your program. POST <API base URL>/reportSchedule For example, POST https://123.123.123.123/vCenter-CB/api/reportSchedule The following is an example request XML. <?xml version="1.0" encoding="UTF-8"?> <Request xmlns="http://www.vmware.com/vcenter/chargeback/1.5.0"> <ReportSchedules> <ReportSchedule> <MetaData> <Name>Name</Name> <Description>Description</Description> <ReportType>COST_REPORT</ReportType> <OwnedBy>1</OwnedBy> <OwnedByName>MyUser</OwnedByName> <ReportPeriodType>WEEKLY</ReportPeriodType> <ReportPeriod>...
Page 31
<!-- <ShowInfoDepth> Removed --> <!-- New Fields --> <ShowCostModelName>true</ShowCostModelName> <ShowInformationalMessageTable>true</ShowInformationalMessageTable> <ShowEntityAttributeTable>true</ShowEntityAttributeTable> <!-- Removed and moved as table attribute <ShowUsageTable>true</ShowUsageTable> <ShowFixedCostTable>true</ShowFixedCostTable> --> <UsageTable show="true"> <Column id="col.id.startTime"> <Order /> </Column> <Column id="col.id.endTime"> <Order>1</Order> </Column> <Column id="col.id.resource"> <Order>2</Order> </Column> <Column id="col.id.baseRate"> <Order>3</Order> VMware, Inc.
Get Report Schedule by Hierarchy Name Depending on how you charge each customer, you might have mapped entities in the hierarchy to appropriate cost models. You can call the Search API to get the report schedule for the hierarchy and verify the entity to cost model mappings. To get schedule by hierarchy name Call the Search API by using the following URL in your program. POST <API base URL>/search In the request XML, specify the following parameters to search schedules. You can use any of these parameters alone or in combination with the others. name desc hierarchyName costModelName You can use search operators such as EQUAL, NOT_EQUAL, BETWEEN, GT, LT, GT_EQUAL, LT_EQUAL, NULL, NOT_NULL, LIKE, NOT_LIKE to filter your search. VMware, Inc.
Page 34
HttpClient client = new HttpClient(); Protocol.registerProtocol("https", new Protocol("https", (ProtocolSocketFactory) new FakeSSLCertificateSocketFactory(), 443)); String uri = "https://" + baseURL + "/vCenter-CB/api/search"; try { post = new PostMethod(uri); post.setRequestBody(bodyString); client.executeMethod(post); System.out.println(post.getResponseBodyAsString()); } finally { if (post != null) { post.releaseConnection(); VMware, Inc.
Chapter 3 Using vCenter Chargeback with a Billing System Reschedule a Report You can update a report schedule using the Reschedule Report API. The Reschedule Report API updates the schedule information. If you want to update the entity to cost model mapping, you must delete the schedule and create a new schedule. To reschedule a report Call the Reschedule Report API by using the following URL in your program. PUT <API base URL>/reportSchedule/{scheduleId} For example, PUT https://123.123.123.123/vCenter-CB/api/reportSchedule/5 The following is an example request XML. <?xml version="1.0" encoding="UTF-8"?> <Request xmlns="http://www.vmware.com/vcenter/chargeback/1.5.0"> <ReportSchedules> <ReportSchedule> <MetaData> <ReportPeriodType>MONTHLY</ReportPeriodType> <ReportPeriod> <Monthly> <StartDayOfMonth>1</StartDayOfMonth> </Monthly> </ReportPeriod> </MetaData> <ScheduleDetail>...
DELETE <API base URL>/reportSchedule/{scheduleId} For example, DELETE https://123.123.123.123/vCenter-CB/api/reportSchedule/5 The response XML indicates that the delete operation is successful. The following is an example program that calls the API. This program assumes that the request XML is populated with the required information. * This method is to delete report schedule in vCenter-ChargeBack * @param baseURL * @param scheduleId * @throws IOException * @throws HttpException * @throws NoSuchAlgorithmException * @throws KeyManagementException * @throws JDOMException VMware, Inc.
GET <API base URL>/report/{reportId}/export?exportFormat=XML For example, GET https://123.123.123.123/vCenter-CB/api/report/10/export?exportFormat=XML The API queues the export report request as a task and returns the status of the queued task. You can check the status of the queued task using the Get Queued Task Status API. After the exported report is ready, the Export Report API returns the report in XML format. In the response XML, check the ComputeData section for the cost details. For a detailed description of all the elements in the response XML, see the vCenter Chargeback API Reference Guide. The following is an example program that calls the API. This program assumes that the request XML is populated with the required information. * This method get report as XML vCenter-ChargeBack * @param reportId * @param baseURL * @throws IOException * @throws HttpException * @throws NoSuchAlgorithmException * @throws KeyManagementException VMware, Inc.
Page 39
FakeSSLCertificateSocketFactory(), 443)); String uri = "https://" + baseURL + "/vCenter-CB/api/report/" + reportId + "/export"; NameValuePair[] parameters = {new NameValuePair("exportFormat", "XML")}; try { get = new GetMethod(uri); get.setQueryString(parameters); client.executeMethod(get); System.out.println(get.getResponseBodyAsString()); } finally { if (get != null) { get.releaseConnection(); VMware, Inc.
Page 40
Chargeback API Programming Guide VMware, Inc.
11, 23 Search 33 CommonUtil class 12, 24 search 28 architecture, REST-based 7 fixed cost vCenter Chargeback APIs 7 add 19 archived reports modify value 20 example program 38 get 37 request XML 37 generate report 21 VMware, Inc.
Page 42
10 modify value of fixed cost 20 vCenter Chargeback API about 7 vCenter Server report add 15 generate 21 in XML 38 reschedule 35 XML format 38 report 38 report schedule XML elements add 30 common 9 VMware, Inc.
Need help?
Do you have a question about the VCENTER CHARGEBACK 1.5 - API and is the answer not in the manual?
Questions and answers