use of com.att.cdp.zones.model.Subnet 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.att.cdp.zones.model.Subnet 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 acl : model.getAccessControl()) {
ServerForCreate.SecurityGroup group = new ServerForCreate.SecurityGroup(acl.getName());
create.getSecurityGroups().add(group);
}
}
/*
* 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 (model.getAccessControl() != null) {
for (ACL entry : model.getAccessControl()) {
create.getSecurityGroups().add(new ServerForCreate.SecurityGroup(entry.getName()));
}
} else {
create.getSecurityGroups().add(new ServerForCreate.SecurityGroup("default"));
}
/*
* 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.att.cdp.zones.model.Subnet in project AJSC by att.
the class OpenStackNetworkService method createSubnet.
@Override
public Subnet createSubnet(Subnet subnet) throws ZoneException {
connect();
Context context = getContext();
trackRequest();
RequestState.put(RequestState.SUBNET, subnet.getName());
RequestState.put(RequestState.SERVICE, "Network");
RequestState.put(RequestState.SERVICE_URL, quantumConnector.getEndpoint());
try {
Quantum client = quantumConnector.getClient();
com.woorea.openstack.quantum.model.Subnet subnetToCreate = new com.woorea.openstack.quantum.model.Subnet();
subnetToCreate.setName(subnet.getName());
subnetToCreate.setNetworkId(subnet.getNetwork());
subnetToCreate.setCidr(subnet.getRouting());
subnetToCreate.setGw(subnet.getGatewayIp());
subnetToCreate.setEnableDHCP(subnet.isDhcp());
subnetToCreate.setDnsNames(subnet.getDns());
if (subnet.isIpv4()) {
subnetToCreate.setIpversion(IpVersion.IPV4);
} else {
subnetToCreate.setIpversion(IpVersion.IPV6);
}
if (!subnet.getHostRoutes().isEmpty()) {
List<com.woorea.openstack.quantum.model.Route> routesToCreate = new ArrayList<>();
List<Route> routes = subnet.getHostRoutes();
for (Route route : routes) {
com.woorea.openstack.quantum.model.Route newRoute = new com.woorea.openstack.quantum.model.Route();
newRoute.setDestination(route.getDestination());
newRoute.setNexthop(route.getNexthop());
routesToCreate.add(newRoute);
}
subnetToCreate.setHostRoutes(routesToCreate);
}
com.woorea.openstack.quantum.model.Subnet openstackSubnet = client.subnets().create(subnetToCreate).execute();
return new OpenStackSubnet(context, openstackSubnet);
} catch (OpenStackBaseException e) {
ExceptionMapper.mapException(e);
}
return null;
}
use of com.att.cdp.zones.model.Subnet in project AJSC by att.
the class TestComputeService method testAttachAndDetachPorts.
/**
* Test the attach and detach of a port
*
* @throws ZoneException
* if the test fails
*/
@SuppressWarnings("nls")
@Test
@Ignore
public void testAttachAndDetachPorts() throws ZoneException {
Context context = connect();
ComputeService compute = context.getComputeService();
NetworkService network = context.getNetworkService();
/*
* Get a list of servers. Make sure that at least one server exists.
* We'll just use the first one we find.
*/
List<Server> servers = compute.getServers();
assertNotNull(servers);
assertFalse(servers.isEmpty());
Server server = servers.get(0);
//
/*
* Now, get the ports attached to that server. Make sure there is at
* least one port. We'll use the first one we find.
*/
List<Port> ports = server.getPorts();
assertNotNull(ports);
assertFalse(ports.isEmpty());
/*
* Use the first port to model the creation of a new port. We'll use the
* same subnet.
*/
Port modelPort = ports.get(0);
Subnet subnet = network.getSubnetById(modelPort.getSubnetId());
Port newPort = network.createPort(subnet);
assertNotNull(newPort);
assertNotNull(newPort.getMacAddr());
assertNotNull(newPort.getId());
/*
* Save the new port's ID. We'll use that to test that it was actually
* attached and detached
*/
String id = newPort.getId();
/*
* Attach the port to the server. Then list all the ports and make sure
* the new port is included
*/
server.attachPort(newPort);
ports = server.getPorts();
boolean found = false;
for (Port p : ports) {
if (p.getId().equals(id)) {
found = true;
break;
}
}
if (!found) {
fail("Port is not listed as attached to the server");
}
/*
* Now detach the port and make sure that it is NOT listed
*/
server.detachPort(newPort);
try {
Thread.sleep(1000L);
} catch (InterruptedException e) {
// ignore
}
ports = server.getPorts();
found = false;
for (Port p : ports) {
if (p.getId().equals(id)) {
found = true;
break;
}
}
if (found) {
fail("Port is still listed as attached to the server");
}
/*
* Finally, delete the new port
*/
network.deletePort(newPort);
}
use of com.att.cdp.zones.model.Subnet in project AJSC by att.
the class TestNetworkService method testCreateAndDeletePort.
/**
* Tests that we can create and delete a port
*
* @throws ZoneException
* If the test cannot connect to the provider
*/
@Test
@Ignore
public void testCreateAndDeletePort() throws ZoneException {
Context context = connect();
NetworkService service = context.getNetworkService();
List<Subnet> subnets = service.getSubnets();
assertNotNull(subnets);
assertFalse(subnets.isEmpty());
Subnet subnet = subnets.get(0);
Port port = service.createPort(subnet);
assertNotNull(port);
assertEquals(port.getSubnetId(), subnet.getId());
assertNotNull(port.getMacAddr());
assertNotNull(port.getId());
assertNotNull(port.getNetwork());
service.deletePort(port);
}
Aggregations