use of com.google.api.services.compute.Compute in project cloudbreak by hortonworks.
the class GcpReservedIpResourceBuilder method delete.
@Override
public CloudResource delete(GcpContext context, AuthenticatedContext auth, CloudResource resource) throws Exception {
Compute compute = context.getCompute();
String projectId = context.getProjectId();
String region = context.getLocation().getRegion().value();
try {
Operation operation = compute.addresses().delete(projectId, region, resource.getName()).execute();
return createOperationAwareCloudResource(resource, operation);
} catch (GoogleJsonResponseException e) {
exceptionHandler(e, resource.getName(), resourceType());
return null;
}
}
use of com.google.api.services.compute.Compute in project cloudbreak by hortonworks.
the class GcpContextBuilder method contextInit.
@Override
public GcpContext contextInit(CloudContext context, AuthenticatedContext auth, Network network, List<CloudResource> resources, boolean build) {
CloudCredential credential = auth.getCloudCredential();
String projectId = GcpStackUtil.getProjectId(credential);
String serviceAccountId = GcpStackUtil.getServiceAccountId(credential);
Compute compute = GcpStackUtil.buildCompute(credential);
Location location = context.getLocation();
boolean noPublicIp = network != null ? GcpStackUtil.noPublicIp(network) : false;
return new GcpContext(context.getName(), location, projectId, serviceAccountId, compute, noPublicIp, PARALLEL_RESOURCE_REQUEST, build);
}
use of com.google.api.services.compute.Compute in project cloudbreak by hortonworks.
the class GcpFirewallInResourceBuilder method update.
@Override
public CloudResourceStatus update(GcpContext context, AuthenticatedContext auth, Group group, Network network, Security security, CloudResource resource) {
String projectId = context.getProjectId();
Compute compute = context.getCompute();
String resourceName = resource.getName();
try {
Firewall fireWall = compute.firewalls().get(projectId, resourceName).execute();
List<String> sourceRanges = getSourceRanges(security);
fireWall.setSourceRanges(sourceRanges);
Operation operation = compute.firewalls().update(projectId, resourceName, fireWall).execute();
CloudResource cloudResource = createOperationAwareCloudResource(resource, operation);
return checkResources(context, auth, Collections.singletonList(cloudResource)).get(0);
} catch (IOException e) {
throw new GcpResourceException("Failed to update resource!", GCP_FIREWALL_IN, resourceName, e);
}
}
use of com.google.api.services.compute.Compute in project cloudbreak by hortonworks.
the class GcpDeleteVpcTest method deleteNetwork.
@AfterSuite
@Parameters("vpcName")
public void deleteNetwork(@Optional("it-vpc") String vpcName) throws Exception {
springTestContextPrepareTestInstance();
String serviceAccountPrivateKey = ResourceUtil.readBase64EncodedContentFromResource(applicationContext, defaultP12File);
HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
PrivateKey privateKey = SecurityUtils.loadPrivateKeyFromKeyStore(SecurityUtils.getPkcs12KeyStore(), new ByteArrayInputStream(Base64.decodeBase64(serviceAccountPrivateKey)), "notasecret", "privatekey", "notasecret");
JacksonFactory jsonFactory = JacksonFactory.getDefaultInstance();
GoogleCredential googleCredential = new GoogleCredential.Builder().setTransport(httpTransport).setJsonFactory(jsonFactory).setServiceAccountId(defaultServiceAccountId).setServiceAccountScopes(Collections.singletonList(ComputeScopes.COMPUTE)).setServiceAccountPrivateKey(privateKey).build();
Compute compute = new Builder(httpTransport, jsonFactory, null).setApplicationName(defaultName).setHttpRequestInitializer(googleCredential).build();
Delete delete = compute.networks().delete(defaultProjectId, vpcName);
Operation operation = delete.execute();
if (operation.getHttpErrorStatusCode() != null) {
throw new IllegalStateException("gcp operation failed: " + operation.getHttpErrorMessage());
}
}
use of com.google.api.services.compute.Compute in project druid by druid-io.
the class GceAutoScaler method idToIpLookup.
/**
* Converts the IDs to IPs - this is actually never called from the outside but it is called once
* from inside the class if terminate is used instead of terminateWithIds
*/
@Override
public List<String> idToIpLookup(List<String> nodeIds) {
log.info("Asked IDs -> IPs for: [%s]", String.join(",", nodeIds));
if (nodeIds.isEmpty()) {
return new ArrayList<>();
}
final String project = envConfig.getProjectId();
final String zone = envConfig.getZoneName();
try {
Compute computeService = createComputeService();
Compute.Instances.List request = computeService.instances().list(project, zone);
request.setFilter(GceUtils.buildFilter(nodeIds, "name"));
List<String> instanceIps = new ArrayList<>();
InstanceList response;
do {
response = request.execute();
if (response.getItems() == null) {
continue;
}
for (Instance instance : response.getItems()) {
// Assuming that every server has at least one network interface...
String ip = instance.getNetworkInterfaces().get(0).getNetworkIP();
// it for maxScalingDuration time before doing anything else
if (ip != null && !"null".equals(ip)) {
instanceIps.add(ip);
} else {
// log and skip it
log.warn("Call returned null IP for %s, skipping", instance.getName());
}
}
request.setPageToken(response.getNextPageToken());
} while (response.getNextPageToken() != null);
return instanceIps;
} catch (Exception e) {
log.error(e, "Unable to convert IDs to IPs.");
}
return new ArrayList<>();
}
Aggregations