Search in sources :

Example 76 with CloudResource

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

the class OpenStackPortBuilder 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);
        Port port = Builders.port().tenantId(context.getStringParameter(OpenStackConstants.TENANT_ID)).networkId(context.getStringParameter(OpenStackConstants.NETWORK_ID)).fixedIp(null, context.getStringParameter(OpenStackConstants.SUBNET_ID)).securityGroup(context.getGroupResources(group.getName()).get(0).getReference()).build();
        port = osClient.networking().port().create(port);
        return Collections.singletonList(createPersistedResource(resource, group.getName(), port.getId(), Collections.singletonMap(OpenStackConstants.PORT_ID, port.getId())));
    } catch (OS4JException ex) {
        throw new OpenStackResourceException("Port creation failed", resourceType(), resource.getName(), ex);
    }
}
Also used : Port(org.openstack4j.model.network.Port) OpenStackResourceException(com.sequenceiq.cloudbreak.cloud.openstack.nativ.OpenStackResourceException) CloudResource(com.sequenceiq.cloudbreak.cloud.model.CloudResource) OS4JException(org.openstack4j.api.exceptions.OS4JException)

Example 77 with CloudResource

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

the class AzureResourceConnector method terminate.

@Override
public List<CloudResourceStatus> terminate(AuthenticatedContext authenticatedContext, CloudStack stack, List<CloudResource> resources) {
    AzureClient client = authenticatedContext.getParameter(AzureClient.class);
    for (CloudResource resource : resources) {
        try {
            try {
                retryService.testWith2SecDelayMax5Times(() -> {
                    if (!client.resourceGroupExists(resource.getName())) {
                        throw new ActionWentFailException("Resource group not exists");
                    }
                    return true;
                });
                client.deleteResourceGroup(resource.getName());
            } catch (ActionWentFailException ignored) {
                LOGGER.info(String.format("Resource group not found with name: %s", resource.getName()));
            }
            if (azureStorage.isPersistentStorage(azureStorage.getPersistentStorageName(stack))) {
                CloudContext cloudCtx = authenticatedContext.getCloudContext();
                AzureCredentialView azureCredentialView = new AzureCredentialView(authenticatedContext.getCloudCredential());
                String imageStorageName = azureStorage.getImageStorageName(azureCredentialView, cloudCtx, stack);
                String imageResourceGroupName = azureStorage.getImageResourceGroupName(cloudCtx, stack);
                String diskContainer = azureStorage.getDiskContainerName(cloudCtx);
                deleteContainer(client, imageResourceGroupName, imageStorageName, diskContainer);
            }
        } catch (CloudException e) {
            if (e.response().code() != AzureConstants.NOT_FOUND) {
                throw new CloudConnectorException(String.format("Could not delete resource group: %s", resource.getName()), e);
            } else {
                return check(authenticatedContext, Collections.emptyList());
            }
        }
    }
    return check(authenticatedContext, resources);
}
Also used : AzureClient(com.sequenceiq.cloudbreak.cloud.azure.client.AzureClient) AzureCredentialView(com.sequenceiq.cloudbreak.cloud.azure.view.AzureCredentialView) CloudConnectorException(com.sequenceiq.cloudbreak.cloud.exception.CloudConnectorException) CloudContext(com.sequenceiq.cloudbreak.cloud.context.CloudContext) CloudResource(com.sequenceiq.cloudbreak.cloud.model.CloudResource) CloudException(com.microsoft.azure.CloudException) ActionWentFailException(com.sequenceiq.cloudbreak.service.Retry.ActionWentFailException)

Example 78 with CloudResource

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

the class AzureResourceConnector method launch.

