Skip to content
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

add testing #88

Merged
merged 7 commits into from
Nov 7, 2023
Merged
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
2 changes: 1 addition & 1 deletion docs/CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
# the repo. Unless a later match takes precedence,
# these users will be requested for
# review when someone opens a pull request.
* @fellipeamedeiros @sylvioneto
* @fellipeamedeiros @sylvioneto @rajasnathak
22 changes: 22 additions & 0 deletions three-tier-app-gce/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,29 @@ sh prereq.sh
gcloud builds submit . --config cloudbuild.yaml
```

## Testing

After you deployed the solution, you can check the resources created and see how they work together.

First, go to [Google Compute Engine](https://console.cloud.google.com/compute/instances) you can see your VMs.

![GCE](assets/gce.png)

Lastly,go to [Cloud LoadBalancing](https://console.cloud.google.com/net-services/loadbalancing/list/loadBalancers) and click on the frontend Load Balancer

![loadbalancer](assets/loadbalancer.png)

You can see all the details of the Load Balancer, and you can copy the Frontend IP section to access the application.

![details](assets/details.png)

Finally, if you paste the ip and enter in your browser, you see the example application

![template](assets/application.png)


## Cleaning up your environment

1. Click on Open in Google Cloud Shell button below.
<a href="https://ssh.cloud.google.com/cloudshell/editor?cloudshell_git_repo=https://github.com/GoogleCloudPlatform/click-to-deploy-solutions&cloudshell_workspace=three-tier-app-gce" target="_new">
<img alt="Open in Cloud Shell" src="https://gstatic.com/cloudssh/images/open-btn.svg">
Expand Down
Binary file added three-tier-app-gce/assets/application.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added three-tier-app-gce/assets/details.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added three-tier-app-gce/assets/gce.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added three-tier-app-gce/assets/loadbalancer.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
19 changes: 19 additions & 0 deletions three-tier-app-gce/cloudbuild.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,34 @@ steps:
-backend-config="bucket=$PROJECT_ID-tf-state" \
-backend-config="prefix=three-tier-app-gce"
dir: terraform

- id: 'tf apply'
name: 'hashicorp/terraform:1.0.0'
args:
- apply
- -auto-approve
dir: terraform

- id: 'upload data'
name: 'gcr.io/cloud-builders/gcloud-slim'
entrypoint: 'sh'
args:
- '-c'
- |
export DATABASE_INSTANCE=$(gcloud sql instances list --format='value(name)' --filter='labels.solution:three-tier-app-gce')
export SERVICE_ACCOUNT=$(gcloud sql instances describe $$DATABASE_INSTANCE --format='value(serviceAccountEmailAddress)')
gsutil mb gs://$PROJECT_ID-sqldata
gsutil cp schema.sql gs://$PROJECT_ID-sqldata
gsutil iam ch serviceAccount:$$SERVICE_ACCOUNT:objectAdmin gs://$PROJECT_ID-sqldata
gcloud sql import sql $$DATABASE_INSTANCE gs://$PROJECT_ID-sqldata/schema.sql -q
gsutil rm gs://$PROJECT_ID-sqldata/schema.sql
gsutil rb gs://$PROJECT_ID-sqldata
dir: sql

options:
env:
- TF_VAR_project_id=$PROJECT_ID
- TF_VAR_project_number=$PROJECT_NUMBER
tags:
- terraform
- three-tier-app-gce
Expand Down
1 change: 1 addition & 0 deletions three-tier-app-gce/cloudbuild_destroy.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ steps:
options:
env:
- TF_VAR_project_id=$PROJECT_ID
- TF_VAR_project_number=$PROJECT_NUMBER
tags:
- terraform
- three-tier-app-gce
Expand Down
45 changes: 45 additions & 0 deletions three-tier-app-gce/sql/schema.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
-- Copyright 2023 Google LLC
--
-- Licensed under the Apache License, Version 2.0 (the "License");
-- you may not use this file except in compliance with the License.
-- You may obtain a copy of the License at
--
-- https://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing, software
-- distributed under the License is distributed on an "AS IS" BASIS,
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-- See the License for the specific language governing permissions and
-- limitations under the License.

CREATE DATABASE IF NOT EXISTS todo;

USE todo;

DROP TABLE IF EXISTS `todo`;

CREATE TABLE `todo` (
`id` int unsigned NOT NULL AUTO_INCREMENT,
`title` varchar(512) DEFAULT NULL,
`updated` datetime DEFAULT NULL,
`completed` datetime DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

LOCK TABLES `todo` WRITE;
/*!40000 ALTER TABLE `todo` DISABLE KEYS */;

INSERT INTO `todo` (`id`, `title`, `updated`, `completed`)
VALUES
(1,'Install and configure todo app','2021-10-28 12:00:00','2021-10-28 12:00:00'),
(2,'Add your own todo','2021-10-28 12:00:00',NULL),
(3,'Mark task 1 done','2021-10-27 14:26:00',NULL);

/*!40000 ALTER TABLE `todo` ENABLE KEYS */;
UNLOCK TABLES;

CREATE USER 'todo_user'@'localhost' IDENTIFIED BY 'todo_pass';
CREATE USER 'todo_user'@'%' IDENTIFIED BY 'todo_pass';

GRANT ALL ON todo.* TO 'todo_user'@'localhost';
GRANT ALL ON todo.* TO 'todo_user'@'%';
28 changes: 28 additions & 0 deletions three-tier-app-gce/terraform/api.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
Copyright 2023 Google LLC

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

https://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

resource "google_project_service" "api" {
count = length(var.apis)

project = var.project_id
service = var.apis[count.index]

timeouts {
create = "30m"
update = "40m"
}
disable_on_destroy = false
}
65 changes: 16 additions & 49 deletions three-tier-app-gce/terraform/cloud_sql.tf
Original file line number Diff line number Diff line change
Expand Up @@ -12,64 +12,31 @@
# See the License for the specific language governing permissions and
# limitations under the License.

locals {
db_instance_name = "${var.application_name}-${random_id.db_name_suffix.hex}"
resource "random_id" "id" {
byte_length = 2
}

resource "random_id" "db_name_suffix" {
byte_length = 4
}

resource "google_sql_database_instance" "instance" {
name = local.db_instance_name
region = var.region
database_version = "POSTGRES_14"
deletion_protection = false # not recommended for PROD

resource "google_sql_database_instance" "cloud_sql" {
name = "${var.application_name}-db-${random_id.id.hex}"
database_version = "MYSQL_5_7"
region = var.region
project = var.project_id
settings {
tier = "db-custom-1-3840"
user_labels = local.resource_labels
tier = "db-g1-small"
user_labels = local.resource_labels
disk_autoresize = true
disk_autoresize_limit = 0
disk_size = 10
disk_type = "PD_SSD"

ip_configuration {
ipv4_enabled = false
private_network = module.vpc.network_self_link
}
}

deletion_protection = false
depends_on = [
google_service_networking_connection.service_networking,
google_project_service.api
]
}

resource "google_sql_database" "database" {
instance = google_sql_database_instance.instance.id
name = "${var.application_name}-db"
}

resource "random_password" "password" {
length = 16
special = true
}

resource "google_secret_manager_secret" "db_password" {
secret_id = "${local.db_instance_name}-password"
labels = local.resource_labels
replication {
user_managed {
replicas {
location = var.region
}
}
}
}

resource "google_secret_manager_secret_version" "db_password_version" {
secret = google_secret_manager_secret.db_password.id
secret_data = random_password.password.result
}

resource "google_sql_user" "user" {
instance = google_sql_database_instance.instance.id
name = var.application_name
password = random_password.password.result
}
}
3 changes: 2 additions & 1 deletion three-tier-app-gce/terraform/gce.tf
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ module "instance_template" {
preemptible = true

startup_script = <<EOF
docker run --rm -p 80:3000 bkimminich/juice-shop:v14.0.1
docker run --rm -p 80:80 fellipemedeiros/todo-frontend:latest
docker run --rm -p 8080:8080 fellipemedeiros/todo-backend:latest
EOF

tags = [
Expand Down
32 changes: 32 additions & 0 deletions three-tier-app-gce/terraform/secretmanager.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/**
Copyright 2023 Google LLC

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

https://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

# Handle secrets
resource "google_secret_manager_secret" "sqlhost" {
project = var.project_number
replication {
automatic = true
}
secret_id = "sqlhost"
depends_on = [google_project_service.api]
}

resource "google_secret_manager_secret_version" "sqlhost" {
enabled = true
secret = "projects/${var.project_number}/secrets/sqlhost"
secret_data = google_sql_database_instance.cloud_sql.private_ip_address
depends_on = [google_project_service.api, google_sql_database_instance.cloud_sql, google_secret_manager_secret.sqlhost]
}
11 changes: 10 additions & 1 deletion three-tier-app-gce/terraform/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ locals {
deployed_by = "cloudbuild"
env = "sandbox"
repo = "click-to-deploy-solutions"
solution = "gke-autopilot-hpa"
solution = "three-tier-app-gce"
terraform = "true"
}

Expand All @@ -40,6 +40,10 @@ variable "network_name" {
description = "VPC name"
}

variable "project_number" {
description = "GCP Project Number"
}

variable "project_id" {
description = "GCP Project ID"
}
Expand All @@ -53,3 +57,8 @@ variable "subnet_cidr" {
type = string
description = "Subnet CIDR"
}

variable "apis" {
description = "APIs required to deploy the project"
default = ["redis.googleapis.com", "compute.googleapis.com", "sqladmin.googleapis.com", "secretmanager.googleapis.com", "servicenetworking.googleapis.com", "cloudbuild.googleapis.com", "cloudresourcemanager.googleapis.com"]
}
Loading