Search in sources :

Example 1 with VirtualMachineInner

use of com.microsoft.azure.management.compute.implementation.VirtualMachineInner in project photon-model by vmware.

the class AzureComputeEnumerationAdapterService method queryForDiskStates.

/**
 * Get all disk states related to given VMs
 */
private void queryForDiskStates(EnumerationContext ctx, ComputeEnumerationSubStages next) {
    if (ctx.virtualMachines.size() == 0) {
        logFine(() -> "No virtual machines found to be associated with local disks");
        if (ctx.regions.isEmpty()) {
            ctx.subStage = ComputeEnumerationSubStages.DISASSOCIATE_COMPUTE_STATES;
            handleSubStage(ctx);
            return;
        } else {
            ctx.subStage = ComputeEnumerationSubStages.UPDATE_COMPUTE_STATES;
            handleSubStage(ctx);
            return;
        }
    }
    ctx.diskStates.clear();
    List<String> diskIdList = new ArrayList<>();
    for (String instanceId : ctx.virtualMachines.keySet()) {
        VirtualMachineInner virtualMachine = ctx.virtualMachines.get(instanceId);
        String diskId = getVhdUri(virtualMachine);
        if (diskId == null) {
            continue;
        }
        diskIdList.add(diskId);
        List<String> dataDiskIDList = getDataDisksID(virtualMachine, AzureUtils.isDiskManaged(virtualMachine));
        if (null != dataDiskIDList && dataDiskIDList.size() > 0) {
            diskIdList.addAll(dataDiskIDList);
        }
    }
    Query.Builder qBuilder = Query.Builder.create().addKindFieldClause(DiskState.class).addInClause(DiskState.FIELD_NAME_ID, diskIdList, Occurance.SHOULD_OCCUR);
    QueryByPages<DiskState> queryDiskStates = new QueryByPages<>(getHost(), qBuilder.build(), DiskState.class, ctx.parentCompute.tenantLinks, // endpointLink
    null, ctx.parentCompute.documentSelfLink).setMaxPageSize(QueryUtils.MAX_RESULT_LIMIT).setClusterType(ServiceTypeCluster.INVENTORY_SERVICE);
    queryDiskStates.collectDocuments(Collectors.toList()).whenComplete((diskStates, e) -> {
        if (e != null) {
            handleError(ctx, e);
            return;
        }
        if (diskStates == null) {
            return;
        }
        diskStates.forEach(diskState -> ctx.diskStates.put(diskState.id, diskState));
        ctx.subStage = next;
        handleSubStage(ctx);
    });
}
Also used : QueryByPages(com.vmware.photon.controller.model.query.QueryUtils.QueryByPages) Query(com.vmware.xenon.services.common.QueryTask.Query) VirtualMachineInner(com.microsoft.azure.management.compute.implementation.VirtualMachineInner) DiskState(com.vmware.photon.controller.model.resources.DiskService.DiskState) ArrayList(java.util.ArrayList) Builder(com.vmware.xenon.services.common.QueryTask.Query.Builder)

Example 2 with VirtualMachineInner

use of com.microsoft.azure.management.compute.implementation.VirtualMachineInner in project photon-model by vmware.

the class AzureComputeEnumerationAdapterService method vmEnumerationCompletion.

/**
 * Completion handler for VM enumeration call. For async calls to list VMs, Azure returns
 * Observable<Page<VirtualMachineInner>>. The following completion subscribes to the Observable
 * and Overrides call(<T>) to include logic to process pages of VMs when we receive them.
 */
