Search in sources :

Example 1 with PowerState

use of com.vmware.photon.controller.model.resources.ComputeService.PowerState 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)

Example 2 with PowerState

use of com.vmware.photon.controller.model.resources.ComputeService.PowerState in project photon-model by vmware.

the class GCPTestUtil method queryComputeStatesWithPowerState.

/**
 * Method to query the number of local compute states with given power state.
 * @param host The test service host.
 * @param resourcePool The default resource pool.
 * @param parentCompute The default compute host.
 * @param powerState The given power state.
 * @param instanceNames The assumed names of compute states.
 */
private static void queryComputeStatesWithPowerState(VerificationHost host, ResourcePoolState resourcePool, ComputeState parentCompute, PowerState powerState, Set<String> instanceNames) {
    Query query = QueryTask.Query.Builder.create().addKindFieldClause(ComputeState.class).addFieldClause(ComputeState.FIELD_NAME_RESOURCE_POOL_LINK, resourcePool.documentSelfLink).addFieldClause(ComputeState.FIELD_NAME_PARENT_LINK, parentCompute.documentSelfLink).build();
    QueryTask q = QueryTask.Builder.createDirectTask().addOption(QueryTask.QuerySpecification.QueryOption.EXPAND_CONTENT).setQuery(query).build();
    host.sendRequest(QueryUtils.createQueryTaskOperation(host, q, ServiceTypeCluster.INVENTORY_SERVICE).setReferer(host.getUri()).setCompletion((o, e) -> {
        if (e != null) {
            host.log(Level.WARNING, String.format("Error: %s", e.getMessage()));
            return;
        }
        QueryTask queryTask = o.getBody(QueryTask.class);
        if (queryTask.results.documentCount > 0) {
            queryTask.results.documents.values().forEach(s -> {
                ComputeState computeState = Utils.fromJson(s, ComputeState.class);
                if (computeState.powerState == powerState) {
                    instanceNames.remove(computeState.name);
                }
            });
        }
    }));
}
Also used : PowerState(com.vmware.photon.controller.model.resources.ComputeService.PowerState) Date(java.util.Date) GCPUtils.privateKeyFromPkcs8(com.vmware.photon.controller.model.adapters.gcp.utils.GCPUtils.privateKeyFromPkcs8) ServiceTypeCluster(com.vmware.photon.controller.model.util.ClusterUtil.ServiceTypeCluster) QueryTask(com.vmware.xenon.services.common.QueryTask) CPU_PLATFORM(com.vmware.photon.controller.model.adapters.gcp.constants.GCPConstants.CPU_PLATFORM) VerificationHost(com.vmware.xenon.common.test.VerificationHost) DEFAULT_IMAGE_REFERENCE(com.vmware.photon.controller.model.adapters.gcp.constants.GCPConstants.DEFAULT_IMAGE_REFERENCE) ServiceDocument(com.vmware.xenon.common.ServiceDocument) Random(java.util.Random) OPERATION_STATUS_DONE(com.vmware.photon.controller.model.adapters.gcp.constants.GCPConstants.OPERATION_STATUS_DONE) ResourceGroupState(com.vmware.photon.controller.model.resources.ResourceGroupService.ResourceGroupState) ServiceAccount(com.google.api.services.compute.model.ServiceAccount) ComputeType(com.vmware.photon.controller.model.resources.ComputeDescriptionService.ComputeDescription.ComputeType) AttachedDisk(com.google.api.services.compute.model.AttachedDisk) ENVIRONMENT_NAME_GCP(com.vmware.photon.controller.model.resources.ComputeDescriptionService.ComputeDescription.ENVIRONMENT_NAME_GCP) Utils(com.vmware.xenon.common.Utils) GeneralSecurityException(java.security.GeneralSecurityException) DISK_TYPE_PERSISTENT(com.vmware.photon.controller.model.adapters.gcp.constants.GCPConstants.DISK_TYPE_PERSISTENT) ResourcePoolService(com.vmware.photon.controller.model.resources.ResourcePoolService) URI(java.net.URI) ResourceEnumerationTaskService(com.vmware.photon.controller.model.tasks.ResourceEnumerationTaskService) Instances(com.google.api.services.compute.Compute.Instances) ComputeDescription(com.vmware.photon.controller.model.resources.ComputeDescriptionService.ComputeDescription) TimeZone(java.util.TimeZone) JacksonFactory(com.google.api.client.json.jackson2.JacksonFactory) HttpTransport(com.google.api.client.http.HttpTransport) TestUtils(com.vmware.photon.controller.model.tasks.TestUtils) Set(java.util.Set) GoogleNetHttpTransport(com.google.api.client.googleapis.javanet.GoogleNetHttpTransport) NetworkInterface(com.google.api.services.compute.model.NetworkInterface) UUID(java.util.UUID) AttachedDiskInitializeParams(com.google.api.services.compute.model.AttachedDiskInitializeParams) ServiceStats(com.vmware.xenon.common.ServiceStats) DEFAULT_CPU_PLATFORM(com.vmware.photon.controller.model.adapters.gcp.constants.GCPConstants.DEFAULT_CPU_PLATFORM) ResourceGroupService(com.vmware.photon.controller.model.resources.ResourceGroupService) List(java.util.List) ProvisioningUtils.getVMCount(com.vmware.photon.controller.model.tasks.ProvisioningUtils.getVMCount) UriUtils(com.vmware.xenon.common.UriUtils) ComputeService(com.vmware.photon.controller.model.resources.ComputeService) DiskService(com.vmware.photon.controller.model.resources.DiskService) Compute(com.google.api.services.compute.Compute) GoogleCredential(com.google.api.client.googleapis.auth.oauth2.GoogleCredential) Instance(com.google.api.services.compute.model.Instance) InstanceList(com.google.api.services.compute.model.InstanceList) SimpleDateFormat(java.text.SimpleDateFormat) ResourcePoolState(com.vmware.photon.controller.model.resources.ResourcePoolService.ResourcePoolState) HashMap(java.util.HashMap) ComputeDescriptionService(com.vmware.photon.controller.model.resources.ComputeDescriptionService) DEFAULT_AUTH_TYPE(com.vmware.photon.controller.model.adapters.gcp.constants.GCPConstants.DEFAULT_AUTH_TYPE) ArrayList(java.util.ArrayList) Level(java.util.logging.Level) HashSet(java.util.HashSet) AuthCredentialsService(com.vmware.xenon.services.common.AuthCredentialsService) Query(com.vmware.xenon.services.common.QueryTask.Query) AccessConfig(com.google.api.services.compute.model.AccessConfig) GCPEnumerationAdapterService(com.vmware.photon.controller.model.adapters.gcp.enumeration.GCPEnumerationAdapterService) ComputeState(com.vmware.photon.controller.model.resources.ComputeService.ComputeState) ResourceEnumerationTaskState(com.vmware.photon.controller.model.tasks.ResourceEnumerationTaskService.ResourceEnumerationTaskState) 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) ProvisioningUtils.createServiceURI(com.vmware.photon.controller.model.tasks.ProvisioningUtils.createServiceURI) GCPConstants(com.vmware.photon.controller.model.adapters.gcp.constants.GCPConstants) TimeUnit(java.util.concurrent.TimeUnit) JsonFactory(com.google.api.client.json.JsonFactory) Collections(java.util.Collections) ComputeState(com.vmware.photon.controller.model.resources.ComputeService.ComputeState) QueryTask(com.vmware.xenon.services.common.QueryTask) Query(com.vmware.xenon.services.common.QueryTask.Query)

