Search in sources :

Example 6 with CloudConnectorException

use of com.sequenceiq.cloudbreak.cloud.exception.CloudConnectorException in project cloudbreak by hortonworks.

the class AwsStackValidator method validate.

@Override
public void validate(AuthenticatedContext ac, CloudStack cloudStack) {
    AwsCredentialView credentialView = new AwsCredentialView(ac.getCloudCredential());
    String regionName = ac.getCloudContext().getLocation().getRegion().value();
    AmazonCloudFormationClient cfClient = awsClient.createCloudFormationClient(credentialView, regionName);
    String cFStackName = cfStackUtil.getCfStackName(ac);
    try {
        cfClient.describeStacks(new DescribeStacksRequest().withStackName(cFStackName));
        throw new CloudConnectorException(String.format("Stack is already exists with the given name: %s", cFStackName));
    } catch (AmazonServiceException ignored) {
    }
}
Also used : AwsCredentialView(com.sequenceiq.cloudbreak.cloud.aws.view.AwsCredentialView) DescribeStacksRequest(com.amazonaws.services.cloudformation.model.DescribeStacksRequest) CloudConnectorException(com.sequenceiq.cloudbreak.cloud.exception.CloudConnectorException) AmazonServiceException(com.amazonaws.AmazonServiceException) AmazonCloudFormationClient(com.amazonaws.services.cloudformation.AmazonCloudFormationClient)

Example 7 with CloudConnectorException

use of com.sequenceiq.cloudbreak.cloud.exception.CloudConnectorException in project cloudbreak by hortonworks.

the class AzureMetadataCollector method collect.

@Override
public List<CloudVmMetaDataStatus> collect(AuthenticatedContext authenticatedContext, List<CloudResource> resources, List<CloudInstance> vms) {
    CloudResource resource = azureUtils.getTemplateResource(resources);
    List<CloudVmMetaDataStatus> results = new ArrayList<>();
    List<InstanceTemplate> templates = Lists.transform(vms, CloudInstance::getTemplate);
    String resourceName = resource.getName();
    Map<String, InstanceTemplate> templateMap = Maps.uniqueIndex(templates, from -> azureUtils.getPrivateInstanceId(resourceName, from.getGroupName(), Long.toString(from.getPrivateId())));
    try {
        for (Entry<String, InstanceTemplate> instance : templateMap.entrySet()) {
            AzureClient azureClient = authenticatedContext.getParameter(AzureClient.class);
            VirtualMachine vm = azureClient.getVirtualMachine(resourceName, instance.getKey());
            String subnetId = vm.getPrimaryNetworkInterface().primaryIPConfiguration().subnetName();
            String privateIp = null;
            String publicIp = null;
            Integer faultDomainCount = azureClient.getFaultDomainNumber(resourceName, vm.name());
            String platform = authenticatedContext.getCloudContext().getPlatform().value();
            String location = authenticatedContext.getCloudContext().getLocation().getRegion().value();
            String hostgroupNm = instance.getValue().getGroupName();
            StringBuilder localityIndicatorBuilder = new StringBuilder();
            localityIndicatorBuilder.append(LOCALITY_SEPARATOR).append(platform).append(LOCALITY_SEPARATOR).append(location).append(LOCALITY_SEPARATOR).append(resourceName).append(LOCALITY_SEPARATOR).append(hostgroupNm).append(LOCALITY_SEPARATOR).append(faultDomainCount);
            AzureUtils.removeBlankSpace(localityIndicatorBuilder);
            List<String> networkInterfaceIdList = vm.networkInterfaceIds();
            for (String networkInterfaceId : networkInterfaceIdList) {
                NetworkInterface networkInterface = azureClient.getNetworkInterfaceById(networkInterfaceId);
                privateIp = networkInterface.primaryPrivateIP();
                PublicIPAddress publicIpAddress = networkInterface.primaryIPConfiguration().getPublicIPAddress();
                List<LoadBalancerBackend> backends = networkInterface.primaryIPConfiguration().listAssociatedLoadBalancerBackends();
                List<LoadBalancerInboundNatRule> inboundNatRules = networkInterface.primaryIPConfiguration().listAssociatedLoadBalancerInboundNatRules();
                if (!backends.isEmpty() || !inboundNatRules.isEmpty()) {
                    publicIp = azureClient.getLoadBalancerIps(resource.getName(), azureUtils.getLoadBalancerId(resource.getName())).get(0);
                }
                if (publicIpAddress != null && publicIpAddress.ipAddress() != null) {
                    publicIp = publicIpAddress.ipAddress();
                }
            }
            String instanceId = instance.getKey();
            CloudInstanceMetaData md = new CloudInstanceMetaData(privateIp, publicIp, faultDomainCount == null ? null : localityIndicatorBuilder.toString());
            InstanceTemplate template = templateMap.get(instanceId);
            if (template != null) {
                Map<String, Object> params = new HashMap<>(1);
                params.put(CloudInstance.SUBNET_ID, subnetId);
                // TODO use shhkey here
                CloudInstance cloudInstance = new CloudInstance(instanceId, template, null, params);
                CloudVmInstanceStatus status = new CloudVmInstanceStatus(cloudInstance, InstanceStatus.CREATED);
                results.add(new CloudVmMetaDataStatus(status, md));
            }
        }
    } catch (RuntimeException e) {
        throw new CloudConnectorException(e.getMessage(), e);
    }
    return results;
}
Also used : HashMap(java.util.HashMap) CloudVmInstanceStatus(com.sequenceiq.cloudbreak.cloud.model.CloudVmInstanceStatus) ArrayList(java.util.ArrayList) PublicIPAddress(com.microsoft.azure.management.network.PublicIPAddress) LoadBalancerInboundNatRule(com.microsoft.azure.management.network.LoadBalancerInboundNatRule) CloudConnectorException(com.sequenceiq.cloudbreak.cloud.exception.CloudConnectorException) CloudInstance(com.sequenceiq.cloudbreak.cloud.model.CloudInstance) NetworkInterface(com.microsoft.azure.management.network.NetworkInterface) LoadBalancerBackend(com.microsoft.azure.management.network.LoadBalancerBackend) AzureClient(com.sequenceiq.cloudbreak.cloud.azure.client.AzureClient) CloudInstanceMetaData(com.sequenceiq.cloudbreak.cloud.model.CloudInstanceMetaData) CloudVmMetaDataStatus(com.sequenceiq.cloudbreak.cloud.model.CloudVmMetaDataStatus) CloudResource(com.sequenceiq.cloudbreak.cloud.model.CloudResource) InstanceTemplate(com.sequenceiq.cloudbreak.cloud.model.InstanceTemplate) VirtualMachine(com.microsoft.azure.management.compute.VirtualMachine)

Example 8 with CloudConnectorException

use of com.sequenceiq.cloudbreak.cloud.exception.CloudConnectorException in project cloudbreak by hortonworks.

the class AzureResourceConnector method upscale.

@Override
public List<CloudResourceStatus> upscale(AuthenticatedContext authenticatedContext, CloudStack stack, List<CloudResource> resources) {
    AzureClient client = authenticatedContext.getParameter(AzureClient.class);
    AzureCredentialView azureCredentialView = new AzureCredentialView(authenticatedContext.getCloudCredential());
    String stackName = azureUtils.getStackName(authenticatedContext.getCloudContext());
    AzureStackView azureStackView = getAzureStack(azureCredentialView, authenticatedContext.getCloudContext(), stack, getNumberOfAvailableIPsInSubnets(client, stack.getNetwork()));
    String customImageId = azureStorage.getCustomImageId(client, authenticatedContext, stack);
    String template = azureTemplateBuilder.build(stackName, customImageId, azureCredentialView, azureStackView, authenticatedContext.getCloudContext(), stack);
    String parameters = azureTemplateBuilder.buildParameters(authenticatedContext.getCloudCredential(), stack.getNetwork(), stack.getImage());
    String resourceGroupName = azureUtils.getResourceGroupName(authenticatedContext.getCloudContext());
    try {
        String region = authenticatedContext.getCloudContext().getLocation().getRegion().value();
        Map<String, AzureDiskType> storageAccounts = azureStackView.getStorageAccounts();
        for (Entry<String, AzureDiskType> entry : storageAccounts.entrySet()) {
            azureStorage.createStorage(client, entry.getKey(), entry.getValue(), resourceGroupName, region, azureStorage.isEncrytionNeeded(stack.getParameters()), stack.getTags());
        }
        Deployment templateDeployment = client.createTemplateDeployment(stackName, stackName, template, parameters);
        LOGGER.info("created template deployment for upscale: {}", templateDeployment.exportTemplate().template());
        CloudResource armTemplate = resources.stream().filter(r -> r.getType() == ResourceType.ARM_TEMPLATE).findFirst().orElseThrow(() -> new CloudConnectorException(String.format("Arm Template not found for: %s  ", stackName)));
        return Collections.singletonList(new CloudResourceStatus(armTemplate, ResourceStatus.IN_PROGRESS));
    } catch (CloudException e) {
        LOGGER.error("Upscale 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 upscale failed, status code %s, error message: %s, details: %s", e.body().code(), e.body().message(), details));
        } else {
            throw new CloudConnectorException(String.format("Stack upscale failed: '%s', please go to Azure Portal for detailed message", e));
        }
    } catch (Exception e) {
        throw new CloudConnectorException(String.format("Could not upscale: %s  ", stackName), e);
    }
}
Also used : CloudConnectorException(com.sequenceiq.cloudbreak.cloud.exception.CloudConnectorException) 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) AzureClient(com.sequenceiq.cloudbreak.cloud.azure.client.AzureClient) AzureCredentialView(com.sequenceiq.cloudbreak.cloud.azure.view.AzureCredentialView) CloudResourceStatus(com.sequenceiq.cloudbreak.cloud.model.CloudResourceStatus) CloudResource(com.sequenceiq.cloudbreak.cloud.model.CloudResource)

