com.microsoft.azure.management.compute.Disk Java Examples

The following examples show how to use com.microsoft.azure.management.compute.Disk. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: DiskInventoryCollector.java    From pacbot with Apache License 2.0 6 votes vote down vote up
public List<DataDiskVH> fetchDataDiskDetails(SubscriptionVH subscription, Map<String, Map<String, String>> tagMap) {
	List<DataDiskVH> dataDiskList = new ArrayList<DataDiskVH>();
	Azure azure = azureCredentialProvider.getClient(subscription.getTenant(),subscription.getSubscriptionId());
	PagedList<Disk> dataDisks = azure.disks().list();
	
	for (Disk dataDisk : dataDisks) {
		DataDiskVH dataDiskVH = new DataDiskVH();
		dataDiskVH.setId(dataDisk.id());
		dataDiskVH.setIsAttachedToVirtualMachine(dataDisk.isAttachedToVirtualMachine());
		dataDiskVH.setKey(dataDisk.key());
		dataDiskVH.setName(dataDisk.name());
		dataDiskVH.setDiskInner(dataDisk.inner());
		dataDiskVH.setRegion(dataDisk.region().toString());
		dataDiskVH.setResourceGroupName(dataDisk.resourceGroupName());
		dataDiskVH.setSizeInGB(dataDisk.sizeInGB());
		dataDiskVH.setTags(Util.tagsList(tagMap, dataDisk.resourceGroupName(), dataDisk.tags()));
		dataDiskVH.setType(dataDisk.type());
		dataDiskVH.setVirtualMachineId(dataDisk.virtualMachineId());
		dataDiskVH.setSubscription(subscription.getSubscriptionId());
		dataDiskVH.setSubscriptionName(subscription.getSubscriptionName());
		dataDiskList.add(dataDiskVH);
	}
	log.info("Target Type : {}  Total: {} ","Disc",dataDiskList.size());
	return dataDiskList;
}
 
Example #2
Source File: VirtualMachineScaleSetVMImpl.java    From azure-libraries-for-java with MIT License 6 votes vote down vote up
@Override
public Update withExistingDataDisk(Disk dataDisk, int lun, CachingTypes cachingTypes, StorageAccountTypes storageAccountTypes) {
    if (!this.isManagedDiskEnabled()) {
        throw new IllegalStateException(ManagedUnmanagedDiskErrors.VM_BOTH_UNMANAGED_AND_MANAGED_DISK_NOT_ALLOWED);
    }
    if (dataDisk.inner().diskState() != DiskState.UNATTACHED) {
        throw new IllegalStateException("Disk need to be in unattached state");
    }

    DataDisk attachDataDisk = new DataDisk()
            .withCreateOption(DiskCreateOptionTypes.ATTACH)
            .withLun(lun)
            .withCaching(cachingTypes)
            .withManagedDisk((ManagedDiskParameters) new ManagedDiskParameters()
                    .withStorageAccountType(storageAccountTypes)
                    .withId(dataDisk.id()));
    return this.withExistingDataDisk(attachDataDisk, lun);
}
 
Example #3
Source File: AzureVolumeResourceBuilder.java    From cloudbreak with Apache License 2.0 6 votes vote down vote up
@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(Collectors.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(Collectors.toList());
}
 
Example #4
Source File: AzureVolumeResourceBuilder.java    From cloudbreak with Apache License 2.0 6 votes vote down vote up
private ResourceStatus getResourceStatus(PagedList<Disk> existingDisks, List<CloudResource> volumeResources) {

        List<String> expectedIdList = volumeResources.stream()
                .map(this::getVolumeSetAttributes)
                .map(VolumeSetAttributes::getVolumes)
                .flatMap(List::stream)
                .map(VolumeSetAttributes.Volume::getId)
                .collect(Collectors.toList());
        List<String> actualIdList = existingDisks.stream()
                .map(Disk::id)
                .collect(Collectors.toList());
        if (!actualIdList.containsAll(expectedIdList)) {
            return ResourceStatus.DELETED;
        }
        List<String> actualAttachedIdList = existingDisks.stream().
                filter(Disk::isAttachedToVirtualMachine).map(Disk::id)
                .collect(Collectors.toList());
        if (actualAttachedIdList.containsAll(expectedIdList)) {
            return ResourceStatus.ATTACHED;
        }
        return ResourceStatus.CREATED;
    }
 
