Skip to content

Commit

Permalink
fix(google): Add partner metadata on instanceTemplate properties (#6334
Browse files Browse the repository at this point in the history
…) (#6335)

* fix(google): include partnerMetadata in InstanceTemplates

* fix(google): remove default view and add tests

* fix(google): format comment

* fix(google): remove unused imports

* fix(google): make FULL the default view in instanceTemplate client

---------

Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
(cherry picked from commit fcc5394)

Co-authored-by: Edgar Garcia <63310723+edgarulg@users.noreply.github.com>
  • Loading branch information
mergify[bot] and edgarulg authored Jan 21, 2025
1 parent 472ffad commit e759801
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ public class InstanceTemplates {
private final Compute.InstanceTemplates computeApi;
private final GoogleNamedAccountCredentials credentials;
private final GlobalGoogleComputeRequestFactory requestFactory;
private static final String defaultView =
"FULL"; // https://cloud.google.com/sdk/gcloud/reference/beta/compute/instance-templates/list

InstanceTemplates(
GoogleNamedAccountCredentials credentials,
Expand Down Expand Up @@ -65,7 +67,11 @@ public PaginatedComputeRequest<Compute.InstanceTemplates.List, InstanceTemplate>
return new PaginatedComputeRequestImpl<>(
pageToken ->
requestFactory.wrapRequest(
computeApi.list(credentials.getProject()).setPageToken(pageToken), "list"),
computeApi
.list(credentials.getProject())
.setPageToken(pageToken)
.setView(defaultView),
"list"),
InstanceTemplateList::getNextPageToken,
InstanceTemplateList::getItems);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,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;
Expand Down Expand Up @@ -190,6 +191,69 @@ 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<Compute.InstanceTemplates.List, InstanceTemplate> request =
instanceTemplates.list();

ImmutableList<InstanceTemplate> 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<Compute.InstanceTemplates.List, InstanceTemplate> request =
instanceTemplates.list();

ImmutableList<InstanceTemplate> 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<Compute.InstanceTemplates.List, InstanceTemplate> request =
instanceTemplates.list();

assertThatIOException().isThrownBy(() -> request.execute());
}

private static InstanceTemplates createInstanceTemplates(HttpTransport transport) {
return createInstanceTemplates(transport, new NoopRegistry());
}
Expand Down

0 comments on commit e759801

Please sign in to comment.