Skip to content

Add gcp cloud function #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
14 changes: 14 additions & 0 deletions gcp/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Google Cloud Function Configuration
PROJECT_ID=<gcp-project-id>
REGION=<gcp-region>
MEMORY=128Mi
TIMEOUT=120s
RUNTIME=nodejs22

# Cluster Credentials
DATA_API_ENDPOINT=<capella-data-api-endpoint>
DB_USERNAME=<capella-cluster-username>
DB_PASSWORD=<capella-cluster-password>
DB_BUCKET_NAME=travel-sample
DB_SCOPE=inventory
DB_COLLECTION=airport
72 changes: 72 additions & 0 deletions gcp/.gcloudignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
# Node.js dependencies
node_modules/
npm-debug.log*
yarn-debug.log*
yarn-error.log*

# Environment files
.env
.env.local
.env.development.local
.env.test.local
.env.production.local

# IDE and editor files
.vscode/
.idea/
*.swp
*.swo
*~

# OS generated files
.DS_Store
.DS_Store?
._*
.Spotlight-V100
.Trashes
ehthumbs.db
Thumbs.db

# Git files
.git/
.gitignore

# Build outputs
dist/
build/
*.tgz

# Logs
logs/
*.log

# Runtime data
pids/
*.pid
*.seed
*.pid.lock

# Coverage directory used by tools like istanbul
coverage/

# Dependency directories
jspm_packages/

# Optional npm cache directory
.npm

# Optional REPL history
.node_repl_history

# Output of 'npm pack'
*.tgz

# Yarn Integrity file
.yarn-integrity

# Scripts and deployment files
scripts/
# *.md

# Spec files
*.yaml
158 changes: 158 additions & 0 deletions gcp/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
# Node.js dependencies
node_modules/
npm-debug.log*
yarn-debug.log*
yarn-error.log*
lerna-debug.log*

# Runtime data
pids
*.pid
*.seed
*.pid.lock

# Coverage directory used by tools like istanbul
coverage/
*.lcov

# nyc test coverage
.nyc_output

# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# Bower dependency directory (https://bower.io/)
bower_components

# node-waf configuration
.lock-wscript

# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release

# Dependency directories
jspm_packages/

# TypeScript cache
*.tsbuildinfo

# Optional npm cache directory
.npm

# Optional eslint cache
.eslintcache

# Microbundle cache
.rpt2_cache/
.rts2_cache_cjs/
.rts2_cache_es/
.rts2_cache_umd/

# Optional REPL history
.node_repl_history

# Output of 'npm pack'
*.tgz

# Yarn Integrity file
.yarn-integrity

# Environment variables
.env
.env.local
.env.development.local
.env.test.local
.env.production.local

# parcel-bundler cache (https://parceljs.org/)
.cache
.parcel-cache

# Next.js build output
.next

# Nuxt.js build / generate output
.nuxt
dist

# Gatsby files
.cache/
public

# Storybook build outputs
.out
.storybook-out

# Temporary folders
tmp/
temp/

# Logs
logs
*.log

# OS generated files
.DS_Store
.DS_Store?
._*
.Spotlight-V100
.Trashes
ehthumbs.db
Thumbs.db

# IDE and Editor files
.vscode/
.idea/
*.swp
*.swo
*~
*.sublime-project
*.sublime-workspace

# Google Cloud specific
.gcloud/
.google-cloud/

# Deployment artifacts
zips/
*.zip
*.tar.gz

# Local development
.local/
local/

# Test files
test-results/
coverage/

# Build outputs
build/
dist/

# Serverless directories
.serverless/

# FuseBox cache
.fusebox/

# DynamoDB Local files
.dynamodb/

# TernJS port file
.tern-port

# Stores VSCode versions used for testing VSCode extensions
.vscode-test

# deployment packages
*.zip

# Local configuration files
config/local.json
config/development.json
config/production.json

# Backup files
*.bak
*.backup
105 changes: 105 additions & 0 deletions gcp/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
# GCP Cloud Functions API for Couchbase

This project demonstrates how to build a serverless API using **GCP Cloud Functions and API Gateway** that interfaces with Couchbase's Data API to manage airport data from the travel-sample dataset.

**Note:** The FTS features require:
1. A Full Text Search index with geo-spatial mapping on hotel documents
2. The travel-sample dataset with hotel documents in the `inventory.hotel` collection
3. Hotels must have geo coordinates (`geo.lat` and `geo.lon` fields) for proximity search

## Prerequisites

- [Node.js](https://nodejs.org/) (v22.x or later)
- [Google Cloud CLI](https://cloud.google.com/sdk/docs/install) configured with appropriate credentials
- Google Cloud Project with Cloud Functions and API Gateway with [services enabled](https://cloud.google.com/api-gateway/docs/secure-traffic-gcloud#enabling_required_services)
- [Couchbase Capella](https://www.couchbase.com/products/capella/) cluster with Data API enabled
- Couchbase [travel-sample](https://docs.couchbase.com/dotnet-sdk/current/ref/travel-app-data-model.html) bucket loaded

## Setup

1. Clone the repository
```bash
git clone https://github.com/couchbase-examples/couchbase-data-api-serverless-cookbook.git
```

2. Navigate to the gcp directory:
```bash
cd couchbase-data-api-serverless-cookbook/gcp
```

3. Install dependencies:
```bash
npm install
```

4. Configure your environment variables:
Copy the example environment file and update with your values:
```bash
cp .env.example .env
```

Update the `.env` file with your actual values:
```env
# Google Cloud Function Configuration
PROJECT_ID=<gcp-project-id>
REGION=europe-west1
MEMORY=128Mi
TIMEOUT=120s
RUNTIME=nodejs22

# Cluster Credentials
DATA_API_ENDPOINT=<capella-data-api-endpoint>
DB_USERNAME=<capella-cluster-username>
DB_PASSWORD=<capella-cluster-password>
DB_BUCKET_NAME=travel-sample
DB_SCOPE=inventory
DB_COLLECTION=airport
```

5. Authenticate with Google Cloud:
```bash
gcloud auth login
```

## FTS Index Setup

Before using the hotel search functionality, you need to create a Full Text Search index. Use the Node.js script provided in the root of the repository.

See [../scripts/README.md](../scripts/README.md) for detailed instructions on creating the required `hotel-geo-index` for geo-spatial hotel searches.


## Deployment

### Deploy Cloud Functions

First, deploy your Cloud Functions:

```bash
npm run deploy-gcp-cloud-function
```

This will deploy all functions defined in the `FUNCTIONS` array in `config.js`.

### Deploy API Gateway

After functions are deployed, run the API Gateway deployment:

```bash
npm run deploy-api-gateway
```

This script will:
1. Create a new API Gateway
2. Set up Cloud Function integrations
3. Create a gateway and output the API endpoint URL

## Testing

### Integration Testing

The integration tests will automatically discover your API Gateway endpoint and test all API operations through it:
```bash
npm run test
```


Loading