Example 3 with PowerState

use of com.vmware.photon.controller.model.resources.ComputeService.PowerState in project photon-model by vmware.

the class VSphereAdapterPowerService method handlePowerRequest.

private void handlePowerRequest(ProvisionContext ctx, ComputePowerRequest request) {
    ctx.pool.submit(ctx.getAdapterManagementReference(), ctx.vSphereCredentials, (connection, ce) -> {
        if (ctx.fail(ce)) {
            return;
        }
        PowerStateClient client = new PowerStateClient(connection);
        ManagedObjectReference vmMoRef;
        try {
            vmMoRef = getVmMoRef(ctx.child);
        } catch (Exception e) {
            ctx.failWithMessage("Cannot extract MoRef from compute state", e);
            return;
        }
        if (vmMoRef == null) {
            ctx.failWithMessage("No VM MoRef found in compute state");
            return;
        }
        PowerState currentState;
        try {
            currentState = client.getPowerState(vmMoRef);
        } catch (Exception e) {
            // probably should ignore this error and assume power state has to change
            ctx.failWithMessage("Cannot get current power state of vmMoRef" + VimUtils.convertMoRefToString(vmMoRef), e);
            return;
        }
        if (currentState == request.powerState) {
            ctx.mgr.finishTask();
            return;
        }
        try {
            long politenessDeadlineMicros = computeDeadline(ctx.task);
            client.changePowerState(vmMoRef, request.powerState, request.powerTransition, politenessDeadlineMicros);
        } catch (Exception e) {
            ctx.failWithMessage("cannot change power state of vmMoRef " + VimUtils.convertMoRefToString(vmMoRef), e);
            return;
        }
        patchComputeAndCompleteRequest(request, ctx);
    });
}
Also used : PowerState(com.vmware.photon.controller.model.resources.ComputeService.PowerState) ManagedObjectReference(com.vmware.vim25.ManagedObjectReference)

