Search in sources :

Example 31 with CloudException

use of com.microsoft.azure.CloudException in project photon-model by vmware.

the class AzureTestUtil method createAzureVirtualNetwork.

private static void createAzureVirtualNetwork(String resourceGroupName, AzureNicSpecs azureNicSpecs, NetworkManagementClientImpl networkManagementClient) throws Exception {
    try {
        VirtualNetworkInner vNet = new VirtualNetworkInner();
        vNet.withLocation(azureNicSpecs.network.zoneId);
        AddressSpace addressSpace = new AddressSpace();
        // Azure's custom serializers don't handle well collections constructed with
        // Collections.singletonList(), so initialize an ArrayList
        List<String> cidrs = new ArrayList<>();
        cidrs.add(azureNicSpecs.network.cidr);
        addressSpace.withAddressPrefixes(cidrs);
        vNet.withAddressSpace(addressSpace);
        List<SubnetInner> subnetList = new ArrayList<>();
        for (int i = 0; i < azureNicSpecs.nicSpecs.size(); i++) {
            SubnetInner subnet = new SubnetInner();
            subnet.withName(azureNicSpecs.nicSpecs.get(i).subnetSpec.name);
            subnet.withAddressPrefix(azureNicSpecs.nicSpecs.get(i).subnetSpec.cidr);
            subnetList.add(subnet);
        }
        vNet.withSubnets(subnetList);
        networkManagementClient.virtualNetworks().createOrUpdate(resourceGroupName, azureNicSpecs.network.name, vNet);
        addAzureGatewayToVirtualNetwork(resourceGroupName, azureNicSpecs, networkManagementClient);
    } catch (CloudException ex) {
        /*
             * CloudException is thrown if the vNet already exists and we are trying to do an
             * update, because there are objects (GatewaySubnet) attached to it
             */
        assertTrue(ex.body().code().equals("InUseSubnetCannotBeDeleted"));
    }
}
Also used : VirtualNetworkInner(com.microsoft.azure.management.network.implementation.VirtualNetworkInner) AddressSpace(com.microsoft.azure.management.network.AddressSpace) SubnetInner(com.microsoft.azure.management.network.implementation.SubnetInner) ArrayList(java.util.ArrayList) CloudException(com.microsoft.azure.CloudException)

Example 32 with CloudException

use of com.microsoft.azure.CloudException in project photon-model by vmware.

the class AzureDeferredResultServiceCallback method consumeError.

/**
 * Hook that might be implemented by descendants to handle failed Azure call.
 *
 * <p>
 * Default implementation should be good enough for most of the cases. It provides a list of 1)
 * exceptions to recover from and 2) customizable CloudError handlers.
 *
 * @see #cloudErrorHandlers
 * @see #addRecoverFromCloudError(String)
 */
protected Throwable consumeError(Throwable exc) {
    if (!(exc instanceof CloudException)) {
        return exc;
    }
    final CloudError cloudError = ((CloudException) exc).body();
    if (cloudError == null) {
        return exc;
    }
    BiFunction<Throwable, CloudError, Throwable> cloudErrorHandler = this.cloudErrorHandlers.getOrDefault(// Lookup handler by code
    cloudError.code(), // Fall back to the default 're-throw' exc handler
    (originalExc, cloudErr) -> originalExc);
    exc = cloudErrorHandler.apply(exc, cloudError);
    return exc;
}
Also used : CloudError(com.microsoft.azure.CloudError) CloudException(com.microsoft.azure.CloudException)

Example 33 with CloudException

use of com.microsoft.azure.CloudException in project cloudbreak by hortonworks.

the class AzureInstanceConnector method check.

@Override
public List<CloudVmInstanceStatus> check(AuthenticatedContext ac, List<CloudInstance> vms) {
    List<CloudVmInstanceStatus> statuses = new ArrayList<>();
    String stackName = armTemplateUtils.getStackName(ac.getCloudContext());
    for (CloudInstance vm : vms) {
        try {
            AzureClient azureClient = ac.getParameter(AzureClient.class);
            boolean virtualMachineExists = azureClient.isVirtualMachineExists(stackName, vm.getInstanceId());
            if (virtualMachineExists) {
                PowerState virtualMachinePowerState = azureClient.getVirtualMachinePowerState(stackName, vm.getInstanceId());
                statuses.add(new CloudVmInstanceStatus(vm, AzureInstanceStatus.get(virtualMachinePowerState)));
            } else {
                statuses.add(new CloudVmInstanceStatus(vm, InstanceStatus.TERMINATED));
            }
        } catch (CloudException e) {
            if (e.body() != null && "ResourceNotFound".equals(e.body().code())) {
                statuses.add(new CloudVmInstanceStatus(vm, InstanceStatus.TERMINATED));
            } else {
                statuses.add(new CloudVmInstanceStatus(vm, InstanceStatus.UNKNOWN));
            }
        } catch (RuntimeException ignored) {
            statuses.add(new CloudVmInstanceStatus(vm, InstanceStatus.UNKNOWN));
        }
    }
    return statuses;
}
Also used : AzureClient(com.sequenceiq.cloudbreak.cloud.azure.client.AzureClient) CloudVmInstanceStatus(com.sequenceiq.cloudbreak.cloud.model.CloudVmInstanceStatus) ArrayList(java.util.ArrayList) CloudInstance(com.sequenceiq.cloudbreak.cloud.model.CloudInstance) CloudException(com.microsoft.azure.CloudException) PowerState(com.microsoft.azure.management.compute.PowerState)

Example 34 with CloudException

use of com.microsoft.azure.CloudException 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 35 with CloudException

use of com.microsoft.azure.CloudException 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)

Aggregations

CloudException (com.microsoft.azure.CloudException)35 Test (org.junit.Test)20 ProductInner (fixtures.lro.implementation.ProductInner)17 SubProductInner (fixtures.lro.implementation.SubProductInner)6 ArrayList (java.util.ArrayList)6 CloudError (com.microsoft.azure.CloudError)5 AzureClient (com.sequenceiq.cloudbreak.cloud.azure.client.AzureClient)5 CloudConnectorException (com.sequenceiq.cloudbreak.cloud.exception.CloudConnectorException)5 HashMap (java.util.HashMap)5 CloudResource (com.sequenceiq.cloudbreak.cloud.model.CloudResource)4 AzureCredentialView (com.sequenceiq.cloudbreak.cloud.azure.view.AzureCredentialView)3 ActionWentFailException (com.sequenceiq.cloudbreak.service.Retry.ActionWentFailException)3 VirtualMachine (com.microsoft.azure.management.compute.VirtualMachine)2 AddressSpace (com.microsoft.azure.management.network.AddressSpace)2 NetworkInterface (com.microsoft.azure.management.network.NetworkInterface)2 SubnetInner (com.microsoft.azure.management.network.implementation.SubnetInner)2 VirtualNetworkInner (com.microsoft.azure.management.network.implementation.VirtualNetworkInner)2 Deployment (com.microsoft.azure.management.resources.Deployment)2 ResourceGroup (com.microsoft.azure.management.resources.ResourceGroup)2 Region (com.microsoft.azure.management.resources.fluentcore.arm.Region)2