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 feature for enabling APIs in Terraform deployments #74

Open
wants to merge 1 commit into
base: master
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
4 changes: 4 additions & 0 deletions example-config/solution.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ spec:
deploymentTool: TERRAFORM
# multiVm option is also available
singleVm:
apis:
- service: aiplatform.googleapis.com
disableOnDestroy: true
- service: compute.googleapis.com
images:
# if more than one image is provided, the user will be able to select which one they want
- project: your-project-id-that-the-image-belongs-to
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ message DeploymentPackageAutogenSpec {
}

// Specifies a solution that deploys a single VM.
// Next ID: 22
// Next ID: 23
//
// (== yamlExample
// partnerId: my-partner-id
Expand Down Expand Up @@ -151,10 +151,13 @@ message SingleVmDeploymentPackageSpec {

// Integration with Stackdriver.
StackdriverSpec stackdriver = 20;

// APIs enablement
repeated APIServiceSpec apis = 22;
}

// Specifies a solution that deploys Multiple VMs.
// Next ID: 9
// Next ID: 10
//
// (== yamlExample
// partnerId: my-partner-id
Expand Down Expand Up @@ -216,6 +219,9 @@ message MultiVmDeploymentPackageSpec {

// Integration with Stackdriver.
StackdriverSpec stackdriver = 8;

// APIs enablement
repeated APIServiceSpec apis = 9;
}

// A tier consists of one or more VMs of the same type. Each VM is
Expand Down Expand Up @@ -1044,6 +1050,15 @@ message BooleanExpression {
}
}

// Allows to enable a single API service.
message APIServiceSpec {
// The service to enable
string service = 1;

// API should be disabled when deployment is destroyed
bool disable_on_destroy = 2;
}

message OptionalInt32 {
int32 value = 1;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import * as constants from 'com/google/cloud/deploymentmanager/autogen/templates/tf/constants.soy';
import * as util from 'com/google/cloud/deploymentmanager/autogen/templates/tf/util.soy';
import {AcceleratorSpec, ApplicationStatusSpec, DeployInputField, DeployInputSpec, DiskSpec, ExternalIpSpec, FirewallRuleSpec, GceMetadataItem, GceStartupScriptSpec, GcpAuthScopeSpec, ImageSpec, InstanceUrlSpec, IpForwardingSpec, LocalSsdSpec, MachineTypeSpec, NetworkInterfacesSpec, PasswordSpec, StackdriverSpec, VmTierSpec, ZoneSpec} from 'java/com/google/cloud/deploymentmanager/autogen/deployment_package_autogen_spec.proto';
import {AcceleratorSpec, APIServiceSpec, ApplicationStatusSpec, DeployInputField, DeployInputSpec, DiskSpec, ExternalIpSpec, FirewallRuleSpec, GceMetadataItem, GceStartupScriptSpec, GcpAuthScopeSpec, ImageSpec, InstanceUrlSpec, IpForwardingSpec, LocalSsdSpec, MachineTypeSpec, NetworkInterfacesSpec, PasswordSpec, StackdriverSpec, VmTierSpec, ZoneSpec} from 'java/com/google/cloud/deploymentmanager/autogen/deployment_package_autogen_spec.proto';

/**
* Produces a google_compute_firewall resource for each FirewallRuleSpec
Expand Down Expand Up @@ -867,4 +867,20 @@ variable "{'ip_forward' |tierprefixed: $tier}" {lb}
{@param ipForwarding: IpForwardingSpec}
{@param? tier: VmTierSpec|null}
| {'ip_forward' |tierprefixed: $tier} | {constants.IP_FORWARD_VAR_DESC} | `bool` | `{not $ipForwarding!.getDefaultOff()}` | no |
{/template}
{/template}


/**
* Produces a google_project_service resource for each APIServiceSpec
*/
{template apis kind="text"}
{@param apisSpecs: list<APIServiceSpec>}
{for $spec in $apisSpecs}
resource "google_project_service" "{call util.apiServiceName}{param spec: $spec/}{/call}" {lb}
project = var.project_id
service = "{$spec.service}"
disable_on_destroy = {$spec.disableOnDestroy}
{rb}
{\n}
{/for}
{/template}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ provider "google" {lb}
project = var.project_id
{rb}
{\n}
{if $spec.getApisList()}
{call blocks.apis}
{param apisSpecs: $spec.getApisList() /}
{/call}
{/if}
{for $tier in $spec.getTiersList()}
module "{$tier.getName() |sanitize}" {lb}
source = "./modules/{$tier.getName() |doublequoted}"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,11 @@ locals {lb}
{param deployInput: $spec.getDeployInput() /}
{/call}
{/if}
{if $spec.getApisList()}
{call blocks.apis}
{param apisSpecs: $spec.getApisList() /}
{/call}
{/if}
resource "google_compute_instance" "instance" {lb}
name = "${lb}var.goog_cm_deployment_name{rb}-vm"
machine_type = var.machine_type
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{namespace util}

import {DiskSpec, FirewallRuleSpec, ImageSpec, InstanceUrlSpec, VmTierSpec, PasswordSpec, GcpAuthScopeSpec} from 'java/com/google/cloud/deploymentmanager/autogen/deployment_package_autogen_spec.proto';
import {APIServiceSpec, DiskSpec, FirewallRuleSpec, ImageSpec, InstanceUrlSpec, VmTierSpec, PasswordSpec, GcpAuthScopeSpec} from 'java/com/google/cloud/deploymentmanager/autogen/deployment_package_autogen_spec.proto';

/**
* Produces Terraform variable name for firewall boolean
Expand Down Expand Up @@ -311,4 +311,13 @@ Allow {nil}
{case GcpAuthScopeSpec.Scope.PROJECTHOSTING}
https://www.googleapis.com/auth/projecthosting{nil}
{/switch}
{/template}
{/template}

/**
* Produces Terraform variable name for enabling a service API
* Ex: aiplatform.googleapis.com -> ai_platform
*/
{template apiServiceName kind="text"}
{@param spec: APIServiceSpec}
{$spec.service.replaceAll(".","_")}{nil}
{/template}
Original file line number Diff line number Diff line change
Expand Up @@ -647,11 +647,18 @@ spec {
default_on: true
}
}
apis {
service: "aiplatform.googleapis.com"
disable_on_destroy: true
}
apis {
service: "compute.googleapis.com"
}
}
}
logo {
raw {
content_type: PNG
content: "\211PNG\r"
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,18 @@ provider "google" {
project = var.project_id
}

resource "google_project_service" "aiplatform_googleapis_com" {
project = var.project_id
service = "aiplatform.googleapis.com"
disable_on_destroy = true
}

resource "google_project_service" "compute_googleapis_com" {
project = var.project_id
service = "compute.googleapis.com"
disable_on_destroy = false
}

module "main" {
source = "./modules/main"
count = var.main_instance_count > 0 ? 1 : 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -542,6 +542,13 @@ spec {
}
monitoring: {}
}
apis {
service: "aiplatform.googleapis.com"
disable_on_destroy: true
}
apis {
service: "compute.googleapis.com"
}
}
}
logo {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,18 @@ resource "google_compute_disk" "disk3" {
size = var.disk3_size
}

resource "google_project_service" "aiplatform_googleapis_com" {
project = var.project_id
service = "aiplatform.googleapis.com"
disable_on_destroy = true
}

resource "google_project_service" "compute_googleapis_com" {
project = var.project_id
service = "compute.googleapis.com"
disable_on_destroy = false
}

resource "google_compute_instance" "instance" {
name = "${var.goog_cm_deployment_name}-vm"
machine_type = var.machine_type
Expand Down