Data Publishing on BigQuery using Authorized Views for Data Sharing Partners

Solution for Data Publishing on BigQuery using Authorized Views for Data Sharing Partners. 1 lab: GSP1041. Fast copy-paste commands for Google Cloud.

GSP1041 — Data Publishing on BigQuery using Authorized Views for Data Sharing Partners

Estimated time: 20 minutes

# 🤝 Data Publishing on BigQuery using Authorized Views for Data Sharing Partners > ⚠️ **Disclaimer:** This is an independent, community-made walkthrough created to help you understand why each step works. Attempt the lab yourself first and use this guide as a learning aid rather than as a shortcut to skip the underlying concepts. This guide is intended for educational purposes, hands-on practice, and Google Cloud certification preparation. It is not affiliated with, endorsed by, or officially

# ==============================================================================
# Color Variables & Branding
# ==============================================================================
GREEN='\e[1;32m'
CYAN='\e[1;36m'
YELLOW='\e[1;33m'
MAGENTA='\e[1;35m'
RED='\e[1;31m'
RESET='\e[0m'
BOLD='\e[1m'

clear
echo -e "${CYAN}${BOLD}"
cat << "EOF"
  ____       _     _ _            __    ___            
 / __ \     | |   (_) |          / _|  / _ \           
| |  | |_ __| |__  _| |_   ___  | |_  | | | |_ __  ___ 
| |  | | '__| '_ \| | __| / _ \ |  _| | | | | '_ \/ __|
| |__| | |  | |_) | | |_ | (_) || |   | |_| | |_) \__ \
 \____/|_|  |_.__/|_|\__| \___/ |_|    \___/| .__/|___/
                                            | |        
                                            |_|        
EOF
echo -e "${RESET}"
echo -e "${MAGENTA}${BOLD}>>> ORBIT OF OPS: GSP1041 PART 1 INITIALIZED <<<${RESET}\n"

echo -e "${YELLOW}${BOLD}[Orbit of Ops] Auto-fetching Project ID...${RESET}"
export PROJECT_ID=$(gcloud config get-value project 2>/dev/null)
if [[ -z "$PROJECT_ID" ]]; then
    export PROJECT_ID=$DEVSHELL_PROJECT_ID
fi
echo -e "✅ Project ID: ${GREEN}$PROJECT_ID${RESET}\n"

echo -e "${CYAN}${BOLD}⚠️  ATTENTION: CUSTOMER CREDENTIALS REQUIRED ⚠️${RESET}"
read -p "Enter Customer A Username (Email): " CUSTOMER_A_EMAIL
read -p "Enter Customer B Username (Email): " CUSTOMER_B_EMAIL
echo ""

echo -e "${CYAN}${BOLD}[Orbit of Ops] Task 1: Creating Authorized Views (TX & CA)...${RESET}"
bq mk --use_legacy_sql=false --view \
"SELECT * FROM \`bigquery-public-data.geo_us_boundaries.zip_codes\` WHERE state_code='TX' LIMIT 4000" \
$PROJECT_ID:demo_dataset.authorized_view_a || true

bq mk --use_legacy_sql=false --view \
"SELECT * FROM \`bigquery-public-data.geo_us_boundaries.zip_codes\` WHERE state_code='CA' LIMIT 4000" \
$PROJECT_ID:demo_dataset.authorized_view_b || true

echo -e "${YELLOW}${BOLD}[Orbit of Ops] Task 2: Assigning IAM Permissions to the Views (Dataset Level)...${RESET}"
bq show --format=prettyjson $PROJECT_ID:demo_dataset > dataset_metadata.json

jq --arg project_id "$PROJECT_ID" '.access += [
    { "view": { "datasetId": "demo_dataset", "projectId": $project_id, "tableId": "authorized_view_a" } },
    { "view": { "datasetId": "demo_dataset", "projectId": $project_id, "tableId": "authorized_view_b" } }
]' dataset_metadata.json > updated_dataset_metadata.json

bq update --source updated_dataset_metadata.json $PROJECT_ID:demo_dataset || true

echo -e "${CYAN}${BOLD}[Orbit of Ops] Task 3: Granting BigQuery Data Viewer to Customers...${RESET}"
# Create policy for Customer A
cat <<EOF > policy_a.json
{
  "bindings": [
    {
      "members": [
        "user:$CUSTOMER_A_EMAIL"
      ],
      "role": "roles/bigquery.dataViewer"
    }
  ]
}
EOF
bq set-iam-policy $PROJECT_ID:demo_dataset.authorized_view_a policy_a.json || true

# Create policy for Customer B
cat <<EOF > policy_b.json
{
  "bindings": [
    {
      "members": [
        "user:$CUSTOMER_B_EMAIL"
      ],
      "role": "roles/bigquery.dataViewer"
    }
  ]
}
EOF
bq set-iam-policy $PROJECT_ID:demo_dataset.authorized_view_b policy_b.json || true

echo -e "\n${GREEN}${BOLD}✅ Part 1 Complete. Please click 'Check my progress' on Tasks 1, 2, & 3!${RESET}"