Created
November 28, 2019 21:18
-
-
Save olivergondza/2fd7656a57ef485af133e6de3bef6d87 to your computer and use it in GitHub Desktop.
openstack4j/openstack4j groovy tester
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
@Grapes([ | |
@Grab('com.github.openstack4j.core:openstack4j-core:3.4-SNAPSHOT'), | |
@Grab('com.github.openstack4j.core.connectors:openstack4j-httpclient:3.4-SNAPSHOT'), | |
@Grab('org.slf4j:slf4j-jdk14:1.7.7') | |
]) | |
import org.openstack4j.openstack.OSFactory | |
import org.openstack4j.model.compute.* | |
import org.openstack4j.model.common.* | |
import org.openstack4j.core.transport.Config | |
import org.openstack4j.model.image.v2.* | |
import org.openstack4j.api.image.v2.* | |
def getClient() { | |
def env = System.getenv() | |
def pn = env['OS_PROJECT_NAME'] | |
def tn = env['OS_TENANT_NAME'] | |
if ((pn == null) == (tn == null)) | |
throw new Error("Either OS_PROJECT_NAME or OS_TENANT_NAME should be provided") | |
Config config = Config.newConfig().withSSLVerificationDisabled() | |
// Keystone v3 | |
if (pn) { | |
def pdid = env['OS_PROJECT_DOMAIN_ID'] | |
def id = pdid == null ? Identifier.byName("Default") : Identifier.byId(pdid) | |
return OSFactory.builderV3() | |
.withConfig(config) | |
.endpoint(env['OS_AUTH_URL']) | |
.credentials(env['OS_USERNAME'], env['OS_PASSWORD'], Identifier.byName(env['OS_USER_DOMAIN_NAME'])) | |
.scopeToProject(Identifier.byName(env['OS_PROJECT_NAME']), id) | |
.authenticate() | |
.useRegion(null) | |
} | |
// Keystone v2 | |
if (tn) { | |
return OSFactory.builderV2() | |
.withConfig(config) | |
.endpoint(env['OS_AUTH_URL']) | |
.credentials(env['OS_USERNAME'], env['OS_PASSWORD']) | |
.tenantName(env['OS_TENANT_NAME']) | |
.authenticate() | |
.useRegion(null) | |
} | |
System.err.println "Unable to locate auth evn vars. Forgot to `source openrc.sh`?" | |
System.exit 1 | |
} | |
def c = getClient() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment