Search in sources :

Example 56 with DiskState

use of com.vmware.photon.controller.model.resources.DiskService.DiskState in project photon-model by vmware.

the class AzureDiskEnumerationServiceTest method testVMAndDiskEnumeration.

@Test
public void testVMAndDiskEnumeration() throws Throwable {
    // Exit if it is mock
    if (this.isMock) {
        return;
    }
    this.resourceGroup = SdkContext.randomResourceName(RESOURCE_GROUP_NAME, RESOURCE_GROUP_NAME.length() + 5);
    // Create a vm with one additional disk
    VirtualMachine vm = getAzureSdkClients().getComputeManager().virtualMachines().define("TestVM").withRegion(Region.US_WEST).withNewResourceGroup(this.resourceGroup).withNewPrimaryNetwork("10.0.0.0/28").withPrimaryPrivateIPAddressDynamic().withoutPrimaryPublicIPAddress().withPopularLinuxImage(KnownLinuxVirtualMachineImage.UBUNTU_SERVER_16_04_LTS).withRootUsername(AzureTestUtil.AZURE_ADMIN_USERNAME).withRootPassword(AzureTestUtil.AZURE_ADMIN_PASSWORD).withNewDataDisk(5).create();
    assertNotNull(vm);
    DataDisk dataDisk = vm.storageProfile().dataDisks().get(0);
    assertNotNull(dataDisk);
    ComputeEnumerateResourceRequest resourceRequest = kickOffEnumeration();
    ComputeEnumerateAdapterRequest request = new ComputeEnumerateAdapterRequest(resourceRequest, this.authState, this.computeHost);
    // Run VM enumeration
    patchServiceSynchronously(AzureComputeEnumerationAdapterService.SELF_LINK, request);
    // Run disk enumeration
    patchServiceSynchronously(AzureDiskEnumerationAdapterService.SELF_LINK, request);
    // Verify disk state is created in local store
    Map<String, DiskState> diskStateMap = ProvisioningUtils.getResourceStates(getHost(), DiskService.FACTORY_LINK, DiskState.class);
    assertTrue(diskStateMap.keySet().stream().anyMatch(s -> s.equalsIgnoreCase(dataDisk.managedDisk().id())));
    // Detach disk
    vm.update().withoutDataDisk(dataDisk.lun()).apply();
    // Run Disk enumeration
    patchServiceSynchronously(AzureDiskEnumerationAdapterService.SELF_LINK, request);
    // Verify if the status of disk state is updated to Available state or not
    diskStateMap = ProvisioningUtils.getResourceStates(getHost(), DiskService.FACTORY_LINK, DiskState.class);
    diskStateMap.values().forEach(diskState -> {
        if (diskState.name.equalsIgnoreCase(dataDisk.name())) {
            assertTrue("Status of disk state should be Available", diskState.status.equals(DiskService.DiskStatus.AVAILABLE));
        }
    });
}
Also used : VirtualMachine(com.microsoft.azure.management.compute.VirtualMachine) ComputeEnumerateResourceRequest(com.vmware.photon.controller.model.adapterapi.ComputeEnumerateResourceRequest) Region(com.microsoft.azure.management.resources.fluentcore.arm.Region) AzureBaseTest(com.vmware.photon.controller.model.adapters.azure.base.AzureBaseTest) TagsUtil(com.vmware.photon.controller.model.adapters.util.TagsUtil) Level(java.util.logging.Level) TagState(com.vmware.photon.controller.model.resources.TagService.TagState) Map(java.util.Map) After(org.junit.After) EnumerationAction(com.vmware.photon.controller.model.adapterapi.EnumerationAction) ProvisioningUtils(com.vmware.photon.controller.model.tasks.ProvisioningUtils) AzureTestUtil(com.vmware.photon.controller.model.adapters.azure.instance.AzureTestUtil) AzureConstants(com.vmware.photon.controller.model.adapters.azure.constants.AzureConstants) DataDisk(com.microsoft.azure.management.compute.DataDisk) Assert.assertNotNull(org.junit.Assert.assertNotNull) Assert.assertTrue(org.junit.Assert.assertTrue) DiskState(com.vmware.photon.controller.model.resources.DiskService.DiskState) Test(org.junit.Test) TimeUnit(java.util.concurrent.TimeUnit) Disk(com.microsoft.azure.management.compute.Disk) UriUtils(com.vmware.xenon.common.UriUtils) PhotonModelConstants(com.vmware.photon.controller.model.constants.PhotonModelConstants) ComputeEnumerateAdapterRequest(com.vmware.photon.controller.model.adapters.util.ComputeEnumerateAdapterRequest) KnownLinuxVirtualMachineImage(com.microsoft.azure.management.compute.KnownLinuxVirtualMachineImage) DiskService(com.vmware.photon.controller.model.resources.DiskService) SdkContext(com.microsoft.azure.management.resources.fluentcore.utils.SdkContext) DataDisk(com.microsoft.azure.management.compute.DataDisk) ComputeEnumerateResourceRequest(com.vmware.photon.controller.model.adapterapi.ComputeEnumerateResourceRequest) ComputeEnumerateAdapterRequest(com.vmware.photon.controller.model.adapters.util.ComputeEnumerateAdapterRequest) DiskState(com.vmware.photon.controller.model.resources.DiskService.DiskState) VirtualMachine(com.microsoft.azure.management.compute.VirtualMachine) AzureBaseTest(com.vmware.photon.controller.model.adapters.azure.base.AzureBaseTest) Test(org.junit.Test)

Example 57 with DiskState

use of com.vmware.photon.controller.model.resources.DiskService.DiskState in project photon-model by vmware.

the class AzureDiskEnumerationServiceTest method testManagedDiskEnumeration.

@Test
public void testManagedDiskEnumeration() throws Throwable {
    // Exit if it is mock
    if (this.isMock) {
        return;
    }
    // create a disk in Azure
    Disk disk = createDisk("disk");
    assertNotNull(disk);
    ComputeEnumerateResourceRequest resourceRequest = kickOffEnumeration();
    ComputeEnumerateAdapterRequest request = new ComputeEnumerateAdapterRequest(resourceRequest, this.authState, this.computeHost);
    // Run disk enumeration
    patchServiceSynchronously(AzureDiskEnumerationAdapterService.SELF_LINK, request);
    // Fetch disks from local store to verify if diskState is created after enumeration
    Map<String, DiskState> diskStateMap = ProvisioningUtils.getResourceStates(getHost(), DiskService.FACTORY_LINK, DiskState.class);
    assertTrue("Newly created disk state is not found.", diskStateMap.keySet().stream().anyMatch(s -> s.equalsIgnoreCase(disk.id())));
    // verify internal tag links
    DiskState createdDisk = diskStateMap.entrySet().stream().filter(en -> en.getKey().equalsIgnoreCase(disk.id())).findFirst().get().getValue();
    assertNotNull(createdDisk.tagLinks);
    TagState typeTag = TagsUtil.newTagState(PhotonModelConstants.TAG_KEY_TYPE, AzureConstants.AzureResourceType.azure_managed_disk.toString(), false, this.computeHost.tenantLinks);
    assertTrue("internal tag not found", createdDisk.tagLinks.stream().anyMatch(s -> s.equalsIgnoreCase(typeTag.documentSelfLink)));
    // verify regionId
    assertNotNull("regionId not found", createdDisk.regionId);
    // Delete disk from Azure
    getAzureSdkClients().getComputeManager().disks().deleteById(disk.id());
    // Run enumeration to sync local disk states with disks in Azure
    patchServiceSynchronously(AzureDiskEnumerationAdapterService.SELF_LINK, request);
    // Need to wait for completion of patch operation on disk states
    TimeUnit.SECONDS.sleep(5);
    // After sync, query local disk states to verify endpoint links of disk state are disassociated
    diskStateMap = ProvisioningUtils.getResourceStates(getHost(), DiskService.FACTORY_LINK, DiskState.class);
    diskStateMap.values().forEach(diskState -> {
        if (diskState.id.equalsIgnoreCase(disk.id())) {
            assertTrue("Endpoint link must be null", diskState.endpointLink.isEmpty());
            assertTrue("Endpoint links in DiskState must be empty", diskState.endpointLinks.isEmpty());
        }
    });
}
Also used : VirtualMachine(com.microsoft.azure.management.compute.VirtualMachine) ComputeEnumerateResourceRequest(com.vmware.photon.controller.model.adapterapi.ComputeEnumerateResourceRequest) Region(com.microsoft.azure.management.resources.fluentcore.arm.Region) AzureBaseTest(com.vmware.photon.controller.model.adapters.azure.base.AzureBaseTest) TagsUtil(com.vmware.photon.controller.model.adapters.util.TagsUtil) Level(java.util.logging.Level) TagState(com.vmware.photon.controller.model.resources.TagService.TagState) Map(java.util.Map) After(org.junit.After) EnumerationAction(com.vmware.photon.controller.model.adapterapi.EnumerationAction) ProvisioningUtils(com.vmware.photon.controller.model.tasks.ProvisioningUtils) AzureTestUtil(com.vmware.photon.controller.model.adapters.azure.instance.AzureTestUtil) AzureConstants(com.vmware.photon.controller.model.adapters.azure.constants.AzureConstants) DataDisk(com.microsoft.azure.management.compute.DataDisk) Assert.assertNotNull(org.junit.Assert.assertNotNull) Assert.assertTrue(org.junit.Assert.assertTrue) DiskState(com.vmware.photon.controller.model.resources.DiskService.DiskState) Test(org.junit.Test) TimeUnit(java.util.concurrent.TimeUnit) Disk(com.microsoft.azure.management.compute.Disk) UriUtils(com.vmware.xenon.common.UriUtils) PhotonModelConstants(com.vmware.photon.controller.model.constants.PhotonModelConstants) ComputeEnumerateAdapterRequest(com.vmware.photon.controller.model.adapters.util.ComputeEnumerateAdapterRequest) KnownLinuxVirtualMachineImage(com.microsoft.azure.management.compute.KnownLinuxVirtualMachineImage) DiskService(com.vmware.photon.controller.model.resources.DiskService) SdkContext(com.microsoft.azure.management.resources.fluentcore.utils.SdkContext) ComputeEnumerateResourceRequest(com.vmware.photon.controller.model.adapterapi.ComputeEnumerateResourceRequest) ComputeEnumerateAdapterRequest(com.vmware.photon.controller.model.adapters.util.ComputeEnumerateAdapterRequest) DiskState(com.vmware.photon.controller.model.resources.DiskService.DiskState) DataDisk(com.microsoft.azure.management.compute.DataDisk) Disk(com.microsoft.azure.management.compute.Disk) TagState(com.vmware.photon.controller.model.resources.TagService.TagState) AzureBaseTest(com.vmware.photon.controller.model.adapters.azure.base.AzureBaseTest) Test(org.junit.Test)

Example 58 with DiskState

use of com.vmware.photon.controller.model.resources.DiskService.DiskState in project photon-model by vmware.

the class GCPEnumerationAdapterService method updateHelper.

/**
 * This function will update the root disk data of a compute state. If the
 * instance on the cloud does not have any disks or does not have a boot
 * disk, the update will skip this instance. After all local vms are updated
 * it will jump to create stage.
 *
 * So far we update ip address, power state in compute state and instance type
 * in compute description and disk custom properties
 * @param ctx The Enumeration Context.
 * @param computeState The compute state to be updated.
 * @param vm The virtual machine used to update compute state.
 * @param numOfUpdates The number of remaining compute states to be updated.
 */
private void updateHelper(EnumerationContext ctx, ComputeState computeState, GCPInstance vm, AtomicInteger numOfUpdates) {
    List<Operation> operations = new ArrayList<>();
    ComputeState computeStatePatch = new ComputeState();
    assignIPAddress(computeStatePatch, vm);
    assignPowerState(computeStatePatch, vm.status);
    operations.add(Operation.createPatch(getHost(), computeState.documentSelfLink).setBody(computeStatePatch));
    ComputeDescription computeDescription = new ComputeDescription();
    computeDescription.instanceType = extractActualInstanceType(vm.machineType);
    operations.add(Operation.createPatch(getHost(), computeState.descriptionLink).setBody(computeDescription));
    if (vm.disks != null && !vm.disks.isEmpty()) {
        for (GCPDisk gcpDisk : vm.disks) {
            if (gcpDisk.boot) {
                DiskState diskState = new DiskState();
                diskState.customProperties = new HashMap<>();
                diskState.customProperties.put(DISK_AUTO_DELETE, gcpDisk.autoDelete.toString());
                diskState.documentSelfLink = computeState.diskLinks.get(0);
                operations.add(Operation.createPatch(getHost(), diskState.documentSelfLink).setBody(diskState));
                break;
            }
        }
    }
    OperationJoin.create(operations).setCompletion((ops, exs) -> {
        if (exs != null) {
            exs.values().forEach(ex -> logWarning(() -> String.format("Error: %s", ex.getMessage())));
        }
        countAndFinishUpdating(ctx, numOfUpdates);
    }).sendWith(this);
}
Also used : GCPUriPaths(com.vmware.photon.controller.model.adapters.gcp.GCPUriPaths) AuthCredentialsServiceState(com.vmware.xenon.services.common.AuthCredentialsService.AuthCredentialsServiceState) ComputeEnumerateResourceRequest(com.vmware.photon.controller.model.adapterapi.ComputeEnumerateResourceRequest) GCPUtils.privateKeyFromPkcs8(com.vmware.photon.controller.model.adapters.gcp.utils.GCPUtils.privateKeyFromPkcs8) QueryTask(com.vmware.xenon.services.common.QueryTask) OSType(com.vmware.photon.controller.model.ComputeProperties.OSType) GCPInstance(com.vmware.photon.controller.model.adapters.gcp.podo.vm.GCPInstance) ResourceGroupState(com.vmware.photon.controller.model.resources.ResourceGroupService.ResourceGroupState) DiskType(com.vmware.photon.controller.model.resources.DiskService.DiskType) ComputeType(com.vmware.photon.controller.model.resources.ComputeDescriptionService.ComputeDescription.ComputeType) ENVIRONMENT_NAME_GCP(com.vmware.photon.controller.model.resources.ComputeDescriptionService.ComputeDescription.ENVIRONMENT_NAME_GCP) Utils(com.vmware.xenon.common.Utils) GeneralSecurityException(java.security.GeneralSecurityException) GCPInstancesList(com.vmware.photon.controller.model.adapters.gcp.podo.vm.GCPInstancesList) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Map(java.util.Map) ServiceDocumentQueryResult(com.vmware.xenon.common.ServiceDocumentQueryResult) URI(java.net.URI) DEFAULT_DISK_SOURCE_IMAGE(com.vmware.photon.controller.model.adapters.gcp.constants.GCPConstants.DEFAULT_DISK_SOURCE_IMAGE) GCPUtils.assignPowerState(com.vmware.photon.controller.model.adapters.gcp.utils.GCPUtils.assignPowerState) GCPUtils.extractActualInstanceType(com.vmware.photon.controller.model.adapters.gcp.utils.GCPUtils.extractActualInstanceType) ComputeDescription(com.vmware.photon.controller.model.resources.ComputeDescriptionService.ComputeDescription) DISK_AUTO_DELETE(com.vmware.photon.controller.model.adapters.gcp.constants.GCPConstants.DISK_AUTO_DELETE) StatelessService(com.vmware.xenon.common.StatelessService) DEFAULT_DISK_SERVICE_REFERENCE(com.vmware.photon.controller.model.adapters.gcp.constants.GCPConstants.DEFAULT_DISK_SERVICE_REFERENCE) Collection(java.util.Collection) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) GCPDisk(com.vmware.photon.controller.model.adapters.gcp.podo.vm.GCPDisk) TaskManager(com.vmware.photon.controller.model.adapters.util.TaskManager) Set(java.util.Set) DiskState(com.vmware.photon.controller.model.resources.DiskService.DiskState) LIST_VM_TEMPLATE_URI(com.vmware.photon.controller.model.adapters.gcp.constants.GCPConstants.LIST_VM_TEMPLATE_URI) UUID(java.util.UUID) ServiceHost(com.vmware.xenon.common.ServiceHost) List(java.util.List) AUTH_HEADER_BEARER_PREFIX(com.vmware.photon.controller.model.adapters.gcp.constants.GCPConstants.AUTH_HEADER_BEARER_PREFIX) PrivateKey(java.security.PrivateKey) UriUtils(com.vmware.xenon.common.UriUtils) ComputeService(com.vmware.photon.controller.model.resources.ComputeService) QueryOption(com.vmware.xenon.services.common.QueryTask.QuerySpecification.QueryOption) NumericRange(com.vmware.xenon.services.common.QueryTask.NumericRange) DiskService(com.vmware.photon.controller.model.resources.DiskService) PhotonModelUriUtils(com.vmware.photon.controller.model.util.PhotonModelUriUtils) HashMap(java.util.HashMap) ComputeDescriptionService(com.vmware.photon.controller.model.resources.ComputeDescriptionService) GCPUtils(com.vmware.photon.controller.model.adapters.gcp.utils.GCPUtils) MAX_RESULTS(com.vmware.photon.controller.model.adapters.gcp.constants.GCPConstants.MAX_RESULTS) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) PAGE_TOKEN(com.vmware.photon.controller.model.adapters.gcp.constants.GCPConstants.PAGE_TOKEN) UriPaths(com.vmware.photon.controller.model.UriPaths) EnumerationStages(com.vmware.photon.controller.model.adapters.util.enums.EnumerationStages) ComputeState(com.vmware.photon.controller.model.resources.ComputeService.ComputeState) EnumerationAction(com.vmware.photon.controller.model.adapterapi.EnumerationAction) LinkedList(java.util.LinkedList) GCPAccessTokenResponse(com.vmware.photon.controller.model.adapters.gcp.podo.authorization.GCPAccessTokenResponse) AdapterUtils(com.vmware.photon.controller.model.adapters.util.AdapterUtils) GCPUtils.extractRegionFromZone(com.vmware.photon.controller.model.adapters.gcp.utils.GCPUtils.extractRegionFromZone) EncryptionUtils(com.vmware.photon.controller.model.security.util.EncryptionUtils) Operation(com.vmware.xenon.common.Operation) QueryUtils(com.vmware.photon.controller.model.query.QueryUtils) IOException(java.io.IOException) ComputeScopes(com.google.api.services.compute.ComputeScopes) Consumer(java.util.function.Consumer) CUSTOM_OS_TYPE(com.vmware.photon.controller.model.ComputeProperties.CUSTOM_OS_TYPE) DEFAULT_DISK_CAPACITY(com.vmware.photon.controller.model.adapters.gcp.constants.GCPConstants.DEFAULT_DISK_CAPACITY) ComputeStateWithDescription(com.vmware.photon.controller.model.resources.ComputeService.ComputeStateWithDescription) GCPUtils.assignIPAddress(com.vmware.photon.controller.model.adapters.gcp.utils.GCPUtils.assignIPAddress) Collections(java.util.Collections) OperationJoin(com.vmware.xenon.common.OperationJoin) PhotonModelUriUtils.createInventoryUri(com.vmware.photon.controller.model.util.PhotonModelUriUtils.createInventoryUri) JSONWebToken(com.vmware.photon.controller.model.adapters.gcp.utils.JSONWebToken) ComputeState(com.vmware.photon.controller.model.resources.ComputeService.ComputeState) ComputeDescription(com.vmware.photon.controller.model.resources.ComputeDescriptionService.ComputeDescription) GCPDisk(com.vmware.photon.controller.model.adapters.gcp.podo.vm.GCPDisk) DiskState(com.vmware.photon.controller.model.resources.DiskService.DiskState) ArrayList(java.util.ArrayList) Operation(com.vmware.xenon.common.Operation)