Example 9 with CloudConnectorException

use of com.sequenceiq.cloudbreak.cloud.exception.CloudConnectorException in project cloudbreak by hortonworks.

the class AzureResourceConnector method collectInstanceResourcesToRemove.

private Map<String, Object> collectInstanceResourcesToRemove(AuthenticatedContext ac, CloudStack stack, AzureClient client, AzureCredentialView azureCredentialView, String stackName, CloudInstance instance) {
    String instanceId = instance.getInstanceId();
    Long privateId = instance.getTemplate().getPrivateId();
    AzureDiskType azureDiskType = AzureDiskType.getByValue(instance.getTemplate().getVolumeType());
    String attachedDiskStorageName = azureStorage.getAttachedDiskStorageName(azureStorage.getArmAttachedStorageOption(stack.getParameters()), azureCredentialView, privateId, ac.getCloudContext(), azureDiskType);
    Map<String, Object> resourcesToRemove = new HashMap<>();
    resourcesToRemove.put(ATTACHED_DISK_STORAGE_NAME, attachedDiskStorageName);
    try {
        VirtualMachine virtualMachine = client.getVirtualMachine(stackName, instanceId);
        List<String> networkInterfaceIds = virtualMachine.networkInterfaceIds();
        Collection<String> networkInterfacesNames = new ArrayList<>();
        Collection<String> publicIpAddressNames = new ArrayList<>();
        for (String interfaceId : networkInterfaceIds) {
            NetworkInterface networkInterface = client.getNetworkInterfaceById(interfaceId);
            String interfaceName = networkInterface.name();
            networkInterfacesNames.add(interfaceName);
            Collection<String> ipNames = new HashSet<>();
            for (NicIPConfiguration ipConfiguration : networkInterface.ipConfigurations().values()) {
                if (ipConfiguration.publicIPAddressId() != null && ipConfiguration.getPublicIPAddress().name() != null) {
                    ipNames.add(ipConfiguration.getPublicIPAddress().name());
                }
            }
            publicIpAddressNames.addAll(ipNames);
        }
        resourcesToRemove.put(NETWORK_INTERFACES_NAMES, networkInterfacesNames);
        resourcesToRemove.put(PUBLIC_ADDRESS_NAME, publicIpAddressNames);
        collectRemovableDisks(resourcesToRemove, virtualMachine);
    } catch (CloudException e) {
        if (e.response().code() != AzureConstants.NOT_FOUND) {
            throw new CloudConnectorException(e.body().message(), e);
        }
    } catch (RuntimeException e) {
        throw new CloudConnectorException("can't collect instance resources", e);
    }
    return resourcesToRemove;
}
Also used : HashMap(java.util.HashMap) CloudConnectorException(com.sequenceiq.cloudbreak.cloud.exception.CloudConnectorException) ArrayList(java.util.ArrayList) NetworkInterface(com.microsoft.azure.management.network.NetworkInterface) CloudException(com.microsoft.azure.CloudException) NicIPConfiguration(com.microsoft.azure.management.network.NicIPConfiguration) VirtualMachine(com.microsoft.azure.management.compute.VirtualMachine) HashSet(java.util.HashSet)

