use of com.sequenceiq.cloudbreak.cloud.azure.view.AzureCredentialView in project cloudbreak by hortonworks.
the class AzureTemplateBuilderTest method buildWithPublicIpAndFirewall.
@Test
public void buildWithPublicIpAndFirewall() {
// GIVEN
Network network = new Network(new Subnet("testSubnet"));
when(azureUtils.isPrivateIp(any())).then(invocation -> false);
when(azureUtils.isNoSecurityGroups(any())).then(invocation -> false);
when(defaultCostTaggingService.prepareAllTagsForTemplate()).thenReturn(defaultTags);
Map<String, String> parameters = new HashMap<>();
parameters.put("persistentStorage", "persistentStorageTest");
parameters.put("attachedStorageOption", "attachedStorageOptionTest");
InstanceAuthentication instanceAuthentication = new InstanceAuthentication("sshkey", "", "cloudbreak");
groups.add(new Group(name, InstanceGroupType.CORE, Collections.singletonList(instance), security, null, instanceAuthentication, instanceAuthentication.getLoginUserName(), instanceAuthentication.getPublicKey()));
cloudStack = new CloudStack(groups, network, image, parameters, tags, azureTemplateBuilder.getTemplateString(), instanceAuthentication, instanceAuthentication.getLoginUserName(), instanceAuthentication.getPublicKey());
azureStackView = new AzureStackView("mystack", 3, groups, azureStorageView, azureSubnetStrategy);
// WHEN
when(azureStorage.getImageStorageName(any(AzureCredentialView.class), any(CloudContext.class), any(CloudStack.class))).thenReturn("test");
when(azureStorage.getDiskContainerName(any(CloudContext.class))).thenReturn("testStorageContainer");
String templateString = azureTemplateBuilder.build(stackName, CUSTOM_IMAGE_NAME, azureCredentialView, azureStackView, cloudContext, cloudStack);
// THEN
gson.fromJson(templateString, Map.class);
assertThat(templateString, containsString("publicIPAddress"));
assertThat(templateString, containsString("networkSecurityGroups"));
}
use of com.sequenceiq.cloudbreak.cloud.azure.view.AzureCredentialView 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);
}
use of com.sequenceiq.cloudbreak.cloud.azure.view.AzureCredentialView 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;
}
use of com.sequenceiq.cloudbreak.cloud.azure.view.AzureCredentialView in project cloudbreak by hortonworks.
the class AzureSetup method copyVhdImageIfNecessary.
private void copyVhdImageIfNecessary(AuthenticatedContext ac, CloudStack stack, Image image, String imageResourceGroupName, String region, AzureClient client) {
AzureCredentialView acv = new AzureCredentialView(ac.getCloudCredential());
String imageStorageName = armStorage.getImageStorageName(acv, ac.getCloudContext(), stack);
String resourceGroupName = azureUtils.getResourceGroupName(ac.getCloudContext());
if (!client.resourceGroupExists(resourceGroupName)) {
client.createResourceGroup(resourceGroupName, region, stack.getTags(), defaultCostTaggingService.prepareTemplateTagging());
}
if (!client.resourceGroupExists(imageResourceGroupName)) {
client.createResourceGroup(imageResourceGroupName, region, stack.getTags(), defaultCostTaggingService.prepareTemplateTagging());
}
armStorage.createStorage(client, imageStorageName, AzureDiskType.LOCALLY_REDUNDANT, imageResourceGroupName, region, armStorage.isEncrytionNeeded(stack.getParameters()), stack.getTags());
client.createContainerInStorage(imageResourceGroupName, imageStorageName, IMAGES_CONTAINER);
if (!storageContainsImage(client, imageResourceGroupName, imageStorageName, image.getImageName())) {
client.copyImageBlobInStorageContainer(imageResourceGroupName, imageStorageName, IMAGES_CONTAINER, image.getImageName());
}
}
use of com.sequenceiq.cloudbreak.cloud.azure.view.AzureCredentialView in project cloudbreak by hortonworks.
the class AzureSetup method checkImageStatus.
@Override
public ImageStatusResult checkImageStatus(AuthenticatedContext ac, CloudStack stack, Image image) {
String imageResourceGroupName = armStorage.getImageResourceGroupName(ac.getCloudContext(), stack);
AzureClient client = ac.getParameter(AzureClient.class);
AzureCredentialView acv = new AzureCredentialView(ac.getCloudCredential());
String imageStorageName = armStorage.getImageStorageName(acv, ac.getCloudContext(), stack);
try {
CopyState copyState = client.getCopyStatus(imageResourceGroupName, imageStorageName, IMAGES_CONTAINER, image.getImageName());
if (CopyStatus.SUCCESS.equals(copyState.getStatus())) {
if (AzureUtils.hasManagedDisk(stack)) {
String customImageId = armStorage.getCustomImageId(client, ac, stack);
if (customImageId == null) {
return new ImageStatusResult(ImageStatus.CREATE_FAILED, ImageStatusResult.COMPLETED);
}
}
return new ImageStatusResult(ImageStatus.CREATE_FINISHED, ImageStatusResult.COMPLETED);
} else if (CopyStatus.ABORTED.equals(copyState.getStatus()) || CopyStatus.INVALID.equals(copyState.getStatus())) {
return new ImageStatusResult(ImageStatus.CREATE_FAILED, 0);
} else {
int percentage = (int) (((double) copyState.getBytesCopied() * ImageStatusResult.COMPLETED) / copyState.getTotalBytes());
LOGGER.info(String.format("CopyStatus Pending %s byte/%s byte: %.4s %%", copyState.getTotalBytes(), copyState.getBytesCopied(), percentage));
return new ImageStatusResult(ImageStatus.IN_PROGRESS, percentage);
}
} catch (RuntimeException ignored) {
return new ImageStatusResult(ImageStatus.IN_PROGRESS, ImageStatusResult.HALF);
}
}
Aggregations