Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@kellyrob99
Created January 22, 2015 17:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kellyrob99/6b83a472d397baf1dcc4 to your computer and use it in GitHub Desktop.
Save kellyrob99/6b83a472d397baf1dcc4 to your computer and use it in GitHub Desktop.
/*
* Copyright (c) 2011-2014 Sonatype, Inc. All rights reserved.
* Includes the third-party code listed at http://links.sonatype.com/products/clm/attributions.
* "Sonatype" is a trademark of Sonatype, Inc.
*/
/**
* @since 1.13.0
*/
@Grab('org.codehaus.groovy.modules.http-builder:http-builder:0.7.1')
import groovy.json.JsonOutput
import groovyx.net.http.HttpResponseDecorator
import groovyx.net.http.RESTClient
import org.apache.http.HttpRequest
import org.apache.http.HttpRequestInterceptor
import org.apache.http.protocol.HttpContext
import static groovyx.net.http.ContentType.JSON
def username = 'admin'
def password = 'admin123'
def String rootUrl = args ? args[0] : 'http://localhost:8070'
private RESTClient configureBuilder(RESTClient builder, username, password) {
builder.client.addRequestInterceptor(new HttpRequestInterceptor() {
void process(HttpRequest httpRequest, HttpContext httpContext) {
httpRequest.addHeader('Authorization', 'Basic ' + "${username}:${password}".bytes.encodeBase64().toString())
}
})
//add a default error handler
builder.handler.failure = {HttpResponseDecorator resp ->
println "Unexpected failure: ${resp.statusLine} \n ${resp.data}"
}
return builder
}
final RESTClient builder = configureBuilder(new RESTClient(rootUrl), username, password)
def org = builder.
post(path: '/rest/organization', requestContentType: JSON, body: JsonOutput.toJson([id: null, name: 'org'])).responseData
println org
println builder.put(path: "/rest/policy/organization/${org.id}/import",
body: 'http://books.sonatype.com/sonatype-clm-book/resources/Sonatype-Enforce-Policy-1.11.json'.toURL().text, requestContentType: JSON).responseData
def app = [id : null,
name : 'app',
organizationId : org.id,
organizationName: org.name,
publicId : 'app']
app = builder.post(path: '/rest/application', requestContentType: JSON, body: JsonOutput.toJson(app)).responseData
println app
@kellyrob99
Copy link
Author

Creates an 'org' and imports policy for it, then creates an 'app' under that org

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