Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 30 additions & 14 deletions agent_starter_pack/cli/commands/register_gemini_enterprise.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
from rich.console import Console

from agent_starter_pack.cli.utils.command import run_gcloud_command
from agent_starter_pack.cli.utils.gcp import _get_user_agent, _get_x_goog_api_client_header

# TOML parser - use standard library for Python 3.11+, fallback to tomli
if sys.version_info >= (3, 11):
Expand Down Expand Up @@ -298,6 +299,32 @@ def get_access_token() -> str:
raise RuntimeError("Failed to get access token") from e


def _build_api_headers(
access_token: str,
project_id: str,
content_type: bool = False,
) -> dict[str, str]:
"""Build headers for Discovery Engine API requests with user-agent.

Args:
access_token: Google Cloud access token
project_id: GCP project ID or number for billing
content_type: Whether to include Content-Type header (for POST/PATCH)

Returns:
Headers dictionary
"""
headers = {
"Authorization": f"Bearer {access_token}",
"x-goog-user-project": project_id,
"User-Agent": _get_user_agent(),
"x-goog-api-client": _get_x_goog_api_client_header(),
}
if content_type:
headers["Content-Type"] = "application/json"
return headers


def get_identity_token() -> str:
"""Get Google Cloud identity token.

Expand Down Expand Up @@ -604,10 +631,7 @@ def list_gemini_enterprise_apps(
f"{base_endpoint}/v1alpha/projects/{project_number}/"
f"locations/{location}/collections/default_collection/engines"
)
headers = {
"Authorization": f"Bearer {access_token}",
"x-goog-user-project": project_number,
}
headers = _build_api_headers(access_token, project_number)

response = requests.get(url, headers=headers, timeout=30)
response.raise_for_status()
Expand Down Expand Up @@ -937,11 +961,7 @@ def register_a2a_agent(
f"locations/{as_location}/collections/{collection}/engines/{engine_id}/"
"assistants/default_assistant/agents"
)
headers = {
"Authorization": f"Bearer {access_token}",
"Content-Type": "application/json",
"x-goog-user-project": project_id,
}
headers = _build_api_headers(access_token, project_id, content_type=True)

# Build payload with A2A agent definition
payload = {
Expand Down Expand Up @@ -1106,11 +1126,7 @@ def register_agent(
)

# Request headers
headers = {
"Authorization": f"Bearer {access_token}",
"Content-Type": "application/json",
"x-goog-user-project": project_id,
}
headers = _build_api_headers(access_token, project_id, content_type=True)

# Request body
payload: dict = {
Expand Down
25 changes: 3 additions & 22 deletions agent_starter_pack/cli/utils/cicd.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
from rich.prompt import IntPrompt, Prompt

from agent_starter_pack.cli.utils.command import get_gcloud_cmd
from agent_starter_pack.cli.utils.gcp import get_project_number

console = Console()

Expand Down Expand Up @@ -149,18 +150,7 @@ def create_github_connection(

# Get the Cloud Build service account and grant permissions with retry logic
try:
project_number_result = run_command(
[
"gcloud",
"projects",
"describe",
project_id,
"--format=value(projectNumber)",
],
capture_output=True,
check=True,
)
project_number = project_number_result.stdout.strip()
project_number = get_project_number(project_id)
cloud_build_sa = (
f"service-{project_number}@gcp-sa-cloudbuild.iam.gserviceaccount.com"
)
Expand Down Expand Up @@ -477,16 +467,7 @@ def ensure_apis_enabled(project_id: str, apis: list[str]) -> None:
capture_output=True,
)

project_number = run_command(
[
"gcloud",
"projects",
"describe",
project_id,
"--format=value(projectNumber)",
],
capture_output=True,
).stdout.strip()
project_number = get_project_number(project_id)

cloudbuild_sa = (
f"service-{project_number}@gcp-sa-cloudbuild.iam.gserviceaccount.com"
Expand Down
42 changes: 42 additions & 0 deletions agent_starter_pack/cli/utils/gcp.py
Original file line number Diff line number Diff line change
Expand Up @@ -268,3 +268,45 @@ def get_account() -> str:
):
raise Exception(_AUTH_ERROR_MESSAGE) from e
raise


def get_project_number(project_id: str) -> str:
"""Get project number from project ID using Resource Manager API.

Args:
project_id: GCP project ID

Returns:
Project number as string

Raises:
Exception: If the API request fails
"""
_, _, token = _get_credentials_and_token()

user_agent = _get_user_agent()
x_goog_api_client = _get_x_goog_api_client_header()

response = requests.get(
f"https://cloudresourcemanager.googleapis.com/v1/projects/{project_id}",
headers={
"Authorization": f"Bearer {token}",
"User-Agent": user_agent,
"x-goog-api-client": x_goog_api_client,
},
timeout=30,
)

if response.status_code == 200:
return response.json()["projectNumber"]
elif response.status_code == 403:
raise Exception(
f"Permission denied accessing project '{project_id}'. "
"Ensure you have the required permissions."
)
elif response.status_code == 404:
raise Exception(f"Project '{project_id}' not found.")
else:
raise Exception(
f"Failed to get project number: {response.status_code} - {response.text}"
)