use of com.google.api.services.serviceusage.v1.model.BatchEnableServicesRequest in project terra-resource-buffer by DataBiosphere.
the class EnableServicesStep method doStep.
@Override
public StepResult doStep(FlightContext flightContext) throws RetryException {
// Skip if enable apis is not set or empty.
if (gcpProjectConfig.getEnabledApis() == null || gcpProjectConfig.getEnabledApis().isEmpty()) {
return StepResult.getStepResultSuccess();
}
String projectId = flightContext.getWorkingMap().get(GOOGLE_PROJECT_ID, String.class);
try {
OperationCow<?> operation = serviceUsageCow.operations().operationCow(serviceUsageCow.services().batchEnable(projectIdToName(projectId), new BatchEnableServicesRequest().setServiceIds(gcpProjectConfig.getEnabledApis())).execute());
pollUntilSuccess(operation, Duration.ofSeconds(5), Duration.ofMinutes(5));
} catch (IOException | InterruptedException e) {
logger.info("Error enabling services GCP project, id: {}", projectId, e);
return new StepResult(StepStatus.STEP_RESULT_FAILURE_RETRY, e);
}
return StepResult.getStepResultSuccess();
}
use of com.google.api.services.serviceusage.v1.model.BatchEnableServicesRequest in project terra-cloud-resource-lib by DataBiosphere.
the class ServiceUsageCowTest method listAndEnableServices.
@Test
public void listAndEnableServices() throws Exception {
ServiceUsageCow serviceUsage = defaultServiceUsage();
Project project = ProjectUtils.executeCreateProject();
String projectName = projectIdToName(project.getProjectId());
String storageServiceName = serviceName(project, STORAGE_SERVICE_ID);
ListServicesResponse response1 = serviceUsage.services().list(projectName).setFilter(ENABLED_FILTER).execute();
assertNull(response1.getServices());
Operation operation = serviceUsage.services().batchEnable(projectName, new BatchEnableServicesRequest().setServiceIds(ImmutableList.of(STORAGE_SERVICE_ID))).execute();
OperationTestUtils.pollAndAssertSuccess(serviceUsage.operations().operationCow(operation), Duration.ofSeconds(5), Duration.ofSeconds(60));
ListServicesResponse response2 = serviceUsage.services().list(projectName).setFilter(ENABLED_FILTER).execute();
List<String> services2 = response2.getServices().stream().map(Service::getName).collect(Collectors.toList());
assertThat(services2, Matchers.hasItem(storageServiceName));
}
use of com.google.api.services.serviceusage.v1.model.BatchEnableServicesRequest in project terra-cloud-resource-lib by DataBiosphere.
the class ServiceUsageUtils method enableServices.
/**
* Enables batch services for a project.
*
* @param projectId: The projectId to enable services on.
* @param services: Services to be enabled. See {@link BatchEnableServicesRequest}
*/
public static void enableServices(String projectId, List<String> services) throws Exception {
Operation operation = getServiceUsageCow().services().batchEnable(projectIdToName(projectId), new BatchEnableServicesRequest().setServiceIds(services)).execute();
OperationTestUtils.pollAndAssertSuccess(serviceUsageCow.operations().operationCow(operation), Duration.ofSeconds(5), Duration.ofSeconds(100));
}
use of com.google.api.services.serviceusage.v1.model.BatchEnableServicesRequest in project jade-data-repo by DataBiosphere.
the class GoogleResourceService method enableServices.
private void enableServices(GoogleProjectResource projectResource) throws InterruptedException {
BatchEnableServicesRequest batchRequest = new BatchEnableServicesRequest().setServiceIds(projectResource.getServiceIds());
try {
ServiceUsage serviceUsage = serviceUsage();
String projectNumberString = "projects/" + projectResource.getGoogleProjectNumber();
logger.info("trying to get services for {} ({})", projectNumberString, projectResource.getGoogleProjectId());
ServiceUsage.Services.List list = serviceUsage.services().list(projectNumberString).setFilter(ENABLED_FILTER);
ListServicesResponse listServicesResponse = list.execute();
logger.info("found: " + String.join(", ", projectResource.getServiceIds()));
List<String> services = projectResource.getServiceIds().stream().map(s -> String.format("%s/services/%s", projectNumberString, s)).collect(Collectors.toList());
List<Service> serviceList = listServicesResponse.getServices();
List<String> actualServiceNames = Collections.emptyList();
if (serviceList != null) {
actualServiceNames = serviceList.stream().map(s -> s.getName()).collect(Collectors.toList());
}
if (actualServiceNames.containsAll(services)) {
logger.info("project already has the right resources enabled, skipping");
} else {
logger.info("project does not have all resources enabled");
ServiceUsage.Services.BatchEnable batchEnable = serviceUsage.services().batchEnable(projectNumberString, batchRequest);
long timeout = resourceConfiguration.getProjectCreateTimeoutSeconds();
blockUntilServiceOperationComplete(serviceUsage, batchEnable.execute(), timeout);
}
} catch (IOException | GeneralSecurityException e) {
throw new GoogleResourceException("Could not enable services", e);
}
}
Aggregations