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

Improve docs for Terraform CLI example #78

Merged
merged 5 commits into from
Jan 11, 2024
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
44 changes: 44 additions & 0 deletions examples/terraform/complex_cli_only/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
> [!IMPORTANT]
> If your module can be used directly as a root module, consider following the [simple_cli_only](../simple_cli_only) example instead.

This example meets the
[Marketplace requirements](https://cloud.google.com/marketplace/docs/partners/vm/configure-terraform-deployment#requirements_for_custom_terraform_modules)
for a Terraform **CLI Only** module

If the `examples/marketplace_test` folder exists, marketplace will validate your
module by executing:

```
terraform -chdir=examples/marketplace_test init
terraform -chdir=examples/marketplace_test plan -var project_id=<test-project>
```

The module in `examples/marketplace_test` must reference your module in the root
directory as shown below:

```
# The test module references the module in the root directory
module "test" {
source = "../.."
}
```

This test module can be used to create resources that the module in your root
directory assumes already exists. For instance, this example creates a second
network interface that is passed as an argument to the module in the root
directory.

For a Marketplace Partner to reuse this module, they must:

1. Declare "image" as a variable in Producer Portal.
1. Replace the default value of "image" with the image declared in Producer
Portal
1. Zip the module and upload to GCS

```
zip terraform.zip * -r
gsutil cp terraform.zip gs://<YOUR-BUCKET>/<FOLDER>/terraform.zip
```

Additionally, Partners should include a README.md containing instructions on how
to deploy their product.
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,25 @@
# limitations under the License.

provider "google" {
project = var.project
project = var.project_id
}

# The test module references the module in the root directory
module "test" {
source = "../.."

other_nic = google_compute_network.new_nic.name
}

# Create a network interface in the test module. The Marketplace validation project
# may not have a resource that your module in the root directory expects to exist.
resource "google_compute_network" "new_nic" {
name = "new-test-nic"
}

# The module must declare a project variable that Marketplace can
# set for validation
variable "project" {
variable "project_id" {
type = string
}

35 changes: 35 additions & 0 deletions examples/terraform/complex_cli_only/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# 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
#
# 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.

resource "google_compute_instance" "default" {
name = "example"
machine_type = "e2-medium"
zone = "us-central1-a"

boot_disk {
initialize_params {
# The boot disk must be set to the variable declared in Producer Portal
image = var.image
}
}

network_interface {
network = "default"
}

network_interface {
network = var.other_nic
}
}

26 changes: 26 additions & 0 deletions examples/terraform/complex_cli_only/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# 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
#
# 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.

# The variable "image" is declared in Producer Portal
variable "image" {
# Set the default value to your image. Marketplace will overwrite this value
# to a Marketplace owned image on publishing the product
default = "projects/<partner-project>/global/images/<image-name>"
}

# Specifies a network interface that this module assumes already exists
variable "other_nic" {
type = string
}

8 changes: 4 additions & 4 deletions examples/terraform/simple_cli_only/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ for a Terraform **CLI Only** module
To validate your module, Marketplace will execute:

```
terraform -chdir=examples/marketplace_test init
terraform -chdir=examples/marketplace_test plan -var project=<test-project>
terraform init
terraform plan -var-file=marketplace_test.tfvars -var project_id=<test-project>
```

The module in examples/marketplace_test must reference your root module (i.e.
the module in the root directory)
The `marketplace_test.tfvars` can be used to set variables to default values for
testing only purposes.

For a Marketplace Partner to reuse this module, they must:

Expand Down
8 changes: 6 additions & 2 deletions examples/terraform/simple_cli_only/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,19 @@
# See the License for the specific language governing permissions and
# limitations under the License.

provider "google" {
project = var.project_id
}

resource "google_compute_instance" "default" {
name = "example"
machine_type = "e2-medium"
zone = "us-central1-a"

boot_disk {
initialize_params {
# The boot disk must be set to the variable declared in Producer Portal
image = var.image
# The boot disk must be set to the variable declared in Producer Portal
image = var.image
}
}

Expand Down
2 changes: 2 additions & 0 deletions examples/terraform/simple_cli_only/marketplace_test.tfvars
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# zone variable must be set because no default value was specified in "variables.tf"
zone = "us-central1-a"
10 changes: 10 additions & 0 deletions examples/terraform/simple_cli_only/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,17 @@

# The variable "image" is declared in Producer Portal
variable "image" {
type = string
# Set the default value to your image. Marketplace will overwrite this value
# to a Marketplace owned image on publishing the product
default = "projects/<partner-project>/global/images/<image-name>"
}

# Project Id is required for Marketplace validation
variable "project_id" {
type = string
}

variable "zone" {
type = string
}
Loading