Skip to content

Instantly share code, notes, and snippets.

@kitplummer
Created June 12, 2013 22:01
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save kitplummer/5769519 to your computer and use it in GitHub Desktop.
Save kitplummer/5769519 to your computer and use it in GitHub Desktop.
Simple Groovy script working with OpenStack's Keystone and Compute(Nova) APIs.
@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
}
}
@adimits2203
Copy link

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:

Caught: java.lang.NoSuchFieldError: defaultRequestContentType
java.lang.NoSuchFieldError: defaultRequestContentType
	at groovyx.net.http.HTTPBuilder$RequestConfigDelegate.<init>(HTTPBuilder.java:934)
	at groovyx.net.http.HTTPBuilder$RequestConfigDelegate.<init>(HTTPBuilder.java:943)
	at groovyx.net.http.RESTClient.post(RESTClient.java:140)
	at groovyx.net.http.RESTClient$post.call(Unknown Source)
	at OpenStack.run(OpenStack.groovy:13)
	at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)

Process finished with exit code 1

Could you please suggest

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment