Search in sources :

Example 6 with ZoneException

use of com.att.cdp.exceptions.ZoneException in project AJSC by att.

the class OpenStackComputeService method detachVolume.

/**
 * Detaches the volume from the specified server.
 *
 * @param server
 *            The server to be manipulated
 * @param deviceName
 *            The device to be detached
 * @throws ZoneException
 *             If the volume cannot be detached for some reason
 * @see com.att.cdp.zones.ComputeService#detachVolume(com.att.cdp.zones.model.Server,
 *      java.lang.String)
 */
@SuppressWarnings("nls")
@Override
public void detachVolume(Server server, String deviceName) throws ZoneException {
    checkArg(server, "server");
    checkArg(deviceName, "deviceName");
    connect();
    Context context = getContext();
    trackRequest();
    RequestState.put(RequestState.SERVER, server.getName());
    RequestState.put(RequestState.DEVICE, deviceName);
    RequestState.put(RequestState.SERVICE, "Compute");
    RequestState.put(RequestState.SERVICE_URL, nova.getEndpoint());
    server.refreshStatus();
    Server.Status status = server.getStatus();
    RequestState.put(RequestState.STATUS, status.toString());
    if (status.equals(Server.Status.RUNNING) || status.equals(Server.Status.READY)) {
        try {
            Map<String, String> attachments = getAttachments(server);
            for (Map.Entry<String, String> entry : attachments.entrySet()) {
                if (entry.getKey().equals(deviceName)) {
                    nova.getClient().servers().detachVolume(server.getId(), entry.getValue()).execute();
                }
                break;
            }
        } catch (OpenStackBaseException ex) {
            ExceptionMapper.mapException(ex);
        }
    } else {
        throw new ZoneException(EELFResourceManager.format(OSMsg.PAL_OS_INVALID_SERVER_STATE, server.getName(), server.getId().toString(), status.toString(), StringHelper.asList(Arrays.asList(Server.Status.READY.toString(), Server.Status.RUNNING.toString()))));
    }
}
Also used : Context(com.att.cdp.zones.Context) OpenStackContext(com.att.cdp.openstack.OpenStackContext) Server(com.att.cdp.zones.model.Server) ConnectedServer(com.att.cdp.zones.spi.model.ConnectedServer) OpenStackServer(com.att.cdp.openstack.model.OpenStackServer) ZoneException(com.att.cdp.exceptions.ZoneException) OpenStackBaseException(com.woorea.openstack.base.client.OpenStackBaseException) Map(java.util.Map) HashMap(java.util.HashMap)

Example 7 with ZoneException

use of com.att.cdp.exceptions.ZoneException 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;
}
Also used : Server(com.att.cdp.zones.model.Server) ConnectedServer(com.att.cdp.zones.spi.model.ConnectedServer) OpenStackServer(com.att.cdp.openstack.model.OpenStackServer) HashMap(java.util.HashMap) ConnectedServer(com.att.cdp.zones.spi.model.ConnectedServer) OpenStackBaseException(com.woorea.openstack.base.client.OpenStackBaseException) Port(com.att.cdp.zones.model.Port) OpenStackPort(com.att.cdp.openstack.model.OpenStackPort) ArrayList(java.util.ArrayList) ServersResource(com.woorea.openstack.nova.api.ServersResource) Boot(com.woorea.openstack.nova.api.ServersResource.Boot) NetworkForCreate(com.woorea.openstack.nova.model.NetworkForCreate) Network(com.att.cdp.zones.model.Network) List(java.util.List) ArrayList(java.util.ArrayList) Context(com.att.cdp.zones.Context) OpenStackContext(com.att.cdp.openstack.OpenStackContext) OpenStackACL(com.att.cdp.openstack.model.OpenStackACL) ACL(com.att.cdp.zones.model.ACL) SecurityGroup(com.woorea.openstack.nova.model.SecurityGroup) OpenStackFault(com.att.cdp.openstack.model.OpenStackFault) ZoneException(com.att.cdp.exceptions.ZoneException) ServerForCreate(com.woorea.openstack.nova.model.ServerForCreate) NetworkService(com.att.cdp.zones.NetworkService) Subnet(com.att.cdp.zones.model.Subnet)