private Action1<Page<VirtualMachineInner>> vmEnumerationCompletion(EnumerationContext ctx, ComputeEnumerationSubStages next) {
    Action1<Page<VirtualMachineInner>> vmEnumCompletion = new Action1<Page<VirtualMachineInner>>() {

        @Override
        public void call(Page<VirtualMachineInner> virtualMachineInnerPage) {
            List<VirtualMachineInner> virtualMachineInners = virtualMachineInnerPage.items();
            ctx.enumNextPageLink = virtualMachineInnerPage.nextPageLink();
            if (virtualMachineInners == null || virtualMachineInners.size() == 0) {
                ctx.subStage = ComputeEnumerationSubStages.GET_COMPUTE_STATES;
                handleSubStage(ctx);
                return;
            }
            logInfo(() -> String.format("Retrieved %d VMs from Azure", virtualMachineInners.size()));
            logFine(() -> String.format("Next page link %s", ctx.enumNextPageLink));
            for (VirtualMachineInner virtualMachine : virtualMachineInners) {
                // We don't want to process VMs that are being terminated.
                if (AZURE_VM_TERMINATION_STATES.contains(virtualMachine.provisioningState())) {
                    logFine(() -> String.format("Not processing %s", virtualMachine.id()));
                    continue;
                }
                // Azure for some case changes the case of the vm id.
                String vmId = virtualMachine.id().toLowerCase();
                ctx.virtualMachines.put(vmId, virtualMachine);
                ctx.vmIds.add(vmId);
            }
            logFine(() -> String.format("Processing %d VMs", ctx.vmIds.size()));
            ctx.subStage = next;
            handleSubStage(ctx);
        }
    };
    return injectOperationContext(vmEnumCompletion);
}
Also used : Action1(rx.functions.Action1) VirtualMachineInner(com.microsoft.azure.management.compute.implementation.VirtualMachineInner) Page(com.microsoft.azure.Page)

Example 3 with VirtualMachineInner

use of com.microsoft.azure.management.compute.implementation.VirtualMachineInner in project photon-model by vmware.

the class AzureComputeEnumerationAdapterService method updateDiskStates.

/**
 * Update disk states with additional custom properties
 */
