Professional Documents
Culture Documents
John Hardy
Product Manager
CloudForms Management BU!
Agenda!
Welcome!
About RESTapi!
Lab Setup!
Inventory!
Requests!
Provisioning !
Reference Material
http://manageiq.org/documentation/development/rest_api/reference/!
!
Technical Syntax
RestAPI Standard Syntax, All REST is URI based and the construct will always be Actions and
Resources.!
!
Get Return resource from a webservice.!
!
/api/services Returns the services.!
/api/hosts Returns the hosts.!
!
Put Change data in an existing resource.!
!
/api/services/1!
{!
"name" : "service_1",!
"description" : "This is an updated description for the first service"!
}!
!
Post Send a new resource to the webservice.!
!
/api/services/1!
{!
"action" : "edit",!
"resource" : {!
"name" : "service_1",!
"description" : "This is an updated description for the first service"!
}!
}!
Technical Syntax
!
Delete Delete a resource from a webservice.!
!
/api/services/<id> - Will remove the service from the system.!
!
Patch Change data in an existing resource.!
!
Supported attribute actions include, add, edit and remove.!
!
/api/services/1!
!
[!
{ "action" : "edit", "path" : "name", "value" : "service_001" },!
{ "action" : "remove", "path" : "description"}!
]!
Lab Setup
We will all share the same CloudForms appliance sitting within Amazon EC2.!
!
The CloudForms public IP address is x.x.x.x!
!
Login Details are!
!
Username = admin!
Password = smartvm!
!
You do not need to logon interactively to the appliance, we will do everything via RESTapi.!
!
You will need the ability to run RESTapi actions. I use RUBY scripting language to demonstrate,
however you can use anything that supports RESTapi, here are some others ways;!
!
CURL!
Python!
SOAPui!
Chrome (Postman plugin)!
!
SSL Problem
When running an example, depending on your OS configuration you might have SSL Cert Verification
errors as follows.!
require 'rest-client'!
require 'json'!
!
#get the inital set of VMS, but using filters for TAG and expanding the resources!
result = RestClient.get 'https://admin:smartvm@<CFME>/api/vms?expand=resources'!
items = JSON.parse(result)!
!
RUBY
SSL Solution
Add to the top of each script!
!
require 'openssl'!
!
OpenSSL::SSL::VERIFY_PEER = OpenSSL::SSL::VERIFY_NONE!
!
Example below!
require 'rest-client'!
require 'json!
!
#Hack to disable SSL verification!
require 'openssl
OpenSSL::SSL::VERIFY_PEER = OpenSSL::SSL::VERIFY_NONE
!
RUBY
#get the inital set of VMS, but using filters for TAG and expanding the resources!
result = RestClient.get 'https://admin:smartvm@<CFME>/api/vms?expand=resources'!
items = JSON.parse(result)!
!
#Process each vm in the returned hash of VMS!
items['resources'].each do |vm|!
puts "Processing VM #{vm['name']} --> vmID = #{vm['id']}"!
puts " flavor_id --> #{vm['flavor_id']}"!
puts " availability_zone_id --> #{vm['availability_zone_id']}"!
end!
!
WARNING: OpenSSL::SSL::VERIFY_PEER == OpenSSL::SSL::VERIFY_NONE!
This dangerous monkey patch leaves you open to MITM attacks!!
Try passing :verify_ssl => false instead.!
Processing VM Tester1 --> vmID = https://<CFME>/api/vms/1000000000157!
WARNING: OpenSSL::SSL::VERIFY_PEER == OpenSSL::SSL::VERIFY_NONE!
This dangerous monkey patch leaves you open to MITM attacks!!
Try passing :verify_ssl => false instead.!
---Processing TAG /managed/jonnyfive/not_here2!
---Processing TAG /managed/jonnyfive/is_here!
---Processing TAG /managed/function/database!
require 'rest-client'!
require 'json'!
!
#get the inital set of VMS, but using filters for TAG and expanding the resources!
result = RestClient.get 'https://admin:smartvm@<CFME>/api/vms?expand=resources'!
items = JSON.parse(result)!
!
RUBY
CURL
{"name":"vms","count":4,"subcount":4,"resources":[{"id":"https://<CFME>/api/vms/1000000000156","vendor":"amazon","name":"First","location":"ec2-54-65-22-200.apnortheast-1.compute.amazonaws.com","created_on":"2014-11-29T12:37:05Z","updated_on":"2014-11-29T12:37:05Z","guid":"6d0db864-77c4-11e4-95e9-0acdf3aaf2e8",
"ems_id":
1000000000002,"uid_ems":"i-0e3e61fc","power_state":"on","state_changed_on":"2014-11-29T12:37:05Z","template":false,"vdi":false,"type":"VmAmazon","ems_ref":"i-0e3e6
1fc","flavor_id":1000000000044,"availability_zone_id":1000000000007,"cloud":true,"cloud_network_id":1000000000003,"cloud_subnet_id":
1000000000004,"raw_power_state":"running"},{"id":"https://<CFME>/api/vms/1000000000157","vendor":"amazon","name":"Tester1","location":"ec2-54-65-67-1.apnortheast-1.compute.amazonaws.com","created_on":"2014-11-29T14:37:31Z","updated_on":"2014-11-29T14:37:31Z","guid":"401cc17c-77d5-11e4a11e-0acdf3aaf2e8","ems_id":1000000000002,"uid_ems":"ia1154b53","power_state":"on","state_changed_on":"2014-11-29T14:37:31Z","template":false,"vdi":false,"type":"VmAmazon","ems_ref":"i-a1154b53","flavor_id":
1000000000044,"availability_zone_id":1000000000007,"cloud":true,"cloud_network_id":1000000000003,"cloud_subnet_id":
1000000000004,"raw_power_state":"running"},{"id":"https://<CFME>/api/vms/1000000000158","vendor":"amazon","name":"restAPI","location":"ec2-54-65-147-172.apnortheast-1.compute.amazonaws.com","created_on":"2014-12-01T06:46:02Z","updated_on":"2014-12-01T06:46:10Z","guid":"b748cccc-7925-11e4a431-0acdf3aaf2e8","ems_id":
1000000000002,"uid_ems":"i-07df52f4","power_state":"on","state_changed_on":"2014-12-01T06:46:02Z","template":false,"vdi":false,"type":"VmAmazon","ems_ref":"i-07df5
2f4","flavor_id":1000000000062,"availability_zone_id":1000000000005,"cloud":true,"cloud_network_id":1000000000003,"cloud_subnet_id":
1000000000003,"raw_power_state":"running"},{"id":"https://<CFME>/api/vms/1000000000159","vendor":"amazon","name":"ProvTest","location":"ec2-54-65-141-103.apnortheast-1.compute.amazonaws.com","created_on":"2014-12-01T06:56:45Z","updated_on":"2014-12-01T06:56:55Z","guid":"36cde79c-7927-11e4a431-0acdf3aaf2e8","ems_id":
1000000000002,"uid_ems":"i-9c643d6e","power_state":"on","state_changed_on":"2014-12-01T06:56:45Z","template":false,"vdi":false,"type":"VmAmazon","ems_ref":"i-9c64
3d6e","flavor_id":1000000000062,"availability_zone_id":1000000000007,"cloud":true,"cloud_network_id":1000000000003,"cloud_subnet_id":
1000000000004,"raw_power_state":"running"}]}!
#get the inital set of VMS, but using filters for TAG and expanding the resources!
result = RestClient.get 'https://admin:smartvm@<CFME>/api/vms?expand=resources&by_tag=/function/database'!
items = JSON.parse(result)!
!
#quick hack to insert authentication, should use token oAuth for multiple transactions!
id = vm['id'].gsub("https://", "https://admin:smartvm@")!
RUBY
!
Note
The next task will be to call an AUTOMATION_REQUEST to
create a category & tag. !
!
This is a demonstration of how you can call *ANYTHING* within
CloudForms externally.!
!
It should be noted that at time of print, native TAGGING and
CATEGORY actions are NOT available in the RESTapi.!
!
When they do become available, then the actions for
CATEGORY and TAG management should be used for their
purposes.!
!
URI = /System/Request/createTAG!
Parameters :!
Category Your NAME!
Tag - Something!
require 'rest-client'!
require 'json'!
!
RUBY
{"results":[{"approval_state":"pending_approval","created_on":"2014-12-01T05:41:19Z","description":"Automation Task","destination_id":null,"destination_type":null,"fulfilled_on":null,"id":
1000000000069,"message":null,"options":{"namespace":"SYSTEM","class_name":"PROCESS","instance_name":"AUTOMATION_REQUEST","user_id":1000000000001,"attrs":
{"userid":"admin"},"uri_parts_keeps":[{"namespace":"System","class":"Request","instance":"createTAG","message":"create"}],"parameters_keeps":
[{"category":"jonnyfive","tag":"is_here"}]},"request_state":"pending","request_type":"automation","requester_id":
1000000000001,"requester_name":"Administrator","source_id":null,"source_type":null,"status":"Ok","updated_on":"2014-12-01T05:41:19Z","userid":"admin"}]}!
URI = /System/Request/createTAG!
Parameters :!
Category Your NAME!
Tag - Something!
require 'rest-client'!
require 'json'!
!
RUBY
{"results":[{"approval_state":"pending_approval","created_on":"2014-12-01T06:15:47Z","description":"Automation Task","destination_id":null,"destination_type":null,"fulfilled_on":null,"id":
1000000000080,"message":null,"options":{"namespace":"SYSTEM","class_name":"PROCESS","instance_name":"AUTOMATION_REQUEST","user_id":1000000000001,"attrs":
{"userid":"admin"},"uri_parts_keeps":[{"namespace":"System","class":"Request","instance":"tagOBJ","message":"create"}],"parameters_keeps":
[{"vmid":"1000000000157","category":"jonnyfive","tag":"not_here2"}]},"request_state":"pending","request_type":"automation","requester_id":
1000000000001,"requester_name":"Administrator","source_id":null,"source_type":null,"status":"Ok","updated_on":"2014-12-01T06:15:47Z","userid":"admin"}]}!
require 'rest-client'!
require 'json'!
!
id = "https://admin:smartvm@<CFME>/api/vms/1000000000157"!
!
RUBY
RUBY
},!
:requester => {!
:user_name => "admin",!
:owner_first_name => "John",!
:owner_last_name => "Hardy",!
:owner_email => "jhardy@redhat.com",!
:auto_approve => "true"!
}!
}.to_json!
require 'rest-client'!
require 'json'!
!
id = "https://admin:smartvm@<CFME>/api/provision_requests/1000000000083"!
!
RUBY