Example 8 with ZoneException

use of com.att.cdp.exceptions.ZoneException in project AJSC by att.

the class TestNetworkService method createSubnetWithDHCPDisabled.

/**
 * Verifies that we can create a subnet with DHCP disabled on a provider
 *
 * @throws ZoneException
 *             If the connection fails, user is not authorized, or the provider cannot perform the operation.
 */
@Test
@Ignore
public void createSubnetWithDHCPDisabled() throws ZoneException {
    Context context = connect();
    NetworkService service = context.getNetworkService();
    try {
        // verify test network exists
        verifyTestNetworkHelper();
        List<Network> networks = service.getNetworksByName("CDP_Test_Network");
        assertNotNull(networks);
        assertFalse(networks.isEmpty());
        Network testNetwork = networks.get(0);
        Subnet subnetWithGateway = new Subnet();
        subnetWithGateway.setName("CDP_Test_IPV4_Subnet_With_DHCP_Disabled");
        subnetWithGateway.setNetwork(testNetwork.getId());
        subnetWithGateway.setIpv4(true);
        subnetWithGateway.setRouting("10.10.30.0/24");
        subnetWithGateway.setGatewayIp("10.10.30.1");
        subnetWithGateway.setDhcp(false);
        service.createSubnet(subnetWithGateway);
        List<Subnet> subnets = service.getSubnetsByName("CDP_Test_IPV4_Subnet_With_DHCP_Disabled");
        assertFalse(subnets.isEmpty());
        for (Subnet subnet : subnets) {
            assertTrue(subnet.getRouting().equalsIgnoreCase("10.10.30.0/24"));
            assertTrue(subnet.getGatewayIp().equalsIgnoreCase("10.10.30.1"));
            assertFalse(subnet.isDhcp());
        }
    } catch (ZoneException ze) {
        ze.printStackTrace();
        fail("Failed to create the ipv4 test subnet with DHCP disabled");
    }
}
Also used : Context(com.att.cdp.zones.Context) ZoneException(com.att.cdp.exceptions.ZoneException) Network(com.att.cdp.zones.model.Network) NetworkService(com.att.cdp.zones.NetworkService) Subnet(com.att.cdp.zones.model.Subnet) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 9 with ZoneException

use of com.att.cdp.exceptions.ZoneException in project AJSC by att.

the class TestNetworkService method verifyTestNetworkHelper.

/**
 * Helper function to create the base test network if it does not already exist. The base test network is needed for
 * all subnet tests This relies on createNetwork functioning properly
 *
 * @throws ZoneException
 *             If the connection fails, user is not authorized, or the provider cannot perform the operation.
 */
protected void verifyTestNetworkHelper() throws ZoneException {
    Context context = connect();
    NetworkService service = context.getNetworkService();
    try {
        List<Network> networks = service.getNetworksByName("CDP_Test_Network");
        if (networks.isEmpty()) {
            Network testNetwork = new Network("CDP_Test_Network");
            service.createNetwork(testNetwork);
            networks = service.getNetworksByName("CDP_Test_Network");
        }
        assertNotNull(networks);
        assertFalse(networks.isEmpty());
    } catch (ZoneException ze) {
        ze.printStackTrace();
        fail("Failed to create the test network");
    }
}
Also used : Context(com.att.cdp.zones.Context) ZoneException(com.att.cdp.exceptions.ZoneException) Network(com.att.cdp.zones.model.Network) NetworkService(com.att.cdp.zones.NetworkService)

Example 10 with ZoneException

use of com.att.cdp.exceptions.ZoneException in project AJSC by att.

the class TestNetworkService method createSubnetWithHostRoutes.

/**
 * Verifies that we can create a subnet with a specified set of host routes on a provider
 *
 * @throws ZoneException
 *             If the connection fails, user is not authorized, or the provider cannot perform the operation.
 */
