Google Cloud Resource Manager API client library for managing projects, folders, organizations, and tags in Google Cloud Platform
—
Comprehensive project lifecycle management functionality for Google Cloud Platform. Projects serve as the fundamental organizational unit that contains and isolates Google Cloud resources, providing the foundation for billing, access control, and resource organization.
Retrieve detailed information about a specific project using its resource name.
def get_project(
self,
request: GetProjectRequest = None,
*,
name: str = None,
retry: OptionalRetry = gapic_v1.method.DEFAULT,
timeout: Union[float, object] = gapic_v1.method.DEFAULT,
metadata: Sequence[Tuple[str, Union[str, bytes]]] = ()
) -> Project:
"""
Retrieves a project identified by the specified resource name.
Args:
name (str): The resource name of the project to retrieve.
Format: projects/{project_id}
retry: Retry configuration for the request
timeout: Request timeout in seconds
metadata: Additional metadata to send with the request
Returns:
Project: The requested project resource
Raises:
google.api_core.exceptions.NotFound: If the project doesn't exist
google.api_core.exceptions.PermissionDenied: If access is denied
"""Usage example:
from google.cloud.resourcemanager import ProjectsClient
client = ProjectsClient()
project = client.get_project(name="projects/my-project-id")
print(f"Project: {project.display_name} ({project.project_id})")List all projects accessible to the caller, optionally filtered by parent organization or folder.
def list_projects(
self,
request: ListProjectsRequest = None,
*,
parent: str = None,
retry: OptionalRetry = gapic_v1.method.DEFAULT,
timeout: Union[float, object] = gapic_v1.method.DEFAULT,
metadata: Sequence[Tuple[str, Union[str, bytes]]] = ()
) -> pagers.ListProjectsPager:
"""
Lists projects that are direct children of the specified parent resource.
Args:
parent (str): The parent resource whose projects are to be listed.
Formats: organizations/{organization_id} or folders/{folder_id}
retry: Retry configuration for the request
timeout: Request timeout in seconds
metadata: Additional metadata to send with the request
Returns:
ListProjectsPager: An iterator over projects that automatically
handles pagination
"""Usage example:
client = ProjectsClient()
# List all projects under an organization
for project in client.list_projects(parent="organizations/123456789"):
print(f"Project: {project.project_id} - {project.display_name}")
# List all projects under a folder
for project in client.list_projects(parent="folders/987654321"):
print(f"Project: {project.project_id} - {project.display_name}")Search for projects using flexible query expressions with support for filtering and sorting.
def search_projects(
self,
request: SearchProjectsRequest = None,
*,
query: str = None,
retry: OptionalRetry = gapic_v1.method.DEFAULT,
timeout: Union[float, object] = gapic_v1.method.DEFAULT,
metadata: Sequence[Tuple[str, Union[str, bytes]]] = ()
) -> pagers.SearchProjectsPager:
"""
Search for projects using a flexible query language.
Args:
query (str): Query expression for filtering projects.
Examples: 'displayName:web*', 'parent.id:123456789',
'lifecycleState:ACTIVE'
retry: Retry configuration for the request
timeout: Request timeout in seconds
metadata: Additional metadata to send with the request
Returns:
SearchProjectsPager: An iterator over matching projects
"""Usage example:
client = ProjectsClient()
# Search for active projects with "web" in the display name
for project in client.search_projects(query="displayName:web* AND lifecycleState:ACTIVE"):
print(f"Found: {project.project_id} - {project.display_name}")Create new projects within the Google Cloud resource hierarchy. This is a long-running operation.
def create_project(
self,
request: CreateProjectRequest = None,
*,
project: Project = None,
retry: OptionalRetry = gapic_v1.method.DEFAULT,
timeout: Union[float, object] = gapic_v1.method.DEFAULT,
metadata: Sequence[Tuple[str, Union[str, bytes]]] = ()
) -> operation.Operation:
"""
Creates a new project. This is a long-running operation.
Args:
project (Project): The project resource to create
retry: Retry configuration for the request
timeout: Request timeout in seconds
metadata: Additional metadata to send with the request
Returns:
Operation: Long-running operation that resolves to the created Project
"""Usage example:
from google.cloud.resourcemanager import ProjectsClient
from google.cloud.resourcemanager_v3.types import Project
client = ProjectsClient()
new_project = Project(
project_id="my-new-project-123",
display_name="My New Project",
parent="organizations/123456789",
labels={"environment": "development", "team": "backend"}
)
operation = client.create_project(project=new_project)
result = operation.result() # Wait for completion
print(f"Created project: {result.project_id}")Update project attributes such as display name, labels, and other metadata. This is a long-running operation.
def update_project(
self,
request: UpdateProjectRequest = None,
*,
project: Project = None,
update_mask: field_mask_pb2.FieldMask = None,
retry: OptionalRetry = gapic_v1.method.DEFAULT,
timeout: Union[float, object] = gapic_v1.method.DEFAULT,
metadata: Sequence[Tuple[str, Union[str, bytes]]] = ()
) -> operation.Operation:
"""
Updates the specified project. This is a long-running operation.
Args:
project (Project): The project resource with updated values
update_mask (FieldMask): Fields to update. If not provided,
all mutable fields are updated
retry: Retry configuration for the request
timeout: Request timeout in seconds
metadata: Additional metadata to send with the request
Returns:
Operation: Long-running operation that resolves to the updated Project
"""Usage example:
from google.protobuf import field_mask_pb2
client = ProjectsClient()
# Get existing project
project = client.get_project(name="projects/my-project-id")
# Update display name and labels
project.display_name = "Updated Project Name"
project.labels["environment"] = "production"
# Specify which fields to update
update_mask = field_mask_pb2.FieldMask(
paths=["display_name", "labels"]
)
operation = client.update_project(
project=project,
update_mask=update_mask
)
result = operation.result()
print(f"Updated project: {result.display_name}")Move projects between different parent resources (organizations or folders). This is a long-running operation.
def move_project(
self,
request: MoveProjectRequest = None,
*,
name: str = None,
destination_parent: str = None,
retry: OptionalRetry = gapic_v1.method.DEFAULT,
timeout: Union[float, object] = gapic_v1.method.DEFAULT,
metadata: Sequence[Tuple[str, Union[str, bytes]]] = ()
) -> operation.Operation:
"""
Moves a project to a different parent resource. This is a long-running operation.
Args:
name (str): The resource name of the project to move.
Format: projects/{project_id}
destination_parent (str): The new parent resource.
Format: organizations/{org_id} or folders/{folder_id}
retry: Retry configuration for the request
timeout: Request timeout in seconds
metadata: Additional metadata to send with the request
Returns:
Operation: Long-running operation that resolves to the moved Project
"""Usage example:
client = ProjectsClient()
operation = client.move_project(
name="projects/my-project-id",
destination_parent="folders/new-folder-id"
)
result = operation.result()
print(f"Moved project to: {result.parent}")Mark projects for deletion. Projects are not immediately deleted but enter a pending deletion state. This is a long-running operation.
def delete_project(
self,
request: DeleteProjectRequest = None,
*,
name: str = None,
retry: OptionalRetry = gapic_v1.method.DEFAULT,
timeout: Union[float, object] = gapic_v1.method.DEFAULT,
metadata: Sequence[Tuple[str, Union[str, bytes]]] = ()
) -> operation.Operation:
"""
Marks the specified project for deletion. This is a long-running operation.
The project enters DELETE_REQUESTED state and will be deleted after a
retention period unless restored with undelete_project.
Args:
name (str): The resource name of the project to delete.
Format: projects/{project_id}
retry: Retry configuration for the request
timeout: Request timeout in seconds
metadata: Additional metadata to send with the request
Returns:
Operation: Long-running operation with no return value
"""Restore projects that are in the deletion pending state. This is a long-running operation.
def undelete_project(
self,
request: UndeleteProjectRequest = None,
*,
name: str = None,
retry: OptionalRetry = gapic_v1.method.DEFAULT,
timeout: Union[float, object] = gapic_v1.method.DEFAULT,
metadata: Sequence[Tuple[str, Union[str, bytes]]] = ()
) -> operation.Operation:
"""
Restores a project from the DELETE_REQUESTED state. This is a long-running operation.
Args:
name (str): The resource name of the project to restore.
Format: projects/{project_id}
retry: Retry configuration for the request
timeout: Request timeout in seconds
metadata: Additional metadata to send with the request
Returns:
Operation: Long-running operation that resolves to the restored Project
"""Usage example:
client = ProjectsClient()
# Delete a project
delete_op = client.delete_project(name="projects/my-project-id")
delete_op.result() # Wait for completion
# Later, restore the project
restore_op = client.undelete_project(name="projects/my-project-id")
restored_project = restore_op.result()
print(f"Restored project: {restored_project.project_id}")Manage IAM (Identity and Access Management) policies for projects, controlling who has access and what permissions they have.
def get_iam_policy(
self,
request: iam_policy_pb2.GetIamPolicyRequest = None,
*,
resource: str = None,
retry: OptionalRetry = gapic_v1.method.DEFAULT,
timeout: Union[float, object] = gapic_v1.method.DEFAULT,
metadata: Sequence[Tuple[str, Union[str, bytes]]] = ()
) -> policy_pb2.Policy:
"""
Gets the IAM access control policy for a project.
Args:
resource (str): Resource name of the project.
Format: projects/{project_id}
Returns:
Policy: The IAM policy for the project
"""
def set_iam_policy(
self,
request: iam_policy_pb2.SetIamPolicyRequest = None,
*,
resource: str = None,
policy: policy_pb2.Policy = None,
retry: OptionalRetry = gapic_v1.method.DEFAULT,
timeout: Union[float, object] = gapic_v1.method.DEFAULT,
metadata: Sequence[Tuple[str, Union[str, bytes]]] = ()
) -> policy_pb2.Policy:
"""
Sets the IAM access control policy for a project.
Args:
resource (str): Resource name of the project
policy (Policy): The new IAM policy
Returns:
Policy: The updated IAM policy
"""
def test_iam_permissions(
self,
request: iam_policy_pb2.TestIamPermissionsRequest = None,
*,
resource: str = None,
permissions: MutableSequence[str] = None,
retry: OptionalRetry = gapic_v1.method.DEFAULT,
timeout: Union[float, object] = gapic_v1.method.DEFAULT,
metadata: Sequence[Tuple[str, Union[str, bytes]]] = ()
) -> iam_policy_pb2.TestIamPermissionsResponse:
"""
Tests the specified permissions against the IAM policy for a project.
Args:
resource (str): Resource name of the project
permissions (Sequence[str]): List of permissions to test
Returns:
TestIamPermissionsResponse: Results of the permission test
"""class Project:
name: str # Resource name: projects/{project_id}
project_id: str # Unique project identifier
display_name: str # Human-readable project name
parent: str # Parent resource: organizations/{id} or folders/{id}
state: Project.State # Current lifecycle state
labels: MutableMapping[str, str] # User-defined key-value labels
create_time: timestamp_pb2.Timestamp # Creation timestamp
update_time: timestamp_pb2.Timestamp # Last update timestamp
delete_time: timestamp_pb2.Timestamp # Deletion timestamp (if deleted)
etag: str # Entity tag for optimistic concurrency
class State(proto.Enum):
STATE_UNSPECIFIED = 0
ACTIVE = 1
DELETE_REQUESTED = 2
# Request/Response types
class GetProjectRequest:
name: str
class ListProjectsRequest:
parent: str
page_token: str
page_size: int
show_deleted: bool
class ListProjectsResponse:
projects: MutableSequence[Project]
next_page_token: str
class SearchProjectsRequest:
query: str
page_token: str
page_size: int
class SearchProjectsResponse:
projects: MutableSequence[Project]
next_page_token: str
class CreateProjectRequest:
project: Project
class UpdateProjectRequest:
project: Project
update_mask: field_mask_pb2.FieldMask
class MoveProjectRequest:
name: str
destination_parent: str
class DeleteProjectRequest:
name: str
class UndeleteProjectRequest:
name: str
# Metadata types for long-running operations
class CreateProjectMetadata:
create_time: timestamp_pb2.Timestamp
gettable: bool
ready: bool
class UpdateProjectMetadata:
# Empty metadata message
class MoveProjectMetadata:
# Empty metadata message
class DeleteProjectMetadata:
# Empty metadata message
class UndeleteProjectMetadata:
# Empty metadata messageInstall with Tessl CLI
npx tessl i tessl/pypi-google-cloud-resource-manager