From 424973a1ab708f1b8b67c1163e560fbe57b14556 Mon Sep 17 00:00:00 2001 From: Edgar Garcia Date: Fri, 17 Jan 2025 11:41:40 -0600 Subject: [PATCH 1/4] fix(google): include partnerMetadata in InstanceTemplates --- .../clouddriver/google/compute/InstanceTemplates.java | 10 ++++++++-- .../agent/AbstractGoogleServerGroupCachingAgent.java | 2 +- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/clouddriver-google/src/main/java/com/netflix/spinnaker/clouddriver/google/compute/InstanceTemplates.java b/clouddriver-google/src/main/java/com/netflix/spinnaker/clouddriver/google/compute/InstanceTemplates.java index c1f3c265934..325bac7b9dd 100644 --- a/clouddriver-google/src/main/java/com/netflix/spinnaker/clouddriver/google/compute/InstanceTemplates.java +++ b/clouddriver-google/src/main/java/com/netflix/spinnaker/clouddriver/google/compute/InstanceTemplates.java @@ -61,11 +61,17 @@ public GoogleComputeOperationRequest insert( return requestFactory.wrapOperationRequest(request, "insert"); } - public PaginatedComputeRequest list() { + public PaginatedComputeRequest list( + String viewInput) { + if (viewInput.isBlank()) { + viewInput = "BASIC"; + } + String view = viewInput; return new PaginatedComputeRequestImpl<>( pageToken -> requestFactory.wrapRequest( - computeApi.list(credentials.getProject()).setPageToken(pageToken), "list"), + computeApi.list(credentials.getProject()).setPageToken(pageToken).setView(view), + "list"), InstanceTemplateList::getNextPageToken, InstanceTemplateList::getItems); } diff --git a/clouddriver-google/src/main/java/com/netflix/spinnaker/clouddriver/google/provider/agent/AbstractGoogleServerGroupCachingAgent.java b/clouddriver-google/src/main/java/com/netflix/spinnaker/clouddriver/google/provider/agent/AbstractGoogleServerGroupCachingAgent.java index 81200a45bae..9dfe24fbf2b 100644 --- a/clouddriver-google/src/main/java/com/netflix/spinnaker/clouddriver/google/provider/agent/AbstractGoogleServerGroupCachingAgent.java +++ b/clouddriver-google/src/main/java/com/netflix/spinnaker/clouddriver/google/provider/agent/AbstractGoogleServerGroupCachingAgent.java @@ -1061,7 +1061,7 @@ Collection retrieveAllInstancesInRegion() throws IOException { private Collection retrieveInstanceTemplates() throws IOException { InstanceTemplates instanceTemplatesApi = computeApiFactory.createInstanceTemplates(credentials); - return instanceTemplatesApi.list().execute(); + return instanceTemplatesApi.list("FULL").execute(); } Collection getZonesForRegion() { From da54ce26b07870eeebfc2f790fcb6d65f1fff856 Mon Sep 17 00:00:00 2001 From: Edgar Garcia Date: Fri, 17 Jan 2025 15:52:27 -0600 Subject: [PATCH 2/4] fix(google): remove default view and add tests --- .../google/compute/InstanceTemplates.java | 6 +- ...AbstractGoogleServerGroupCachingAgent.java | 4 +- .../google/compute/InstanceTemplatesTest.java | 92 +++++++++++++++++++ 3 files changed, 96 insertions(+), 6 deletions(-) diff --git a/clouddriver-google/src/main/java/com/netflix/spinnaker/clouddriver/google/compute/InstanceTemplates.java b/clouddriver-google/src/main/java/com/netflix/spinnaker/clouddriver/google/compute/InstanceTemplates.java index 325bac7b9dd..42379dcfa12 100644 --- a/clouddriver-google/src/main/java/com/netflix/spinnaker/clouddriver/google/compute/InstanceTemplates.java +++ b/clouddriver-google/src/main/java/com/netflix/spinnaker/clouddriver/google/compute/InstanceTemplates.java @@ -62,11 +62,7 @@ public GoogleComputeOperationRequest insert( } public PaginatedComputeRequest list( - String viewInput) { - if (viewInput.isBlank()) { - viewInput = "BASIC"; - } - String view = viewInput; + String view) { return new PaginatedComputeRequestImpl<>( pageToken -> requestFactory.wrapRequest( diff --git a/clouddriver-google/src/main/java/com/netflix/spinnaker/clouddriver/google/provider/agent/AbstractGoogleServerGroupCachingAgent.java b/clouddriver-google/src/main/java/com/netflix/spinnaker/clouddriver/google/provider/agent/AbstractGoogleServerGroupCachingAgent.java index 9dfe24fbf2b..8d4352ad437 100644 --- a/clouddriver-google/src/main/java/com/netflix/spinnaker/clouddriver/google/provider/agent/AbstractGoogleServerGroupCachingAgent.java +++ b/clouddriver-google/src/main/java/com/netflix/spinnaker/clouddriver/google/provider/agent/AbstractGoogleServerGroupCachingAgent.java @@ -1061,7 +1061,9 @@ Collection retrieveAllInstancesInRegion() throws IOException { private Collection retrieveInstanceTemplates() throws IOException { InstanceTemplates instanceTemplatesApi = computeApiFactory.createInstanceTemplates(credentials); - return instanceTemplatesApi.list("FULL").execute(); + return instanceTemplatesApi + .list("FULL") + .execute(); // https://cloud.google.com/sdk/gcloud/reference/beta/compute/instance-templates/list } Collection getZonesForRegion() { diff --git a/clouddriver-google/src/test/groovy/com/netflix/spinnaker/clouddriver/google/compute/InstanceTemplatesTest.java b/clouddriver-google/src/test/groovy/com/netflix/spinnaker/clouddriver/google/compute/InstanceTemplatesTest.java index 1f1550737ec..3c1769d1662 100644 --- a/clouddriver-google/src/test/groovy/com/netflix/spinnaker/clouddriver/google/compute/InstanceTemplatesTest.java +++ b/clouddriver-google/src/test/groovy/com/netflix/spinnaker/clouddriver/google/compute/InstanceTemplatesTest.java @@ -18,6 +18,7 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatIOException; +import static org.mockito.Mockito.*; import com.google.api.client.http.HttpTransport; import com.google.api.client.json.gson.GsonFactory; @@ -25,6 +26,7 @@ import com.google.api.client.testing.http.MockLowLevelHttpResponse; import com.google.api.services.compute.Compute; import com.google.api.services.compute.model.InstanceTemplate; +import com.google.common.collect.ImmutableList; import com.netflix.spectator.api.BasicTag; import com.netflix.spectator.api.DefaultRegistry; import com.netflix.spectator.api.NoopRegistry; @@ -190,6 +192,96 @@ public void get_errorMetrics() { assertThat(timer.totalTime()).isEqualTo(CLOCK_STEP_TIME_NS); } + @Test + public void list_success() throws IOException { + + MockHttpTransport transport = + new MockHttpTransport.Builder() + .setLowLevelHttpResponse( + new MockLowLevelHttpResponse() + .setStatusCode(200) + .addHeader("Content-Type", "application/json") + .setContent( + "{\"items\": [{\"name\": \"template1\"}, {\"name\": \"template2\"}], \"nextPageToken\": \"\"}")) + .build(); + + InstanceTemplates instanceTemplates = createInstanceTemplates(transport); + + PaginatedComputeRequest request = + instanceTemplates.list("BASIC"); + + ImmutableList response = request.execute(); + assertThat(response).hasSize(2); + assertThat(response.get(0).getName()).isEqualTo("template1"); + assertThat(response.get(1).getName()).isEqualTo("template2"); + } + + @Test + public void list_noResults() throws IOException { + + MockHttpTransport transport = + new MockHttpTransport.Builder() + .setLowLevelHttpResponse( + new MockLowLevelHttpResponse() + .setStatusCode(200) + .setContent("{\"items\": [], \"nextPageToken\": \"\"}")) + .build(); + + InstanceTemplates instanceTemplates = createInstanceTemplates(transport); + + PaginatedComputeRequest request = + instanceTemplates.list("BASIC"); + + ImmutableList response = request.execute(); + assertThat(response).isEmpty(); + } + + @Test + public void list_errorResponse() { + + MockHttpTransport transport = + new MockHttpTransport.Builder() + .setLowLevelHttpResponse( + new MockLowLevelHttpResponse() + .setStatusCode(500) + .setContent("{\"error\": \"Internal Server Error\"}")) + .build(); + + InstanceTemplates instanceTemplates = createInstanceTemplates(transport); + + PaginatedComputeRequest request = + instanceTemplates.list("BASIC"); + + assertThatIOException().isThrownBy(() -> request.execute()); + } + + @Test + public void list_withFullView_success() throws IOException { + + MockHttpTransport transport = + new MockHttpTransport.Builder() + .setLowLevelHttpResponse( + new MockLowLevelHttpResponse() + .setStatusCode(200) + .addHeader("Content-Type", "application/json") + .setContent( + "{\"items\": [{\"name\": \"template1\", \"description\": \"Detailed description for template1\"}, {\"name\": \"template2\", \"description\": \"Detailed description for template2\"}], \"nextPageToken\": \"\"}")) + .build(); + + InstanceTemplates instanceTemplates = createInstanceTemplates(transport); + + PaginatedComputeRequest request = + instanceTemplates.list("FULL"); + + ImmutableList response = request.execute(); + + assertThat(response).hasSize(2); + assertThat(response.get(0).getName()).isEqualTo("template1"); + assertThat(response.get(0).getDescription()).isEqualTo("Detailed description for template1"); + assertThat(response.get(1).getName()).isEqualTo("template2"); + assertThat(response.get(1).getDescription()).isEqualTo("Detailed description for template2"); + } + private static InstanceTemplates createInstanceTemplates(HttpTransport transport) { return createInstanceTemplates(transport, new NoopRegistry()); } From 180d3940fd44a6049000f19cdc146fb002b485a2 Mon Sep 17 00:00:00 2001 From: Edgar Garcia Date: Fri, 17 Jan 2025 15:54:00 -0600 Subject: [PATCH 3/4] fix(google): format comment --- .../agent/AbstractGoogleServerGroupCachingAgent.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/clouddriver-google/src/main/java/com/netflix/spinnaker/clouddriver/google/provider/agent/AbstractGoogleServerGroupCachingAgent.java b/clouddriver-google/src/main/java/com/netflix/spinnaker/clouddriver/google/provider/agent/AbstractGoogleServerGroupCachingAgent.java index 8d4352ad437..8879c553429 100644 --- a/clouddriver-google/src/main/java/com/netflix/spinnaker/clouddriver/google/provider/agent/AbstractGoogleServerGroupCachingAgent.java +++ b/clouddriver-google/src/main/java/com/netflix/spinnaker/clouddriver/google/provider/agent/AbstractGoogleServerGroupCachingAgent.java @@ -1062,8 +1062,9 @@ Collection retrieveAllInstancesInRegion() throws IOException { private Collection retrieveInstanceTemplates() throws IOException { InstanceTemplates instanceTemplatesApi = computeApiFactory.createInstanceTemplates(credentials); return instanceTemplatesApi - .list("FULL") - .execute(); // https://cloud.google.com/sdk/gcloud/reference/beta/compute/instance-templates/list + .list("FULL") // See view options: + // https://cloud.google.com/sdk/gcloud/reference/beta/compute/instance-templates/list + .execute(); } Collection getZonesForRegion() { From 74a161d7405d36f5e995339d49f85a5682a44e6e Mon Sep 17 00:00:00 2001 From: Edgar Garcia Date: Fri, 17 Jan 2025 16:05:30 -0600 Subject: [PATCH 4/4] fix(google): remove unused imports --- .../clouddriver/google/compute/InstanceTemplatesTest.java | 1 - 1 file changed, 1 deletion(-) diff --git a/clouddriver-google/src/test/groovy/com/netflix/spinnaker/clouddriver/google/compute/InstanceTemplatesTest.java b/clouddriver-google/src/test/groovy/com/netflix/spinnaker/clouddriver/google/compute/InstanceTemplatesTest.java index 3c1769d1662..7f92edcd5d9 100644 --- a/clouddriver-google/src/test/groovy/com/netflix/spinnaker/clouddriver/google/compute/InstanceTemplatesTest.java +++ b/clouddriver-google/src/test/groovy/com/netflix/spinnaker/clouddriver/google/compute/InstanceTemplatesTest.java @@ -18,7 +18,6 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatIOException; -import static org.mockito.Mockito.*; import com.google.api.client.http.HttpTransport; import com.google.api.client.json.gson.GsonFactory;