private void updateDiskStates(EnumerationContext ctx, ComputeEnumerationSubStages next) {
    if (ctx.virtualMachines.isEmpty()) {
        logFine(() -> "No virtual machines found to be associated with local disks");
        if (ctx.regions.isEmpty()) {
            ctx.subStage = ComputeEnumerationSubStages.DISASSOCIATE_COMPUTE_STATES;
            handleSubStage(ctx);
            return;
        } else {
            ctx.subStage = ComputeEnumerationSubStages.UPDATE_COMPUTE_STATES;
            handleSubStage(ctx);
            return;
        }
    }
    Iterator<Entry<String, VirtualMachineInner>> iterator = ctx.virtualMachines.entrySet().iterator();
    Collection<Operation> opCollection = new ArrayList<>();
    while (iterator.hasNext()) {
        Entry<String, VirtualMachineInner> vmEntry = iterator.next();
        VirtualMachineInner virtualMachine = vmEntry.getValue();
        if (virtualMachine.storageProfile() == null || virtualMachine.storageProfile().imageReference() == null) {
            continue;
        }
        if (virtualMachine.storageProfile().osDisk() == null) {
            logWarning(() -> "VM has empty OS disk.");
            continue;
        }
        String osDiskUri = getVhdUri(virtualMachine);
        if (osDiskUri == null) {
            logFine(() -> String.format("OS Disk URI not found for vm: %s", virtualMachine.id()));
            continue;
        }
        updateOSDiskProperties(ctx, virtualMachine, osDiskUri, opCollection);
        // create data disk states and update their custom properties
        updateDataDiskProperties(ctx, virtualMachine, opCollection);
    }
    if (opCollection.isEmpty()) {
        logFine(() -> "No local disk states fount to update.");
        ctx.subStage = next;
        handleSubStage(ctx);
        return;
    }
    OperationJoin.create(opCollection).setCompletion((ops, exs) -> {
        if (exs != null) {
            // TODO: https://jira-hzn.eng.vmware.com/browse/VSYM-3256
            exs.values().forEach(ex -> logWarning(() -> String.format("Error: %s", ex.getMessage())));
        }
        logFine(() -> "Continue on to create network interfaces.");
        ctx.subStage = next;
        handleSubStage(ctx);
    }).sendWith(this);
}
Also used : PowerState(com.vmware.photon.controller.model.resources.ComputeService.PowerState) Arrays(java.util.Arrays) ComputeEnumerateResourceRequest(com.vmware.photon.controller.model.adapterapi.ComputeEnumerateResourceRequest) ServiceTypeCluster(com.vmware.photon.controller.model.util.ClusterUtil.ServiceTypeCluster) LifecycleState(com.vmware.photon.controller.model.resources.ComputeService.LifecycleState) DISK_CONTROLLER_NUMBER(com.vmware.photon.controller.model.adapters.azure.constants.AzureConstants.DISK_CONTROLLER_NUMBER) Action1(rx.functions.Action1) StringUtils(org.apache.commons.lang3.StringUtils) Azure(com.microsoft.azure.management.Azure) Utils(com.vmware.xenon.common.Utils) Pair(org.apache.commons.lang3.tuple.Pair) SubnetState(com.vmware.photon.controller.model.resources.SubnetService.SubnetState) Map(java.util.Map) StorageDescription(com.vmware.photon.controller.model.resources.StorageDescriptionService.StorageDescription) OSDisk(com.microsoft.azure.management.compute.OSDisk) ResourceEnumerationTaskService(com.vmware.photon.controller.model.tasks.ResourceEnumerationTaskService) NetworkInterfaceState(com.vmware.photon.controller.model.resources.NetworkInterfaceService.NetworkInterfaceState) StatelessService(com.vmware.xenon.common.StatelessService) Set(java.util.Set) NetworkInterfaceService(com.vmware.photon.controller.model.resources.NetworkInterfaceService) StorageAccountTypes(com.microsoft.azure.management.compute.StorageAccountTypes) TagService(com.vmware.photon.controller.model.resources.TagService) CompletionHandler(com.vmware.xenon.common.Operation.CompletionHandler) SOURCE_TASK_LINK(com.vmware.photon.controller.model.constants.PhotonModelConstants.SOURCE_TASK_LINK) InstanceViewStatus(com.microsoft.azure.management.compute.InstanceViewStatus) DeferredResult(com.vmware.xenon.common.DeferredResult) UriUtils(com.vmware.xenon.common.UriUtils) ComputeService(com.vmware.photon.controller.model.resources.ComputeService) NumericRange(com.vmware.xenon.services.common.QueryTask.NumericRange) AZURE_DATA_DISK_CACHING(com.vmware.photon.controller.model.adapters.azure.constants.AzureConstants.AZURE_DATA_DISK_CACHING) ImageReferenceInner(com.microsoft.azure.management.compute.implementation.ImageReferenceInner) VirtualMachinesInner(com.microsoft.azure.management.compute.implementation.VirtualMachinesInner) ComputeDescriptionService(com.vmware.photon.controller.model.resources.ComputeDescriptionService) PhotonModelUtils(com.vmware.photon.controller.model.resources.util.PhotonModelUtils) RegionInfo(com.vmware.photon.controller.model.adapterapi.RegionEnumerationResponse.RegionInfo) TagsUtil(com.vmware.photon.controller.model.adapters.util.TagsUtil) ArrayList(java.util.ArrayList) TagState(com.vmware.photon.controller.model.resources.TagService.TagState) ServiceStateCollectionUpdateRequest(com.vmware.xenon.common.ServiceStateCollectionUpdateRequest) Query(com.vmware.xenon.services.common.QueryTask.Query) EnumerationStages(com.vmware.photon.controller.model.adapters.util.enums.EnumerationStages) OperatingSystemTypes(com.microsoft.azure.management.compute.OperatingSystemTypes) BiConsumer(java.util.function.BiConsumer) AZURE_DIAGNOSTIC_STORAGE_ACCOUNT_LINK(com.vmware.photon.controller.model.adapters.azure.constants.AzureConstants.AZURE_DIAGNOSTIC_STORAGE_ACCOUNT_LINK) AZURE_STORAGE_ACCOUNT_URI(com.vmware.photon.controller.model.adapters.azure.constants.AzureConstants.AZURE_STORAGE_ACCOUNT_URI) VirtualMachineInner(com.microsoft.azure.management.compute.implementation.VirtualMachineInner) AdapterUtils(com.vmware.photon.controller.model.adapters.util.AdapterUtils) DataDisk(com.microsoft.azure.management.compute.DataDisk) ResourceState(com.vmware.photon.controller.model.resources.ResourceState) QueryUtils(com.vmware.photon.controller.model.query.QueryUtils) ENVIRONMENT_NAME_AZURE(com.vmware.photon.controller.model.resources.ComputeDescriptionService.ComputeDescription.ENVIRONMENT_NAME_AZURE) QueryTop(com.vmware.photon.controller.model.query.QueryUtils.QueryTop) CUSTOM_OS_TYPE(com.vmware.photon.controller.model.ComputeProperties.CUSTOM_OS_TYPE) ComputeStateWithDescription(com.vmware.photon.controller.model.resources.ComputeService.ComputeStateWithDescription) PhotonModelConstants(com.vmware.photon.controller.model.constants.PhotonModelConstants) ComputeEnumerateAdapterRequest(com.vmware.photon.controller.model.adapters.util.ComputeEnumerateAdapterRequest) RegionEnumerationResponse(com.vmware.photon.controller.model.adapterapi.RegionEnumerationResponse) QuerySpecification(com.vmware.xenon.services.common.QueryTask.QuerySpecification) PhotonModelUriUtils.createInventoryUri(com.vmware.photon.controller.model.util.PhotonModelUriUtils.createInventoryUri) AuthCredentialsServiceState(com.vmware.xenon.services.common.AuthCredentialsService.AuthCredentialsServiceState) QueryTask(com.vmware.xenon.services.common.QueryTask) AZURE_RESOURCE_GROUP_NAME(com.vmware.photon.controller.model.adapters.azure.constants.AzureConstants.AZURE_RESOURCE_GROUP_NAME) OSType(com.vmware.photon.controller.model.ComputeProperties.OSType) AzureUriPaths(com.vmware.photon.controller.model.adapters.azure.AzureUriPaths) AZURE_MANAGED_DISK_TYPE(com.vmware.photon.controller.model.adapters.azure.constants.AzureConstants.AZURE_MANAGED_DISK_TYPE) ComputeType(com.vmware.photon.controller.model.resources.ComputeDescriptionService.ComputeDescription.ComputeType) AzureSdkClients(com.vmware.photon.controller.model.adapters.azure.utils.AzureSdkClients) AzureUtils.injectOperationContext(com.vmware.photon.controller.model.adapters.azure.utils.AzureUtils.injectOperationContext) CUSTOM_PROP_ENDPOINT_LINK(com.vmware.photon.controller.model.constants.PhotonModelConstants.CUSTOM_PROP_ENDPOINT_LINK) URI(java.net.URI) TagsUtil.newTagState(com.vmware.photon.controller.model.adapters.util.TagsUtil.newTagState) EndpointState(com.vmware.photon.controller.model.resources.EndpointService.EndpointState) AzureConstants(com.vmware.photon.controller.model.adapters.azure.constants.AzureConstants) AzureConstants.getQueryResultLimit(com.vmware.photon.controller.model.adapters.azure.constants.AzureConstants.getQueryResultLimit) ComputeDescription(com.vmware.photon.controller.model.resources.ComputeDescriptionService.ComputeDescription) Collection(java.util.Collection) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) DiskState(com.vmware.photon.controller.model.resources.DiskService.DiskState) Occurance(com.vmware.xenon.services.common.QueryTask.Query.Occurance) UUID(java.util.UUID) Collectors(java.util.stream.Collectors) List(java.util.List) AzureUtils(com.vmware.photon.controller.model.adapters.azure.utils.AzureUtils) AzureUtils.getResourceGroupName(com.vmware.photon.controller.model.adapters.azure.utils.AzureUtils.getResourceGroupName) TAG_KEY_TYPE(com.vmware.photon.controller.model.constants.PhotonModelConstants.TAG_KEY_TYPE) Entry(java.util.Map.Entry) NetworkInterfacesInner(com.microsoft.azure.management.network.implementation.NetworkInterfacesInner) QueryOption(com.vmware.xenon.services.common.QueryTask.QuerySpecification.QueryOption) InstanceViewTypes(com.microsoft.azure.management.compute.InstanceViewTypes) TagsUtil.setTagLinksToResourceState(com.vmware.photon.controller.model.adapters.util.TagsUtil.setTagLinksToResourceState) Builder(com.vmware.xenon.services.common.QueryTask.Query.Builder) DiskService(com.vmware.photon.controller.model.resources.DiskService) AzureUtils.isDiskManaged(com.vmware.photon.controller.model.adapters.azure.utils.AzureUtils.isDiskManaged) Default(com.vmware.photon.controller.model.adapters.azure.utils.AzureDeferredResultServiceCallback.Default) QueryByPages(com.vmware.photon.controller.model.query.QueryUtils.QueryByPages) AZURE_OSDISK_CACHING(com.vmware.photon.controller.model.adapters.azure.constants.AzureConstants.AZURE_OSDISK_CACHING) PublicIPAddress(com.microsoft.azure.management.network.PublicIPAddress) HashMap(java.util.HashMap) HashSet(java.util.HashSet) AuthCredentialsService(com.vmware.xenon.services.common.AuthCredentialsService) TagsUtil.updateLocalTagStates(com.vmware.photon.controller.model.adapters.util.TagsUtil.updateLocalTagStates) ComputeState(com.vmware.photon.controller.model.resources.ComputeService.ComputeState) EnumerationAction(com.vmware.photon.controller.model.adapterapi.EnumerationAction) AzureResourceType(com.vmware.photon.controller.model.adapters.azure.constants.AzureConstants.AzureResourceType) ExecutorService(java.util.concurrent.ExecutorService) Iterator(java.util.Iterator) NetworkInterfaceIPConfigurationInner(com.microsoft.azure.management.network.implementation.NetworkInterfaceIPConfigurationInner) Operation(com.vmware.xenon.common.Operation) Page(com.microsoft.azure.Page) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) NetworkInterfaceReferenceInner(com.microsoft.azure.management.compute.implementation.NetworkInterfaceReferenceInner) AzureDeferredResultServiceCallback(com.vmware.photon.controller.model.adapters.azure.utils.AzureDeferredResultServiceCallback) NetworkInterfaceInner(com.microsoft.azure.management.network.implementation.NetworkInterfaceInner) Collections(java.util.Collections) OperationJoin(com.vmware.xenon.common.OperationJoin) RESOURCE_GROUP_NAME(com.vmware.photon.controller.model.ComputeProperties.RESOURCE_GROUP_NAME) Entry(java.util.Map.Entry) VirtualMachineInner(com.microsoft.azure.management.compute.implementation.VirtualMachineInner) ArrayList(java.util.ArrayList) Operation(com.vmware.xenon.common.Operation)

