use of com.sequenceiq.cloudbreak.cloud.azure.context.AzureContext in project cloudbreak by hortonworks.
the class AzureComputeResourceService method initContext.
private AzureContext initContext(AuthenticatedContext ac, CloudStack cloudStack) {
AzureContext context = contextBuilder.contextInit(ac.getCloudContext(), ac, cloudStack.getNetwork(), null, true);
String stackCrn = cloudStack.getParameters().getOrDefault(PlatformParametersConsts.RESOURCE_CRN_PARAMETER, "");
if (stackCrn.isEmpty()) {
LOGGER.warn("Stack crn is not set in CloudStack, it can cause errors during infrastructure creation!");
}
context.putParameter(PlatformParametersConsts.RESOURCE_CRN_PARAMETER, cloudStack.getParameters().getOrDefault(PlatformParametersConsts.RESOURCE_CRN_PARAMETER, ""));
return context;
}
use of com.sequenceiq.cloudbreak.cloud.azure.context.AzureContext in project cloudbreak by hortonworks.
the class AzureAttachmentResourceBuilder method build.
@Override
public List<CloudResource> build(AzureContext context, CloudInstance instance, long privateId, AuthenticatedContext auth, Group group, List<CloudResource> buildableResource, CloudStack cloudStack) {
CloudResource cloudResourceInstance = buildableResource.stream().filter(cloudResource -> cloudResource.getType().equals(ResourceType.AZURE_INSTANCE)).findFirst().orElseThrow(() -> new AzureResourceException("Instance resource not found"));
LOGGER.info("Attach disk to the instance {}", cloudResourceInstance);
CloudContext cloudContext = auth.getCloudContext();
String resourceGroupName = azureResourceGroupMetadataProvider.getResourceGroupName(cloudContext, cloudStack);
AzureClient client = getAzureClient(auth);
VirtualMachine vm = client.getVirtualMachineByResourceGroup(resourceGroupName, cloudResourceInstance.getName());
Set<String> diskIds = vm.dataDisks().values().stream().map(VirtualMachineDataDisk::id).collect(Collectors.toSet());
CloudResource volumeSet = buildableResource.stream().filter(cloudResource -> cloudResource.getType().equals(ResourceType.AZURE_VOLUMESET)).filter(cloudResource -> !cloudResourceInstance.getInstanceId().equals(cloudResource.getInstanceId())).findFirst().orElseThrow(() -> new AzureResourceException("Volume set resource not found"));
VolumeSetAttributes volumeSetAttributes = getVolumeSetAttributes(volumeSet);
volumeSetAttributes.getVolumes().forEach(volume -> {
Disk disk = client.getDiskById(volume.getId());
if (!diskIds.contains(disk.id())) {
if (disk.isAttachedToVirtualMachine()) {
detachDiskFromVmByVmId(client, disk);
}
attachDiskToVm(client, disk, vm);
} else {
LOGGER.info("Managed disk {} is already attached to VM {}", disk, vm);
}
});
volumeSet.setInstanceId(cloudResourceInstance.getInstanceId());
volumeSet.setStatus(CommonStatus.CREATED);
LOGGER.info("Volume set {} attached successfully", volumeSet);
return List.of(volumeSet);
}
use of com.sequenceiq.cloudbreak.cloud.azure.context.AzureContext in project cloudbreak by hortonworks.
the class AzureVolumeResourceBuilder method build.
@Override
public List<CloudResource> build(AzureContext context, CloudInstance instance, long privateId, AuthenticatedContext auth, Group group, List<CloudResource> buildableResource, CloudStack cloudStack) throws Exception {
LOGGER.info("Create volumes on provider");
AzureClient client = getAzureClient(auth);
Map<String, List<VolumeSetAttributes.Volume>> volumeSetMap = Collections.synchronizedMap(new HashMap<>());
List<Future<?>> futures = new ArrayList<>();
List<CloudResource> requestedResources = buildableResource.stream().filter(cloudResource -> CommonStatus.REQUESTED.equals(cloudResource.getStatus())).collect(toList());
CloudContext cloudContext = auth.getCloudContext();
String resourceGroupName = azureResourceGroupMetadataProvider.getResourceGroupName(cloudContext, cloudStack);
String region = cloudContext.getLocation().getRegion().getRegionName();
String diskEncryptionSetId = getDiskEncryptionSetId(group);
for (CloudResource resource : requestedResources) {
volumeSetMap.put(resource.getName(), Collections.synchronizedList(new ArrayList<>()));
VolumeSetAttributes volumeSet = getVolumeSetAttributes(resource);
DeviceNameGenerator generator = new DeviceNameGenerator(DEVICE_NAME_TEMPLATE, 1);
futures.addAll(volumeSet.getVolumes().stream().map(volume -> intermediateBuilderExecutor.submit(() -> {
Disk result = client.getDiskByName(resourceGroupName, volume.getId());
if (result == null) {
result = client.createManagedDisk(volume.getId(), volume.getSize(), AzureDiskType.getByValue(volume.getType()), region, resourceGroupName, cloudStack.getTags(), diskEncryptionSetId);
} else {
LOGGER.info("Managed disk for resource group: {}, name: {} already exists: {}", resourceGroupName, volume.getId(), result);
}
String volumeId = result.id();
volumeSetMap.get(resource.getName()).add(new VolumeSetAttributes.Volume(volumeId, generator.next(), volume.getSize(), volume.getType(), volume.getCloudVolumeUsageType()));
})).collect(toList()));
}
for (Future<?> future : futures) {
future.get();
}
return buildableResource.stream().peek(resource -> {
List<VolumeSetAttributes.Volume> volumes = volumeSetMap.get(resource.getName());
if (!CollectionUtils.isEmpty(volumes)) {
getVolumeSetAttributes(resource).setVolumes(volumes);
}
}).map(copyResourceWithNewStatus(CommonStatus.CREATED)).collect(toList());
}
use of com.sequenceiq.cloudbreak.cloud.azure.context.AzureContext in project cloudbreak by hortonworks.
the class AzureVolumeResourceBuilder method checkResources.
@Override
protected List<CloudResourceStatus> checkResources(ResourceType type, AzureContext context, AuthenticatedContext auth, Iterable<CloudResource> resources) {
AzureClient client = getAzureClient(auth);
List<CloudResource> volumeResources = StreamSupport.stream(resources.spliterator(), false).filter(r -> r.getType().equals(resourceType())).collect(toList());
CloudResource resourceGroup = context.getNetworkResources().stream().filter(r -> r.getType().equals(ResourceType.AZURE_RESOURCE_GROUP)).findFirst().orElseThrow(() -> new AzureResourceException("Resource group resource not found"));
String resourceGroupName = resourceGroup.getName();
PagedList<Disk> existingDisks = client.listDisksByResourceGroup(resourceGroupName);
ResourceStatus volumeSetStatus = getResourceStatus(existingDisks, volumeResources);
return volumeResources.stream().map(resource -> new CloudResourceStatus(resource, volumeSetStatus)).collect(toList());
}
use of com.sequenceiq.cloudbreak.cloud.azure.context.AzureContext in project cloudbreak by hortonworks.
the class AzureVolumeResourceBuilder method create.
@Override
public List<CloudResource> create(AzureContext context, CloudInstance instance, long privateId, AuthenticatedContext auth, Group group, Image image) {
LOGGER.info("Creating volume resources");
List<CloudResource> computeResources = context.getComputeResources(privateId);
if (Objects.isNull(computeResources) || computeResources.isEmpty()) {
return null;
}
CloudResource vm = context.getComputeResources(privateId).stream().filter(cloudResource -> ResourceType.AZURE_INSTANCE.equals(cloudResource.getType())).findFirst().get();
Optional<CloudResource> reattachableVolumeSet = computeResources.stream().filter(resource -> ResourceType.AZURE_VOLUMESET.equals(resource.getType())).filter(cloudResource -> CommonStatus.DETACHED.equals(cloudResource.getStatus()) || vm.getInstanceId().equals(cloudResource.getInstanceId())).findFirst();
LOGGER.info("Reattachable volume set {}", reattachableVolumeSet.map(cloudResource -> "is present with name:" + cloudResource.getName()).orElse("is not present"));
return List.of(reattachableVolumeSet.orElseGet(createVolumeSet(privateId, auth, group, vm, context.getStringParameter(PlatformParametersConsts.RESOURCE_CRN_PARAMETER))));
}
Aggregations