Example #5
Source File: VirtualMachineImpl.java    From azure-libraries-for-java with MIT License 6 votes vote down vote up
private void setAttachableNewDataDisks(Func0<Integer> nextLun) {
    List<DataDisk> dataDisks = vm.inner().storageProfile().dataDisks();
    for (Map.Entry<String, DataDisk> entry : this.newDisksToAttach.entrySet()) {
        Disk managedDisk = vm.<Disk>taskResult(entry.getKey());
        DataDisk dataDisk = entry.getValue();
        dataDisk.withCreateOption(DiskCreateOptionTypes.ATTACH);
        if (dataDisk.lun() == -1) {
            dataDisk.withLun(nextLun.call());
        }
        dataDisk.withManagedDisk(new ManagedDiskParameters());
        dataDisk.managedDisk().withId(managedDisk.id());
        if (dataDisk.caching() == null) {
            dataDisk.withCaching(getDefaultCachingType());
        }
        // Don't set default storage account type for the attachable managed disks, it is already
        // defined in the managed disk and not allowed to change.
        dataDisk.withName(null);
        dataDisks.add(dataDisk);
    }
}
 
Example #6
Source File: TestVirtualMachine.java    From azure-libraries-for-java with MIT License 5 votes vote down vote up
@Override
public VirtualMachine createResource(VirtualMachines virtualMachines) throws Exception {
    final String vmName = "vm" + this.testId;
    final VirtualMachine[] vms = new VirtualMachine[1];
    final SettableFuture<VirtualMachine> future = SettableFuture.create();

    Observable<Indexable> resourceStream = virtualMachines.define(vmName)
            .withRegion(Region.US_EAST)
            .withNewResourceGroup()
            .withNewPrimaryNetwork("10.0.0.0/28")
            .withPrimaryPrivateIPAddressDynamic()
            .withoutPrimaryPublicIPAddress()
            .withPopularWindowsImage(KnownWindowsVirtualMachineImage.WINDOWS_SERVER_2012_R2_DATACENTER)
            .withAdminUsername("testuser")
            .withAdminPassword("12NewPA$$w0rd!")
            .withNewDataDisk(150)
            .withSize(VirtualMachineSizeTypes.STANDARD_D1_V2)
            .createAsync();

    Utils.<VirtualMachine>rootResource(resourceStream)
            .subscribe(new Action1<VirtualMachine>() {
                @Override
                public void call(VirtualMachine virtualMachine) {
                    future.set(virtualMachine);
                }
            });
    vms[0] = future.get();

    Assert.assertEquals(1, vms[0].dataDisks().size());
    VirtualMachineDataDisk dataDisk = vms[0].dataDisks().values().iterator().next();
    Assert.assertEquals(150, dataDisk.size());
    Assert.assertEquals(128, vms[0].osDiskSize());
    Disk osDisk = virtualMachines.manager().disks().getById(vms[0].osDiskId());
    Assert.assertNotNull(osDisk);
    Assert.assertEquals(128, osDisk.sizeInGB());

    return vms[0];
}
 
Example #7
Source File: SnapshotImpl.java    From azure-libraries-for-java with MIT License 5 votes vote down vote up
@Override
public SnapshotImpl withWindowsFromDisk(Disk sourceDisk) {
    withWindowsFromDisk(sourceDisk.id());
    if (sourceDisk.osType() != null) {
        this.withOSType(sourceDisk.osType());
    }
    this.withSku(sourceDisk.sku());
    return this;
}
 
