Skip to content

Commit 0033d12

Browse files
authored
feat(cloudsql/mysql): update to V2 sample (GoogleCloudPlatform#2312)
* feat(cloudsql): update to V2 sample * Add connector example in addition to TCP and UNIX socket example * Add support for Cloud Functions - Create a cmd/app directory for the main package - Rename the top-level package "cloudsql" - Move template into Go file for simple deployment - use "/Votes" as endpoint for casting a vote to match default Cloud Functions naming scheme
1 parent 126fbb2 commit 0033d12

15 files changed

+1498
-533
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,4 @@
3333
**/sponge_log.log
3434

3535
testing/kokoro/test-env.sh
36+
.envrc

cloudsql/mysql/database-sql/Dockerfile

+3-4
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ RUN go mod download
3030
COPY . ./
3131

3232
# Build the binary.
33-
RUN go build -v -o server
33+
RUN go build -v -o server ./cmd/app
3434

3535
# Use the official Debian slim image for a lean production container.
3636
# https://hub.docker.com/_/debian
@@ -42,10 +42,9 @@ RUN set -x && apt-get update && DEBIAN_FRONTEND=noninteractive apt-get install -
4242

4343
# Copy the binary to the production image from the builder stage.
4444
COPY --from=builder /app/server /app/server
45-
COPY ./templates /app/templates
46-
# Copy any certificates IFF present.
47-
COPY ./certs /app/certs
4845

46+
# Copy any certificates IF present.
47+
COPY ./certs /app/certs
4948
# Run the web service on container startup.
5049
WORKDIR /app
5150
CMD ["/app/server"]

cloudsql/mysql/database-sql/README.md

+45-24
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,18 @@ This repo contains the Go source code for a simple web app that can be deployed
44

55
## Before you begin
66

7-
1. If you haven't already, set up a Go Development Environment by following the [Go setup guide](https://cloud.google.com/go/docs/setup) and
7+
1. If you haven't already, set up a Go Development Environment by following the [Go setup guide](https://cloud.google.com/go/docs/setup) and
88
[create a project](https://cloud.google.com/resource-manager/docs/creating-managing-projects#creating_a_project).
99

10-
1. Create a Cloud SQL for MySQL instance by following these
10+
1. Create a Cloud SQL for MySQL instance by following these
1111
[instructions](https://cloud.google.com/sql/docs/mysql/create-instance).
1212
Note the connection string, database user, and database password that you create.
1313

14-
1. Create a database for your application by following these
14+
1. Create a database for your application by following these
1515
[instructions](https://cloud.google.com/sql/docs/mysql/create-manage-databases).
16-
Note the database name.
16+
Note the database name.
1717

18-
1. Create a service account with the 'Cloud SQL Client' permissions by following these
18+
1. Create a service account with the 'Cloud SQL Client' permissions by following these
1919
[instructions](https://cloud.google.com/sql/docs/mysql/connect-external-app#4_if_required_by_your_authentication_method_create_a_service_account).
2020
Download a JSON key to use to authenticate your connection.
2121

@@ -35,7 +35,7 @@ To run the sample locally with a TCP connection, set environment variables and l
3535
Use these terminal commands to initialize environment variables:
3636
```bash
3737
export GOOGLE_APPLICATION_CREDENTIALS=/path/to/service/account/key.json
38-
export DB_TCP_HOST='127.0.0.1'
38+
export INSTANCE_HOST='127.0.0.1'
3939
export DB_PORT='3306'
4040
export DB_USER='<DB_USER_NAME>'
4141
export DB_PASS='<DB_PASSWORD>'
@@ -51,7 +51,7 @@ Then use this command to launch the proxy in the background:
5151
Use these PowerShell commands to initialize environment variables:
5252
```powershell
5353
$env:GOOGLE_APPLICATION_CREDENTIALS="<CREDENTIALS_JSON_FILE>"
54-
$env:DB_TCP_HOST="127.0.0.1"
54+
$env:INSTANCE_HOST="127.0.0.1"
5555
$env:DB_PORT="3306"
5656
$env:DB_USER="<DB_USER_NAME>"
5757
$env:DB_PASS="<DB_PASSWORD>"
@@ -72,24 +72,18 @@ sudo mkdir ./cloudsql
7272
sudo chown -R $USER ./cloudsql
7373
```
7474

75-
You'll also need to initialize an environment variable containing the directory you just created:
76-
77-
```bash
78-
export DB_SOCKET_DIR=./cloudsql
79-
```
80-
8175
Use these terminal commands to initialize environment variables:
8276
```bash
8377
export GOOGLE_APPLICATION_CREDENTIALS=/path/to/service/account/key.json
84-
export INSTANCE_CONNECTION_NAME='<MY-PROJECT>:<INSTANCE-REGION>:<INSTANCE-NAME>'
78+
export INSTANCE_UNIX_SOCKET='./cloudsql/<MY-PROJECT>:<INSTANCE-REGION>:<INSTANCE-NAME>'
8579
export DB_USER='<DB_USER_NAME>'
8680
export DB_PASS='<DB_PASSWORD>'
8781
export DB_NAME='<DB_NAME>'
8882
```
8983

9084
Then use this command to launch the proxy in the background:
9185
```bash
92-
./cloud_sql_proxy -dir=$DB_SOCKET_DIR --instances=$INSTANCE_CONNECTION_NAME --credential_file=$GOOGLE_APPLICATION_CREDENTIALS &
86+
./cloud_sql_proxy -dir=./cloudsql --instances=$INSTANCE_CONNECTION_NAME --credential_file=$GOOGLE_APPLICATION_CREDENTIALS &
9387
```
9488

9589
### Testing the application
@@ -102,16 +96,16 @@ To test the application locally, follow these steps after the proxy is running:
10296

10397
## Deploying to App Engine Standard
10498

105-
To run the sample on GAE-Standard, create an App Engine project by following the setup for these
99+
To run the sample on GAE-Standard, create an App Engine project by following the setup for these
106100
[instructions](https://cloud.google.com/appengine/docs/standard/go/quickstart#before-you-begin).
107101

108-
First, update `app.standard.yaml` with the correct values to pass the environment
102+
First, update `app.standard.yaml` with the correct values to pass the environment
109103
variables into the runtime. Your `app.standard.yaml` file should look like this:
110104

111105
```yaml
112106
runtime: go113
113107
env_variables:
114-
INSTANCE_CONNECTION_NAME: <project-id>:<region>:<instance-name>
108+
INSTANCE_UNIX_SOCKET: /cloudsql/<project-id>:<region>:<instance-name>
115109
DB_USER: YOUR_DB_USER
116110
DB_PASS: YOUR_DB_PASS
117111
DB_NAME: YOUR_DB
@@ -127,22 +121,22 @@ gcloud app deploy app.standard.yaml
127121

128122
## Deploying to App Engine Flexible
129123

130-
To run the sample on GAE-Flex, create an App Engine project by following the setup for these
124+
To run the sample on GAE-Flex, create an App Engine project by following the setup for these
131125
[instructions](https://cloud.google.com/appengine/docs/standard/go/quickstart#before-you-begin).
132126

133-
First, update `app.flexible.yaml` with the correct values to pass the environment
127+
First, update `app.flexible.yaml` with the correct values to pass the environment
134128
variables into the runtime. Your `app.flexible.yaml` file should look like this:
135129
```yaml
136130
runtime: custom
137131
env: flex
138132

139133
env_variables:
140-
INSTANCE_CONNECTION_NAME: <project>:<region>:<instance>
134+
INSTANCE_UNIX_SOCKET: /cloudsql/<project>:<region>:<instance>
141135
DB_USER: <your_database_username>
142136
DB_PASS: <your_database_password>
143137
DB_NAME: <your_database_name>
144138

145-
beta_settings:
139+
beta_settings:
146140
cloud_sql_instances: <project>:<region>:<instance>
147141
```
148142
@@ -169,7 +163,7 @@ gcloud builds submit --tag gcr.io/[YOUR_PROJECT_ID]/run-sql
169163
```sh
170164
gcloud run deploy run-sql --image gcr.io/[YOUR_PROJECT_ID]/run-sql \
171165
--add-cloudsql-instances '<MY-PROJECT>:<INSTANCE-REGION>:<INSTANCE-NAME>' \
172-
--update-env-vars INSTANCE_CONNECTION_NAME='<MY-PROJECT>:<INSTANCE-REGION>:<INSTANCE-NAME>' \
166+
--update-env-vars INSTANCE_UNIX_SOCKET='/cloudsql/<MY-PROJECT>:<INSTANCE-REGION>:<INSTANCE-NAME>' \
173167
--update-env-vars DB_USER='<DB_USER_NAME>' \
174168
--update-env-vars DB_PASS='<DB_PASSWORD>' \
175169
--update-env-vars DB_NAME='<DB_NAME>'
@@ -202,4 +196,31 @@ gcloud beta run deploy SERVICE --image gcr.io/[YOUR_PROJECT_ID]/run-sql \
202196

203197
4. Navigate your browser to the URL noted in step 2.
204198

205-
For more details about using Cloud Run see http://cloud.run.
199+
For more details about using Cloud Run see http://cloud.run.
200+
201+
## Running Integration Tests
202+
203+
The integration tests depend on a Unix socket and a TCP listener provided by the
204+
Cloud SQL Auth Proxy. To run the tests, you will need to start two instances of
205+
the Cloud SQL Auth Proxy, one for a TCP connection and one for a Unix socket
206+
connection.
207+
208+
```
209+
cloud_sql_proxy -instances=some-project:some-region:some-instance=tcp:3306
210+
cloud_sql_proxy -instances=some-project:some-region:some-instance -dir /cloudsql
211+
```
212+
213+
To run integration tests, use the following command, setting environment
214+
variables to the correct values:
215+
216+
```
217+
GOLANG_SAMPLES_E2E_TEST="yes" \
218+
MYSQL_USER=some-user \
219+
MYSQL_PASSWORD=some-pass
220+
MYSQL_DATABASE=some-db \
221+
MYSQL_PORT=3307 \
222+
MYSQL_HOST=127.0.0.1
223+
MYSQL_UNIX_SOCKET='/cloudsql/some-project:some-region:some-instance'
224+
MYSQL_INSTANCE='some-project:some-region:some-instance' \
225+
go test -v
226+
```

0 commit comments

Comments
 (0)