Search in sources :

Example 1 with OpenStackResourceException

use of com.sequenceiq.cloudbreak.cloud.openstack.nativ.OpenStackResourceException in project cloudbreak by hortonworks.

the class OpenStackAttachedDiskResourceBuilder method build.

@Override
public List<CloudResource> build(OpenStackContext context, long privateId, AuthenticatedContext auth, Group group, Image image, List<CloudResource> buildableResource, Map<String, String> tags) throws Exception {
    List<CloudResource> resources = new ArrayList<>();
    List<CloudResource> syncedResources = Collections.synchronizedList(resources);
    Collection<Future<Void>> futures = new ArrayList<>();
    for (CloudResource cloudResource : buildableResource) {
        Future<Void> submit = intermediateBuilderExecutor.submit(() -> {
            CinderVolumeView volumeView = cloudResource.getParameter(VOLUME_VIEW, CinderVolumeView.class);
            Volume osVolume = Builders.volume().name(cloudResource.getName()).size(volumeView.getSize()).build();
            try {
                OSClient<?> osClient = createOSClient(auth);
                osVolume = osClient.blockStorage().volumes().create(osVolume);
                CloudResource newRes = createPersistedResource(cloudResource, group.getName(), osVolume.getId());
                newRes.putParameter(OpenStackConstants.VOLUME_MOUNT_POINT, volumeView.getDevice());
                syncedResources.add(newRes);
            } catch (OS4JException ex) {
                throw new OpenStackResourceException("Volume creation failed", resourceType(), cloudResource.getName(), ex);
            }
            return null;
        });
        futures.add(submit);
    }
    for (Future<Void> future : futures) {
        future.get();
    }
    return resources;
}
Also used : CinderVolumeView(com.sequenceiq.cloudbreak.cloud.openstack.view.CinderVolumeView) Volume(org.openstack4j.model.storage.block.Volume) OpenStackResourceException(com.sequenceiq.cloudbreak.cloud.openstack.nativ.OpenStackResourceException) ArrayList(java.util.ArrayList) Future(java.util.concurrent.Future) CloudResource(com.sequenceiq.cloudbreak.cloud.model.CloudResource) OS4JException(org.openstack4j.api.exceptions.OS4JException)

Example 2 with OpenStackResourceException

use of com.sequenceiq.cloudbreak.cloud.openstack.nativ.OpenStackResourceException in project cloudbreak by hortonworks.

the class OpenStackFloatingIPBuilder method build.

@Override
public List<CloudResource> build(OpenStackContext context, long privateId, AuthenticatedContext auth, Group group, Image image, List<CloudResource> buildableResource, Map<String, String> tags) {
    CloudResource resource = buildableResource.get(0);
    try {
        String publicNetId = context.getStringParameter(OpenStackConstants.PUBLIC_NET_ID);
        if (publicNetId != null) {
            OSClient<?> osClient = createOSClient(auth);
            List<CloudResource> computeResources = context.getComputeResources(privateId);
            CloudResource instance = getInstance(computeResources);
            FloatingIP unusedIp = osClient.compute().floatingIps().allocateIP(publicNetId);
            ActionResponse response = osClient.compute().floatingIps().addFloatingIP(instance.getParameter(OpenStackConstants.SERVER, Server.class), unusedIp.getFloatingIpAddress());
            if (!response.isSuccess()) {
                throw new OpenStackResourceException("Add floating-ip to server failed", resourceType(), resource.getName(), auth.getCloudContext().getId(), response.getFault());
            }
            return Collections.singletonList(createPersistedResource(resource, group.getName(), unusedIp.getId()));
        }
        return Collections.emptyList();
    } catch (OS4JException ex) {
        throw new OpenStackResourceException("Add floating-ip to server failed", resourceType(), resource.getName(), ex);
    }
}
Also used : Server(org.openstack4j.model.compute.Server) OpenStackResourceException(com.sequenceiq.cloudbreak.cloud.openstack.nativ.OpenStackResourceException) CloudResource(com.sequenceiq.cloudbreak.cloud.model.CloudResource) FloatingIP(org.openstack4j.model.compute.FloatingIP) OS4JException(org.openstack4j.api.exceptions.OS4JException) ActionResponse(org.openstack4j.model.common.ActionResponse)

Example 3 with OpenStackResourceException

use of com.sequenceiq.cloudbreak.cloud.openstack.nativ.OpenStackResourceException in project cloudbreak by hortonworks.

