package com.mirantis.plugins.murano.tomcat.client; import org.openstack4j.api.OSClient; import org.openstack4j.model.compute.Flavor; import org.openstack4j.model.compute.Image; import org.openstack4j.model.compute.Keypair; import org.openstack4j.openstack.OSFactory; import java.util.*; import java.util.logging.Level; import java.util.logging.Logger; /** * Client Class to talk to Openstack/Murano APIs */ public class OpenstackClient { private final static Logger LOG = Logger.getLogger(OpenstackClient.class.getName()); private String serverUrl; private String username; private String password; private String tenantName; private Map imagesMap = new HashMap(); private OSClient.OSClientV2 os = null; public OpenstackClient(String serverUrl, String username, String password, String tenantName) { this.serverUrl = serverUrl; this.username = username; this.password = password; this.tenantName = tenantName; } /** * Authenticate to the Openstack instance given the credentials in constructor * @return whether the auth was successful */ public boolean authenticate() { boolean success = false; try { this.os = OSFactory.builderV2() .endpoint(this.serverUrl + ":5000/v2.0") .credentials(this.username, this.password) .tenantName(this.tenantName) .authenticate(); success = true; } catch(Exception ex) { LOG.log(Level.SEVERE, "Error connecting to Client", ex); success = false; } return success; } /** * Get all the Flavors the user has access to * @return A List of Flavor objects */ public ArrayList getFlavors() { ArrayList flavors = new ArrayList(); if (this.os != null) { List flavorsList = this.os.compute().flavors().list(); for (Flavor f : flavorsList) { flavors.add(f.getName()); } } return flavors; } /** * Get all the Keypairs the user has access to * @return A List of Keypair object */ public ArrayList getKeypairs() { ArrayList keypairs = new ArrayList(); if (this.os != null) { List kpList = this.os.compute().keypairs().list(); for (Keypair k : kpList) { keypairs.add(k.getName()); } } return keypairs; } public Set getImages() { imagesMap.clear(); ArrayList images = new ArrayList(); if (this.os != null) { List iList = this.os.compute().images().list(); for (Image i : iList) { imagesMap.put(i.getName(), i.getId()); } } return imagesMap.keySet(); } public String getImageId(String name) { return imagesMap.get(name); } /** * Helper object to return the OSClient * @return OSClient V2 */ public OSClient.OSClientV2 getOSClient() { return this.os; } }