Example 4 with VirtualMachineInner

use of com.microsoft.azure.management.compute.implementation.VirtualMachineInner in project photon-model by vmware.

the class AzureComputeEnumerationAdapterService method patchVMInstanceDetails.

private DeferredResult<ComputeState> patchVMInstanceDetails(EnumerationContext ctx, VirtualMachinesInner vmOps, ComputeState computeState) {
    String resourceGroupName = getResourceGroupName(computeState.id);
    String vmName = computeState.name;
    AzureDeferredResultServiceCallback<VirtualMachineInner> handler = new Default<>(this, "Load virtual machine instance view:" + vmName);
    PhotonModelUtils.runInExecutor(this.executorService, () -> {
        vmOps.getByResourceGroupAsync(resourceGroupName, vmName, InstanceViewTypes.INSTANCE_VIEW, handler);
    }, handler::failure);
    return handler.toDeferredResult().thenApply(vm -> {
        for (InstanceViewStatus status : vm.instanceView().statuses()) {
            if (computeState.creationTimeMicros == null && status.time() != null) {
                computeState.creationTimeMicros = TimeUnit.MILLISECONDS.toMicros(status.time().getMillis());
            }
            if (status.code().equals(AzureConstants.AZURE_VM_POWER_STATE_RUNNING)) {
                computeState.powerState = PowerState.ON;
            } else if (status.code().equals(AzureConstants.AZURE_VM_POWER_STATE_STOPPED)) {
                computeState.powerState = PowerState.OFF;
            } else if (status.code().equals(AzureConstants.AZURE_VM_POWER_STATE_DEALLOCATED)) {
                computeState.powerState = PowerState.SUSPEND;
            }
        }
        if (computeState.customProperties == null) {
            computeState.customProperties = new HashMap<>();
        }
        computeState.customProperties.put(RESOURCE_GROUP_NAME, resourceGroupName);
        computeState.endpointLinks.add(ctx.request.endpointLink);
        computeState.type = ComputeType.VM_GUEST;
        computeState.environmentName = ComputeDescription.ENVIRONMENT_NAME_AZURE;
        return computeState;
    });
}
Also used : InstanceViewStatus(com.microsoft.azure.management.compute.InstanceViewStatus) VirtualMachineInner(com.microsoft.azure.management.compute.implementation.VirtualMachineInner) Default(com.vmware.photon.controller.model.adapters.azure.utils.AzureDeferredResultServiceCallback.Default)

