Search in sources :

Example 31 with Operation

use of com.google.api.services.compute.model.Operation in project terra-cloud-resource-lib by DataBiosphere.

the class NetworkUtils method exceuteCreateNetwork.

/**
 * Creates a new Network in a given GCP project for testing.
 */
public static Network exceuteCreateNetwork(String projectId, boolean autoCreateSubnetworks) throws Exception {
    String netWorkName = randomNetworkName();
    Network network = new Network().setName(netWorkName).setAutoCreateSubnetworks(autoCreateSubnetworks);
    Operation networkOperation = getCloudComputeCow().networks().insert(projectId, network).execute();
    OperationTestUtils.pollAndAssertSuccess(getCloudComputeCow().globalOperations().operationCow(projectId, networkOperation), Duration.ofSeconds(5), Duration.ofSeconds(100));
    return getCloudComputeCow().networks().get(projectId, netWorkName).execute();
}
Also used : Network(com.google.api.services.compute.model.Network) Operation(com.google.api.services.compute.model.Operation)

Example 32 with Operation

use of com.google.api.services.compute.model.Operation in project cloudbreak by hortonworks.

the class GcpInstanceStateCheckerTest method checkBasedOnOperationWhenGetOperationReturns.

@ParameterizedTest(name = "checkBasedOnOperationWhenGetOperationReturns with operation status: {0}, type: {1} then the expected instance status is: {3}")
@MethodSource("checkBasedOnOperationWhenGetOperationReturnsData")
void checkBasedOnOperationWhenGetOperationReturns(String operationStatus, String operationType, boolean operationFinished, InstanceStatus expectedInstanceStatus) throws Exception {
    stubGcpContext();
    CloudInstance cloudInstance = getCloudInstanceWithOperation(AVAILABILITY_ZONE, OPERATION_ID);
    Compute.ZoneOperations.Get get = mock(Compute.ZoneOperations.Get.class);
    Operation operation = getOperation(OPERATION_ID, operationStatus, operationType);
    when(get.execute()).thenReturn(operation);
    when(gcpStackUtil.zoneOperation(gcpCompute, PROJECT_ID, OPERATION_ID, AVAILABILITY_ZONE)).thenReturn(get);
    when(gcpStackUtil.isOperationFinished(operation)).thenReturn(operationFinished);
    List<CloudVmInstanceStatus> vmStatuses = underTest.checkBasedOnOperation(gcpContext, List.of(cloudInstance));
    verify(gcpStackUtil).zoneOperation(gcpCompute, PROJECT_ID, OPERATION_ID, AVAILABILITY_ZONE);
    assertFalse(vmStatuses.isEmpty());
    assertTrue(vmStatuses.stream().allMatch(vmStatus -> expectedInstanceStatus.equals(vmStatus.getStatus())));
}
Also used : AbstractGcpComputeBaseResourceChecker(com.sequenceiq.cloudbreak.cloud.gcp.service.checker.AbstractGcpComputeBaseResourceChecker) Instance(com.google.api.services.compute.model.Instance) Mock(org.mockito.Mock) CloudInstance(com.sequenceiq.cloudbreak.cloud.model.CloudInstance) Mockito.verifyNoInteractions(org.mockito.Mockito.verifyNoInteractions) GoogleJsonResponseException(com.google.api.client.googleapis.json.GoogleJsonResponseException) Assertions.assertFalse(org.junit.jupiter.api.Assertions.assertFalse) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) GcpContext(com.sequenceiq.cloudbreak.cloud.gcp.context.GcpContext) CloudVmInstanceStatus(com.sequenceiq.cloudbreak.cloud.model.CloudVmInstanceStatus) MethodSource(org.junit.jupiter.params.provider.MethodSource) GcpStackUtil(com.sequenceiq.cloudbreak.cloud.gcp.util.GcpStackUtil) InjectMocks(org.mockito.InjectMocks) MockitoExtension(org.mockito.junit.jupiter.MockitoExtension) HttpHeaders(com.google.api.client.http.HttpHeaders) GoogleJsonError(com.google.api.client.googleapis.json.GoogleJsonError) IOException(java.io.IOException) Mockito.when(org.mockito.Mockito.when) HttpResponseException(com.google.api.client.http.HttpResponseException) Operation(com.google.api.services.compute.model.Operation) Mockito.verify(org.mockito.Mockito.verify) Test(org.junit.jupiter.api.Test) List(java.util.List) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) HttpStatusCodes(com.google.api.client.http.HttpStatusCodes) InstanceStatus(com.sequenceiq.cloudbreak.cloud.model.InstanceStatus) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) Assertions(org.junit.jupiter.api.Assertions) NotNull(org.jetbrains.annotations.NotNull) Compute(com.google.api.services.compute.Compute) GcpResourceException(com.sequenceiq.cloudbreak.cloud.gcp.GcpResourceException) Mockito.mock(org.mockito.Mockito.mock) CloudVmInstanceStatus(com.sequenceiq.cloudbreak.cloud.model.CloudVmInstanceStatus) CloudInstance(com.sequenceiq.cloudbreak.cloud.model.CloudInstance) Operation(com.google.api.services.compute.model.Operation) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 33 with Operation

