Search in sources :

Example 81 with Instance

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()));
}
Also used : Instance(com.google.api.services.notebooks.v1.model.Instance) ApiGcpAiNotebookInstanceCreationParameters(bio.terra.workspace.generated.model.ApiGcpAiNotebookInstanceCreationParameters) Test(org.junit.jupiter.api.Test) BaseUnitTest(bio.terra.workspace.common.BaseUnitTest)

Example 82 with 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();
}
Also used : Instance(com.google.api.services.notebooks.v1.model.Instance) Test(org.junit.jupiter.api.Test)

Example 83 with Instance

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));
    }
}
Also used : Matchers.aMapWithSize(org.hamcrest.Matchers.aMapWithSize) NoneCircuitBreakerService(org.opensearch.indices.breaker.NoneCircuitBreakerService) HttpStatus(org.apache.http.HttpStatus) Version(org.opensearch.Version) MockTransportService(org.opensearch.test.transport.MockTransportService) SeedHostsResolver(org.opensearch.discovery.SeedHostsResolver) NamedWriteableRegistry(org.opensearch.common.io.stream.NamedWriteableRegistry) Streams(org.opensearch.common.io.Streams) Map(java.util.Map) Instance(com.amazonaws.services.ec2.model.Instance) MockNioTransport(org.opensearch.transport.nio.MockNioTransport) SeedHostsProvider(org.opensearch.discovery.SeedHostsProvider) SuppressForbidden(org.opensearch.common.SuppressForbidden) UTF_8(java.nio.charset.StandardCharsets.UTF_8) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Matchers(org.hamcrest.Matchers) Settings(org.opensearch.common.settings.Settings) IOException(java.io.IOException) HttpMethodName(com.amazonaws.http.HttpMethodName) TransportService(org.opensearch.transport.TransportService) Collectors(java.util.stream.Collectors) TransportAddress(org.opensearch.common.transport.TransportAddress) List(java.util.List) URLEncodedUtils(org.apache.http.client.utils.URLEncodedUtils) Matchers.is(org.hamcrest.Matchers.is) NameValuePair(org.apache.http.NameValuePair) Collections(java.util.Collections) PageCacheRecycler(org.opensearch.common.util.PageCacheRecycler) NameValuePair(org.apache.http.NameValuePair) Instance(com.amazonaws.services.ec2.model.Instance) TransportAddress(org.opensearch.common.transport.TransportAddress) SeedHostsResolver(org.opensearch.discovery.SeedHostsResolver) SeedHostsProvider(org.opensearch.discovery.SeedHostsProvider) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Example 84 with Instance

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;
}
Also used : Instance(com.amazonaws.services.ec2.model.Instance) TransportAddress(org.opensearch.common.transport.TransportAddress) AmazonClientException(com.amazonaws.AmazonClientException) ArrayList(java.util.ArrayList) Collections.disjoint(java.util.Collections.disjoint) AmazonClientException(com.amazonaws.AmazonClientException) GroupIdentifier(com.amazonaws.services.ec2.model.GroupIdentifier) DescribeInstancesResult(com.amazonaws.services.ec2.model.DescribeInstancesResult) Reservation(com.amazonaws.services.ec2.model.Reservation) Supplier(org.apache.logging.log4j.util.Supplier) ParameterizedMessage(org.apache.logging.log4j.message.ParameterizedMessage) Tag(com.amazonaws.services.ec2.model.Tag)

Example 85 with Instance

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;
    }
}
Also used : IntStream(java.util.stream.IntStream) Arrays(java.util.Arrays) NoneCircuitBreakerService(org.opensearch.indices.breaker.NoneCircuitBreakerService) HttpStatus(org.apache.http.HttpStatus) Version(org.opensearch.Version) HashMap(java.util.HashMap) MockTransportService(org.opensearch.test.transport.MockTransportService) ArrayList(java.util.ArrayList) Transport(org.opensearch.transport.Transport) InetAddress(java.net.InetAddress) Streams(org.opensearch.common.io.Streams) Map(java.util.Map) Matchers.hasSize(org.hamcrest.Matchers.hasSize) InstanceState(com.amazonaws.services.ec2.model.InstanceState) Instance(com.amazonaws.services.ec2.model.Instance) MockNioTransport(org.opensearch.transport.nio.MockNioTransport) SuppressForbidden(org.opensearch.common.SuppressForbidden) InstanceStateName(com.amazonaws.services.ec2.model.InstanceStateName) UTF_8(java.nio.charset.StandardCharsets.UTF_8) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Settings(org.opensearch.common.settings.Settings) IOException(java.io.IOException) HttpMethodName(com.amazonaws.http.HttpMethodName) TransportService(org.opensearch.transport.TransportService) UnknownHostException(java.net.UnknownHostException) Collectors(java.util.stream.Collectors) TransportAddress(org.opensearch.common.transport.TransportAddress) List(java.util.List) Tag(com.amazonaws.services.ec2.model.Tag) URLEncodedUtils(org.apache.http.client.utils.URLEncodedUtils) NetworkService(org.opensearch.common.network.NetworkService) Matchers.is(org.hamcrest.Matchers.is) NameValuePair(org.apache.http.NameValuePair) Collections(java.util.Collections) Matchers.containsString(org.hamcrest.Matchers.containsString) PageCacheRecycler(org.opensearch.common.util.PageCacheRecycler) NameValuePair(org.apache.http.NameValuePair) HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Instance(com.amazonaws.services.ec2.model.Instance) TransportAddress(org.opensearch.common.transport.TransportAddress) Matchers.containsString(org.hamcrest.Matchers.containsString) IOException(java.io.IOException) InstanceState(com.amazonaws.services.ec2.model.InstanceState) ArrayList(java.util.ArrayList) List(java.util.List)

Aggregations

Instance (com.amazonaws.services.ec2.model.Instance)185 Reservation (com.amazonaws.services.ec2.model.Reservation)84 ArrayList (java.util.ArrayList)82 DescribeInstancesResult (com.amazonaws.services.ec2.model.DescribeInstancesResult)71 DescribeInstancesRequest (com.amazonaws.services.ec2.model.DescribeInstancesRequest)48 List (java.util.List)48 Tag (com.amazonaws.services.ec2.model.Tag)41 Test (org.junit.jupiter.api.Test)38 CloudInstance (com.sequenceiq.cloudbreak.cloud.model.CloudInstance)36 Map (java.util.Map)36 Collectors (java.util.stream.Collectors)32 HashMap (java.util.HashMap)26 Filter (com.amazonaws.services.ec2.model.Filter)25 Set (java.util.Set)23 CloudResource (com.sequenceiq.cloudbreak.cloud.model.CloudResource)22 CloudVmMetaDataStatus (com.sequenceiq.cloudbreak.cloud.model.CloudVmMetaDataStatus)20 AmazonEC2 (com.amazonaws.services.ec2.AmazonEC2)18 InstanceState (com.amazonaws.services.ec2.model.InstanceState)18 Volume (com.amazonaws.services.ec2.model.Volume)18 ComputeState (com.vmware.photon.controller.model.resources.ComputeService.ComputeState)18