Example 59 with DiskState

use of com.vmware.photon.controller.model.resources.DiskService.DiskState in project photon-model by vmware.

the class ModelUtils method createDiskState.

public static DiskState createDiskState(BaseModelTest test, String name) throws Throwable {
    DiskState d = new DiskState();
    d.id = UUID.randomUUID().toString();
    d.name = name;
    d.documentSelfLink = d.id;
    d.type = DiskType.HDD;
    d.sourceImageReference = new URI("http://sourceImageReference");
    DiskState returnState = test.postServiceSynchronously(DiskService.FACTORY_LINK, d, DiskState.class);
    return returnState;
}
Also used : DiskState(com.vmware.photon.controller.model.resources.DiskService.DiskState) URI(java.net.URI)

Example 60 with DiskState

use of com.vmware.photon.controller.model.resources.DiskService.DiskState in project photon-model by vmware.

the class DiskServiceTest method buildValidStartState.

private static DiskState buildValidStartState(boolean assignHost) throws Throwable {
    DiskState disk = new DiskState();
    disk.id = UUID.randomUUID().toString();
    disk.type = DiskService.DiskType.HDD;
    disk.name = "friendly-name";
    disk.capacityMBytes = 100L;
    if (assignHost) {
        disk.computeHostLink = "host-1";
    }
    return disk;
}
Also used : DiskState(com.vmware.photon.controller.model.resources.DiskService.DiskState)