use of com.google.api.services.compute.model.Operation in project cloudbreak by hortonworks.

the class GcpInstanceStateCheckerTest method checkBasedOnOperationWhenGetOperationSucceedsAndFinishedButTheOperationTypeIsNotSupported.

@Test
void checkBasedOnOperationWhenGetOperationSucceedsAndFinishedButTheOperationTypeIsNotSupported() throws Exception {
    stubGcpContext();
    CloudInstance cloudInstance = getCloudInstanceWithOperation(AVAILABILITY_ZONE, OPERATION_ID);
    Compute.ZoneOperations.Get get = mock(Compute.ZoneOperations.Get.class);
    Operation operation = getOperation(OPERATION_ID, "DONE", "UNKNOWN");
    when(get.execute()).thenReturn(operation);
    when(gcpStackUtil.zoneOperation(gcpCompute, PROJECT_ID, OPERATION_ID, AVAILABILITY_ZONE)).thenReturn(get);
    when(gcpStackUtil.isOperationFinished(operation)).thenReturn(Boolean.TRUE);
    Assertions.assertThrows(GcpResourceException.class, () -> underTest.checkBasedOnOperation(gcpContext, List.of(cloudInstance)));
    verify(gcpStackUtil).zoneOperation(gcpCompute, PROJECT_ID, OPERATION_ID, AVAILABILITY_ZONE);
}
Also used : CloudInstance(com.sequenceiq.cloudbreak.cloud.model.CloudInstance) Operation(com.google.api.services.compute.model.Operation) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 34 with Operation

use of com.google.api.services.compute.model.Operation in project cloudbreak by hortonworks.

the class GcpFirewallInResourceBuilderTest method testDeleteWhenEverythingGoesFine.

@Test
public void testDeleteWhenEverythingGoesFine() throws Exception {
    CloudResource resource = new CloudResource.Builder().type(ResourceType.GCP_FIREWALL_IN).status(CommonStatus.CREATED).group("master").name("super").instanceId("id-123").params(new HashMap<>()).persistent(true).build();
    GcpContext gcpContext = mock(GcpContext.class);
    AuthenticatedContext authenticatedContext = mock(AuthenticatedContext.class);
    Network network = mock(Network.class);
    Compute compute = mock(Compute.class);
    Compute.Firewalls firewalls = mock(Compute.Firewalls.class);
    Compute.Firewalls.Delete firewallsDelete = mock(Compute.Firewalls.Delete.class);
    Operation operation = mock(Operation.class);
    when(gcpContext.getCompute()).thenReturn(compute);
    when(gcpContext.getProjectId()).thenReturn("id");
    when(compute.firewalls()).thenReturn(firewalls);
    when(firewalls.delete(anyString(), anyString())).thenReturn(firewallsDelete);
    when(firewallsDelete.execute()).thenReturn(operation);
    when(operation.getName()).thenReturn("name");
    CloudResource delete = underTest.delete(gcpContext, authenticatedContext, resource, network);
    Assert.assertEquals(ResourceType.GCP_FIREWALL_IN, delete.getType());
    Assert.assertEquals(CommonStatus.CREATED, delete.getStatus());
    Assert.assertEquals("super", delete.getName());
    Assert.assertEquals("master", delete.getGroup());
    Assert.assertEquals("id-123", delete.getInstanceId());
}
Also used : GcpContext(com.sequenceiq.cloudbreak.cloud.gcp.context.GcpContext) Network(com.sequenceiq.cloudbreak.cloud.model.Network) Compute(com.google.api.services.compute.Compute) CloudResource(com.sequenceiq.cloudbreak.cloud.model.CloudResource) AuthenticatedContext(com.sequenceiq.cloudbreak.cloud.context.AuthenticatedContext) Operation(com.google.api.services.compute.model.Operation) Test(org.junit.jupiter.api.Test)

