use of com.woorea.openstack.base.client.OpenStackBaseException in project AJSC by att.
the class OpenStackComputeService method createServer.
/**
* Create a server using the supplied server object as the pattern
*
* @param model
* The server to create. The model object must contain the following attributes in order to be legal for
* creating a new server:
* <ul>
* <li>The name of the server</li>
* <li>The id of the template to be used</li>
* <li>The id of the image to be used</li>
* </ul>
* In addition, the model object may also contain a list of ACL's that are to be assigned.
* @return A reference to the connected server. The template server (the argument passed) remains disconnected. The
* user is encouraged to use the referenced returned from this method for any further operations on the
* server.
* @throws ZoneException
* If the server cannot be created
* @see com.att.cdp.zones.ComputeService#createServer(com.att.cdp.zones.model.Server)
*/
@SuppressWarnings("nls")
@Override
public Server createServer(Server model) throws ZoneException {
checkArg(model, "model");
connect();
Context context = getContext();
trackRequest();
RequestState.put(RequestState.SERVER, model.getName());
RequestState.put(RequestState.IMAGE, model.getImage());
RequestState.put(RequestState.TEMPLATE, model.getTemplate());
RequestState.put(RequestState.KEYPAIR, model.getKeyName());
RequestState.put(RequestState.SERVICE, "Compute");
RequestState.put(RequestState.SERVICE_URL, nova.getEndpoint());
try {
HashMap<String, String> dictionary = new HashMap<>();
dictionary.put("name", "name");
dictionary.put("image", "imageRef");
dictionary.put("template", "flavorRef");
dictionary.put("availabilityZone", "availabilityZone");
ServerForCreate create = new ServerForCreate();
ObjectMapper.map(model, create, dictionary);
/*
* If an ACL list is present, then use the specified ACL models to request the appropriate security groups
*/
if (model.getAccessControl() != null) {
for (ACL entry : model.getAccessControl()) {
create.getSecurityGroups().add(new ServerForCreate.SecurityGroup(entry.getName()));
}
} else {
create.getSecurityGroups().add(new ServerForCreate.SecurityGroup("default"));
}
/*
* If the key pair name is specified in the server object, set that on the request
*/
if (model.getKeyName() != null) {
create.setKeyName(model.getKeyName());
}
/*
* If there is an init script, set it up in the request
*/
if (model.getInitScript() != null) {
String initScript = model.getInitScript();
String encoded = Base64.encodeBase64String(initScript.getBytes());
create.setUserData(encoded);
}
/*
* Process ports if they are specified. Fall-back to the deprecated network associations if ports are not
* specified.
*/
com.woorea.openstack.nova.model.Server osServer = null;
List<NetworkForCreate> networks = new ArrayList<>();
NetworkService networkService = context.getNetworkService();
if (model.getPorts() != null && !model.getPorts().isEmpty()) {
for (Port port : model.getPorts()) {
/*
* See if the port is connected (exists) or is disconnected (it is a model). If disconnected, then
* we have to create the port first.
*/
if (!port.isConnected()) {
Subnet subnet = networkService.getSubnetById(port.getSubnetId());
port = networkService.createPort(subnet);
}
NetworkForCreate newNet = new NetworkForCreate();
newNet.setPort(port.getId());
networks.add(newNet);
}
create.setNetworks(networks);
} else if (!model.getNetworks().isEmpty()) {
for (Network networkModel : model.getNetworks()) {
List<Network> networkList = networkService.getNetworksByName(networkModel.getName());
if (networkList.isEmpty()) {
throw new ZoneException(String.format("Network %s cannot be found", networkModel.getName()));
}
Network network = networkList.get(0);
NetworkForCreate newNet = new NetworkForCreate();
newNet.setId(network.getId());
networks.add(newNet);
}
create.setNetworks(networks);
}
ServersResource resource = nova.getClient().servers();
Boot boot = resource.boot(create);
osServer = boot.execute();
com.woorea.openstack.nova.model.Server.Fault osFault = osServer.getFault();
ConnectedServer server = (ConnectedServer) getServer(osServer.getId());
if (osFault != null) {
server.setFault(new OpenStackFault(context, osFault));
}
return server;
} catch (OpenStackBaseException ex) {
ExceptionMapper.mapException(ex);
}
// for the compiler
return null;
}
use of com.woorea.openstack.base.client.OpenStackBaseException in project AJSC by att.
the class OpenStackImageService method getImageByName.
/**
* This method is used to get the image where the name of the image exactly matches the supplied name.
*
* @param name
* The name that is compared to the image name to select it for the returned image.
* @return The image that match the specified name
* @throws ZoneException
* If anything fails
* @see com.att.cdp.zones.ImageService#getImageByName(java.lang.String)
*/
@SuppressWarnings("nls")
@Override
public Image getImageByName(String name) throws ZoneException {
checkArg(name, "name");
connect();
Context context = getContext();
trackRequest();
RequestState.put(RequestState.IMAGE, name);
RequestState.put(RequestState.SERVICE, "Image");
RequestState.put(RequestState.SERVICE_URL, glance.getEndpoint());
Image image = null;
try {
com.woorea.openstack.glance.ImagesResource.List openStackRequest = glance.getClient().images().list(false);
openStackRequest.queryParam("name", name);
com.woorea.openstack.glance.model.Images images = openStackRequest.execute();
if (images.getList() != null && images.getList().size() == 1) {
image = new OpenStackImage(context, images.getList().get(0));
}
} catch (OpenStackBaseException ex) {
ExceptionMapper.mapException(ex);
}
return image;
}
use of com.woorea.openstack.base.client.OpenStackBaseException in project AJSC by att.
the class OpenStackImageService method listImages.
/**
* This method is used to list all of the images where the name of the image matches the supplied pattern.
*
* @param pattern
* The pattern (regular expression) that is compared to the image name to select it for the returned
* list.
* @return The list of images that match the specified name pattern
* @throws ZoneException
* If anything fails
* @see com.att.cdp.zones.ImageService#listImages(java.lang.String)
*/
@SuppressWarnings("nls")
@Override
public List<Image> listImages(String pattern) throws ZoneException {
connect();
Context context = getContext();
trackRequest();
RequestState.put(RequestState.IMAGE, pattern);
RequestState.put(RequestState.SERVICE, "Image");
RequestState.put(RequestState.SERVICE_URL, glance.getEndpoint());
ArrayList<Image> list = new ArrayList<>();
try {
/*
* Since a subset of the collection of images is returned, next link is provided to get the next set of
* images.
*/
String next = null;
do {
com.woorea.openstack.glance.ImagesResource.List openStackRequest = glance.getClient().images().list(false);
if (next != null) {
int index = next.indexOf("?");
openStackRequest.queryString(next.substring(++index));
}
com.woorea.openstack.glance.model.Images images = openStackRequest.execute();
for (com.woorea.openstack.glance.model.Image osImage : images) {
if (pattern != null) {
if (osImage.getName().matches(pattern)) {
list.add(new OpenStackImage(context, osImage));
}
} else {
list.add(new OpenStackImage(context, osImage));
}
}
next = images.getNext();
} while (next != null);
} catch (OpenStackBaseException ex) {
ExceptionMapper.mapException(ex);
}
return list;
}
use of com.woorea.openstack.base.client.OpenStackBaseException in project AJSC by att.
the class OpenStackImageService method getImage.
/**
* @see com.att.cdp.zones.ImageService#getImage(java.lang.String)
*/
@SuppressWarnings("nls")
@Override
public Image getImage(String id) throws ZoneException {
checkArg(id, "id");
connect();
Context context = getContext();
trackRequest();
RequestState.put(RequestState.IMAGE, id);
RequestState.put(RequestState.SERVICE, "Image");
RequestState.put(RequestState.SERVICE_URL, glance.getEndpoint());
try {
com.woorea.openstack.glance.model.Image image = glance.getClient().images().show(id).execute();
return new OpenStackImage(context, image);
} catch (OpenStackBaseException ex) {
ExceptionMapper.mapException(ex);
}
// just for the compiler
return null;
}
use of com.woorea.openstack.base.client.OpenStackBaseException in project AJSC by att.
the class OpenStackImageService method listImages.
/**
* This method is used to list all of the images that are available within the context.
*
* @return The list of images or an empty list if there are none available.
* @throws ZoneException
* If the service fails.
* @see com.att.cdp.zones.ImageService#listImages()
*/
@SuppressWarnings("nls")
@Override
public List<Image> listImages() throws ZoneException {
connect();
Context context = getContext();
trackRequest();
RequestState.put(RequestState.SERVICE, "Image");
RequestState.put(RequestState.SERVICE_URL, glance.getEndpoint());
ArrayList<Image> list = new ArrayList<>();
try {
/*
* Since a subset of the collection of images is returned, next link is provided to get the next set of
* images.
*/
String next = null;
do {
com.woorea.openstack.glance.ImagesResource.List openStackRequest = glance.getClient().images().list(false);
if (next != null) {
int index = next.indexOf("?");
openStackRequest.queryString(next.substring(++index));
}
com.woorea.openstack.glance.model.Images images = openStackRequest.execute();
for (com.woorea.openstack.glance.model.Image osImage : images) {
list.add(new OpenStackImage(context, osImage));
}
next = images.getNext();
} while (next != null);
} catch (OpenStackBaseException ex) {
ExceptionMapper.mapException(ex);
}
return list;
}
Aggregations