# `valstorm-plugin-gcp` — Google Cloud Platform & BigQuery Plugin (`NF-486`) Foundational Google Cloud Platform (GCP) infrastructure and data warehouse plugin for **Open Valstorm**, mounted at `platform.gcp` (`VALSTORM_PLUGINS=gcp` or `*`). Provides multi-tenant, encrypted Service Account authentication for: - **Google BigQuery (`platform.gcp.bigquery`)**: Dataset/table discovery, schema inspection, parameterized SQL query execution, and streaming row insertion. - **Google Cloud Pub/Sub (`platform.gcp.pubsub`)**: Topic creation, push/pull subscription provisioning (including Gmail push watch topics), and message publishing. - **Extensible GCP Service Hooks**: `.storage`, `.compute`, `.iam`, and `.billing`. --- ## 🤖 Agent-Assisted Quick Setup (Let the Valstorm Agent Set It Up) Instead of clicking through the Google Cloud Console manually, you can authenticate the `gcloud` CLI once on your machine and let your **Valstorm AI Agent** provision the APIs, Service Account, IAM bindings, Pub/Sub topic, encrypted Valstorm credentials, and live connection verification automatically. ### 1. One-Time Human Prerequisite (60 Seconds) Install the Google Cloud CLI and sign in with your Google Cloud admin account: ```bash # macOS (Homebrew) brew install --cask google-cloud-sdk # Authenticate and select your target GCP project gcloud auth login gcloud config set project YOUR_GCP_PROJECT_ID ``` ### 2. Copy-Paste Prompt for Your Valstorm Agent Drop this prompt into Valstorm Chat (or your local agent): > *"Set up `valstorm-plugin-gcp` for my GCP project `` (default BigQuery dataset: ``). Use the steps in `plugins/valstorm-plugin-gcp/README.md` to enable the APIs via `gcloud`, create the `valstorm-gcp-integration` service account and IAM bindings, save the encrypted credentials into `platform.credentials` under `'gcp'`, delete any temporary key file, and run `gcp_verify_connection`."* ### 3. Agent Execution Runbook (Instructions for the AI Agent) When a user asks you to set up `valstorm-plugin-gcp`, execute this sequence autonomously: 1. **Verify `gcloud` Auth & Project**: - Run `gcloud config get-value project` and `gcloud auth list --filter=status:ACTIVE --format="value(account)"` to confirm an active authenticated session. 2. **Enable Required GCP APIs**: - Run `gcloud services enable bigquery.googleapis.com pubsub.googleapis.com iamcredentials.googleapis.com --project=`. 3. **Idempotently Create Service Account & Bind IAM Roles**: - Create `valstorm-gcp-integration@.iam.gserviceaccount.com` (ignore `ALREADY_EXISTS`). - Bind `roles/bigquery.dataEditor`, `roles/bigquery.jobUser`, and `roles/pubsub.editor`. - If the user also uses Gmail push sync, create topic `gmail-watch-events` and bind `serviceAccount:gmail-api-push@system.gserviceaccount.com` with `roles/pubsub.publisher`. 4. **Provision Key, Save Encrypted Credential, & Shred Temp File**: - Create a temporary key at `/tmp/vst_gcp_sa.json`, read and parse the JSON in Python, call `await platform.credentials.save("gcp", {**sa_json, "default_dataset": dataset})`, and **immediately delete `/tmp/vst_gcp_sa.json`** (`os.remove`). 5. **Verify Live Connection**: - Call `gcp_verify_connection` (or `await platform.gcp.verify_connection()`) and `gcp_bigquery_list_datasets` to confirm end-to-end access. --- ## 🛠️ End-to-End DIY Setup Guide (Manual / CLI) ### Step 1: Enable Required Google Cloud APIs In the [Google Cloud Console](https://console.cloud.google.com/), select or create your GCP project and enable: 1. **BigQuery API** (`bigquery.googleapis.com`) 2. **Cloud Pub/Sub API** (`pubsub.googleapis.com`) 3. **IAM Service Account Credentials API** (`iamcredentials.googleapis.com`) ```bash gcloud services enable \ bigquery.googleapis.com \ pubsub.googleapis.com \ iamcredentials.googleapis.com \ --project=YOUR_GCP_PROJECT_ID ``` ### Step 2: Create a Service Account & Grant IAM Roles Create a dedicated service account for Open Valstorm and grant least-privilege roles for BigQuery and Pub/Sub: ```bash # 1. Create the service account gcloud iam service-accounts create valstorm-gcp-integration \ --display-name="Valstorm GCP Integration" \ --project=YOUR_GCP_PROJECT_ID # 2. Grant BigQuery Data Editor & Job User roles gcloud projects add-iam-policy-binding YOUR_GCP_PROJECT_ID \ --member="serviceAccount:valstorm-gcp-integration@YOUR_GCP_PROJECT_ID.iam.gserviceaccount.com" \ --role="roles/bigquery.dataEditor" gcloud projects add-iam-policy-binding YOUR_GCP_PROJECT_ID \ --member="serviceAccount:valstorm-gcp-integration@YOUR_GCP_PROJECT_ID.iam.gserviceaccount.com" \ --role="roles/bigquery.jobUser" # 3. Grant Cloud Pub/Sub Editor role (for topic/subscription management & publishing) gcloud projects add-iam-policy-binding YOUR_GCP_PROJECT_ID \ --member="serviceAccount:valstorm-gcp-integration@YOUR_GCP_PROJECT_ID.iam.gserviceaccount.com" \ --role="roles/pubsub.editor" ``` ### Step 3: (Optional) Grant Gmail Push Publisher Access to Your Pub/Sub Topic If you are using `valstorm-plugin-gcp` together with `valstorm-plugin-google-workspace` for real-time Gmail push notifications, grant Google's official Gmail push service account (`gmail-api-push@system.gserviceaccount.com`) permission to publish to your Pub/Sub topic: ```bash gcloud pubsub topics add-iam-policy-binding gmail-watch-events \ --member="serviceAccount:gmail-api-push@system.gserviceaccount.com" \ --role="roles/pubsub.publisher" \ --project=YOUR_GCP_PROJECT_ID ``` ### Step 4: Generate & Download the Service Account JSON Key ```bash gcloud iam service-accounts keys create ./valstorm-gcp-sa.json \ --iam-account="valstorm-gcp-integration@YOUR_GCP_PROJECT_ID.iam.gserviceaccount.com" ``` *(Delete the local `./valstorm-gcp-sa.json` file immediately after saving it into Open Valstorm's encrypted credential store below.)* --- ## 2. Configuring Credentials in Open Valstorm Open Valstorm never reads local JSON key files from disk in production. Credentials are encrypted at rest per tenant in PostgreSQL (`auth_credential`) using `AES-256-CBC` / tenant BYOK envelope encryption via `platform.credentials`. ### Option A: Save via Python `PlatformContext` ```python await platform.credentials.save( "gcp", { "type": "service_account", "project_id": "your-gcp-project-id", "private_key_id": "key-id-from-json", "private_key": "-----BEGIN PRIVATE KEY-----\n...\n-----END PRIVATE KEY-----\n", "client_email": "valstorm-gcp-integration@your-gcp-project-id.iam.gserviceaccount.com", "client_id": "1234567890", "token_uri": "https://oauth2.googleapis.com/token", "default_dataset": "analytics_warehouse", }, ) ``` ### Option B: Verify Connection via REST API or AI Tool Once saved, verify connectivity and IAM permissions: ```bash curl -X POST http://localhost:8000/v1/gcp/verify \ -H "Authorization: Bearer " ``` Or ask the Valstorm AI Agent to run `gcp_verify_connection` or `gcp_get_setup_guide`. --- ## 3. Activating the Plugin Set `VALSTORM_PLUGINS` in your environment or `.env`: ```env # Enable all plugins (default) VALSTORM_PLUGINS=* # Or enable explicitly alongside related plugins VALSTORM_PLUGINS=gcp,google-workspace,google-marketing ``` --- ## 4. Usage Examples (`platform.gcp`) ```python # 1. List BigQuery datasets & tables datasets = await platform.gcp.bigquery.list_datasets() tables = await platform.gcp.bigquery.list_tables("analytics_warehouse") # 2. Inspect a BigQuery table schema schema = await platform.gcp.bigquery.get_table_schema("analytics_warehouse", "events") # 3. Execute a parameterized SQL query in BigQuery rows = await platform.gcp.bigquery.query( "SELECT event_name, COUNT(*) AS total FROM `analytics_warehouse.events` WHERE org_id = @org_id GROUP BY 1", params={"org_id": platform.org_id}, ) # 4. Stream rows into BigQuery await platform.gcp.bigquery.insert_rows( dataset="analytics_warehouse", table="events", rows=[{"event_name": "lead_created", "org_id": platform.org_id}], ) # 5. Publish a message to Google Cloud Pub/Sub msg_id = await platform.gcp.pubsub.publish( topic="crm-events", data=b'{"event": "deal_closed_won"}', attributes={"org_id": platform.org_id}, ) ``` --- ## 5. REST API Endpoints (`/v1/gcp/*`) | Method | Endpoint | Description | | :--- | :--- | :--- | | `GET` | `/v1/gcp/setup-guide` | Interactive step-by-step GCP setup instructions | | `POST` | `/v1/gcp/verify` | Validates GCP Service Account credentials & BigQuery reachability | | `GET` | `/v1/gcp/bigquery/datasets` | Lists all BigQuery datasets in the project | | `GET` | `/v1/gcp/bigquery/datasets/{dataset}/tables` | Lists tables inside a BigQuery dataset | | `GET` | `/v1/gcp/bigquery/datasets/{dataset}/tables/{table}` | Returns column schema for a BigQuery table | | `POST` | `/v1/gcp/bigquery/query` | Executes a SQL query against BigQuery | | `POST` | `/v1/gcp/bigquery/insert` | Streams JSON rows into a BigQuery table | | `POST` | `/v1/gcp/pubsub/publish` | Publishes a message to a Cloud Pub/Sub topic | --- ## 6. AI Agent Tools (`get_gcp_tools()`) - `gcp_get_setup_guide` - `gcp_verify_connection` - `gcp_bigquery_list_datasets` - `gcp_bigquery_list_tables` - `gcp_bigquery_get_table_schema` - `gcp_bigquery_query` - `gcp_bigquery_insert_rows` - `gcp_pubsub_publish` --- ## 7. Running Tests ```bash cd apps/open-api .venv/bin/pytest tests/test_plugin_gcp.py -v ```