the class OpenStackInstanceBuilder method build.

@Override
public List<CloudResource> build(OpenStackContext context, long privateId, AuthenticatedContext auth, Group group, Image image, List<CloudResource> buildableResource, Map<String, String> tags) {
    CloudResource resource = buildableResource.get(0);
    try {
        OSClient<?> osClient = createOSClient(auth);
        InstanceTemplate template = getInstanceTemplate(group, privateId);
        CloudResource port = getPort(context.getComputeResources(privateId));
        KeystoneCredentialView osCredential = new KeystoneCredentialView(auth);
        NovaInstanceView novaInstanceView = new NovaInstanceView(context.getName(), template, group.getType(), group.getLoginUserName());
        String imageId = osClient.imagesV2().list(Collections.singletonMap("name", image.getImageName())).get(0).getId();
        LOGGER.info("Selected image id: {}", imageId);
        Map<String, String> metadata = mergeMetadata(novaInstanceView.getMetadataMap(), tags);
        ServerCreateBuilder serverCreateBuilder = Builders.server().name(resource.getName()).image(imageId).flavor(getFlavorId(osClient, novaInstanceView.getFlavor())).keypairName(osCredential.getKeyPairName()).addMetadata(metadata).addNetworkPort(port.getStringParameter(OpenStackConstants.PORT_ID)).userData(new String(Base64.encodeBase64(image.getUserDataByType(group.getType()).getBytes())));
        BlockDeviceMappingBuilder blockDeviceMappingBuilder = Builders.blockDeviceMapping().uuid(imageId).sourceType(BDMSourceType.IMAGE).deviceName("/dev/vda").bootIndex(0).deleteOnTermination(true).destinationType(BDMDestType.LOCAL);
        serverCreateBuilder = serverCreateBuilder.blockDevice(blockDeviceMappingBuilder.build());
        for (CloudResource computeResource : context.getComputeResources(privateId)) {
            if (computeResource.getType() == ResourceType.OPENSTACK_ATTACHED_DISK) {
                BlockDeviceMappingCreate blockDeviceMappingCreate = Builders.blockDeviceMapping().uuid(computeResource.getReference()).deviceName(computeResource.getStringParameter(OpenStackConstants.VOLUME_MOUNT_POINT)).sourceType(BDMSourceType.VOLUME).destinationType(BDMDestType.VOLUME).build();
                serverCreateBuilder.blockDevice(blockDeviceMappingCreate);
            }
        }
        ServerCreate serverCreate = serverCreateBuilder.build();
        Server server = osClient.compute().servers().boot(serverCreate);
        return Collections.singletonList(createPersistedResource(resource, group.getName(), server.getId(), Collections.singletonMap(OpenStackConstants.SERVER, server)));
    } catch (OS4JException ex) {
        LOGGER.error("Failed to create OpenStack instance with privateId: {}", privateId, ex);
        throw new OpenStackResourceException("Instance creation failed", resourceType(), resource.getName(), ex);
    }
}
Also used : Server(org.openstack4j.model.compute.Server) ServerCreate(org.openstack4j.model.compute.ServerCreate) BlockDeviceMappingCreate(org.openstack4j.model.compute.BlockDeviceMappingCreate) BlockDeviceMappingBuilder(org.openstack4j.model.compute.builder.BlockDeviceMappingBuilder) OS4JException(org.openstack4j.api.exceptions.OS4JException) NovaInstanceView(com.sequenceiq.cloudbreak.cloud.openstack.view.NovaInstanceView) ServerCreateBuilder(org.openstack4j.model.compute.builder.ServerCreateBuilder) OpenStackResourceException(com.sequenceiq.cloudbreak.cloud.openstack.nativ.OpenStackResourceException) KeystoneCredentialView(com.sequenceiq.cloudbreak.cloud.openstack.view.KeystoneCredentialView) CloudResource(com.sequenceiq.cloudbreak.cloud.model.CloudResource) InstanceTemplate(com.sequenceiq.cloudbreak.cloud.model.InstanceTemplate)

Example 4 with OpenStackResourceException

use of com.sequenceiq.cloudbreak.cloud.openstack.nativ.OpenStackResourceException in project cloudbreak by hortonworks.

the class OpenStackNetworkResourceBuilder method checkStatus.

