Search in sources :

Example 71 with CloudResource

use of com.sequenceiq.cloudbreak.cloud.model.CloudResource in project cloudbreak by hortonworks.

the class OpenStackMetadataCollector method collect.

@Override
public List<CloudVmMetaDataStatus> collect(AuthenticatedContext authenticatedContext, List<CloudResource> resources, List<CloudInstance> vms) {
    CloudResource resource = utils.getHeatResource(resources);
    String stackName = utils.getStackName(authenticatedContext);
    String heatStackId = resource.getName();
    List<InstanceTemplate> templates = Lists.transform(vms, CloudInstance::getTemplate);
    Map<String, InstanceTemplate> templateMap = Maps.uniqueIndex(templates, from -> utils.getPrivateInstanceId(from.getGroupName(), Long.toString(from.getPrivateId())));
    OSClient<?> client = openStackClient.createOSClient(authenticatedContext);
    Stack heatStack = client.heat().stacks().getDetails(stackName, heatStackId);
    List<CloudVmMetaDataStatus> results = new ArrayList<>();
    List<Map<String, Object>> outputs = heatStack.getOutputs();
    for (Map<String, Object> map : outputs) {
        String instanceUUID = (String) map.get("output_value");
        if (!StringUtils.isEmpty(instanceUUID)) {
            Server server = client.compute().servers().get(instanceUUID);
            Map<String, String> metadata = server.getMetadata();
            String privateInstanceId = utils.getPrivateInstanceId(metadata);
            InstanceTemplate template = templateMap.get(privateInstanceId);
            if (template != null) {
                CloudInstanceMetaData md = cloudInstanceMetaDataExtractor.extractMetadata(client, server, instanceUUID);
                // TODO use here sshkey
                CloudInstance cloudInstance = new CloudInstance(instanceUUID, template, null);
                CloudVmInstanceStatus status = new CloudVmInstanceStatus(cloudInstance, InstanceStatus.CREATED);
                results.add(new CloudVmMetaDataStatus(status, md));
            }
        }
    }
    return results;
}
Also used : Server(org.openstack4j.model.compute.Server) CloudVmInstanceStatus(com.sequenceiq.cloudbreak.cloud.model.CloudVmInstanceStatus) ArrayList(java.util.ArrayList) CloudInstance(com.sequenceiq.cloudbreak.cloud.model.CloudInstance) Stack(org.openstack4j.model.heat.Stack) CloudInstanceMetaData(com.sequenceiq.cloudbreak.cloud.model.CloudInstanceMetaData) CloudVmMetaDataStatus(com.sequenceiq.cloudbreak.cloud.model.CloudVmMetaDataStatus) CloudResource(com.sequenceiq.cloudbreak.cloud.model.CloudResource) Map(java.util.Map) InstanceTemplate(com.sequenceiq.cloudbreak.cloud.model.InstanceTemplate)

Example 72 with CloudResource

use of com.sequenceiq.cloudbreak.cloud.model.CloudResource in project cloudbreak by hortonworks.

the class OpenStackResourceConnector method collectResources.

private List<CloudResource> collectResources(AuthenticatedContext authenticatedContext, PersistenceNotifier notifier, Stack heatStack, CloudStack stack, NeutronNetworkView neutronNetworkView) {
    List<CloudResource> cloudResources = newArrayList();
    CloudResource heatResource = new Builder().type(ResourceType.HEAT_STACK).name(heatStack.getId()).build();
    try {
        notifier.notifyAllocation(heatResource, authenticatedContext.getCloudContext());
    } catch (RuntimeException ignored) {
        // Rollback
        terminate(authenticatedContext, stack, Collections.singletonList(heatResource));
    }
    cloudResources.add(heatResource);
    if (!neutronNetworkView.isProviderNetwork()) {
        if (!neutronNetworkView.isExistingNetwork()) {
            CloudResource r = CloudResource.builder().type(ResourceType.OPENSTACK_NETWORK).name(OpenStackConstants.NETWORK_ID).build();
            cloudResources.add(r);
        }
        if (!neutronNetworkView.isExistingSubnet()) {
            CloudResource r = CloudResource.builder().type(ResourceType.OPENSTACK_SUBNET).name(OpenStackConstants.SUBNET_ID).build();
            cloudResources.add(r);
        }
    }
    return cloudResources;
}
Also used : Builder(com.sequenceiq.cloudbreak.cloud.model.CloudResource.Builder) CloudResource(com.sequenceiq.cloudbreak.cloud.model.CloudResource)

