Search in sources :

Example 1 with ACL

use of com.att.cdp.zones.model.ACL 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 2 with ACL

use of com.att.cdp.zones.model.ACL 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 3 with ACL

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

the class OpenStackComputeService method getAccessControlLists.

/**
 * @see com.att.cdp.zones.ComputeService#getAccessControlLists()
 */
@Override
public List<ACL> getAccessControlLists() throws ZoneException {
    connect();
    Context context = getContext();
    trackRequest();
    RequestState.put(RequestState.SERVICE, "Compute");
    RequestState.put(RequestState.SERVICE_URL, nova.getEndpoint());
    ArrayList<ACL> list = new ArrayList<>();
    try {
        for (SecurityGroup group : nova.getClient().securityGroups().listSecurityGroups().execute()) {
            list.add(new OpenStackACL(context, group));
        }
    } catch (OpenStackBaseException ex) {
        ExceptionMapper.mapException(ex);
    }
    return list;
}
Also used : Context(com.att.cdp.zones.Context) OpenStackContext(com.att.cdp.openstack.OpenStackContext) OpenStackBaseException(com.woorea.openstack.base.client.OpenStackBaseException) ArrayList(java.util.ArrayList) OpenStackACL(com.att.cdp.openstack.model.OpenStackACL) ACL(com.att.cdp.zones.model.ACL) SecurityGroup(com.woorea.openstack.nova.model.SecurityGroup) OpenStackACL(com.att.cdp.openstack.model.OpenStackACL)

Example 4 with ACL

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

the class OpenStackComputeService method getAccessControlLists.

/**
 * @see com.att.cdp.zones.ComputeService#getAccessControlLists()
 */
@Override
public List<ACL> getAccessControlLists() throws ZoneException {
    connect();
    Context context = getContext();
    ArrayList<ACL> list = new ArrayList<>();
    trackRequest();
    RequestState.put(RequestState.SERVICE, "Compute");
    RequestState.put(RequestState.SERVICE_URL, nova.getEndpoint());
    try {
        for (SecurityGroup group : nova.getClient().securityGroups().listSecurityGroups().execute()) {
            list.add(new OpenStackACL(context, group));
        }
    } catch (OpenStackBaseException ex) {
        ExceptionMapper.mapException(ex);
    }
    return list;
}
Also used : Context(com.att.cdp.zones.Context) OpenStackContext(com.att.cdp.openstack.OpenStackContext) OpenStackBaseException(com.woorea.openstack.base.client.OpenStackBaseException) ArrayList(java.util.ArrayList) OpenStackACL(com.att.cdp.openstack.model.OpenStackACL) ACL(com.att.cdp.zones.model.ACL) SecurityGroup(com.woorea.openstack.nova.model.SecurityGroup) OpenStackACL(com.att.cdp.openstack.model.OpenStackACL)

Example 5 with ACL

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

the class TestComputeService method testComputeService.

/**
 * @throws ZoneException
 *             If something goes horribly wrong
 */
@Ignore
@Test
public void testComputeService() throws ZoneException {
    Context context = connect();
    ComputeService computeService = context.getComputeService();
    List<Server> servers = computeService.getServers();
    for (Server server : servers) {
        computeService.getServers(server.getName());
        computeService.getServer(server.getId());
        computeService.getAttachments(server);
        computeService.getAttachments(server.getId());
        computeService.getConsoleOutput(server);
    }
    List<Template> templates = computeService.getTemplates();
    for (Template template : templates) {
        computeService.getTemplate(template.getId());
    }
    List<ACL> accessControlLists = computeService.getAccessControlLists();
    for (ACL acl : accessControlLists) {
        computeService.getAccessControlList(acl.getId());
    }
}
Also used : OpenStackContext(com.att.cdp.openstack.OpenStackContext) Context(com.att.cdp.zones.Context) OpenStackServer(com.att.cdp.openstack.model.OpenStackServer) Server(com.att.cdp.zones.model.Server) ACL(com.att.cdp.zones.model.ACL) ComputeService(com.att.cdp.zones.ComputeService) Template(com.att.cdp.zones.model.Template) Ignore(org.junit.Ignore) Test(org.junit.Test)

Aggregations

Context (com.att.cdp.zones.Context)6 ACL (com.att.cdp.zones.model.ACL)6 OpenStackContext (com.att.cdp.openstack.OpenStackContext)5 OpenStackACL (com.att.cdp.openstack.model.OpenStackACL)4 OpenStackBaseException (com.woorea.openstack.base.client.OpenStackBaseException)4 ArrayList (java.util.ArrayList)4 ZoneException (com.att.cdp.exceptions.ZoneException)3 OpenStackServer (com.att.cdp.openstack.model.OpenStackServer)3 Server (com.att.cdp.zones.model.Server)3 SecurityGroup (com.woorea.openstack.nova.model.SecurityGroup)3 OpenStackFault (com.att.cdp.openstack.model.OpenStackFault)2 OpenStackPort (com.att.cdp.openstack.model.OpenStackPort)2 ComputeService (com.att.cdp.zones.ComputeService)2 NetworkService (com.att.cdp.zones.NetworkService)2 Network (com.att.cdp.zones.model.Network)2 Port (com.att.cdp.zones.model.Port)2 Subnet (com.att.cdp.zones.model.Subnet)2 ConnectedServer (com.att.cdp.zones.spi.model.ConnectedServer)2 ServersResource (com.woorea.openstack.nova.api.ServersResource)2 Boot (com.woorea.openstack.nova.api.ServersResource.Boot)2