Example 35 with Operation

use of com.google.api.services.compute.model.Operation in project cloudbreak by hortonworks.

the class GcpFirewallInternalResourceBuilderTest method testDeleteWhenEverythingGoesFine.

@Test
public void testDeleteWhenEverythingGoesFine() throws Exception {
    CloudResource resource = new CloudResource.Builder().type(ResourceType.GCP_FIREWALL_INTERNAL).status(CommonStatus.CREATED).group("master").name("super").instanceId("id-123").params(new HashMap<>()).persistent(true).build();
    GcpContext gcpContext = mock(GcpContext.class);
    AuthenticatedContext authenticatedContext = mock(AuthenticatedContext.class);
    Network network = mock(Network.class);
    Compute compute = mock(Compute.class);
    Compute.Firewalls firewalls = mock(Compute.Firewalls.class);
    Compute.Firewalls.Delete firewallsDelete = mock(Compute.Firewalls.Delete.class);
    Operation operation = mock(Operation.class);
    when(gcpContext.getCompute()).thenReturn(compute);
    when(gcpContext.getProjectId()).thenReturn("id");
    when(compute.firewalls()).thenReturn(firewalls);
    when(firewalls.delete(anyString(), anyString())).thenReturn(firewallsDelete);
    when(firewallsDelete.execute()).thenReturn(operation);
    when(operation.getName()).thenReturn("name");
    CloudResource delete = underTest.delete(gcpContext, authenticatedContext, resource, network);
    Assert.assertEquals(ResourceType.GCP_FIREWALL_INTERNAL, delete.getType());
    Assert.assertEquals(CommonStatus.CREATED, delete.getStatus());
    Assert.assertEquals("super", delete.getName());
    Assert.assertEquals("master", delete.getGroup());
    Assert.assertEquals("id-123", delete.getInstanceId());
}
Also used : GcpContext(com.sequenceiq.cloudbreak.cloud.gcp.context.GcpContext) Network(com.sequenceiq.cloudbreak.cloud.model.Network) Compute(com.google.api.services.compute.Compute) CloudResource(com.sequenceiq.cloudbreak.cloud.model.CloudResource) AuthenticatedContext(com.sequenceiq.cloudbreak.cloud.context.AuthenticatedContext) Operation(com.google.api.services.compute.model.Operation) Test(org.junit.jupiter.api.Test)

Aggregations

Operation (com.google.api.services.compute.model.Operation)96 Compute (com.google.api.services.compute.Compute)40 GoogleJsonResponseException (com.google.api.client.googleapis.json.GoogleJsonResponseException)37 IOException (java.io.IOException)37 GcpResourceException (com.sequenceiq.cloudbreak.cloud.gcp.GcpResourceException)23 CloudResource (com.sequenceiq.cloudbreak.cloud.model.CloudResource)21 Test (org.junit.Test)19 ArrayList (java.util.ArrayList)17 Instance (com.google.api.services.compute.model.Instance)15 CloudInstance (com.sequenceiq.cloudbreak.cloud.model.CloudInstance)15 AutoScalingData (org.apache.druid.indexing.overlord.autoscaling.AutoScalingData)14 GcpContext (com.sequenceiq.cloudbreak.cloud.gcp.context.GcpContext)12 InstanceGroupManagersListManagedInstancesResponse (com.google.api.services.compute.model.InstanceGroupManagersListManagedInstancesResponse)10 Location (com.sequenceiq.cloudbreak.cloud.model.Location)10 HashMap (java.util.HashMap)10 InstanceTemplate (com.sequenceiq.cloudbreak.cloud.model.InstanceTemplate)9 Disk (com.google.api.services.compute.model.Disk)8 InstanceGroupManagersDeleteInstancesRequest (com.google.api.services.compute.model.InstanceGroupManagersDeleteInstancesRequest)8 AuthenticatedContext (com.sequenceiq.cloudbreak.cloud.context.AuthenticatedContext)8 CloudContext (com.sequenceiq.cloudbreak.cloud.context.CloudContext)8