Example 10 with CloudConnectorException

use of com.sequenceiq.cloudbreak.cloud.exception.CloudConnectorException in project cloudbreak by hortonworks.

the class AzureResourceConnector method check.

@Override
public List<CloudResourceStatus> check(AuthenticatedContext authenticatedContext, List<CloudResource> resources) {
    List<CloudResourceStatus> result = new ArrayList<>();
    AzureClient client = authenticatedContext.getParameter(AzureClient.class);
    String stackName = azureUtils.getStackName(authenticatedContext.getCloudContext());
    for (CloudResource resource : resources) {
        switch(resource.getType()) {
            case ARM_TEMPLATE:
                LOGGER.info("Checking Azure group stack status of: {}", stackName);
                try {
                    CloudResourceStatus templateResourceStatus;
                    if (client.templateDeploymentExists(stackName, stackName)) {
                        Deployment resourceGroupDeployment = client.getTemplateDeployment(stackName, stackName);
                        templateResourceStatus = azureUtils.getTemplateStatus(resource, resourceGroupDeployment, client, stackName);
                    } else {
                        templateResourceStatus = new CloudResourceStatus(resource, ResourceStatus.DELETED);
                    }
                    result.add(templateResourceStatus);
                } catch (CloudException e) {
                    if (e.response().code() == AzureConstants.NOT_FOUND) {
                        result.add(new CloudResourceStatus(resource, ResourceStatus.DELETED));
                    } else {
                        throw new CloudConnectorException(e.body().message(), e);
                    }
                } catch (RuntimeException e) {
                    throw new CloudConnectorException(String.format("Invalid resource exception: %s", e.getMessage()), e);
                }
                break;
            case AZURE_NETWORK:
            case AZURE_SUBNET:
                break;
            default:
                throw new CloudConnectorException(String.format("Invalid resource type: %s", resource.getType()));
        }
    }
    return result;
}
Also used : AzureClient(com.sequenceiq.cloudbreak.cloud.azure.client.AzureClient) CloudConnectorException(com.sequenceiq.cloudbreak.cloud.exception.CloudConnectorException) CloudResourceStatus(com.sequenceiq.cloudbreak.cloud.model.CloudResourceStatus) ArrayList(java.util.ArrayList) Deployment(com.microsoft.azure.management.resources.Deployment) CloudResource(com.sequenceiq.cloudbreak.cloud.model.CloudResource) CloudException(com.microsoft.azure.CloudException)

Aggregations

CloudConnectorException (com.sequenceiq.cloudbreak.cloud.exception.CloudConnectorException)64 CloudResource (com.sequenceiq.cloudbreak.cloud.model.CloudResource)14 StorageException (com.microsoft.azure.storage.StorageException)11 AwsCredentialView (com.sequenceiq.cloudbreak.cloud.aws.view.AwsCredentialView)9 AzureClient (com.sequenceiq.cloudbreak.cloud.azure.client.AzureClient)9 AmazonServiceException (com.amazonaws.AmazonServiceException)8 AmazonEC2Client (com.amazonaws.services.ec2.AmazonEC2Client)8 CloudBlobContainer (com.microsoft.azure.storage.blob.CloudBlobContainer)8 ArrayList (java.util.ArrayList)8 IOException (java.io.IOException)7 URISyntaxException (java.net.URISyntaxException)7 ActionWentFailException (com.sequenceiq.cloudbreak.service.Retry.ActionWentFailException)6 HashMap (java.util.HashMap)6 CloudException (com.microsoft.azure.CloudException)5 CloudResourceStatus (com.sequenceiq.cloudbreak.cloud.model.CloudResourceStatus)5 AmazonCloudFormationClient (com.amazonaws.services.cloudformation.AmazonCloudFormationClient)4 DescribeStacksRequest (com.amazonaws.services.cloudformation.model.DescribeStacksRequest)3 CloudInstance (com.sequenceiq.cloudbreak.cloud.model.CloudInstance)3 Builder (com.sequenceiq.cloudbreak.cloud.model.CloudResource.Builder)3 Group (com.sequenceiq.cloudbreak.cloud.model.Group)3