@Override
public List<CloudResourceStatus> launch(AuthenticatedContext ac, CloudStack stack, PersistenceNotifier notifier, AdjustmentType adjustmentType, Long threshold) {
    AzureCredentialView azureCredentialView = new AzureCredentialView(ac.getCloudCredential());
    String stackName = azureUtils.getStackName(ac.getCloudContext());
    String resourceGroupName = azureUtils.getResourceGroupName(ac.getCloudContext());
    AzureClient client = ac.getParameter(AzureClient.class);
    AzureStackView azureStackView = getAzureStack(azureCredentialView, ac.getCloudContext(), stack, getNumberOfAvailableIPsInSubnets(client, stack.getNetwork()));
    String customImageId = azureStorage.getCustomImageId(client, ac, stack);
    String template = azureTemplateBuilder.build(stackName, customImageId, azureCredentialView, azureStackView, ac.getCloudContext(), stack);
    String parameters = azureTemplateBuilder.buildParameters(ac.getCloudCredential(), stack.getNetwork(), stack.getImage());
    Boolean encrytionNeeded = azureStorage.isEncrytionNeeded(stack.getParameters());
    try {
        String region = ac.getCloudContext().getLocation().getRegion().value();
        if (AzureUtils.hasUnmanagedDisk(stack)) {
            Map<String, AzureDiskType> storageAccounts = azureStackView.getStorageAccounts();
            for (Entry<String, AzureDiskType> entry : storageAccounts.entrySet()) {
                azureStorage.createStorage(client, entry.getKey(), entry.getValue(), resourceGroupName, region, encrytionNeeded, stack.getTags());
            }
        }
        if (!client.templateDeploymentExists(resourceGroupName, stackName)) {
            Deployment templateDeployment = client.createTemplateDeployment(resourceGroupName, stackName, template, parameters);
            LOGGER.info("created template deployment for launch: {}", templateDeployment.exportTemplate().template());
            if (!azureUtils.isExistingNetwork(stack.getNetwork())) {
                client.collectAndSaveNetworkAndSubnet(resourceGroupName, stackName, notifier, ac.getCloudContext());
            }
        }
    } catch (CloudException e) {
        LOGGER.error("Provisioning error, cloud exception happened: ", e);
        if (e.body() != null && e.body().details() != null) {
            String details = e.body().details().stream().map(CloudError::message).collect(Collectors.joining(", "));
            throw new CloudConnectorException(String.format("Stack provisioning failed, status code %s, error message: %s, details: %s", e.body().code(), e.body().message(), details));
        } else {
            throw new CloudConnectorException(String.format("Stack provisioning failed: '%s', please go to Azure Portal for detailed message", e));
        }
    } catch (Exception e) {
        LOGGER.error("Provisioning error:", e);
        throw new CloudConnectorException(String.format("Error in provisioning stack %s: %s", stackName, e.getMessage()));
    }
    CloudResource cloudResource = new Builder().type(ResourceType.ARM_TEMPLATE).name(stackName).build();
    List<CloudResourceStatus> resources = check(ac, Collections.singletonList(cloudResource));
    LOGGER.debug("Launched resources: {}", resources);
    return resources;
}
Also used : CloudConnectorException(com.sequenceiq.cloudbreak.cloud.exception.CloudConnectorException) Builder(com.sequenceiq.cloudbreak.cloud.model.CloudResource.Builder) Deployment(com.microsoft.azure.management.resources.Deployment) AzureStackView(com.sequenceiq.cloudbreak.cloud.azure.view.AzureStackView) CloudError(com.microsoft.azure.CloudError) CloudException(com.microsoft.azure.CloudException) ActionWentFailException(com.sequenceiq.cloudbreak.service.Retry.ActionWentFailException) CloudConnectorException(com.sequenceiq.cloudbreak.cloud.exception.CloudConnectorException) CloudException(com.microsoft.azure.CloudException) AzureCredentialView(com.sequenceiq.cloudbreak.cloud.azure.view.AzureCredentialView) AzureClient(com.sequenceiq.cloudbreak.cloud.azure.client.AzureClient) CloudResourceStatus(com.sequenceiq.cloudbreak.cloud.model.CloudResourceStatus) CloudResource(com.sequenceiq.cloudbreak.cloud.model.CloudResource)

Example 79 with CloudResource

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

the class AzureSetup method prerequisites.

@Override
public void prerequisites(AuthenticatedContext ac, CloudStack stack, PersistenceNotifier persistenceNotifier) {
    String storageGroup = azureUtils.getResourceGroupName(ac.getCloudContext());
    CloudResource cloudResource = new Builder().type(ResourceType.ARM_TEMPLATE).name(storageGroup).build();
    String region = ac.getCloudContext().getLocation().getRegion().value();
    try {
        AzureClient client = ac.getParameter(AzureClient.class);
        persistenceNotifier.notifyAllocation(cloudResource, ac.getCloudContext());
        if (!client.resourceGroupExists(storageGroup)) {
            client.createResourceGroup(storageGroup, region, stack.getTags(), defaultCostTaggingService.prepareTemplateTagging());
        }
    } catch (Exception ex) {
        throw new CloudConnectorException(ex);
    }
    LOGGER.debug("setup has been executed");
}
Also used : AzureClient(com.sequenceiq.cloudbreak.cloud.azure.client.AzureClient) CloudConnectorException(com.sequenceiq.cloudbreak.cloud.exception.CloudConnectorException) Builder(com.sequenceiq.cloudbreak.cloud.model.CloudResource.Builder) CloudResource(com.sequenceiq.cloudbreak.cloud.model.CloudResource) URISyntaxException(java.net.URISyntaxException) CloudConnectorException(com.sequenceiq.cloudbreak.cloud.exception.CloudConnectorException) StorageException(com.microsoft.azure.storage.StorageException) UnknownHostException(java.net.UnknownHostException) InvalidKeyException(java.security.InvalidKeyException)

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