use of com.google.api.services.compute.Compute in project elasticsearch by elastic.
the class RetryHttpInitializerWrapperTests method testRetryWaitTooLong.
public void testRetryWaitTooLong() throws Exception {
TimeValue maxWaitTime = TimeValue.timeValueMillis(10);
int maxRetryTimes = 50;
FailThenSuccessBackoffTransport fakeTransport = new FailThenSuccessBackoffTransport(HttpStatusCodes.STATUS_CODE_SERVER_ERROR, maxRetryTimes);
JsonFactory jsonFactory = new JacksonFactory();
MockGoogleCredential credential = RetryHttpInitializerWrapper.newMockCredentialBuilder().build();
MockSleeper oneTimeSleeper = new MockSleeper() {
@Override
public void sleep(long millis) throws InterruptedException {
Thread.sleep(maxWaitTime.getMillis());
// important number, use this to get count
super.sleep(0);
}
};
RetryHttpInitializerWrapper retryHttpInitializerWrapper = new RetryHttpInitializerWrapper(credential, oneTimeSleeper, maxWaitTime);
Compute client = new Compute.Builder(fakeTransport, jsonFactory, null).setHttpRequestInitializer(retryHttpInitializerWrapper).setApplicationName("test").build();
HttpRequest request1 = client.getRequestFactory().buildRequest("Get", new GenericUrl("http://elasticsearch.com"), null);
try {
request1.execute();
fail("Request should fail if wait too long");
} catch (HttpResponseException e) {
assertThat(e.getStatusCode(), equalTo(HttpStatusCodes.STATUS_CODE_SERVER_ERROR));
// should only retry once.
assertThat(oneTimeSleeper.getCount(), lessThan(maxRetryTimes));
}
}
use of com.google.api.services.compute.Compute in project elasticsearch by elastic.
the class RetryHttpInitializerWrapperTests method testSimpleRetry.
public void testSimpleRetry() throws Exception {
FailThenSuccessBackoffTransport fakeTransport = new FailThenSuccessBackoffTransport(HttpStatusCodes.STATUS_CODE_SERVER_ERROR, 3);
MockGoogleCredential credential = RetryHttpInitializerWrapper.newMockCredentialBuilder().build();
MockSleeper mockSleeper = new MockSleeper();
RetryHttpInitializerWrapper retryHttpInitializerWrapper = new RetryHttpInitializerWrapper(credential, mockSleeper, TimeValue.timeValueSeconds(5));
Compute client = new Compute.Builder(fakeTransport, new JacksonFactory(), null).setHttpRequestInitializer(retryHttpInitializerWrapper).setApplicationName("test").build();
HttpRequest request = client.getRequestFactory().buildRequest("Get", new GenericUrl("http://elasticsearch.com"), null);
HttpResponse response = request.execute();
assertThat(mockSleeper.getCount(), equalTo(3));
assertThat(response.getStatusCode(), equalTo(200));
}
use of com.google.api.services.compute.Compute in project halyard by spinnaker.
the class GoogleDistributedService method deleteVersion.
@Override
default void deleteVersion(AccountDeploymentDetails<GoogleAccount> details, ServiceSettings settings, Integer version) {
String migName = getVersionedName(version);
String zone = settings.getLocation();
String project = details.getAccount().getProject();
Compute compute = GoogleProviderUtils.getCompute(details);
InstanceGroupManager mig;
try {
mig = compute.instanceGroupManagers().get(project, zone, migName).execute();
} catch (GoogleJsonResponseException e) {
if (e.getStatusCode() == 404) {
return;
} else {
throw new HalException(FATAL, "Failed to load mig " + migName + " in " + zone, e);
}
} catch (IOException e) {
throw new HalException(FATAL, "Failed to load mig " + migName + " in " + zone, e);
}
try {
GoogleProviderUtils.waitOnZoneOperation(compute, project, zone, compute.instanceGroupManagers().delete(project, zone, migName).execute());
} catch (IOException e) {
throw new HalException(FATAL, "Failed to delete mig " + migName + " in " + zone, e);
}
String instanceTemplateName = mig.getInstanceTemplate();
instanceTemplateName = instanceTemplateName.substring(instanceTemplateName.lastIndexOf('/') + 1);
try {
GoogleProviderUtils.waitOnGlobalOperation(compute, project, compute.instanceTemplates().delete(project, instanceTemplateName).execute());
} catch (IOException e) {
throw new HalException(FATAL, "Failed to delete template " + instanceTemplateName + " in " + zone, e);
}
}
use of com.google.api.services.compute.Compute in project halyard by spinnaker.
the class GoogleDistributedService method getRunningServiceDetails.
@Override
default RunningServiceDetails getRunningServiceDetails(AccountDeploymentDetails<GoogleAccount> details, SpinnakerRuntimeSettings runtimeSettings) {
ServiceSettings settings = runtimeSettings.getServiceSettings(getService());
RunningServiceDetails result = new RunningServiceDetails();
// All GCE load balancing is done via consul
result.setLoadBalancer(new RunningServiceDetails.LoadBalancer().setExists(true));
Compute compute = GoogleProviderUtils.getCompute(details);
GoogleAccount account = details.getAccount();
List<InstanceGroupManager> migs;
try {
migs = compute.instanceGroupManagers().list(account.getProject(), settings.getLocation()).execute().getItems();
if (migs == null) {
migs = Collections.emptyList();
}
} catch (IOException e) {
throw new HalException(FATAL, "Failed to load MIGS: " + e.getMessage(), e);
}
boolean consulEnabled = getSidecars(runtimeSettings).stream().anyMatch(s -> s.getService().getType().equals(SpinnakerService.Type.CONSUL_CLIENT));
Set<String> healthyConsulInstances = consulEnabled ? getConsulServerService().connectToPrimaryService(details, runtimeSettings).serviceHealth(getService().getCanonicalName(), true).stream().map(s -> s != null && s.getNode() != null ? s.getNode().getNodeName() : null).filter(Objects::nonNull).collect(Collectors.toSet()) : new HashSet<>();
String serviceName = getService().getServiceName();
migs = migs.stream().filter(ig -> ig.getName().startsWith(serviceName + "-v")).collect(Collectors.toList());
Map<Integer, List<RunningServiceDetails.Instance>> instances = migs.stream().reduce(new HashMap<>(), (map, mig) -> {
Names names = Names.parseName(mig.getName());
Integer version = names.getSequence();
List<RunningServiceDetails.Instance> computeInstances;
try {
List<ManagedInstance> managedInstances = compute.instanceGroupManagers().listManagedInstances(account.getProject(), settings.getLocation(), mig.getName()).execute().getManagedInstances();
if (managedInstances == null) {
managedInstances = new ArrayList<>();
}
computeInstances = managedInstances.stream().map(i -> {
String instanceUrl = i.getInstance();
String instanceStatus = i.getInstanceStatus();
boolean running = instanceStatus != null && instanceStatus.equalsIgnoreCase("running");
String instanceName = instanceUrl.substring(instanceUrl.lastIndexOf('/') + 1, instanceUrl.length());
return new RunningServiceDetails.Instance().setId(instanceName).setLocation(settings.getLocation()).setRunning(running).setHealthy(!consulEnabled || healthyConsulInstances.contains(instanceName));
}).collect(Collectors.toList());
} catch (IOException e) {
throw new HalException(FATAL, "Failed to load target pools for " + serviceName, e);
}
map.put(version, computeInstances);
return map;
}, (m1, m2) -> {
m1.putAll(m2);
return m1;
});
result.setInstances(instances);
return result;
}
use of com.google.api.services.compute.Compute in project halyard by spinnaker.
the class GoogleProviderUtils method ensureSpinnakerNetworkExists.
static String ensureSpinnakerNetworkExists(AccountDeploymentDetails<GoogleAccount> details) {
String networkName = getNetworkName();
String project = details.getAccount().getProject();
Compute compute = getCompute(details);
boolean exists = true;
try {
compute.networks().get(project, networkName).execute();
} catch (GoogleJsonResponseException e) {
if (e.getStatusCode() == 404) {
exists = false;
} else {
throw new HalException(FATAL, "Google error encountered retrieving network: " + e.getMessage(), e);
}
} catch (IOException e) {
throw new HalException(FATAL, "Failed to check if spinnaker network exists: " + e.getMessage(), e);
}
if (!exists) {
String networkUrl;
Network network = new Network().setAutoCreateSubnetworks(true).setName(networkName).setDescription("Spinnaker network auto-created by Halyard");
try {
DaemonTaskHandler.message("Creating a spinnaker network...");
Operation operation = compute.networks().insert(project, network).execute();
networkUrl = operation.getTargetLink();
GoogleProviderUtils.waitOnGlobalOperation(compute, project, operation);
} catch (IOException e) {
throw new HalException(FATAL, "Failed to create Spinnaker network: " + e.getMessage(), e);
}
Firewall.Allowed allowSsh = new Firewall.Allowed().setPorts(Collections.singletonList("22")).setIPProtocol("tcp");
Firewall firewallSsh = new Firewall().setNetwork(networkUrl).setAllowed(Collections.singletonList(allowSsh)).setName(networkName + "-allow-ssh").setSourceRanges(Collections.singletonList("0.0.0.0/0"));
Firewall.Allowed allowInternalTcp = new Firewall.Allowed().setPorts(Collections.singletonList("1-65535")).setIPProtocol("tcp");
Firewall.Allowed allowInternalUdp = new Firewall.Allowed().setPorts(Collections.singletonList("1-65535")).setIPProtocol("udp");
Firewall.Allowed allowInternalIcmp = new Firewall.Allowed().setIPProtocol("icmp");
List<Firewall.Allowed> allowInteral = new ArrayList<>();
allowInteral.add(allowInternalTcp);
allowInteral.add(allowInternalUdp);
allowInteral.add(allowInternalIcmp);
Firewall firewallInternal = new Firewall().setNetwork(networkUrl).setAllowed(allowInteral).setName(networkName + "-allow-internal").setSourceRanges(Collections.singletonList("10.0.0.0/8"));
try {
DaemonTaskHandler.message("Adding firewall rules...");
compute.firewalls().insert(project, firewallSsh).execute();
compute.firewalls().insert(project, firewallInternal).execute();
} catch (IOException e) {
throw new HalException(FATAL, "Failed to create Firewall rule network: " + e.getMessage(), e);
}
}
return String.format("projects/%s/global/networks/%s", project, networkName);
}
Aggregations