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 cloud endpoints resource #933

Merged
merged 8 commits into from
Jan 24, 2018
Merged
Show file tree
Hide file tree
Changes from 7 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
3 changes: 3 additions & 0 deletions examples/endpoints-on-compute-engine/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
terraform.tfstate
terraform.tfstate.backup
terraform.tfvars
134 changes: 134 additions & 0 deletions examples/endpoints-on-compute-engine/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
provider "google" {
region = "${var.region}"
credentials = "${file("${var.credentials_file_path}")}"
}

provider "random" {}

resource "random_id" "project_name" {
byte_length = 8
}

resource "google_project" "endpoints_project" {
name = "Endpoints Project"
project_id = "tf-ep-${random_id.project_name.hex}"
org_id = "${var.org_id}"
billing_account = "${var.billing_account_id}"
}

resource "google_project_service" "endpoints_project" {
project = "${google_project.endpoints_project.project_id}"
service = "compute.googleapis.com"
}

resource "google_project_service" "endpoints_project_sm" {
project = "${google_project.endpoints_project.project_id}"
service = "servicemanagement.googleapis.com"
}

resource "google_endpoints_service" "endpoints_service" {
service_name = "echo-api.endpoints.${google_project.endpoints_project.project_id}.cloud.goog"
project = "${google_project.endpoints_project.project_id}"
config_text = <<EOF
swagger: "2.0"
info:
description: "A simple Google Cloud Endpoints API example."
title: "Endpoints Example"
version: "1.0.0"
host: "echo-api.endpoints.${google_project.endpoints_project.project_id}.cloud.goog"
basePath: "/"
consumes:
- "application/json"
produces:
- "application/json"
schemes:
- "https"
paths:
"/echo":
post:
description: "Echo back a given message."
operationId: "echo"
produces:
- "application/json"
responses:
200:
description: "Echo"
schema:
$ref: "#/definitions/echoMessage"
parameters:
- description: "Message to echo"
in: body
name: message
required: true
schema:
$ref: "#/definitions/echoMessage"
security:
- api_key: []
definitions:
echoMessage:
properties:
message:
type: "string"
EOF
depends_on = ["google_project_service.endpoints_project_sm"]
}

resource "google_compute_network" "network" {
name = "ep-network"
auto_create_subnetworks = "true"
project = "${google_project.endpoints_project.project_id}"
depends_on = ["google_project_service.endpoints_project"]
}

# Allow the hosted network to be hit over ICMP, SSH, and HTTP.
resource "google_compute_firewall" "network" {
name = "allow-ssh-and-icmp"
network = "${google_compute_network.network.self_link}"
project = "${google_compute_network.network.project}"

allow {
protocol = "icmp"
}

allow {
protocol = "tcp"
ports = ["22", "80"]
}
}

resource "google_compute_instance" "project_1_vm" {
name = "tf-ep-vm"
project = "${google_project.endpoints_project.project_id}"
machine_type = "f1-micro"
zone = "${var.region_zone}"

boot_disk {
initialize_params {
image = "projects/debian-cloud/global/images/family/debian-8"
}
}

metadata {
endpoints-service-name = "${google_endpoints_service.endpoints_service.service_name}"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: alignment (terraform fmt in this directory should take care of it)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

endpoints-service-config-id = "${google_endpoints_service.endpoints_service.config_id}"
startup-script = "${file("scripts/install-vm.sh")}"
}

network_interface {
network = "${google_compute_firewall.network.network}"

access_config {
// Ephemeral IP
}
}

service_account {
scopes = ["https://www.googleapis.com/auth/cloud-platform"]
}

depends_on = ["google_project_service.endpoints_project_sm", "google_project_service.endpoints_project"]
}

output "ip" {
value = "${google_compute_instance.project_1_vm.network_interface.0.access_config.0.assigned_nat_ip}"
}
14 changes: 14 additions & 0 deletions examples/endpoints-on-compute-engine/scripts/install-vm.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/bin/bash

sudo apt-get install git -y
curl https://storage.googleapis.com/golang/go1.7.3.linux-amd64.tar.gz | tar xzf -
wget https://raw.githubusercontent.com/GoogleCloudPlatform/golang-samples/master/endpoints/getting-started/app.go
GOPATH=$PWD GOROOT=$PWD/go go/bin/go get ./... 2> /tmp/goget_log.txt > /tmp/goget_outlog.txt
echo "deb http://packages.cloud.google.com/apt google-cloud-endpoints-jessie main" | sudo tee /etc/apt/sources.list.d/google-cloud-endpoints.list
curl --silent https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add -
sudo apt-get update && sudo apt-get install google-cloud-sdk
sudo apt-get install endpoints-runtime
sudo echo "PORT=80" >> /etc/default/nginx
PORT=8081 GOPATH=$PWD GOROOT=$PWD/go go/bin/go run app.go > /tmp/gorun_outlog.txt 2> /tmp/gorun_log.txt &
sleep 10
sudo service nginx restart
20 changes: 20 additions & 0 deletions examples/endpoints-on-compute-engine/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
variable "region" {
default = "us-central1"
}

variable "region_zone" {
default = "us-central1-f"
}

variable "org_id" {
description = "The ID of the Google Cloud Organization."
}

variable "billing_account_id" {
description = "The ID of the associated billing account (optional)."
}

variable "credentials_file_path" {
description = "Location of the credentials to use."
default = "~/.gcloud/Terraform.json"
}
1 change: 1 addition & 0 deletions google/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ func Provider() terraform.ResourceProvider {
"google_dataproc_job": resourceDataprocJob(),
"google_dns_managed_zone": resourceDnsManagedZone(),
"google_dns_record_set": resourceDnsRecordSet(),
"google_endpoints_service": resourceEndpointsService(),
"google_folder": resourceGoogleFolder(),
"google_folder_iam_policy": ResourceIamPolicyWithImport(IamFolderSchema, NewFolderIamUpdater, FolderIdParseFunc),
"google_folder_organization_policy": resourceGoogleFolderOrganizationPolicy(),
Expand Down
Loading