Created
June 12, 2013 22:01
-
-
Save kitplummer/5769519 to your computer and use it in GitHub Desktop.
Simple Groovy script working with OpenStack's Keystone and Compute(Nova) APIs.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Grab(group="org.codehaus.groovy.modules.http-builder", module="http-builder", version='0.5.2') | |
import groovyx.net.http.RESTClient | |
import groovy.util.slurpersupport.GPathResult | |
import groovy.json.* | |
keystone = new RESTClient( 'http://10.0.1.49:5000/v2.0/' ) | |
resp = keystone.post( path : 'tokens', | |
body : [auth:[passwordCredentials:[username: "admin", password:"stack"], tenantId: "2ba2d60c5e8d4d1b86549d988131fe48"]], | |
contentType : 'application/json' ) | |
def token = resp.data.access.token.id | |
for (endpoint in resp.data.access.serviceCatalog) { | |
if (endpoint.type == 'compute' ) { | |
compute = new RESTClient(endpoint.endpoints.adminURL[0] + "/") | |
resp = compute.get( path : 'images', | |
headers : ['X-Auth-Token' : token] ) | |
for (image in resp.data.images) { | |
println JsonOutput.toJson(image) | |
} | |
resp = compute.get( path : 'servers', | |
headers : ['X-Auth-Token' : token] ) | |
for (server in resp.data.servers) { | |
println JsonOutput.prettyPrint(JsonOutput.toJson(server)) | |
details = compute.get ( path : 'servers/' + server.id, | |
headers : [ 'X-Auth-Token' : token] ) | |
println details.data | |
} | |
//resp = compute.post( path : 'servers', | |
// contentType : 'application/json', | |
// body : '''{"server":{"flavorRef":"1", | |
// "imageRef":"3012812c-c178-408b-9b54-b292525834c3", | |
// "key_name":"opendx_demo", | |
// "name":"groovy2"}}''', | |
// headers : ['X-Auth-Token' : token]) | |
//println resp.data | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hello,
Thanks for sharing this example for people like us who are new to Groovy.
When i try to run the above code, get below error: