Search in sources :

Example 1 with Server

use of com.att.cdp.zones.model.Server in project AJSC by att.

the class OpenStackComputeService method getServers.

/**
 * Obtain a list of servers from the compute service.
 *
 * @return The list of servers that are defined.
 * @throws ZoneException
 *             - If any of the following conditions are true:
 *             <ul>
 *             <li>the user has not successfully logged in to the provider</li>
 *             <li>the context has been closed and this service is requested</li>
 *             <li>the current user does not have the rights to perform this operation</li>
 *             <li>the user and/or credentials are not valid</li>
 *             </ul>
 * @see com.att.cdp.zones.ComputeService#getServers()
 */
@Override
public List<Server> getServers() throws ZoneException {
    connect();
    Context context = getContext();
    trackRequest();
    RequestState.put(RequestState.SERVICE, "Compute");
    RequestState.put(RequestState.SERVICE_URL, nova.getEndpoint());
    ArrayList<Server> list = new ArrayList<>();
    try {
        com.woorea.openstack.nova.model.Servers servers = nova.getClient().servers().list(true).execute();
        for (com.woorea.openstack.nova.model.Server s : servers.getList()) {
            list.add(new OpenStackServer(context, s));
        }
    } catch (OpenStackBaseException e) {
        ExceptionMapper.mapException(e);
    }
    return list;
}
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) OpenStackBaseException(com.woorea.openstack.base.client.OpenStackBaseException) OpenStackServer(com.att.cdp.openstack.model.OpenStackServer) ArrayList(java.util.ArrayList) Servers(com.woorea.openstack.nova.model.Servers)

Example 2 with Server

use of com.att.cdp.zones.model.Server in project AJSC by att.

the class OpenStackComputeService method getServers.

/**
 * Returns the list of servers that match the name pattern supplied.
 *
 * @param name
 *            A regular expression that can be used to filter server names. A string that is suitable to use in the
 *            Java <code>String.matches()</code> method.
 * @return The server
 * @throws ZoneException
 *             If the host cannot be found
 * @see java.lang.String#matches(String)
 * @see com.att.cdp.zones.ComputeService#getServers(java.lang.String)
 */
@Override
public List<Server> getServers(String name) throws ZoneException {
    // checkArg(name, "name");
    connect();
    Context context = getContext();
    trackRequest();
    RequestState.put(RequestState.SERVICE, "Compute");
    RequestState.put(RequestState.SERVICE_URL, nova.getEndpoint());
    RequestState.put(RequestState.SERVER, name);
    ArrayList<Server> list = new ArrayList<>();
    try {
        com.woorea.openstack.nova.model.Servers servers = nova.getClient().servers().list(true).execute();
        for (com.woorea.openstack.nova.model.Server s : servers.getList()) {
            if (name != null) {
                if (s.getName().matches(name)) {
                    list.add(new OpenStackServer(context, s));
                }
            } else {
                list.add(new OpenStackServer(context, s));
            }
        }
    } catch (OpenStackBaseException e) {
        ExceptionMapper.mapException(e);
    }
    return list;
}
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) OpenStackBaseException(com.woorea.openstack.base.client.OpenStackBaseException) OpenStackServer(com.att.cdp.openstack.model.OpenStackServer) ArrayList(java.util.ArrayList) Servers(com.woorea.openstack.nova.model.Servers)

Example 3 with Server

use of com.att.cdp.zones.model.Server 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;
}
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) OpenStackNetwork(com.att.cdp.openstack.model.OpenStackNetwork) 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) 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 4 with Server

use of com.att.cdp.zones.model.Server in project AJSC by att.

the class OpenStackComputeService method getServers.

/**
 * Returns the list of servers that match the name pattern supplied.
 *
 * @param name
 *            A regular expression that can be used to filter server names.
 *            A string that is suitable to use in the Java
 *            <code>String.matches()</code> method.
 * @return The server
 * @throws ZoneException
 *             If the host cannot be found
 * @see java.lang.String#matches(String)
 * @see com.att.cdp.zones.ComputeService#getServers(java.lang.String)
 */
@Override
public List<Server> getServers(String name) throws ZoneException {
    connect();
    Context context = getContext();
    trackRequest();
    RequestState.put(RequestState.SERVICE, "Compute");
    RequestState.put(RequestState.SERVICE_URL, nova.getEndpoint());
    RequestState.put(RequestState.SERVER, name);
    ArrayList<Server> list = new ArrayList<>();
    try {
        com.woorea.openstack.nova.model.Servers servers = nova.getClient().servers().list(true).execute();
        for (com.woorea.openstack.nova.model.Server s : servers.getList()) {
            if (name != null) {
                if (s.getName().matches(name)) {
                    list.add(new OpenStackServer(context, s));
                }
            } else {
                list.add(new OpenStackServer(context, s));
            }
        }
    } catch (OpenStackBaseException e) {
        ExceptionMapper.mapException(e);
    }
    return list;
}
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) OpenStackBaseException(com.woorea.openstack.base.client.OpenStackBaseException) OpenStackServer(com.att.cdp.openstack.model.OpenStackServer) ArrayList(java.util.ArrayList) Servers(com.woorea.openstack.nova.model.Servers)

Example 5 with Server

use of com.att.cdp.zones.model.Server in project AJSC by att.

the class OpenStackComputeService method deleteServer.

/**
 * Delete the specified server using it's id.
 *
 * @param serverId
 *            The server to be deleted.
 * @throws ZoneException
 *             If the server does not exist or cannot be deleted for some
 *             reason.
 * @see com.att.cdp.zones.ComputeService#deleteServer(java.lang.String)
 */
@SuppressWarnings("nls")
@Override
public void deleteServer(String serverId) throws ZoneException {
    checkArg(serverId, "serverId");
    connect();
    Context context = getContext();
    trackRequest();
    RequestState.put(RequestState.SERVER, serverId);
    RequestState.put(RequestState.SERVICE, "Compute");
    RequestState.put(RequestState.SERVICE_URL, nova.getEndpoint());
    try {
        Server server = getServer(serverId);
        Server.Status status = server.getStatus();
        RequestState.put(RequestState.STATUS, status.toString());
        if (status.equals(Server.Status.RUNNING) || status.equals(Server.Status.READY) || status.equals(Server.Status.ERROR)) {
            List<Port> ports = server.getPorts();
            for (Port port : ports) {
                port.delete();
            }
            nova.getClient().servers().delete(serverId).execute();
        } else {
            throw new ZoneException(EELFResourceManager.format(OSMsg.PAL_OS_INVALID_SERVER_STATE, server.getName(), server.getId(), status.toString(), StringHelper.asList(Arrays.asList(Server.Status.READY.toString(), Server.Status.RUNNING.toString(), Server.Status.ERROR.toString()))));
        }
    } catch (OpenStackBaseException ex) {
        ExceptionMapper.mapException(ex);
    }
}
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) Port(com.att.cdp.zones.model.Port) OpenStackPort(com.att.cdp.openstack.model.OpenStackPort)

Aggregations

Server (com.att.cdp.zones.model.Server)26 Context (com.att.cdp.zones.Context)22 OpenStackServer (com.att.cdp.openstack.model.OpenStackServer)17 OpenStackContext (com.att.cdp.openstack.OpenStackContext)16 Ignore (org.junit.Ignore)13 ComputeService (com.att.cdp.zones.ComputeService)12 ConnectedServer (com.att.cdp.zones.spi.model.ConnectedServer)12 OpenStackBaseException (com.woorea.openstack.base.client.OpenStackBaseException)11 Test (org.junit.Test)11 ZoneException (com.att.cdp.exceptions.ZoneException)9 ArrayList (java.util.ArrayList)7 Port (com.att.cdp.zones.model.Port)6 OpenStackPort (com.att.cdp.openstack.model.OpenStackPort)4 NetworkService (com.att.cdp.zones.NetworkService)4 Network (com.att.cdp.zones.model.Network)4 Servers (com.woorea.openstack.nova.model.Servers)4 HashMap (java.util.HashMap)4 ACL (com.att.cdp.zones.model.ACL)3 Subnet (com.att.cdp.zones.model.Subnet)3 List (java.util.List)3