Get hands-on experience with 20+ free Google Cloud products and $300 in free credit for new customers.

GCP function gen-1 to get project id is failing

I am trying to create a gcp function in python which can simply check the project id and display as a starting point. Below is my code


import os
from google.cloud import resource_manager_v3
import subprocess
import csv

def list_projects_ids(request):
  try:
    gcloud_command = ["gcloud", "projects", "list", "--format=value(projectId)"]
    project_id_output = subprocess.check_output(gcloud_command, universal_newlines=True, stderr=subprocess.STDOUT)
    project_ids = project_id_output.strip().split("\n")
    return {"project_id: project_ids"}

  except subprocess.CalledProcessError as e:
      return {"error: {e}"}

I am getting below error when i am trying to test it before deploying (looks like its automatically  trying to test in cloud shell).
4:35:34 PM] - spec.loader.exec_module(source_module)
File "<frozen importlib._bootstrap_external>", line 883, in exec_module
File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
File "/workspace/main.py", line 3, in <module>
from google.cloud import resource_manager_v3
ImportError: cannot import name 'resource_manager_v3' from 'google.cloud' (unknown location)
   
Can you please let me know what is missing in the code, any specific package that i need to add in code or requirement.txt file ? or in cloud shell anything needs to install?
 

   
1 2 1,125
2 REPLIES 2

Hi @gcloudLearning,

Welcome back to Google Cloud Community!


Your main.py file and requirements.txt file should be in the same directory, and this should also be the same directory you're deploying your function from.

Can you indicate your file structure?

Thanks

@christianpaula I am getting below error. The same function is running fine locally . I tried both using gen1 and 2. same error i am getting.FileNotFoundError: [Errno 2] No such file or directory: 'gcloud'.

I am using gcloud command as subprocess like this .-

    gcp_project_list = ["gcloud", "projects", "describe",project_id, "--format=json"]
        gcp_project_list_output= subprocess.run(gcp_project_list, universal_newlines=True, stdout=subprocess.PIPE)
        gcp_project_list_output_json = json.loads(gcp_project_list_output.stdout)
        tags = gcp_project_list_output_json.get("labels",{}).get("product_id", "")
 
error -
at ._execute_child ( /layers/google.python.runtime/python/lib/python3.10/subprocess.py:1863 )
at .__init__ ( /layers/google.python.runtime/python/lib/python3.10/subprocess.py:971 )
at .run ( /layers/google.python.runtime/python/lib/python3.10/subprocess.py:503 )
at .check_output ( /layers/google.python.runtime/python/lib/python3.10/subprocess.py:421 )
at .extract_vmdetails ( /workspace/main.py:42 )
at .view_func ( /layers/google.python.pip/pip/lib/python3.10/site-packages/functions_framework/__init__.py:99 )
at .dispatch_request ( /layers/google.python.pip/pip/lib/python3.10/site-packages/flask/app.py:1799 )
at .full_dispatch_request ( /layers/google.python.pip/pip/lib/python3.10/site-packages/flask/app.py:1823 )
at .full_dispatch_request ( /layers/google.python.pip/pip/lib/python3.10/site-packages/flask/app.py:1825 )
at .wsgi_app ( /layers/google.python.pip/pip/lib/python3.10/site-packages/flask/app.py:2529 )