use of com.microsoft.azure.CloudException in project photon-model by vmware.
the class AzureInstanceService method handleCloudError.
/**
* This method tries to detect specific CloudErrors by their code and apply some additional
* handling. In case a subscription registration error is detected the method register
* subscription for given namespace. In case invalid parameter error is detected, the error
* message is made better human-readable. Otherwise the fallback is to transition to error state
* through next specific error handler.
*/
private void handleCloudError(String msg, AzureInstanceContext ctx, String namespace, Throwable e) {
if (e instanceof CloudException) {
CloudException ce = (CloudException) e;
CloudError body = ce.body();
if (body != null) {
String code = body.code();
if (MISSING_SUBSCRIPTION_CODE.equals(code)) {
registerSubscription(ctx, namespace);
return;
} else if (INVALID_PARAMETER.equals(code) || INVALID_RESOURCE_GROUP.equals(code)) {
String invalidParameterMsg = String.format("%s Invalid parameter. %s", msg, body.message());
e = new IllegalStateException(invalidParameterMsg, ctx.error);
handleError(ctx, e);
return;
}
}
}
handleError(ctx, e);
}
use of com.microsoft.azure.CloudException 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);
}
}
use of com.microsoft.azure.CloudException 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;
}
use of com.microsoft.azure.CloudException 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;
}
use of com.microsoft.azure.CloudException in project autorest.java by Azure.
the class LROSADsTests method putNonRetry201Creating400.
@Test
public void putNonRetry201Creating400() throws Exception {
ProductInner product = new ProductInner();
product.withLocation("West US");
try {
client.lROSADs().putNonRetry201Creating400(product);
fail();
} catch (CloudException ex) {
Assert.assertEquals(400, ex.response().code());
}
}
Aggregations