Example #8
Source File: SnapshotImpl.java    From azure-libraries-for-java with MIT License 5 votes vote down vote up
@Override
public SnapshotImpl withLinuxFromDisk(Disk sourceDisk) {
    withLinuxFromDisk(sourceDisk.id());
    if (sourceDisk.osType() != null) {
        this.withOSType(sourceDisk.osType());
    }
    this.withSku(sourceDisk.sku());
    return this;
}
 
Example #9
Source File: VirtualMachineImpl.java    From azure-libraries-for-java with MIT License 5 votes vote down vote up
@Override
public VirtualMachineImpl withExistingDataDisk(Disk disk, int newSizeInGB, int lun, CachingTypes cachingType) {
    throwIfManagedDiskDisabled(ManagedUnmanagedDiskErrors.VM_BOTH_UNMANAGED_AND_MANAGED_DISK_NOT_ALLOWED);
    ManagedDiskParameters managedDiskParameters = new ManagedDiskParameters();
    managedDiskParameters.withId(disk.id());
    this.managedDataDisks.existingDisksToAttach.add(new DataDisk()
            .withLun(lun)
            .withDiskSizeGB(newSizeInGB)
            .withManagedDisk(managedDiskParameters)
            .withCaching(cachingType));
    return this;
}
 
Example #10
Source File: VirtualMachineImpl.java    From azure-libraries-for-java with MIT License 5 votes vote down vote up
@Override
public VirtualMachineImpl withExistingDataDisk(Disk disk, int lun, CachingTypes cachingType) {
    throwIfManagedDiskDisabled(ManagedUnmanagedDiskErrors.VM_BOTH_UNMANAGED_AND_MANAGED_DISK_NOT_ALLOWED);
    ManagedDiskParameters managedDiskParameters = new ManagedDiskParameters();
    managedDiskParameters.withId(disk.id());
    this.managedDataDisks.existingDisksToAttach.add(new DataDisk()
            .withLun(lun)
            .withManagedDisk(managedDiskParameters)
            .withCaching(cachingType));
    return this;
}
 
Example #11
Source File: VirtualMachineImpl.java    From azure-libraries-for-java with MIT License 5 votes vote down vote up
@Override
public VirtualMachineImpl withExistingDataDisk(Disk disk) {
    throwIfManagedDiskDisabled(ManagedUnmanagedDiskErrors.VM_BOTH_UNMANAGED_AND_MANAGED_DISK_NOT_ALLOWED);
    ManagedDiskParameters managedDiskParameters = new ManagedDiskParameters();
    managedDiskParameters.withId(disk.id());
    this.managedDataDisks.existingDisksToAttach.add(new DataDisk()
            .withLun(-1)
            .withManagedDisk(managedDiskParameters));
    return this;
}
 
Example #12
Source File: VirtualMachineImpl.java    From azure-libraries-for-java with MIT License 5 votes vote down vote up
@Override
public VirtualMachineImpl withNewDataDisk(Creatable<Disk> creatable, int lun, CachingTypes cachingType) {
    throwIfManagedDiskDisabled(ManagedUnmanagedDiskErrors.VM_BOTH_UNMANAGED_AND_MANAGED_DISK_NOT_ALLOWED);
    this.managedDataDisks.newDisksToAttach.put(this.addDependency(creatable),
            new DataDisk()
                    .withLun(lun)
                    .withCaching(cachingType));
    return this;
}
 
Example #13
Source File: VirtualMachineImpl.java    From azure-libraries-for-java with MIT License 5 votes vote down vote up
@Override
public VirtualMachineImpl withNewDataDisk(Creatable<Disk> creatable) {
    throwIfManagedDiskDisabled(ManagedUnmanagedDiskErrors.VM_BOTH_UNMANAGED_AND_MANAGED_DISK_NOT_ALLOWED);
    this.managedDataDisks.newDisksToAttach.put(this.addDependency(creatable),
            new DataDisk().withLun(-1));
    return this;
}
 