Example 5 with VirtualMachineInner

use of com.microsoft.azure.management.compute.implementation.VirtualMachineInner in project photon-model by vmware.

the class AzurePowerServiceTest method assertVmCurrentPowerState.

private void assertVmCurrentPowerState(PowerState powerState) {
    if (this.isMock) {
        // return. Nothing provisioned on Azure so nothing to check
        return;
    }
    try {
        PowerState vmPowerState = PowerState.UNKNOWN;
        VirtualMachineInner vm = AzureTestUtil.getAzureVirtualMachineWithExtension(this.computeManagementClient, azureVMName, azureVMName, EXPAND_INSTANCE_VIEW_PARAM);
        for (InstanceViewStatus status : vm.instanceView().statuses()) {
            if (status.code().equals(AzureConstants.AZURE_VM_POWER_STATE_RUNNING)) {
                vmPowerState = PowerState.ON;
            } else if (status.code().equals(AzureConstants.AZURE_VM_POWER_STATE_STOPPED)) {
                vmPowerState = PowerState.OFF;
            }
        }
        assertEquals("VM current power state does not match expected value.", powerState, vmPowerState);
    } catch (Exception e) {
        fail("Unable to verify current Machine Power state on Azure");
        e.printStackTrace();
    }
}
Also used : InstanceViewStatus(com.microsoft.azure.management.compute.InstanceViewStatus) VirtualMachineInner(com.microsoft.azure.management.compute.implementation.VirtualMachineInner) PowerState(com.vmware.photon.controller.model.resources.ComputeService.PowerState)

