@Test public void addSSHKey () { GcpComputeInstanceContainer container = GcpComputeInstanceContainer.builder() .withZone("my-datacenter") .withUniqueIdentifier("1234567890") .build(); Metadata existingMetadata = Metadata.getDefaultInstance(); ArgumentCaptor<Metadata> metadataCaptor = ArgumentCaptor.forClass(Metadata.class); Instance instance = Instance.newBuilder().setMetadata(existingMetadata).build(); Operation operation = mock(Operation.class); ArgumentCaptor<Operation> operationCaptor = ArgumentCaptor.forClass(Operation.class); ProjectZoneInstanceName expectedInstance = ProjectZoneInstanceName.of("1234567890", MY_AWESOME_PROJECT, "my-datacenter"); doReturn(instance).when(instanceClient).getInstance(expectedInstance); doReturn(operation).when(instanceClient).setMetadataInstance(eq(expectedInstance), metadataCaptor.capture()); doNothing().when(gcpComputePlatform).waitForOperation(any()); doNothing().when(gcpComputePlatform).addSSHKey(container, false); gcpComputePlatform.addSSHKey(container); verify(gcpComputePlatform).waitForOperation(operationCaptor.capture()); String capturedKey = metadataCaptor.getValue() .getItemsList() .stream() .filter(items -> items.getKey().equals("ssh-keys")) .map(Items::getValue) .map(String::strip) .findFirst() .orElseThrow(); assertEquals(container.getGcpSSHKeyMetadata().toString(), capturedKey); assertSame(operation, operationCaptor.getValue()); verify(gcpComputePlatform).addSSHKey(container, false); }
void addSSHKey (GcpComputeInstanceContainer container, boolean allowRetry) { ProjectZoneInstanceName instanceName = getProjectZoneInstanceNameOfContainer(container, getProjectName()); Metadata existingMetadata = getInstanceMetadata(instanceName); Map<String, String> metadataMap = itemListAsMap(existingMetadata.getItemsList()); String sshKeys = metadataMap.get("ssh-keys"); Set<GcpSSHKeyMetadata> existingKeys = new LinkedHashSet<>(GcpSSHKeyMetadata.parseMetadata(sshKeys != null ? sshKeys : "")); if (!existingKeys.add(container.getGcpSSHKeyMetadata())) return; Predicate<GcpSSHKeyMetadata> isNewKey = gcpSSHKeyMetadata -> gcpSSHKeyMetadata.equals(container.getGcpSSHKeyMetadata()); Predicate<GcpSSHKeyMetadata> isChaosKey = gcpSSHKeyMetadata -> Objects.equals(gcpSSHKeyMetadata.getUsername(), CHAOS_USERNAME); List<GcpSSHKeyMetadata> filteredKeys = existingKeys.stream() .filter(isNewKey.or(isChaosKey.negate())) .collect(Collectors.toList()); log.info("Key did not exist in container {}, adding new public key to metadata", v(DATADOG_CONTAINER_KEY, container)); metadataMap.put("ssh-keys", GcpSSHKeyMetadata.metadataFormat(filteredKeys)); Metadata newMetadata = Metadata.newBuilder() .addAllItems(mapAsItemList(metadataMap)) .setFingerprint(existingMetadata.getFingerprint()) .build(); Operation operation = getInstanceClient().setMetadataInstance(instanceName, newMetadata); waitForOperation(operation); if (allowRetry) addSSHKey(container, false); }
GcpComputePlatform extends Platform implements SshBasedExperiment<GcpComputeInstanceContainer> { void addSSHKey (GcpComputeInstanceContainer container, boolean allowRetry) { ProjectZoneInstanceName instanceName = getProjectZoneInstanceNameOfContainer(container, getProjectName()); Metadata existingMetadata = getInstanceMetadata(instanceName); Map<String, String> metadataMap = itemListAsMap(existingMetadata.getItemsList()); String sshKeys = metadataMap.get("ssh-keys"); Set<GcpSSHKeyMetadata> existingKeys = new LinkedHashSet<>(GcpSSHKeyMetadata.parseMetadata(sshKeys != null ? sshKeys : "")); if (!existingKeys.add(container.getGcpSSHKeyMetadata())) return; Predicate<GcpSSHKeyMetadata> isNewKey = gcpSSHKeyMetadata -> gcpSSHKeyMetadata.equals(container.getGcpSSHKeyMetadata()); Predicate<GcpSSHKeyMetadata> isChaosKey = gcpSSHKeyMetadata -> Objects.equals(gcpSSHKeyMetadata.getUsername(), CHAOS_USERNAME); List<GcpSSHKeyMetadata> filteredKeys = existingKeys.stream() .filter(isNewKey.or(isChaosKey.negate())) .collect(Collectors.toList()); log.info("Key did not exist in container {}, adding new public key to metadata", v(DATADOG_CONTAINER_KEY, container)); metadataMap.put("ssh-keys", GcpSSHKeyMetadata.metadataFormat(filteredKeys)); Metadata newMetadata = Metadata.newBuilder() .addAllItems(mapAsItemList(metadataMap)) .setFingerprint(existingMetadata.getFingerprint()) .build(); Operation operation = getInstanceClient().setMetadataInstance(instanceName, newMetadata); waitForOperation(operation); if (allowRetry) addSSHKey(container, false); } }
GcpComputePlatform extends Platform implements SshBasedExperiment<GcpComputeInstanceContainer> { void addSSHKey (GcpComputeInstanceContainer container, boolean allowRetry) { ProjectZoneInstanceName instanceName = getProjectZoneInstanceNameOfContainer(container, getProjectName()); Metadata existingMetadata = getInstanceMetadata(instanceName); Map<String, String> metadataMap = itemListAsMap(existingMetadata.getItemsList()); String sshKeys = metadataMap.get("ssh-keys"); Set<GcpSSHKeyMetadata> existingKeys = new LinkedHashSet<>(GcpSSHKeyMetadata.parseMetadata(sshKeys != null ? sshKeys : "")); if (!existingKeys.add(container.getGcpSSHKeyMetadata())) return; Predicate<GcpSSHKeyMetadata> isNewKey = gcpSSHKeyMetadata -> gcpSSHKeyMetadata.equals(container.getGcpSSHKeyMetadata()); Predicate<GcpSSHKeyMetadata> isChaosKey = gcpSSHKeyMetadata -> Objects.equals(gcpSSHKeyMetadata.getUsername(), CHAOS_USERNAME); List<GcpSSHKeyMetadata> filteredKeys = existingKeys.stream() .filter(isNewKey.or(isChaosKey.negate())) .collect(Collectors.toList()); log.info("Key did not exist in container {}, adding new public key to metadata", v(DATADOG_CONTAINER_KEY, container)); metadataMap.put("ssh-keys", GcpSSHKeyMetadata.metadataFormat(filteredKeys)); Metadata newMetadata = Metadata.newBuilder() .addAllItems(mapAsItemList(metadataMap)) .setFingerprint(existingMetadata.getFingerprint()) .build(); Operation operation = getInstanceClient().setMetadataInstance(instanceName, newMetadata); waitForOperation(operation); if (allowRetry) addSSHKey(container, false); }  }
GcpComputePlatform extends Platform implements SshBasedExperiment<GcpComputeInstanceContainer> { void addSSHKey (GcpComputeInstanceContainer container, boolean allowRetry) { ProjectZoneInstanceName instanceName = getProjectZoneInstanceNameOfContainer(container, getProjectName()); Metadata existingMetadata = getInstanceMetadata(instanceName); Map<String, String> metadataMap = itemListAsMap(existingMetadata.getItemsList()); String sshKeys = metadataMap.get("ssh-keys"); Set<GcpSSHKeyMetadata> existingKeys = new LinkedHashSet<>(GcpSSHKeyMetadata.parseMetadata(sshKeys != null ? sshKeys : "")); if (!existingKeys.add(container.getGcpSSHKeyMetadata())) return; Predicate<GcpSSHKeyMetadata> isNewKey = gcpSSHKeyMetadata -> gcpSSHKeyMetadata.equals(container.getGcpSSHKeyMetadata()); Predicate<GcpSSHKeyMetadata> isChaosKey = gcpSSHKeyMetadata -> Objects.equals(gcpSSHKeyMetadata.getUsername(), CHAOS_USERNAME); List<GcpSSHKeyMetadata> filteredKeys = existingKeys.stream() .filter(isNewKey.or(isChaosKey.negate())) .collect(Collectors.toList()); log.info("Key did not exist in container {}, adding new public key to metadata", v(DATADOG_CONTAINER_KEY, container)); metadataMap.put("ssh-keys", GcpSSHKeyMetadata.metadataFormat(filteredKeys)); Metadata newMetadata = Metadata.newBuilder() .addAllItems(mapAsItemList(metadataMap)) .setFingerprint(existingMetadata.getFingerprint()) .build(); Operation operation = getInstanceClient().setMetadataInstance(instanceName, newMetadata); waitForOperation(operation); if (allowRetry) addSSHKey(container, false); }  void setRoutableCIDRBlocks(Collection<String> routableCIDRBlocks); @Override ApiStatus getApiStatus(); @Override PlatformLevel getPlatformLevel(); @Override PlatformHealth getPlatformHealth(); Collection<Items> getIncludeFilter(); void setIncludeFilter(Map<String, String> includeFilter); Collection<Items> getExcludeFilter(); void setExcludeFilter(Map<String, String> excludeFilter); @Override boolean isContainerRecycled(Container container); String stopInstance(GcpComputeInstanceContainer container); ContainerHealth isContainerRunning(GcpComputeInstanceContainer gcpComputeInstanceContainer); String simulateMaintenance(GcpComputeInstanceContainer container); String setTags(GcpComputeInstanceContainer container, List<String> tags); String setTagsSafe(GcpComputeInstanceContainer container, List<String> newTags, String oldFingerprint); boolean isContainerGroupAtCapacity(GcpComputeInstanceContainer container); boolean checkTags(GcpComputeInstanceContainer container, List<String> expectedTags); void startInstance(GcpComputeInstanceContainer container); String restartContainer(GcpComputeInstanceContainer container); String getBestEndpoint(String privateIPAddress, String publicIPAddress); boolean isAddressRoutable(String privateIPAddress); String getLatestInstanceId(String instanceName, String zone); Fingerprint<List<String>> getFirewallTags(GcpComputeInstanceContainer container); void setProjectId(String projectId); @Override String getEndpoint(GcpComputeInstanceContainer container); @Override SSHCredentials getSshCredentials(GcpComputeInstanceContainer container); @Override String getRunningDirectory(); @Override void recycleContainer(GcpComputeInstanceContainer container); String recreateInstanceInInstanceGroup(GcpComputeInstanceContainer container); boolean isOperationComplete(String operationId); }
GcpComputePlatform extends Platform implements SshBasedExperiment<GcpComputeInstanceContainer> { void addSSHKey (GcpComputeInstanceContainer container, boolean allowRetry) { ProjectZoneInstanceName instanceName = getProjectZoneInstanceNameOfContainer(container, getProjectName()); Metadata existingMetadata = getInstanceMetadata(instanceName); Map<String, String> metadataMap = itemListAsMap(existingMetadata.getItemsList()); String sshKeys = metadataMap.get("ssh-keys"); Set<GcpSSHKeyMetadata> existingKeys = new LinkedHashSet<>(GcpSSHKeyMetadata.parseMetadata(sshKeys != null ? sshKeys : "")); if (!existingKeys.add(container.getGcpSSHKeyMetadata())) return; Predicate<GcpSSHKeyMetadata> isNewKey = gcpSSHKeyMetadata -> gcpSSHKeyMetadata.equals(container.getGcpSSHKeyMetadata()); Predicate<GcpSSHKeyMetadata> isChaosKey = gcpSSHKeyMetadata -> Objects.equals(gcpSSHKeyMetadata.getUsername(), CHAOS_USERNAME); List<GcpSSHKeyMetadata> filteredKeys = existingKeys.stream() .filter(isNewKey.or(isChaosKey.negate())) .collect(Collectors.toList()); log.info("Key did not exist in container {}, adding new public key to metadata", v(DATADOG_CONTAINER_KEY, container)); metadataMap.put("ssh-keys", GcpSSHKeyMetadata.metadataFormat(filteredKeys)); Metadata newMetadata = Metadata.newBuilder() .addAllItems(mapAsItemList(metadataMap)) .setFingerprint(existingMetadata.getFingerprint()) .build(); Operation operation = getInstanceClient().setMetadataInstance(instanceName, newMetadata); waitForOperation(operation); if (allowRetry) addSSHKey(container, false); }  void setRoutableCIDRBlocks(Collection<String> routableCIDRBlocks); @Override ApiStatus getApiStatus(); @Override PlatformLevel getPlatformLevel(); @Override PlatformHealth getPlatformHealth(); Collection<Items> getIncludeFilter(); void setIncludeFilter(Map<String, String> includeFilter); Collection<Items> getExcludeFilter(); void setExcludeFilter(Map<String, String> excludeFilter); @Override boolean isContainerRecycled(Container container); String stopInstance(GcpComputeInstanceContainer container); ContainerHealth isContainerRunning(GcpComputeInstanceContainer gcpComputeInstanceContainer); String simulateMaintenance(GcpComputeInstanceContainer container); String setTags(GcpComputeInstanceContainer container, List<String> tags); String setTagsSafe(GcpComputeInstanceContainer container, List<String> newTags, String oldFingerprint); boolean isContainerGroupAtCapacity(GcpComputeInstanceContainer container); boolean checkTags(GcpComputeInstanceContainer container, List<String> expectedTags); void startInstance(GcpComputeInstanceContainer container); String restartContainer(GcpComputeInstanceContainer container); String getBestEndpoint(String privateIPAddress, String publicIPAddress); boolean isAddressRoutable(String privateIPAddress); String getLatestInstanceId(String instanceName, String zone); Fingerprint<List<String>> getFirewallTags(GcpComputeInstanceContainer container); void setProjectId(String projectId); @Override String getEndpoint(GcpComputeInstanceContainer container); @Override SSHCredentials getSshCredentials(GcpComputeInstanceContainer container); @Override String getRunningDirectory(); @Override void recycleContainer(GcpComputeInstanceContainer container); String recreateInstanceInInstanceGroup(GcpComputeInstanceContainer container); boolean isOperationComplete(String operationId);  }