Example #14
Source File: VirtualMachineImpl.java    From azure-libraries-for-java with MIT License 5 votes vote down vote up
@Override
public VirtualMachineImpl withSpecializedOSDisk(Disk disk, OperatingSystemTypes osType) {
    ManagedDiskParameters diskParametersInner = new ManagedDiskParameters();
    diskParametersInner.withId(disk.id());
    this.inner().storageProfile().osDisk().withCreateOption(DiskCreateOptionTypes.ATTACH);
    this.inner().storageProfile().osDisk().withManagedDisk(diskParametersInner);
    this.inner().storageProfile().osDisk().withOsType(osType);
    this.inner().storageProfile().osDisk().withVhd(null);
    return this;
}
 
Example #15
Source File: AzureAttachmentResourceBuilder.java    From cloudbreak with Apache License 2.0 5 votes vote down vote up
@Override
public List<CloudResource> build(AzureContext context, long privateId, AuthenticatedContext auth, Group group,
        List<CloudResource> buildableResource, CloudStack cloudStack) {
    LOGGER.info("Attach disk to instance");

    CloudResource instance = buildableResource.stream()
            .filter(cloudResource -> cloudResource.getType().equals(ResourceType.AZURE_INSTANCE))
            .findFirst()
            .orElseThrow(() -> new AzureResourceException("Instance resource not found"));

    CloudContext cloudContext = auth.getCloudContext();
    String resourceGroupName = azureResourceGroupMetadataProvider.getResourceGroupName(cloudContext, cloudStack);
    AzureClient client = getAzureClient(auth);
    VirtualMachine vm = client.getVirtualMachine(resourceGroupName, instance.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 -> !instance.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()) {
                        client.detachDiskFromVm(disk.id(), client.getVirtualMachine(disk.virtualMachineId()));
                    }
                    client.attachDiskToVm(disk, vm);
                } else {
                    LOGGER.debug("Managed disk {} is already attached to VM {}", disk, vm);
                }
            });
    volumeSet.setInstanceId(instance.getInstanceId());
    volumeSet.setStatus(CommonStatus.CREATED);
    return List.of(volumeSet);
}
 
Example #16
Source File: AzureClient.java    From cloudbreak with Apache License 2.0 5 votes vote down vote up
public Disk createManagedDisk(String diskName, int diskSize, AzureDiskType diskType, String region, String resourceGroupName, Map<String, String> tags) {
    LOGGER.debug("create managed disk with name={}", diskName);
    return azure.disks().define(diskName)
            .withRegion(Region.findByLabelOrName(region))
            .withExistingResourceGroup(resourceGroupName)
            .withData()
            .withSizeInGB(diskSize)
            .withTags(tags)
            .withSku(convertAzureDiskTypeToDiskSkuTypes(diskType))
            .create();
}
 
Example #17
Source File: DiskImpl.java    From azure-libraries-for-java with MIT License 5 votes vote down vote up
@Override
public DiskImpl withWindowsFromDisk(Disk sourceDisk) {
    withWindowsFromDisk(sourceDisk.id());
    if (sourceDisk.osType() != null) {
        this.withOSType(sourceDisk.osType());
    }
    this.withSku(sourceDisk.sku());
    return this;
}
 
Example #18
Source File: DiskImpl.java    From azure-libraries-for-java with MIT License 5 votes vote down vote up
@Override
public DiskImpl withLinuxFromDisk(Disk sourceDisk) {
    withLinuxFromDisk(sourceDisk.id());
    if (sourceDisk.osType() != null) {
        this.withOSType(sourceDisk.osType());
    }
    this.withSku(sourceDisk.sku());
    return this;
}
 
Example #19
Source File: AzureClient.java    From cloudbreak with Apache License 2.0 4 votes vote down vote up
public void attachDiskToVm(Disk disk, VirtualMachine vm) {
    LOGGER.debug("attach managed disk {} to VM {}", disk, vm);
    vm.update().withExistingDataDisk(disk).apply();
}
 