Aggregations

VirtualMachineInner (com.microsoft.azure.management.compute.implementation.VirtualMachineInner)10 DiskState (com.vmware.photon.controller.model.resources.DiskService.DiskState)5 DataDisk (com.microsoft.azure.management.compute.DataDisk)4 OSDisk (com.microsoft.azure.management.compute.OSDisk)4 AzureUriPaths (com.vmware.photon.controller.model.adapters.azure.AzureUriPaths)4 AzureConstants (com.vmware.photon.controller.model.adapters.azure.constants.AzureConstants)4 Default (com.vmware.photon.controller.model.adapters.azure.utils.AzureDeferredResultServiceCallback.Default)4 InstanceViewStatus (com.microsoft.azure.management.compute.InstanceViewStatus)3 StorageAccountTypes (com.microsoft.azure.management.compute.StorageAccountTypes)3 ImageReferenceInner (com.microsoft.azure.management.compute.implementation.ImageReferenceInner)3 NetworkInterfaceReferenceInner (com.microsoft.azure.management.compute.implementation.NetworkInterfaceReferenceInner)3 NetworkInterfaceIPConfigurationInner (com.microsoft.azure.management.network.implementation.NetworkInterfaceIPConfigurationInner)3 NetworkInterfaceInner (com.microsoft.azure.management.network.implementation.NetworkInterfaceInner)3 NetworkInterfacesInner (com.microsoft.azure.management.network.implementation.NetworkInterfacesInner)3 NetworkSecurityGroupInner (com.microsoft.azure.management.network.implementation.NetworkSecurityGroupInner)3 VirtualNetworkInner (com.microsoft.azure.management.network.implementation.VirtualNetworkInner)3 RESOURCE_GROUP_NAME (com.vmware.photon.controller.model.ComputeProperties.RESOURCE_GROUP_NAME)3 EnumerationAction (com.vmware.photon.controller.model.adapterapi.EnumerationAction)3 AZURE_DATA_DISK_CACHING (com.vmware.photon.controller.model.adapters.azure.constants.AzureConstants.AZURE_DATA_DISK_CACHING)3 AZURE_MANAGED_DISK_TYPE (com.vmware.photon.controller.model.adapters.azure.constants.AzureConstants.AZURE_MANAGED_DISK_TYPE)3