use of com.google.api.services.notebooks.v1.model.Instance in project terra-workspace-manager by DataBiosphere.
the class CreateAiNotebookInstanceStepTest method setFieldsThrowsForReservedMetadataKeys.
@Test
public void setFieldsThrowsForReservedMetadataKeys() {
assertThrows(ReservedMetadataKeyException.class, () -> CreateAiNotebookInstanceStep.setFields(new ApiGcpAiNotebookInstanceCreationParameters().metadata(Map.of("terra-workspace-id", "fakeworkspaceid")), "foo@bar.com", "workspaceId", "server-id", new Instance()));
assertThrows(ReservedMetadataKeyException.class, () -> CreateAiNotebookInstanceStep.setFields(new ApiGcpAiNotebookInstanceCreationParameters().metadata(Map.of("terra-cli-server", "fakeserver")), "foo@bar.com", "workspaceId", "server-id", new Instance()));
assertThrows(ReservedMetadataKeyException.class, () -> CreateAiNotebookInstanceStep.setFields(new ApiGcpAiNotebookInstanceCreationParameters().metadata(Map.of("proxy-mode", "mail")), "foo@bar.com", "workspaceId", "server-id", new Instance()));
}
use of com.google.api.services.notebooks.v1.model.Instance in project terra-cloud-resource-lib by DataBiosphere.
the class AIPlatformNotebooksCowTest method updateNotebookInstanceMetadata.
@Test
public void updateNotebookInstanceMetadata() throws Exception {
InstanceName instanceName = defaultInstanceName().instanceId("instance-with-foobar-metadata").build();
createInstance(instanceName);
Instance retrievedInstance = notebooks.instances().get(instanceName).execute();
assertEquals(instanceName.formatName(), retrievedInstance.getName());
notebooks.instances().updateMetadataItems(instanceName.formatName(), ImmutableMap.of("foo", "bar", "count", "3")).execute();
retrievedInstance = notebooks.instances().get(instanceName).execute();
var metadata = retrievedInstance.getMetadata();
assertEquals("bar", metadata.get("foo"));
assertEquals("3", metadata.get("count"));
notebooks.instances().delete(instanceName).execute();
}
use of com.google.api.services.notebooks.v1.model.Instance in project OpenSearch by opensearch-project.
the class EC2RetriesTests method testEC2DiscoveryRetriesOnRateLimiting.
public void testEC2DiscoveryRetriesOnRateLimiting() throws IOException {
final String accessKey = "ec2_access";
final List<String> hosts = Collections.singletonList("127.0.0.1:9300");
final Map<String, Integer> failedRequests = new ConcurrentHashMap<>();
// retry the same request 5 times at most
final int maxRetries = randomIntBetween(1, 5);
httpServer.createContext("/", exchange -> {
if (exchange.getRequestMethod().equals(HttpMethodName.POST.name())) {
final String request = Streams.readFully(exchange.getRequestBody()).utf8ToString();
final String userAgent = exchange.getRequestHeaders().getFirst("User-Agent");
if (userAgent != null && userAgent.startsWith("aws-sdk-java")) {
final String auth = exchange.getRequestHeaders().getFirst("Authorization");
if (auth == null || auth.contains(accessKey) == false) {
throw new IllegalArgumentException("wrong access key: " + auth);
}
if (failedRequests.compute(exchange.getRequestHeaders().getFirst("Amz-sdk-invocation-id"), (requestId, count) -> (count == null ? 0 : count) + 1) < maxRetries) {
exchange.sendResponseHeaders(HttpStatus.SC_SERVICE_UNAVAILABLE, -1);
return;
}
// Simulate an EC2 DescribeInstancesResponse
byte[] responseBody = null;
for (NameValuePair parse : URLEncodedUtils.parse(request, UTF_8)) {
if ("Action".equals(parse.getName())) {
responseBody = generateDescribeInstancesResponse(hosts.stream().map(address -> new Instance().withPublicIpAddress(address)).collect(Collectors.toList()));
break;
}
}
responseBody = responseBody == null ? new byte[0] : responseBody;
exchange.getResponseHeaders().set("Content-Type", "text/xml; charset=UTF-8");
exchange.sendResponseHeaders(HttpStatus.SC_OK, responseBody.length);
exchange.getResponseBody().write(responseBody);
return;
}
}
fail("did not send response");
});
try (Ec2DiscoveryPlugin plugin = new Ec2DiscoveryPlugin(buildSettings(accessKey))) {
final SeedHostsProvider seedHostsProvider = plugin.getSeedHostProviders(transportService, networkService).get("ec2").get();
final SeedHostsResolver resolver = new SeedHostsResolver("test", Settings.EMPTY, transportService, seedHostsProvider);
resolver.start();
final List<TransportAddress> addressList = seedHostsProvider.getSeedAddresses(null);
assertThat(addressList, Matchers.hasSize(1));
assertThat(addressList.get(0).toString(), is(hosts.get(0)));
assertThat(failedRequests, aMapWithSize(1));
assertThat(failedRequests.values().iterator().next(), is(maxRetries));
}
}
use of com.google.api.services.notebooks.v1.model.Instance in project OpenSearch by opensearch-project.
the class AwsEc2SeedHostsProvider method fetchDynamicNodes.
protected List<TransportAddress> fetchDynamicNodes() {
final List<TransportAddress> dynamicHosts = new ArrayList<>();
final DescribeInstancesResult descInstances;
try (AmazonEc2Reference clientReference = awsEc2Service.client()) {
// Query EC2 API based on AZ, instance state, and tag.
// NOTE: we don't filter by security group during the describe instances request for two reasons:
// 1. differences in VPCs require different parameters during query (ID vs Name)
// 2. We want to use two different strategies: (all security groups vs. any security groups)
descInstances = SocketAccess.doPrivileged(() -> clientReference.get().describeInstances(buildDescribeInstancesRequest()));
} catch (final AmazonClientException e) {
logger.info("Exception while retrieving instance list from AWS API: {}", e.getMessage());
logger.debug("Full exception:", e);
return dynamicHosts;
}
logger.trace("finding seed nodes...");
for (final Reservation reservation : descInstances.getReservations()) {
for (final Instance instance : reservation.getInstances()) {
// lets see if we can filter based on groups
if (!groups.isEmpty()) {
final List<GroupIdentifier> instanceSecurityGroups = instance.getSecurityGroups();
final List<String> securityGroupNames = new ArrayList<>(instanceSecurityGroups.size());
final List<String> securityGroupIds = new ArrayList<>(instanceSecurityGroups.size());
for (final GroupIdentifier sg : instanceSecurityGroups) {
securityGroupNames.add(sg.getGroupName());
securityGroupIds.add(sg.getGroupId());
}
if (bindAnyGroup) {
// We check if we can find at least one group name or one group id in groups.
if (disjoint(securityGroupNames, groups) && disjoint(securityGroupIds, groups)) {
logger.trace("filtering out instance {} based on groups {}, not part of {}", instance.getInstanceId(), instanceSecurityGroups, groups);
// continue to the next instance
continue;
}
} else {
// We need tp match all group names or group ids, otherwise we ignore this instance
if (!(securityGroupNames.containsAll(groups) || securityGroupIds.containsAll(groups))) {
logger.trace("filtering out instance {} based on groups {}, does not include all of {}", instance.getInstanceId(), instanceSecurityGroups, groups);
// continue to the next instance
continue;
}
}
}
String address = null;
if (hostType.equals(PRIVATE_DNS)) {
address = instance.getPrivateDnsName();
} else if (hostType.equals(PRIVATE_IP)) {
address = instance.getPrivateIpAddress();
} else if (hostType.equals(PUBLIC_DNS)) {
address = instance.getPublicDnsName();
} else if (hostType.equals(PUBLIC_IP)) {
address = instance.getPublicIpAddress();
} else if (hostType.startsWith(TAG_PREFIX)) {
// Reading the node host from its metadata
final String tagName = hostType.substring(TAG_PREFIX.length());
logger.debug("reading hostname from [{}] instance tag", tagName);
final List<Tag> tags = instance.getTags();
for (final Tag tag : tags) {
if (tag.getKey().equals(tagName)) {
address = tag.getValue();
logger.debug("using [{}] as the instance address", address);
}
}
} else {
throw new IllegalArgumentException(hostType + " is unknown for discovery.ec2.host_type");
}
if (address != null) {
try {
final TransportAddress[] addresses = transportService.addressesFromString(address);
for (int i = 0; i < addresses.length; i++) {
logger.trace("adding {}, address {}, transport_address {}", instance.getInstanceId(), address, addresses[i]);
dynamicHosts.add(addresses[i]);
}
} catch (final Exception e) {
final String finalAddress = address;
logger.warn((Supplier<?>) () -> new ParameterizedMessage("failed to add {}, address {}", instance.getInstanceId(), finalAddress), e);
}
} else {
logger.trace("not adding {}, address is null, host_type {}", instance.getInstanceId(), hostType);
}
}
}
logger.debug("using dynamic transport addresses {}", dynamicHosts);
return dynamicHosts;
}
use of com.google.api.services.notebooks.v1.model.Instance in project OpenSearch by opensearch-project.
the class Ec2DiscoveryTests method buildDynamicHosts.
protected List<TransportAddress> buildDynamicHosts(Settings nodeSettings, int nodes, List<List<Tag>> tagsList) {
final String accessKey = "ec2_key";
try (Ec2DiscoveryPlugin plugin = new Ec2DiscoveryPlugin(buildSettings(accessKey))) {
AwsEc2SeedHostsProvider provider = new AwsEc2SeedHostsProvider(nodeSettings, transportService, plugin.ec2Service);
httpServer.createContext("/", exchange -> {
if (exchange.getRequestMethod().equals(HttpMethodName.POST.name())) {
final String request = Streams.readFully(exchange.getRequestBody()).toBytesRef().utf8ToString();
final String userAgent = exchange.getRequestHeaders().getFirst("User-Agent");
if (userAgent != null && userAgent.startsWith("aws-sdk-java")) {
final String auth = exchange.getRequestHeaders().getFirst("Authorization");
if (auth == null || auth.contains(accessKey) == false) {
throw new IllegalArgumentException("wrong access key: " + auth);
}
// Simulate an EC2 DescribeInstancesResponse
final Map<String, List<String>> tagsIncluded = new HashMap<>();
final String[] params = request.split("&");
Arrays.stream(params).filter(entry -> entry.startsWith("Filter.") && entry.contains("=tag%3A")).forEach(entry -> {
final int startIndex = "Filter.".length();
final int filterId = Integer.parseInt(entry.substring(startIndex, entry.indexOf(".", startIndex)));
tagsIncluded.put(entry.substring(entry.indexOf("=tag%3A") + "=tag%3A".length()), Arrays.stream(params).filter(param -> param.startsWith("Filter." + filterId + ".Value.")).map(param -> param.substring(param.indexOf("=") + 1)).collect(Collectors.toList()));
});
final List<Instance> instances = IntStream.range(1, nodes + 1).mapToObj(node -> {
final String instanceId = "node" + node;
final Instance instance = new Instance().withInstanceId(instanceId).withState(new InstanceState().withName(InstanceStateName.Running)).withPrivateDnsName(PREFIX_PRIVATE_DNS + instanceId + SUFFIX_PRIVATE_DNS).withPublicDnsName(PREFIX_PUBLIC_DNS + instanceId + SUFFIX_PUBLIC_DNS).withPrivateIpAddress(PREFIX_PRIVATE_IP + node).withPublicIpAddress(PREFIX_PUBLIC_IP + node);
if (tagsList != null) {
instance.setTags(tagsList.get(node - 1));
}
return instance;
}).filter(instance -> tagsIncluded.entrySet().stream().allMatch(entry -> instance.getTags().stream().filter(t -> t.getKey().equals(entry.getKey())).map(Tag::getValue).collect(Collectors.toList()).containsAll(entry.getValue()))).collect(Collectors.toList());
for (NameValuePair parse : URLEncodedUtils.parse(request, UTF_8)) {
if ("Action".equals(parse.getName())) {
final byte[] responseBody = generateDescribeInstancesResponse(instances);
exchange.getResponseHeaders().set("Content-Type", "text/xml; charset=UTF-8");
exchange.sendResponseHeaders(HttpStatus.SC_OK, responseBody.length);
exchange.getResponseBody().write(responseBody);
return;
}
}
}
}
fail("did not send response");
});
List<TransportAddress> dynamicHosts = provider.getSeedAddresses(null);
logger.debug("--> addresses found: {}", dynamicHosts);
return dynamicHosts;
} catch (IOException e) {
fail("Unexpected IOException");
return null;
}
}
Aggregations