@Test
@Ignore
public void createSubnetWithHostRoutes() throws ZoneException {
    Context context = connect();
    NetworkService service = context.getNetworkService();
    try {
        // verify test network exists
        verifyTestNetworkHelper();
        List<Network> networks = service.getNetworksByName("CDP_Test_Network");
        assertNotNull(networks);
        assertFalse(networks.isEmpty());
        Network testNetwork = networks.get(0);
        Subnet subnetWithGateway = new Subnet();
        subnetWithGateway.setName("CDP_Test_IPV4_Subnet_With_Host_Routes");
        subnetWithGateway.setNetwork(testNetwork.getId());
        subnetWithGateway.setIpv4(true);
        subnetWithGateway.setRouting("10.10.40.0/24");
        subnetWithGateway.setGatewayIp("10.10.40.1");
        List<Route> hostRoutes = new ArrayList<Route>();
        Route newRoute1 = new Route();
        Route newRoute2 = new Route();
        newRoute1.setDestination("10.10.10.0/24");
        newRoute1.setNexthop("10.10.20.1");
        newRoute2.setDestination("10.10.30.0/24");
        newRoute2.setNexthop("10.10.40.1");
        hostRoutes.add(newRoute1);
        hostRoutes.add(newRoute2);
        subnetWithGateway.setHostRoutes(hostRoutes);
        service.createSubnet(subnetWithGateway);
        List<Subnet> subnets = service.getSubnetsByName("CDP_Test_IPV4_Subnet_With_Host_Routes");
        assertFalse(subnets.isEmpty());
        for (Subnet subnet : subnets) {
            assertTrue(subnet.getRouting().equalsIgnoreCase("10.10.40.0/24"));
            assertTrue(subnet.getGatewayIp().equalsIgnoreCase("10.10.40.1"));
            assertFalse(subnet.getHostRoutes().isEmpty());
            assertTrue(subnet.getHostRoutes().contains(newRoute1));
            assertTrue(subnet.getHostRoutes().contains(newRoute2));
        }
    } catch (ZoneException ze) {
        ze.printStackTrace();
        fail("Failed to create the ipv4 test subnet with host routes");
    }
}
Also used : Context(com.att.cdp.zones.Context) ZoneException(com.att.cdp.exceptions.ZoneException) Network(com.att.cdp.zones.model.Network) ArrayList(java.util.ArrayList) NetworkService(com.att.cdp.zones.NetworkService) Subnet(com.att.cdp.zones.model.Subnet) Route(com.att.cdp.zones.model.Route) Ignore(org.junit.Ignore) Test(org.junit.Test)

Aggregations

ZoneException (com.att.cdp.exceptions.ZoneException)40 Context (com.att.cdp.zones.Context)30 OpenStackContext (com.att.cdp.openstack.OpenStackContext)18 NetworkService (com.att.cdp.zones.NetworkService)15 OpenStackBaseException (com.woorea.openstack.base.client.OpenStackBaseException)15 Test (org.junit.Test)15 Network (com.att.cdp.zones.model.Network)13 Ignore (org.junit.Ignore)13 OpenStackConnectException (com.woorea.openstack.base.client.OpenStackConnectException)11 OpenStackResponseException (com.woorea.openstack.base.client.OpenStackResponseException)11 Server (com.att.cdp.zones.model.Server)9 OpenStackServer (com.att.cdp.openstack.model.OpenStackServer)8 Subnet (com.att.cdp.zones.model.Subnet)8 ConnectedServer (com.att.cdp.zones.spi.model.ConnectedServer)8 ContextConnectionException (com.att.cdp.exceptions.ContextConnectionException)7 ArrayList (java.util.ArrayList)7 HashMap (java.util.HashMap)6 InvalidRequestException (com.att.cdp.exceptions.InvalidRequestException)5 ResourceNotFoundException (com.att.cdp.exceptions.ResourceNotFoundException)5 ContextClosedException (com.att.cdp.exceptions.ContextClosedException)4