-
Notifications
You must be signed in to change notification settings - Fork 1.1k
feat(bigquery): add internal listProjects API to core client #13429
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
keshavdandeva
wants to merge
3
commits into
main
Choose a base branch
from
add-listProjects
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -25,6 +25,7 @@ | |
| import com.google.api.gax.paging.Page; | ||
| import com.google.api.services.bigquery.model.ErrorProto; | ||
| import com.google.api.services.bigquery.model.GetQueryResultsResponse; | ||
| import com.google.api.services.bigquery.model.ProjectList; | ||
| import com.google.api.services.bigquery.model.QueryRequest; | ||
| import com.google.api.services.bigquery.model.TableDataInsertAllRequest; | ||
| import com.google.api.services.bigquery.model.TableDataInsertAllRequest.Rows; | ||
|
|
@@ -65,6 +66,25 @@ | |
|
|
||
| final class BigQueryImpl extends BaseService<BigQueryOptions> implements BigQuery { | ||
|
|
||
| private static class ProjectPageFetcher implements NextPageFetcher<Project> { | ||
|
|
||
| private static final long serialVersionUID = 1L; | ||
| private final Map<BigQueryRpc.Option, ?> requestOptions; | ||
| private final BigQueryOptions serviceOptions; | ||
|
|
||
| ProjectPageFetcher( | ||
| BigQueryOptions serviceOptions, String cursor, Map<BigQueryRpc.Option, ?> optionMap) { | ||
| this.requestOptions = | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Will it automatically populate maxResults/pageSize? |
||
| PageImpl.nextRequestOptions(BigQueryRpc.Option.PAGE_TOKEN, cursor, optionMap); | ||
| this.serviceOptions = serviceOptions; | ||
| } | ||
|
|
||
| @Override | ||
| public Page<Project> getNextPage() { | ||
| return listProjects(serviceOptions, requestOptions); | ||
| } | ||
| } | ||
|
|
||
| private static class DatasetPageFetcher implements NextPageFetcher<Dataset> { | ||
|
|
||
| private static final long serialVersionUID = -3057564042439021278L; | ||
|
|
@@ -307,6 +327,49 @@ public com.google.api.services.bigquery.model.Dataset call() throws IOException | |
| } | ||
| } | ||
|
|
||
| @Override | ||
| public Page<Project> listProjects(ProjectListOption... options) { | ||
| Span projectsList = null; | ||
| if (getOptions().isOpenTelemetryTracingEnabled() | ||
| && getOptions().getOpenTelemetryTracer() != null) { | ||
| projectsList = | ||
| getOptions() | ||
| .getOpenTelemetryTracer() | ||
| .spanBuilder("com.google.cloud.bigquery.BigQuery.listProjects") | ||
| .setAllAttributes(otelAttributesFromOptions(options)) | ||
| .startSpan(); | ||
| } | ||
| try (Scope projectsListScope = projectsList != null ? projectsList.makeCurrent() : null) { | ||
| return listProjects(getOptions(), optionMap(options)); | ||
| } finally { | ||
| if (projectsList != null) { | ||
| projectsList.end(); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private static Page<Project> listProjects( | ||
| final BigQueryOptions serviceOptions, final Map<BigQueryRpc.Option, ?> optionsMap) { | ||
| Tuple<String, Iterable<ProjectList.Projects>> result = | ||
| serviceOptions.getBigQueryRpcV2().listProjects(optionsMap); | ||
| String nextPageToken = result.x(); | ||
| Iterable<Project> projects = | ||
| Iterables.transform( | ||
| result.y() != null ? result.y() : ImmutableList.<ProjectList.Projects>of(), | ||
| projectPb -> | ||
| new Project( | ||
| projectPb.getId(), | ||
| projectPb.getNumericId() != null | ||
| ? String.valueOf(projectPb.getNumericId()) | ||
| : null, | ||
| projectPb.getProjectReference() != null | ||
| ? projectPb.getProjectReference().getProjectId() | ||
| : null, | ||
| projectPb.getFriendlyName())); | ||
| return new PageImpl<>( | ||
| new ProjectPageFetcher(serviceOptions, nextPageToken, optionsMap), nextPageToken, projects); | ||
| } | ||
|
|
||
| @Override | ||
| public Table create(TableInfo tableInfo, TableOption... options) { | ||
| final com.google.api.services.bigquery.model.Table tablePb = | ||
|
|
||
88 changes: 88 additions & 0 deletions
88
java-bigquery/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/Project.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| /* | ||
| * Copyright 2026 Google LLC | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package com.google.cloud.bigquery; | ||
|
|
||
| import com.google.api.core.BetaApi; | ||
| import java.io.Serializable; | ||
| import java.util.Objects; | ||
|
|
||
| @BetaApi | ||
| public class Project implements Serializable { | ||
| private static final long serialVersionUID = 1L; | ||
|
|
||
| private final String id; | ||
| private final String numericId; | ||
| private final String projectId; | ||
| private final String friendlyName; | ||
|
|
||
| public Project(String id, String numericId, String projectId, String friendlyName) { | ||
| this.id = id; | ||
| this.numericId = numericId; | ||
| this.projectId = projectId; | ||
| this.friendlyName = friendlyName; | ||
| } | ||
|
|
||
| public String getId() { | ||
| return id; | ||
| } | ||
|
|
||
| public String getNumericId() { | ||
| return numericId; | ||
| } | ||
|
|
||
| public String getProjectId() { | ||
| return projectId; | ||
| } | ||
|
|
||
| public String getFriendlyName() { | ||
| return friendlyName; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean equals(Object o) { | ||
| if (this == o) return true; | ||
| if (o == null || getClass() != o.getClass()) return false; | ||
| Project project = (Project) o; | ||
| return Objects.equals(id, project.id) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shouldn't we use String.equals there? |
||
| && Objects.equals(numericId, project.numericId) | ||
| && Objects.equals(projectId, project.projectId) | ||
| && Objects.equals(friendlyName, project.friendlyName); | ||
| } | ||
|
|
||
| @Override | ||
| public int hashCode() { | ||
| return Objects.hash(id, numericId, projectId, friendlyName); | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return "Project{" | ||
| + "id='" | ||
| + id | ||
| + '\'' | ||
| + ", numericId='" | ||
| + numericId | ||
| + '\'' | ||
| + ", projectId='" | ||
| + projectId | ||
| + '\'' | ||
| + ", friendlyName='" | ||
| + friendlyName | ||
| + '\'' | ||
| + '}'; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we keep name consistent with API?
maxResults?