@Override
protected boolean checkStatus(OpenStackContext context, AuthenticatedContext auth, CloudResource resource) {
    CloudContext cloudContext = auth.getCloudContext();
    OSClient<?> osClient = createOSClient(auth);
    org.openstack4j.model.network.Network osNetwork = osClient.networking().network().get(resource.getReference());
    if (osNetwork != null && context.isBuild()) {
        State networkStatus = osNetwork.getStatus();
        if (State.ERROR == networkStatus) {
            throw new OpenStackResourceException("Network in failed state", resource.getType(), resource.getName(), cloudContext.getId(), networkStatus.name());
        }
        return networkStatus == State.ACTIVE;
    } else {
        return osNetwork == null && !context.isBuild();
    }
}
Also used : State(org.openstack4j.model.network.State) CloudContext(com.sequenceiq.cloudbreak.cloud.context.CloudContext) OpenStackResourceException(com.sequenceiq.cloudbreak.cloud.openstack.nativ.OpenStackResourceException)

Example 5 with OpenStackResourceException

use of com.sequenceiq.cloudbreak.cloud.openstack.nativ.OpenStackResourceException in project cloudbreak by hortonworks.

the class OpenStackRouterResourceBuilder method build.

@Override
public CloudResource build(OpenStackContext context, AuthenticatedContext auth, Network network, Security security, CloudResource resource) {
    try {
        OSClient<?> osClient = createOSClient(auth);
        NeutronNetworkView networkView = new NeutronNetworkView(network);
        String routerId = networkView.getCustomRouterId();
        if (!networkView.isExistingNetwork()) {
            Router router = Builders.router().name(resource.getName()).adminStateUp(true).tenantId(context.getStringParameter(OpenStackConstants.TENANT_ID)).externalGateway(networkView.getPublicNetId()).build();
            Router newRouter = osClient.networking().router().create(router);
            if (newRouter == null) {
                throw new OpenStackResourceException("Router creation failed, maybe network does not exists", resourceType(), resource.getName());
            }
            routerId = newRouter.getId();
        }
        if (!networkView.isExistingSubnet()) {
            osClient.networking().router().attachInterface(routerId, AttachInterfaceType.SUBNET, context.getStringParameter(OpenStackConstants.SUBNET_ID));
        }
        return createPersistedResource(resource, routerId);
    } catch (OS4JException ex) {
        throw new OpenStackResourceException("Router creation failed", resourceType(), resource.getName(), ex);
    }
}
Also used : OpenStackResourceException(com.sequenceiq.cloudbreak.cloud.openstack.nativ.OpenStackResourceException) Router(org.openstack4j.model.network.Router) NeutronNetworkView(com.sequenceiq.cloudbreak.cloud.openstack.view.NeutronNetworkView) OS4JException(org.openstack4j.api.exceptions.OS4JException)

Aggregations

OpenStackResourceException (com.sequenceiq.cloudbreak.cloud.openstack.nativ.OpenStackResourceException)18 OS4JException (org.openstack4j.api.exceptions.OS4JException)15 ActionResponse (org.openstack4j.model.common.ActionResponse)8 NeutronNetworkView (com.sequenceiq.cloudbreak.cloud.openstack.view.NeutronNetworkView)7 CloudResource (com.sequenceiq.cloudbreak.cloud.model.CloudResource)4 CloudContext (com.sequenceiq.cloudbreak.cloud.context.CloudContext)3 Server (org.openstack4j.model.compute.Server)2 Router (org.openstack4j.model.network.Router)2 State (org.openstack4j.model.network.State)2 Volume (org.openstack4j.model.storage.block.Volume)2 InstanceTemplate (com.sequenceiq.cloudbreak.cloud.model.InstanceTemplate)1 PortDefinition (com.sequenceiq.cloudbreak.cloud.model.PortDefinition)1 SecurityRule (com.sequenceiq.cloudbreak.cloud.model.SecurityRule)1 CinderVolumeView (com.sequenceiq.cloudbreak.cloud.openstack.view.CinderVolumeView)1 KeystoneCredentialView (com.sequenceiq.cloudbreak.cloud.openstack.view.KeystoneCredentialView)1 NovaInstanceView (com.sequenceiq.cloudbreak.cloud.openstack.view.NovaInstanceView)1 ArrayList (java.util.ArrayList)1 Future (java.util.concurrent.Future)1 ComputeSecurityGroupService (org.openstack4j.api.compute.ComputeSecurityGroupService)1 BlockDeviceMappingCreate (org.openstack4j.model.compute.BlockDeviceMappingCreate)1