Aggregations

DiskState (com.vmware.photon.controller.model.resources.DiskService.DiskState)77 ArrayList (java.util.ArrayList)24 Operation (com.vmware.xenon.common.Operation)23 DiskService (com.vmware.photon.controller.model.resources.DiskService)18 ComputeState (com.vmware.photon.controller.model.resources.ComputeService.ComputeState)16 List (java.util.List)15 EnumerationAction (com.vmware.photon.controller.model.adapterapi.EnumerationAction)13 UriUtils (com.vmware.xenon.common.UriUtils)13 Utils (com.vmware.xenon.common.Utils)13 HashMap (java.util.HashMap)13 TimeUnit (java.util.concurrent.TimeUnit)13 QueryTask (com.vmware.xenon.services.common.QueryTask)12 Query (com.vmware.xenon.services.common.QueryTask.Query)12 HashSet (java.util.HashSet)12 Test (org.junit.Test)11 AzureConstants (com.vmware.photon.controller.model.adapters.azure.constants.AzureConstants)10 PhotonModelUriUtils.createInventoryUri (com.vmware.photon.controller.model.util.PhotonModelUriUtils.createInventoryUri)10 ServiceDocumentQueryResult (com.vmware.xenon.common.ServiceDocumentQueryResult)10 AuthCredentialsService (com.vmware.xenon.services.common.AuthCredentialsService)10 Map (java.util.Map)10