Example 73 with CloudResource

use of com.sequenceiq.cloudbreak.cloud.model.CloudResource in project cloudbreak by hortonworks.

the class OpenStackResourceConnector method check.

@Override
public List<CloudResourceStatus> check(AuthenticatedContext authenticatedContext, List<CloudResource> resources) {
    List<CloudResourceStatus> results = newArrayList();
    OSClient<?> client = openStackClient.createOSClient(authenticatedContext);
    String stackName = utils.getStackName(authenticatedContext);
    List<CloudResource> osResourceList = openStackClient.getResources(stackName, authenticatedContext.getCloudCredential());
    List<CloudResource> otherResources = newArrayList();
    CloudResourceStatus heatStatus = null;
    for (CloudResource resource : resources) {
        if (resource.getType() == ResourceType.HEAT_STACK) {
            heatStatus = checkByResourceType(authenticatedContext, client, stackName, osResourceList, resource);
            results.add(heatStatus);
        } else {
            otherResources.add(resource);
        }
    }
    if (heatStatus != null) {
        for (CloudResource resource : otherResources) {
            if (heatStatus.getStatus() == ResourceStatus.CREATED) {
                results.add(checkByResourceType(authenticatedContext, client, stackName, osResourceList, resource));
            } else {
                results.add(new CloudResourceStatus(resource, ResourceStatus.CREATED));
            }
        }
    }
    return results;
}
Also used : CloudResourceStatus(com.sequenceiq.cloudbreak.cloud.model.CloudResourceStatus) CloudResource(com.sequenceiq.cloudbreak.cloud.model.CloudResource)

Example 74 with CloudResource

use of com.sequenceiq.cloudbreak.cloud.model.CloudResource in project cloudbreak by hortonworks.

the class MockResourceConnector method upscale.

@Override
public List<CloudResourceStatus> upscale(AuthenticatedContext authenticatedContext, CloudStack stack, List<CloudResource> resources) {
    List<CloudResourceStatus> cloudResourceStatuses = new ArrayList<>();
    for (CloudResource cloudResource : resources) {
        CloudResourceStatus cloudResourceStatus = new CloudResourceStatus(cloudResource, CREATED);
        cloudResourceStatuses.add(cloudResourceStatus);
    }
    int createResourceCount = 0;
    for (int i = 0; i < stack.getGroups().size(); i++) {
        createResourceCount += stack.getGroups().get(i).getInstancesSize();
    }
    createResourceCount -= resources.size();
    if (createResourceCount > 0) {
        for (int i = 0; i < createResourceCount; i++) {
            CloudResource cloudResource = new Builder().type(ResourceType.MOCK_INSTANCE).status(CommonStatus.CREATED).name("cloudinstance" + cloudResourceStatuses.size()).reference("").persistent(true).build();
            cloudResourceStatuses.add(new CloudResourceStatus(cloudResource, CREATED));
        }
    }
    return cloudResourceStatuses;
}
Also used : CloudResourceStatus(com.sequenceiq.cloudbreak.cloud.model.CloudResourceStatus) Builder(com.sequenceiq.cloudbreak.cloud.model.CloudResource.Builder) ArrayList(java.util.ArrayList) CloudResource(com.sequenceiq.cloudbreak.cloud.model.CloudResource)

Example 75 with CloudResource

use of com.sequenceiq.cloudbreak.cloud.model.CloudResource in project cloudbreak by hortonworks.

the class DownscaleStackHandler method accept.