Example 4 with PowerState

use of com.vmware.photon.controller.model.resources.ComputeService.PowerState in project photon-model by vmware.

the class PowerStateClient method changePowerState.

/**
 * @param vm
 * @param targetState
 * @param transition
 * @param politenessDeadlineMicros Used only when doing a soft power-off,
 *         how long to be polite before doing hard power-off.
 * @throws Exception
 */
public void changePowerState(ManagedObjectReference vm, PowerState targetState, PowerTransition transition, long politenessDeadlineMicros) throws Exception {
    PowerState currentState = getPowerState(vm);
    if (currentState == targetState) {
        return;
    }
    ManagedObjectReference task;
    if (targetState == PowerState.ON) {
        task = getVimPort().powerOnVMTask(vm, null);
        awaitTaskEnd(task);
        return;
    }
    if (currentState == PowerState.ON && targetState == PowerState.SUSPEND) {
        task = getVimPort().suspendVMTask(vm);
        awaitTaskEnd(task);
        return;
    }
    if (currentState == PowerState.SUSPEND && targetState == PowerState.OFF) {
        // In SUSPENDED state only "Power.Off" and "Power.On" operations are valid, "Power.Off" corresponds to HardPowerOff
        hardPowerOff(vm);
    }
    if (currentState == PowerState.ON && targetState == PowerState.OFF) {
        if (transition == PowerTransition.SOFT) {
            softPowerOff(vm, politenessDeadlineMicros);
        } else {
            hardPowerOff(vm);
        }
    }
}
Also used : PowerState(com.vmware.photon.controller.model.resources.ComputeService.PowerState) VirtualMachinePowerState(com.vmware.vim25.VirtualMachinePowerState) ManagedObjectReference(com.vmware.vim25.ManagedObjectReference)

Aggregations

PowerState (com.vmware.photon.controller.model.resources.ComputeService.PowerState)4 ManagedObjectReference (com.vmware.vim25.ManagedObjectReference)2 GoogleCredential (com.google.api.client.googleapis.auth.oauth2.GoogleCredential)1 GoogleNetHttpTransport (com.google.api.client.googleapis.javanet.GoogleNetHttpTransport)1 HttpTransport (com.google.api.client.http.HttpTransport)1 JsonFactory (com.google.api.client.json.JsonFactory)1 JacksonFactory (com.google.api.client.json.jackson2.JacksonFactory)1 Compute (com.google.api.services.compute.Compute)1 Instances (com.google.api.services.compute.Compute.Instances)1 ComputeScopes (com.google.api.services.compute.ComputeScopes)1 AccessConfig (com.google.api.services.compute.model.AccessConfig)1 AttachedDisk (com.google.api.services.compute.model.AttachedDisk)1 AttachedDiskInitializeParams (com.google.api.services.compute.model.AttachedDiskInitializeParams)1 Instance (com.google.api.services.compute.model.Instance)1 InstanceList (com.google.api.services.compute.model.InstanceList)1 NetworkInterface (com.google.api.services.compute.model.NetworkInterface)1 ServiceAccount (com.google.api.services.compute.model.ServiceAccount)1 InstanceViewStatus (com.microsoft.azure.management.compute.InstanceViewStatus)1 VirtualMachineInner (com.microsoft.azure.management.compute.implementation.VirtualMachineInner)1 GCPConstants (com.vmware.photon.controller.model.adapters.gcp.constants.GCPConstants)1