diff --git a/.gitignore b/.gitignore index 477cdaf3..7fc273af 100644 --- a/.gitignore +++ b/.gitignore @@ -23,6 +23,7 @@ Session.vim # Local .terraform directories **/.terraform/* +.terraform.lock.hcl # .tfstate files *.tfstate diff --git a/.kitchen.yml b/.kitchen.yml index f6cd4cdc..d884d8ac 100644 --- a/.kitchen.yml +++ b/.kitchen.yml @@ -1,4 +1,4 @@ -# Copyright 2019 Google LLC +# Copyright 2021 Google LLC # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -159,3 +159,23 @@ suites: backend: local controls: - gcloud + - name: "submodule_vpc_serverless_connector_beta" + driver: + name: "terraform" + command_timeout: 1800 + root_module_directory: test/fixtures/submodule_vpc_serverless_connector_beta/ + verifier: + name: terraform + color: true + systems: + - name: inspec-gcp + attrs_outputs: + customized_inspec_attribute: output_connector_ids + backend: gcp + controls: + - gcp + - inspec_attributes + - name: local + backend: local + controls: + - gcloud diff --git a/build/int.cloudbuild.yaml b/build/int.cloudbuild.yaml index a23af78a..4e75e19b 100644 --- a/build/int.cloudbuild.yaml +++ b/build/int.cloudbuild.yaml @@ -161,6 +161,26 @@ steps: - verify submodule-network-peering-local name: 'gcr.io/cloud-foundation-cicd/$_DOCKER_IMAGE_DEVELOPER_TOOLS:$_DOCKER_TAG_VERSION_DEVELOPER_TOOLS' args: ['/bin/bash', '-c', 'source /usr/local/bin/task_helper_functions.sh && kitchen_do destroy submodule-network-peering-local'] +- id: create submodule-vpc-serverless-connector-beta + waitFor: + - prepare + name: 'gcr.io/cloud-foundation-cicd/$_DOCKER_IMAGE_DEVELOPER_TOOLS:$_DOCKER_TAG_VERSION_DEVELOPER_TOOLS' + args: ['/bin/bash', '-c', 'source /usr/local/bin/task_helper_functions.sh && kitchen_do create submodule-vpc-serverless-connector-beta'] +- id: converge submodule-vpc-serverless-connector-beta + waitFor: + - create submodule-vpc-serverless-connector-beta + name: 'gcr.io/cloud-foundation-cicd/$_DOCKER_IMAGE_DEVELOPER_TOOLS:$_DOCKER_TAG_VERSION_DEVELOPER_TOOLS' + args: ['/bin/bash', '-c', 'source /usr/local/bin/task_helper_functions.sh && kitchen_do converge submodule-vpc-serverless-connector-beta'] +- id: verify submodule-vpc-serverless-connector-beta + waitFor: + - converge submodule-vpc-serverless-connector-beta + name: 'gcr.io/cloud-foundation-cicd/$_DOCKER_IMAGE_DEVELOPER_TOOLS:$_DOCKER_TAG_VERSION_DEVELOPER_TOOLS' + args: ['/bin/bash', '-c', 'source /usr/local/bin/task_helper_functions.sh && kitchen_do verify submodule-vpc-serverless-connector-beta'] +- id: destroy submodule-vpc-serverless-connector-beta + waitFor: + - verify submodule-vpc-serverless-connector-beta + name: 'gcr.io/cloud-foundation-cicd/$_DOCKER_IMAGE_DEVELOPER_TOOLS:$_DOCKER_TAG_VERSION_DEVELOPER_TOOLS' + args: ['/bin/bash', '-c', 'source /usr/local/bin/task_helper_functions.sh && kitchen_do destroy submodule-vpc-serverless-connector-beta'] tags: - 'ci' - 'integration' diff --git a/examples/submodule_vpc_serverless_connector/README.md b/examples/submodule_vpc_serverless_connector/README.md new file mode 100644 index 00000000..a768e858 --- /dev/null +++ b/examples/submodule_vpc_serverless_connector/README.md @@ -0,0 +1,19 @@ +# VPC Serverless Connector Beta + +This example deploys a single vpc serverless connector in the us-central1 region. + + +## Inputs + +| Name | Description | Type | Default | Required | +|------|-------------|------|---------|:--------:| +| project\_id | Project in which the vpc connector will be deployed. | `string` | n/a | yes | + +## Outputs + +| Name | Description | +|------|-------------| +| connector\_ids | ID of the vpc serverless connector that was deployed. | +| project\_id | The ID of the project being used | + + diff --git a/examples/submodule_vpc_serverless_connector/main.tf b/examples/submodule_vpc_serverless_connector/main.tf new file mode 100644 index 00000000..c9c2f7b6 --- /dev/null +++ b/examples/submodule_vpc_serverless_connector/main.tf @@ -0,0 +1,64 @@ +/** + * Copyright 2021 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 + * + * http://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. + */ + +provider "google" { + version = "~> 3.62" +} + +provider "google-beta" { + version = "~> 3.62" +} + +module "test-vpc-module" { + source = "terraform-google-modules/network/google" + version = "~> 3.2.0" + project_id = var.project_id # Replace this with your project ID in quotes + network_name = "my-serverless-network" + mtu = 1460 + + subnets = [ + { + subnet_name = "serverless-subnet" + subnet_ip = "10.10.10.0/28" + subnet_region = "us-central1" + } + ] +} + +module "serverless-connector" { + source = "../../modules/vpc-serverless-connector-beta" + project_id = var.project_id + vpc_connectors = [{ + name = "central-serverless" + region = "us-central1" + subnet_name = module.test-vpc-module.subnets["us-central1/serverless-subnet"].name + # host_project_id = var.host_project_id # Leverage host_project_id if using a shared VPC + machine_type = "e2-standard-4" + min_instances = 2 + max_instances = 7 + } + # Uncomment below to leverage ip_cidr_range + # , { + # name = "central-serverless2" + # region = "us-central1" + # network = module.test-vpc-module.network_name + # ip_cidr_range = "10.10.11.0/28" + # subnet_name = null + # machine_type = "e2-standard-4" + # min_instances = 2 + # max_instances = 7 } + ] +} diff --git a/examples/submodule_vpc_serverless_connector/outputs.tf b/examples/submodule_vpc_serverless_connector/outputs.tf new file mode 100644 index 00000000..c759423a --- /dev/null +++ b/examples/submodule_vpc_serverless_connector/outputs.tf @@ -0,0 +1,25 @@ +/** + * Copyright 2021 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 + * + * http://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. + */ + +output "connector_ids" { + value = module.serverless-connector.connector_ids + description = "ID of the vpc serverless connector that was deployed." +} + +output "project_id" { + value = var.project_id + description = "The ID of the project being used" +} diff --git a/examples/submodule_vpc_serverless_connector/variables.tf b/examples/submodule_vpc_serverless_connector/variables.tf new file mode 100644 index 00000000..403f797e --- /dev/null +++ b/examples/submodule_vpc_serverless_connector/variables.tf @@ -0,0 +1,20 @@ +/** + * Copyright 2021 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 + * + * http://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. + */ + +variable "project_id" { + type = string + description = "Project in which the vpc connector will be deployed." +} diff --git a/examples/submodule_vpc_serverless_connector/versions.tf b/examples/submodule_vpc_serverless_connector/versions.tf new file mode 100644 index 00000000..5e8a4ffa --- /dev/null +++ b/examples/submodule_vpc_serverless_connector/versions.tf @@ -0,0 +1,19 @@ +/** + * Copyright 2021 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 + * + * http://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. + */ + +terraform { + required_version = ">=0.13.0" +} diff --git a/modules/vpc-serverless-connector-beta/README.md b/modules/vpc-serverless-connector-beta/README.md new file mode 100644 index 00000000..08dc1ea8 --- /dev/null +++ b/modules/vpc-serverless-connector-beta/README.md @@ -0,0 +1,44 @@ +# Terraform VPC Serverless Connector Beta + +This submodule is part of the the `terraform-google-network` module. It creates the vpc serverless connector using the beta components available. + +It supports creating: + +- serverless connector +- serverless vpc access connector + +## Usage + +Basic usage of this submodule is as follows: + +```hcl +module "serverless-connector" { + source = "terraform-google-modules/network/google//modules/vpc-serverless-connector-beta" + project_id = + vpc_connectors = [{ + name = "central-serverless" + region = "us-central1" + subnet_name = "" + host_project_id = "" + machine_type = "e2-standard-4" + min_instances = 2 + max_instances = 3 + }] +} +``` + + +## Inputs + +| Name | Description | Type | Default | Required | +|------|-------------|------|---------|:--------:| +| project\_id | Project in which the vpc connector will be deployed. | `string` | n/a | yes | +| vpc\_connectors | List of VPC serverless connectors. | `list(map(string))` | `[]` | no | + +## Outputs + +| Name | Description | +|------|-------------| +| connector\_ids | VPC serverless connector ID. | + + diff --git a/modules/vpc-serverless-connector-beta/main.tf b/modules/vpc-serverless-connector-beta/main.tf new file mode 100644 index 00000000..9aff807d --- /dev/null +++ b/modules/vpc-serverless-connector-beta/main.tf @@ -0,0 +1,37 @@ +/** + * Copyright 2021 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 + * + * http://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. + */ + +# Pending new google-cloud-beta provider release Estimated Release 03/22 +# https://github.com/hashicorp/terraform-provider-google/issues/8475 +resource "google_vpc_access_connector" "connector_beta" { + for_each = { for connector in var.vpc_connectors : connector.name => connector } + provider = google-beta + name = each.value.name + project = var.project_id + region = each.value.region + ip_cidr_range = lookup(each.value, "ip_cidr_range", null) + network = lookup(each.value, "network", null) + dynamic "subnet" { + for_each = each.value.subnet_name == null ? [] : [each.value] + content { + name = each.value.subnet_name + project_id = lookup(each.value, "host_project_id", null) + } + } + machine_type = lookup(each.value, "machine_type", null) + min_instances = lookup(each.value, "min_instances", null) + max_instances = lookup(each.value, "max_instances", null) +} diff --git a/modules/vpc-serverless-connector-beta/outputs.tf b/modules/vpc-serverless-connector-beta/outputs.tf new file mode 100644 index 00000000..fc255f99 --- /dev/null +++ b/modules/vpc-serverless-connector-beta/outputs.tf @@ -0,0 +1,21 @@ +/** + * Copyright 2021 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 + * + * http://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. + */ + +output "connector_ids" { + value = toset([ + for k in google_vpc_access_connector.connector_beta : k.id]) + description = "VPC serverless connector ID." +} diff --git a/modules/vpc-serverless-connector-beta/variables.tf b/modules/vpc-serverless-connector-beta/variables.tf new file mode 100644 index 00000000..a4b3956a --- /dev/null +++ b/modules/vpc-serverless-connector-beta/variables.tf @@ -0,0 +1,26 @@ +/** + * Copyright 2021 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 + * + * http://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. + */ + +variable "project_id" { + type = string + description = "Project in which the vpc connector will be deployed." +} + +variable "vpc_connectors" { + type = list(map(string)) + default = [] + description = "List of VPC serverless connectors." +} diff --git a/modules/vpc-serverless-connector-beta/versions.tf b/modules/vpc-serverless-connector-beta/versions.tf new file mode 100644 index 00000000..386dd9b5 --- /dev/null +++ b/modules/vpc-serverless-connector-beta/versions.tf @@ -0,0 +1,29 @@ +/** + * Copyright 2021 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 + * + * http://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. + */ + +terraform { + required_version = ">= 0.13.0" + required_providers { + google-beta = { + source = "hashicorp/google-beta" + version = "~> 3.62" + } + } + + provider_meta "google-beta" { + module_name = "blueprints/terraform/terraform-google-network:vpc-serverless-connector-beta/v3.2.0" + } +} diff --git a/test/fixtures/submodule_vpc_serverless_connector_beta/main.tf b/test/fixtures/submodule_vpc_serverless_connector_beta/main.tf new file mode 100644 index 00000000..7ef470a5 --- /dev/null +++ b/test/fixtures/submodule_vpc_serverless_connector_beta/main.tf @@ -0,0 +1,20 @@ +/** + * Copyright 2021 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 + * + * http://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. + */ + +module "example" { + source = "../../../examples/submodule_vpc_serverless_connector" + project_id = var.project_id +} diff --git a/test/fixtures/submodule_vpc_serverless_connector_beta/outputs.tf b/test/fixtures/submodule_vpc_serverless_connector_beta/outputs.tf new file mode 100644 index 00000000..cf83f782 --- /dev/null +++ b/test/fixtures/submodule_vpc_serverless_connector_beta/outputs.tf @@ -0,0 +1,25 @@ +/** + * Copyright 2021 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 + * + * http://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. + */ + +output "output_connector_ids" { + value = module.example.connector_ids + description = "ID of the vpc serverless connector that was deployed." +} + +output "project_id" { + value = var.project_id + description = "The ID of the project being used" +} diff --git a/test/fixtures/submodule_vpc_serverless_connector_beta/variables.tf b/test/fixtures/submodule_vpc_serverless_connector_beta/variables.tf new file mode 100644 index 00000000..403f797e --- /dev/null +++ b/test/fixtures/submodule_vpc_serverless_connector_beta/variables.tf @@ -0,0 +1,20 @@ +/** + * Copyright 2021 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 + * + * http://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. + */ + +variable "project_id" { + type = string + description = "Project in which the vpc connector will be deployed." +} diff --git a/test/integration/submodule_vpc_serverless_connector_beta/controls/gcloud.rb b/test/integration/submodule_vpc_serverless_connector_beta/controls/gcloud.rb new file mode 100644 index 00000000..5c669e79 --- /dev/null +++ b/test/integration/submodule_vpc_serverless_connector_beta/controls/gcloud.rb @@ -0,0 +1,45 @@ +# Copyright 2019 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. + +project_id = attribute('project_id') +host_project_id = attribute('host_project_id') +name = attribute('name') +subnet_name = attribute('subnet_name') +region = attribute('region') +machine_type = attribute('machine_type') +min_instances = attribute('min_instances') +max_instances = attribute('max_instances') + +control "gcloud" do + title "gcloud configuration" + + describe command("gcloud beta compute networks my-serverless-network connectors describe #{name} --region #{region} --project #{project_id} --format json") do + its(:exit_status) { should eq 0 } + its(:stderr) { should eq '' } + + let(:data) do + if subject.exit_status == 0 + JSON.parse(subject.stdout) + else + {} + end + end + + it "should exist" do + expect(data).to include( + "machineType" => "e2-standard-4" + ) + end + end +end diff --git a/test/integration/submodule_vpc_serverless_connector_beta/controls/inspec_attributes.rb b/test/integration/submodule_vpc_serverless_connector_beta/controls/inspec_attributes.rb new file mode 100644 index 00000000..c3ead89a --- /dev/null +++ b/test/integration/submodule_vpc_serverless_connector_beta/controls/inspec_attributes.rb @@ -0,0 +1,27 @@ +# Copyright 2021 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. + +project_id = attribute('project_id') +name = attribute('name') + + +control "inspec_attributes" do + title "Terraform Outputs" + desc "Terraform Outputs" + + + describe attribute("output_connector_ids") do + it { should eq ["projects/#{project_id}/locations/us-central1/connectors/#{name}"] } + end +end diff --git a/test/integration/submodule_vpc_serverless_connector_beta/inspec.yml b/test/integration/submodule_vpc_serverless_connector_beta/inspec.yml new file mode 100644 index 00000000..a6a49454 --- /dev/null +++ b/test/integration/submodule_vpc_serverless_connector_beta/inspec.yml @@ -0,0 +1,41 @@ +name: submodule_vpc_serverless_connector_beta +depends: + - name: inspec-gcp + git: https://github.com/inspec/inspec-gcp.git + tag: v1.8.8 +supports: + - platform: gcp +attributes: + - name: project_id + required: true + type: string + - name: host_project_id + required: true + type: string + - name: name + required: true + type: string + value: central-serverless + - name: region + required: true + type: string + value: us-central1 + - name: subnet_name + required: true + type: string + value: serverless-reserve-central + - name: machine_type + required: true + type: string + value: e2-standard-4 + - name: min_instances + required: true + type: numeric + value: 2 + - name: max_instances + required: true + type: numeric + value: 7 + - name: output_connector_ids + required: true + type: array diff --git a/test/setup/iam.tf b/test/setup/iam.tf index fa3c7904..d31f9ece 100644 --- a/test/setup/iam.tf +++ b/test/setup/iam.tf @@ -19,6 +19,7 @@ locals { "roles/compute.networkAdmin", "roles/compute.securityAdmin", "roles/iam.serviceAccountUser", + "roles/vpcaccess.admin" ] } diff --git a/test/setup/main.tf b/test/setup/main.tf index 284cd57a..3b7153be 100644 --- a/test/setup/main.tf +++ b/test/setup/main.tf @@ -28,6 +28,7 @@ module "project" { activate_apis = [ "cloudresourcemanager.googleapis.com", "compute.googleapis.com", - "serviceusage.googleapis.com" + "serviceusage.googleapis.com", + "vpcaccess.googleapis.com" ] }