How to get the project Name and project Number through audit logs or through API's in google bigquery

Viewed 659

I am trying to retrieve the project details such as projectName and projectNumber in bigquery, but anywhere I can see only the projectId but not other details. I have explored the audit logs in my project when I ran a query and seen all the types of audit logs whether anywhere I can be able to find those details, but I was able to see the projectId only. I also explored the API's like is there any API serving my purpose, I have found one API which gives the list of projects but there also they are not providing the projectName and projectNumber details. So is there any other way I can get those details, please let me know.

EDIT: Looking for the JAVA API client

2 Answers

From bigquery resources you can use its client library (which makes use of the API) to get that information.

UPDATED 12/07/2021: JAVA API

Unfortunately this was take out of JAVA API BIGQUERY Client and there is no news if it will make a return. So, you wont be able to get such information from BigQuery latest client. You can see one discussion about it here.

Regardless of that you will be able to get that info using resource manager. ( although you will need to have the information from the hierarchy above or at least the project name )

libraries used

import com.google.cloud.resourcemanager.v3.Project;
import com.google.cloud.resourcemanager.v3.ProjectName;
import com.google.cloud.resourcemanager.v3.ProjectsClient;

code

try (ProjectsClient projectsClient = ProjectsClient.create()) { 
     ProjectName name = ProjectName.of("[PROJECT]");
     Project prjct = projectsClient.getProject(name); 
     System.out.println(prjct.getProjectId());
     System.out.println(prjct.getName()); # returns `projects/415104041262`
}

To setup DOM and dependencies, you can check the official documentation for resource manager. Also, here is the link where can check the client used to get projects, project client

Workarounds:

Alternatively you can use other libraries and cmds to get that information, it makes use of bigquery python client library to get the information you are looking for:

from google.cloud import bigquery 
from google.oauth2 import service_account

# key_path = "path/to/service_account.json"
key_path = "my-service-account-key.json"

credentials = service_account.Credentials.from_service_account_file(
    key_path, scopes=["https://www.googleapis.com/auth/cloud-platform"],
)

client = bigquery.Client(credentials=credentials, project=credentials.project_id,)

print(client.get_service_account_email())

projects = client.list_projects(max_results=10)

for project in list(projects):
    print(project.project_id)
    print(project.numeric_id)

Note: follow this link for guidance on how to get a service account json.

output

bq-service-account-project-number@bigquery-account.iam.gserviceaccount.com
project id: my-project-id
project number: 7788990000xxx

Another options outside bigquery resources to get info about projects. You can get that information you will have to open google cloud shell and perform the following command:

gcloud projects describe <my-project-id>

output:

createTime: '2015-10-20T10:12:09.452Z'
lifecycleState: ACTIVE
name: my-project-id
parent:
  id: '111222333xxx'
  type: folder
projectId: my-project-id
projectNumber: '777555000xxx'

Also, I'm leaving these links where you can get project related info:

Finally, If you are looking about why its difficult to get info from bigquery about projects I'm providing this link that discuss about resource-hierarchy.

I have implemented it in this way, here the methods are deprecated, finding out the alternatives for this like moving of this artifact to our own artifactory like that which is out of the context currently, but I got the info that I want through the java client.

     <dependency>
        <groupId>com.google.cloud</groupId>
        <artifactId>google-cloud-resourcemanager</artifactId>
        <version>1.1.4</version>
    </dependency>

This is the dependency that I have added in my pom.xml file

import com.google.cloud.resourcemanager.ResourceManager;
import com.google.cloud.resourcemanager.ResourceManagerOptions;
import com.google.cloud.resourcemanager.Project;
String credentialsPath = "tmp/credentials.json";
ResourceManager resourceManager = ResourceManagerOptions.newBuilder().setProjectId("my-project-id").
            setCredentials(GoogleCredentials.fromStream(new FileInputStream(credentialsPath))).build().getService();
  

 Project project = resourceManager.get(projectId);
// You got the project object which contains the details you needed
    

You need two permissions to get the details of the project

  1. projects.get
  2. Enable this API for your project

If these permissions are correct , you will get the details otherwise null object will be returned from the google side

Related