Example #20
Source File: AzureClient.java    From cloudbreak with Apache License 2.0 4 votes vote down vote up
public PagedList<Disk> listDisksByResourceGroup(String resourceGroupName) {
    return azure.disks().listByResourceGroup(resourceGroupName);
}
 
Example #21
Source File: AzureClient.java    From cloudbreak with Apache License 2.0 4 votes vote down vote up
public Disk getDiskById(String id) {
    return azure.disks().getById(id);
}
 
Example #22
Source File: AzureClient.java    From cloudbreak with Apache License 2.0 4 votes vote down vote up
public Disk getDiskByName(String resourceGroupName, String diskName) {
    return azure.disks().getByResourceGroup(resourceGroupName, diskName);
}
 
Example #23
Source File: AzureVolumeResourceBuilder.java    From cloudbreak with Apache License 2.0 4 votes vote down vote up
@Override
public List<CloudResource> build(AzureContext context, 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(Collectors.toList());
    CloudContext cloudContext = auth.getCloudContext();
    String resourceGroupName = azureResourceGroupMetadataProvider.getResourceGroupName(cloudContext, cloudStack);
    String region = cloudContext.getLocation().getRegion().getRegionName();
    for (CloudResource resource : requestedResources) {
        volumeSetMap.put(resource.getName(), Collections.synchronizedList(new ArrayList<>()));
        VolumeSetAttributes volumeSet = getVolumeSetAttributes(resource);
        DeviceNameGenerator generator = new DeviceNameGenerator();
        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());
                    } else {
                        LOGGER.debug("Managed disk for resourcegroup: {}, 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()));
                }))
                .collect(Collectors.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(Collectors.toList());
}
 
Example #24
Source File: CustomImageDataDiskImpl.java    From azure-libraries-for-java with MIT License 4 votes vote down vote up
@Override
public CustomImageDataDiskImpl fromManagedDisk(Disk sourceManagedDisk) {
    this.inner().withManagedDisk(new SubResource().withId(sourceManagedDisk.id()));
    return this;
}
 
Example #25
Source File: SnapshotImpl.java    From azure-libraries-for-java with MIT License 4 votes vote down vote up
@Override
public SnapshotImpl withDataFromDisk(Disk managedDisk) {
    return withDataFromDisk(managedDisk.id())
            .withOSType(managedDisk.osType())
            .withSku(managedDisk.sku());
}
 
Example #26
Source File: DisksImpl.java    From azure-libraries-for-java with MIT License 4 votes vote down vote up
@Override
public Disk.DefinitionStages.Blank define(String name) {
    return this.wrapModel(name);
}
 
Example #27
Source File: VirtualMachineCustomImageImpl.java    From azure-libraries-for-java with MIT License 4 votes vote down vote up
@Override
public VirtualMachineCustomImageImpl withLinuxFromDisk(Disk sourceManagedDisk, OperatingSystemStateTypes osState) {
    return withLinuxFromDisk(sourceManagedDisk.id(), osState);
}
 
Example #28
Source File: VirtualMachineCustomImageImpl.java    From azure-libraries-for-java with MIT License 4 votes vote down vote up
@Override
public VirtualMachineCustomImageImpl withWindowsFromDisk(Disk sourceManagedDisk, OperatingSystemStateTypes osState) {
    return withWindowsFromDisk(sourceManagedDisk.id(), osState);
}
 
Example #29
Source File: DiskImpl.java    From azure-libraries-for-java with MIT License 4 votes vote down vote up
@Override
public Observable<Disk> createResourceAsync() {
    return manager().inner().disks().createOrUpdateAsync(resourceGroupName(), name(), this.inner())
            .map(innerToFluentMap(this));
}
 
Example #30
Source File: DiskImpl.java    From azure-libraries-for-java with MIT License 4 votes vote down vote up
@Override
public DiskImpl fromDisk(Disk managedDisk) {
    return fromDisk(managedDisk.id())
            .withOSType(managedDisk.osType())
            .withSku(managedDisk.sku());
}