This is an overview of the new RESTful API in the ManageIQ Anand release. Build cross-cloud applications and management systems using ManageIQ as a developer platform.
More more on ManageIQ, see http://manageiq.org/
2. Agenda
• What is RestAPI
• Why have RestAPI
• Use Cases
• How to Play
• Authentication
• Technical Syntax
‹#›
• Get
• Post
• Put
• Automation Request
• Provisioning Request
• Further Information
• Q&A
3. What is RestAPI?
REST stands for Representational State Transfer. It relies
‹#›
on a stateless, client-server, cacheable communications
protocol -- and in virtually all cases, the HTTP protocol
is used.
REST is an architecture style for designing networked
applications. The idea is that, rather than using complex
mechanisms such as CORBA, RPC or SOAP to connect
between machines, simple HTTP is used to make calls
between machines.
4. Why have RestAPI?
RESTs sweet spot is when you are exposing a public API over the
‹#›
internet to handle CRUD operations on data. REST is focused
on accessing named resources through a single consistent
interface.
REST permits many different data formats where as SOAP only
permits XML. While this may seem like it adds complexity to
REST because you need to handle multiple formats, in my
experience it has actually been quite beneficial. JSON usually is
a better fit for data and parses much faster. REST allows better
support for browser clients due to it’s support for JSON.
REST has better performance and scalability. REST reads can be
cached, SOAP based reads cannot be cached.
5. Use Cases - 1
Northbound and Southbound interfacing.
We are automated
ITSM products connect to ManageIQ to gather data or initiate
‹#›
tasks in ManageIQ.
Example – Service Catalogue reads from ManageIQ the list of
VM Templates in the system for presentation in a list box.
Example – Help Desk software initiates a smart state analysis
upon a software ticket to help better diagnose the issue.
Example – Business Process Management initiates a VM
deployment, by calling into ManageIQ to provision using criteria
it has chosen or allowing ManageIQ to intelligently make the
decisions itself.
6. Use Cases – 2
Northbound and Southbound interfacing.
We automate
ManageIQ calls out to external services for automation and
‹#›
information.
Example – We request information from an external system to
enhance the provisioning placement of virtual machines.
Example – We call automation in other systems to do things for
us, like create a virtual distributed switch via vCO in vSphere.
Example – We create a ticket in an ITSM product adding
enhanced reporting detail to the ticket.
7. How to play
Various RestAPI tools are available, as its HTTP based anything thing can be
‹#›
used, here are a few;
Internet Browser – Usually the first thing to try with RestAPI is to put a RestAPI
call into your browser URL field and see what happens. (It works!)
CURL – Command Line HTTP client, Example;
curl --user admin:smartvm -i -X GET -H "Accept: application/json"
http://localhost:3000/api/services
SOAPUI – A great utility available on Mac OS X, Linux and Windows. Allows you
to graphically interact with Rest and Soap services.
CFME Rest API Client – The developer behind the RestAPI for CFME has kindly
added a command line Rest Client to make things quicker/easier. More
information on this later in this deck.
8. CFME RestAPI Client - 1
The CFME RestAPI Client can be found in;
/var/www/miq/lib/cfme_client
You can either run the /bin/rest_api.rb or link to the file as I have done for these
demos;
ln –s <path to file>rest_api.rb <path to file>api
To test
./api -- version
returns
api 1.0 - CFME REST API Access Script
I had to install some GEMs in addition to the standard ones I had on MAC OS X.
• faraday_middleware
• faraday
‹#›
9. CFME RestAPI Client - 2
api 1.0 - CFME REST API Access Script
Usage: api [options] <action> [parameters] [resource]
‹#›
action - is the action to use for the request, i.e. get, post, patch, edit ...
[parameters] include: expand, attributes, limit, offset, sort_by, sort_order, sqlfilter, by_tag
specify --help for additional help
[resource] - is the optional resource i.e. services
api [options] vi|edit [script]
Edit optional api_* scripts. script names must be specified without the
api_ prefix or .rb suffix. Edits this script if not specified.
api [options] run script [method]
Run optional api_* scripts
api [options] ls
List optional api_* scripts (without the api_ prefix)
api options are:
--verbose, -v: Verbose mode, show details of the communication
--apiversion, -V <s>: Version of the API to access (default: )
--url, -l <s>: Base URL of CFME to access (default: http://localhost:3000)
--user, -u <s>: User to authentication as (default: admin)
--password, -p <s>: Password for user specified to authenticate as (default: smartvm)
--token, -t <s>: Token to use for authentication instead of user/password (default: )
--format, -f <s>: How to format Json, pretty|none (default: pretty)
--inputfile, -i <s>: File to use as input to the POST/PUT/PATCH methods (default: )
--scriptdir, -s <s>: Directory where optional api_* scripts live (default: /Users/johnhardy/bin)
--version, -e: Print version and exit
--help, -h: Show this message
10. CFME RestAPI Client - 3
To use the CFME RestAPI Client you will need to set the URL to connect to;
--url https://192.168.201.202
Then you set the action (get, post, put, delete etc..)
Lastly set the resource you wish to connect to, the first resource you will want to
investigate will be;
/api
Here is the full example;
./api --url https://192.168.201.202 get /api
Will return all the Resource Collections in the namespace of /api, example few are;
"name": "CFME API",
"description": "ManageIQ Management Engine REST API",
"version": "1.0",
"versions": [
{
"name": "1.0",
"href": "https://192.168.201.202/api/v1.0"
}
],
"collections": [
{
"name": "automation_requests",
"href": "https://192.168.201.202/api/automation_requests",
"description": "Automation Requests"
},
‹#›
12. 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"
}
}
‹#›
13. 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"}
]
‹#›
17. Technical Syntax - GET
GET /api/<collection>
GET /api/<collection>/<c_id>
GET /api/<collection>/<c_id>/<subcollection>
GET /api/<collection>/<c_id>/<subcollection>/<s_id>
Query VMs
‹#›
- Return first 100 VMs,
- named test*
- displaying name, vendor and guid
- sort by name in ascending order
GET /api/vms?
offset=0&limit=100&
sqlfilter=“name LIKE ‘test*’”&
expand=resources&attributes=name,vendor,guid&
sort_by=name&sort_order=asc
RUBY puts RestClient.get 'https://admin:smartvm@192.168.201.202/api/vms?limit=5&exp
and=resources'
{"name":"vms","count":10,"subcount":5,"resources":[{"id":"https://192.168.201.202/api/vms/118","vendor":"redhat","name":
"All-In-One_0004","location":"fd1e107c-cf4e-4e46-a421-ba8f67c53829.ovf","created_on":"2014-09-02T02:55:18Z","updat
ed_on":"2014-09-02T03:03:42Z","guid":"923e4db4-324c-11e4-9ef3-000c293afc3e","uid_ems":"fd1e107c-cf4e-4e46-a421
-ba8f67c53829","boot_time":"2014-09-02T02:55:34Z","power_state":"unknown","state_changed_on":"2014-09-02T03:03:
42Z","previous_state":"on","connection_state":"connected","template":false,"evm_o
24. Technical Syntax - POST
CURL
‹#›
curl -k -u admin:smartvm -H "Content-Type: application/json" -X POST https://192.1
68.201.202/api/service_catalogs/1/service_templates -d '{ "action":"order","resource
s":[{"href":"https://192.168.201.202/api/service_catalogs/1/service_templates/5","ssh
User":"OSEroot","rebootServers":"false"}]}'
{"results":[{"approval_state":"pending_approval","created_on":"2014-09-10T15:12:01Z","descripti
on":"Provisioning Service [JOpenShift All-In-One] from [JOpenShift All-In-One]","destination_id":
null,"destination_type":null,"fulfilled_on":null,"id":195,"message":"Service_Template_Provisioning
- Request Created","options":{"dialog":{"dialog_sshUser":"OSEroot","dialog_rebootServers":"fals
e"}},"request_state":"pending","request_type":"clone_to_service","requester_id":1,"requester_na
me":"Administrator","source_id":5,"source_type":"ServiceTemplate","status":"Ok","updated_on":"
2014-09-10T15:12:03Z","userid":"admin"}]}
25. Technical Syntax - PUT
PUT /api/<collection>/<c_id> - Targeting a single resource
{
<attr_1> : <value_1>,
<attr_2> : <value_2>,
…
}
Updating attributes of service 1
PUT /api/services/1
{
"name" : "service_1",
"description" : "This is an updated description for the first service"
}
‹#›
26. Technical Syntax - PUT
RUBY
‹#›
require 'rest-client'
require 'json'
result = RestClient.get 'https://admin:smartvm@192.168.201.202/api/services/223'
entity = JSON.parse(result)
puts "Old Name = #{entity['name']}"
newname = "OpenShift All-In-One"
puts RestClient.put 'https://admin:smartvm@192.168.201.202/api/services/223',{
:name => newname
}.to_json
result = RestClient.get 'https://admin:smartvm@192.168.201.202/api/services/223'
entity = JSON.parse(result)
puts "NEW Name = #{entity['name']}"
Old Name = TEST
{"id":"https://192.168.201.202/api/services/223","name":"OpenShift All-In-One","description":"Re
d Hat OpenShift Enterprise All-In-One Installation","guid":"d399812e-38c9-11e4-b079-000c293af
c3e","service_template_id":5,"options":{"button_order":["cb-3"]},"display":true,"created_at":"2014
-09-10T09:07:01Z","updated_at":"2014-09-10T09:26:29Z","evm_owner_id":1,"miq_group_id":1}
NEW Name = OpenShift All-In-One
34. Gotchas
• Create Group
• Create Role
• Add Provider
• Create Policy (assign Tag)
• Assign policy (to provider)
• VM Operations = (start/stop/delete)
• Add collection for Cloud and Service Workloads (Instance/Image/AZ,
‹#›
Security Group, VPC, Subnet)
• Collection relationships (ex: IP/.…)
• Invoke a task associated with a deployed Service
• Query tags for friendly names.
35. Gotchas
Currently the RestAPI does not offer functions for;
Categories/Tags – Create or Delete is absent.
Workaround – Simply write the Create/Delete Category or Tag
‹#›
functions as a method using the standard built-in commands
such as “$evm.execute(‘category_create’,……) Call this method
from a RestAPI resource “Automation Request”
36. Further Information
The RestAPI documentation is available here;
https://github.com/ManageIQ/guides/tree/master/rest_api
The current version of the RestAPI is version 1.0
The design specification can be found here;
https://github.com/ManageIQ/guides/blob/master/rest_api/
‹#›
design.md
37. ‹#›
Q & A
Blog – cloudformsnow.com
YouTube -
https://www.youtube.com/user/cloudfor
msnow