@Override
public void accept(Event<DownscaleStackRequest> downscaleStackRequestEvent) {
    LOGGER.info("Received event: {}", downscaleStackRequestEvent);
    DownscaleStackRequest request = downscaleStackRequestEvent.getData();
    DownscaleStackResult result;
    try {
        CloudContext cloudContext = request.getCloudContext();
        CloudConnector connector = cloudPlatformConnectors.get(cloudContext.getPlatformVariant());
        AuthenticatedContext ac = connector.authentication().authenticate(cloudContext, request.getCloudCredential());
        List<CloudResourceStatus> resourceStatus = connector.resources().downscale(ac, request.getCloudStack(), request.getCloudResources(), request.getInstances(), request.getResourcesToScale());
        List<CloudResource> resources = ResourceLists.transform(resourceStatus);
        PollTask<ResourcesStatePollerResult> task = statusCheckFactory.newPollResourcesStateTask(ac, resources, true);
        ResourcesStatePollerResult statePollerResult = ResourcesStatePollerResults.build(cloudContext, resourceStatus);
        if (!task.completed(statePollerResult)) {
            statePollerResult = syncPollingScheduler.schedule(task);
        }
        LOGGER.info("Downscale successfully finished for {}", cloudContext);
        result = new DownscaleStackResult(request, ResourceLists.transform(statePollerResult.getResults()));
    } catch (Exception e) {
        LOGGER.error("Failed to handle DownscaleStackRequest.", e);
        result = new DownscaleStackResult(e.getMessage(), e, request);
    }
    request.getResult().onNext(result);
    eventBus.notify(result.selector(), new Event<>(downscaleStackRequestEvent.getHeaders(), result));
}
Also used : CloudConnector(com.sequenceiq.cloudbreak.cloud.CloudConnector) CloudContext(com.sequenceiq.cloudbreak.cloud.context.CloudContext) AuthenticatedContext(com.sequenceiq.cloudbreak.cloud.context.AuthenticatedContext) DownscaleStackResult(com.sequenceiq.cloudbreak.cloud.event.resource.DownscaleStackResult) CloudResourceStatus(com.sequenceiq.cloudbreak.cloud.model.CloudResourceStatus) DownscaleStackRequest(com.sequenceiq.cloudbreak.cloud.event.resource.DownscaleStackRequest) CloudResource(com.sequenceiq.cloudbreak.cloud.model.CloudResource) ResourcesStatePollerResult(com.sequenceiq.cloudbreak.cloud.task.ResourcesStatePollerResult)

Aggregations

CloudResource (com.sequenceiq.cloudbreak.cloud.model.CloudResource)79 ArrayList (java.util.ArrayList)33 CloudResourceStatus (com.sequenceiq.cloudbreak.cloud.model.CloudResourceStatus)29 CloudContext (com.sequenceiq.cloudbreak.cloud.context.CloudContext)20 CloudInstance (com.sequenceiq.cloudbreak.cloud.model.CloudInstance)17 CloudConnectorException (com.sequenceiq.cloudbreak.cloud.exception.CloudConnectorException)16 Builder (com.sequenceiq.cloudbreak.cloud.model.CloudResource.Builder)13 List (java.util.List)11 InstanceTemplate (com.sequenceiq.cloudbreak.cloud.model.InstanceTemplate)9 AuthenticatedContext (com.sequenceiq.cloudbreak.cloud.context.AuthenticatedContext)8 CloudStack (com.sequenceiq.cloudbreak.cloud.model.CloudStack)8 Location (com.sequenceiq.cloudbreak.cloud.model.Location)8 Group (com.sequenceiq.cloudbreak.cloud.model.Group)7 Operation (com.google.api.services.compute.model.Operation)6 AzureClient (com.sequenceiq.cloudbreak.cloud.azure.client.AzureClient)6 CloudCredential (com.sequenceiq.cloudbreak.cloud.model.CloudCredential)6 CloudVmInstanceStatus (com.sequenceiq.cloudbreak.cloud.model.CloudVmInstanceStatus)6 CloudVmMetaDataStatus (com.sequenceiq.cloudbreak.cloud.model.CloudVmMetaDataStatus)6 Map (java.util.Map)6 CloudConnector (com.sequenceiq.cloudbreak.cloud.CloudConnector)5