|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# Copyright 2024 Google LLC |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | + |
| 17 | +set -e # exit on first error |
| 18 | + |
| 19 | +SCRIPTPATH="$( cd "$(dirname "$0")" || exit >/dev/null 2>&1 ; pwd -P )" |
| 20 | +export PATH="$PATH:$SCRIPTPATH/../../tools/apigee-sackmesser/bin" |
| 21 | + |
| 22 | +PROJECT_ID=$(gcloud config get-value project) |
| 23 | +GCP_REGION=europe-west2 |
| 24 | + |
| 25 | +# Generate the gRCP Gateway based on the proto file |
| 26 | +rm -rdf generated || true |
| 27 | +./generate-gateway.sh --proto-path ./examples/currency.proto |
| 28 | + |
| 29 | +# Build the gRPC Gateway |
| 30 | +(cd generated/gateway && CGO_ENABLED=0 go build -o grpcgateway .) |
| 31 | + |
| 32 | +# Build the grpc-gateway container and push it to Artifact Registry |
| 33 | +(cd generated/gateway && docker build -t grpc-gateway:latest .) |
| 34 | + |
| 35 | +DOCKER_REPO="devrel" |
| 36 | +REPO_LOCATION="europe" |
| 37 | + |
| 38 | +if [ -z "$(gcloud artifacts repositories describe $DOCKER_REPO \ |
| 39 | + --location=$REPO_LOCATION \ |
| 40 | + --project "$PROJECT_ID" \ |
| 41 | + --format='get(name)')" ]; then \ |
| 42 | + |
| 43 | + gcloud artifacts repositories create $DOCKER_REPO \ |
| 44 | + --repository-format=docker \ |
| 45 | + --location=$REPO_LOCATION \ |
| 46 | + --project="$PROJECT_ID" |
| 47 | +fi |
| 48 | + |
| 49 | +IMAGE_PATH="$REPO_LOCATION-docker.pkg.dev/$PROJECT_ID/$DOCKER_REPO/grpc-gateway" |
| 50 | +docker tag grpc-gateway:latest "$IMAGE_PATH:latest" |
| 51 | +docker push "$IMAGE_PATH" |
| 52 | + |
| 53 | +# Deploy grpc-gateway container to Cloud Run |
| 54 | +sed -i.bak "s|GRPC_GATEWAY_IMAGE|$IMAGE_PATH|g" "examples/currency-v1/cloud-run-service.yaml" |
| 55 | + |
| 56 | +gcloud run services replace examples/currency-v1/cloud-run-service.yaml \ |
| 57 | + --project "$PROJECT_ID" --region $GCP_REGION \ |
| 58 | + --platform managed |
| 59 | + |
| 60 | +# Generate and deploy an Apigee API proxy for the currency-service |
| 61 | +SA_EMAIL="apigee-test-cloudrun@$APIGEE_X_ORG.iam.gserviceaccount.com" |
| 62 | + |
| 63 | +if [ -z "$(gcloud iam service-accounts list --filter "$SA_EMAIL" --format="value(email)" --project "$APIGEE_X_ORG")" ]; then |
| 64 | + gcloud iam service-accounts create apigee-test-cloudrun \ |
| 65 | + --description="Apigee Test Cloud Run" --project "$APIGEE_X_ORG" |
| 66 | +fi |
| 67 | + |
| 68 | +gcloud run services add-iam-policy-binding currency-service \ |
| 69 | + --member="serviceAccount:$SA_EMAIL" \ |
| 70 | + --role='roles/run.invoker' \ |
| 71 | + --region=$GCP_REGION \ |
| 72 | + --platform=managed --project "$PROJECT_ID" |
| 73 | + |
| 74 | +CLOUD_RUN_URL=$(gcloud run services list --filter currency-service --format="value(status.url)" --limit 1) |
| 75 | +sed -i "s|CLOUD_RUN_URL|$CLOUD_RUN_URL|g" "examples/currency-v1/apiproxy/targets/default.xml" |
| 76 | + |
| 77 | +TOKEN="$(gcloud config config-helper --force-auth-refresh --format json | jq -r '.credential.access_token')" |
| 78 | +sackmesser deploy -d "$SCRIPTPATH/examples/currency-v1" -o "$APIGEE_X_ORG" -e "$APIGEE_X_ENV" -t "$TOKEN" --deployment-sa "$SA_EMAIL" |
| 79 | + |
| 80 | +# Test the Apigee API |
| 81 | +curl -X GET "https://$APIGEE_X_HOSTNAME/currency/v1/currencies" |
| 82 | + |
| 83 | +curl -X POST "https://$APIGEE_X_HOSTNAME/currency/v1/convert" \ |
| 84 | + -d '{"from": {"units": 3, "currency_code": "USD", "nanos": 0}, "to_code": "CHF"}' |
| 85 | + |
| 86 | +# Clean up |
| 87 | + # undeploy and delete proxy |
| 88 | + # delete CR service |
| 89 | + # delete SA |
| 90 | + # delete AR registry |
0 commit comments