2016년 1월 9일 토요일

manageIQ REST API examples with cURL and Python 3

Recently at work I've been working with Redhat Cloudforms 4 (based off of the upstream opensource project manageIQ). A client has requested some customizations of Cloudforms which requires calling some info through the CFME/manageiq REST API.

Redhat Korea gave my company an API reference guide for Cloudforms v3 but it is quite incomplete. It is much better to just refer to the upstream manageiq docs on github:

https://github.com/ManageIQ/manageiq_docs/tree/master/api

Here are some examples of communicating with the v2.2 REST API.

Creating an openstack cloud provider using cURL:

curl --user admin:smartvm \
-i -k -X POST -H "Accept: application/json" \
-d '{ "action" : "create", "resource" : { "type" : "ManageIQ::Providers::Openstack::CloudManager", "name" : "openstack-cfme", "hostname" : "192.168.40.198", "ipaddress" : "192.168.40.198", "port" : "5000", "credentials" : [ { "userid" : "admin", "password" : "admin" }, { "userid" : "guest", "password" : "guest", "auth_type" : "amqp" } ] } }' \
https://192.168.40.250/api/providers

Creating an openstack cloud provider using python 3:

# cfme_add_provider.py
# Python 3 script that adds an Openstack cloud provider through
# the CFME API (v2.1.0+) for CFME v4
import requests, pprint, json
payload = json.dumps({'action' : 'create',
'resource' :
{"name" : "openstack-cfme",
"type" : "ManageIQ::Providers::Openstack::CloudManager",
"hostname" : "192.168.40.198",
"ipaddress" : "192.168.40.198",
"port" : "5000",
"credentials" :
[ { "userid" : "admin",
"password" : "admin" },
{ "userid" : "guest",
"password" : "guest",
"auth_type" : "amqp" }]
}
}
)
resp = requests.post('https://192.168.40.250/api/providers',
auth=('admin', 'smartvm'),
data = payload,
verify = False)
pprint.pprint(resp.json())

Querying a list of cloud providers using cURL:

curl --user admin:smartvm \
-i -k -X GET -H "Accept: application/json" \
https://192.168.40.250/api/providers

Querying a list of cloud providers (and get detailed info on each provider) using python 3:

# cfme_check_provider.py
# Python3 script that queries Cloud providers through
# the CFME API (v2.1.0+) for CFME v4 and returns
# details for each provider
import requests
import pprint
import doctest
import re
def parse_url(some_string):
'''
String -> String
Given some_string, strip out all braces, whitespace, and the string
"'href': ". Returns clean url as string
>>> parse_url("{'href': 'https://192.168.40.250/api/providers/7'}")
'https://192.168.40.250/api/providers/7'
'''
# the input is in the form {'a':'b'} which is interpreted as a
# dict instead of a simple string; therefore it must first be
# type cast to type 'str'
clean_string = str(some_string)
regexp = re.compile(r"'href': |'|{|}")
clean_string = re.sub(regexp, '', clean_string)
return clean_string
def main ():
doctest.testmod()
resp = requests.get('https://192.168.40.250/api/providers',
auth=('admin', 'smartvm'), verify=False)
pprint.pprint(resp.json())
num_providers = int(resp.json().get('count'))
# Return detailed info on all providers
list_providers = []
for i in range(num_providers):
list_providers.append(resp.json().get('resources')[i])
for j in list_providers:
clean_url = parse_url(j)
resp1 = requests.get(clean_url,
auth=('admin', 'smartvm'), verify=False)
pprint.pprint(resp1.json())
# MAIN PROGRAM
main()
# TODO
# Extend this script to check and return detailed info for not only
# providers but also categories, tags, zones, availability zones,
# VM's and other collections listed in
# https://github.com/ManageIQ/manageiq_docs/blob/master/api/reference/collections.adoc

Note that in when using both cURL and python requests, SSL verification must be turned OFF (using -k flag in cURL, and verify=False in requests) because the Cloudforms instance uses a private certificate that is unknown to Certificate Authorities.

댓글 없음:

댓글 쓰기