Skip to content

Commit c4157b0

Browse files
committed
example for source_dependent_files
1 parent 5939de7 commit c4157b0

File tree

7 files changed

+254
-0
lines changed

7 files changed

+254
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ credentials.json
5151
examples/automatic-labelling-folder/function_source.zip
5252
examples/automatic-labelling-from-localhost/function_source.zip
5353
examples/automatic-labelling-from-repository/function_source_copy
54+
examples/dynamic-files/function_source.zip
55+
examples/dynamic-files/function_source/terraform_created_file.txt
5456

5557
node_modules
5658
yarn.lock

examples/dynamic-files/README.md

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
# Automatic Labelling for folder projects
2+
3+
This example demonstrates how to use the
4+
[root module][root-module] that will contain source
5+
code files generated from terraform itself.
6+
7+
## Usage
8+
9+
To provision this example, populate `terraform.tfvars` with the [required variables](#inputs) and run the following commands within
10+
this directory:
11+
12+
- `terraform init` to initialize the directory
13+
- `terraform plan` to generate the execution plan
14+
- `terraform apply` to apply the execution plan
15+
- `terraform destroy` to destroy the infrastructure
16+
17+
<!-- BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
18+
## Inputs
19+
20+
| Name | Description | Type | Default | Required |
21+
|------|-------------|:----:|:-----:|:-----:|
22+
| folder\_id | The ID of the folder to look for changes. | string | n/a | yes |
23+
| project\_id | The ID of the project to which resources will be applied. | string | n/a | yes |
24+
| region | The region in which resources will be applied. | string | n/a | yes |
25+
26+
## Outputs
27+
28+
| Name | Description |
29+
|------|-------------|
30+
| function\_name | The name of the function created |
31+
| project\_id | The ID of the project to which resources are applied. |
32+
| random\_file\_string | The content of the terraform created file in the source directory. |
33+
| region | The region in which resources are applied. |
34+
35+
<!-- END OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
36+
37+
## Requirements
38+
39+
The following sections describe the requirements which must be met in
40+
order to invoke this module. The requirements of the
41+
[root module][root-module-requirements] and the
42+
[event-folder-log-entry submodule][event-folder-log-entry-submodule-requirements]
43+
must also be met.
44+
45+
### Software Dependencies
46+
47+
The following software dependencies must be installed on the system
48+
from which this module will be invoked:
49+
50+
- [Terraform][terraform-site] v0.12
51+
52+
### IAM Roles
53+
54+
The Service Account which will be used to invoke this module must have
55+
the following IAM roles:
56+
57+
- Logs Configuration Writer: `roles/logging.configWriter`
58+
- Pub/Sub Admin: `roles/pubsub.admin`
59+
- Service Account User: `roles/iam.serviceAccountUser`
60+
61+
- Default AppSpot user: `roles/owner`
62+
- Your user: `roles/resourcemanager.projectCreator`
63+
64+
### APIs
65+
66+
The project against which this module will be invoked must have the
67+
following APIs enabled:
68+
69+
- Cloud Pub/Sub API: `pubsub.googleapis.com`
70+
- Stackdriver Logging API: `logging.googleapis.com`
71+
72+
[event-folder-log-entry-submodule-requirements]: ../../modules/event-folder-log-entry/README.md#requirements
73+
[event-folder-log-entry-submodule]: ../../modules/event-folder-log-entry
74+
[root-module-requirements]: ../../README.md#requirements
75+
[root-module]: ../..
76+
[terraform-site]: https://terraform.io/
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/**
2+
* Copyright 2019 Google Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
const fs = require("fs");
18+
const path = require("path");
19+
const filePath = path.join(__dirname, "terraform_created_file.txt");
20+
21+
/**
22+
* Cloud function entrypoint
23+
*
24+
* @param {!Object} event Event payload and metadata.
25+
* @param {!Function} callback Callback function to signal completion.
26+
*/
27+
exports.fileContent = (data, context, callback) => {
28+
console.log("Received event");
29+
30+
fs.readFile(filePath, { encoding: "utf-8" }, function(err, data) {
31+
if (!err) {
32+
callback(null, data);
33+
} else {
34+
callback(err);
35+
}
36+
});
37+
};
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"name": "dynamic-files",
3+
"version": "0.0.1"
4+
}

examples/dynamic-files/main.tf

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/**
2+
* Copyright 2019 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
terraform {
18+
required_version = "~> 0.12.0"
19+
}
20+
21+
resource "random_pet" "main" {
22+
length = 2
23+
separator = "-"
24+
}
25+
26+
resource "google_storage_bucket" "trigger_bucket" {
27+
name = "${random_pet.main.id}-trigger"
28+
force_destroy = true
29+
location = var.region
30+
project = var.project_id
31+
storage_class = "REGIONAL"
32+
}
33+
34+
resource "random_string" "random" {
35+
length = 16
36+
special = false
37+
}
38+
39+
resource "local_file" "file" {
40+
content = random_string.random.result
41+
filename = "${path.module}/function_source/terraform_created_file.txt"
42+
}
43+
44+
module "localhost_function" {
45+
source = "../.."
46+
47+
description = "Returns back the random file content"
48+
entry_point = "fileContent"
49+
50+
event_trigger = {
51+
event_type = "google.storage.object.finalize"
52+
resource = google_storage_bucket.trigger_bucket.name
53+
}
54+
55+
name = random_pet.main.id
56+
project_id = var.project_id
57+
region = var.region
58+
source_directory = "${path.module}/function_source"
59+
runtime = "nodejs8"
60+
61+
source_dependent_files = [local_file.file]
62+
}
63+
64+
resource "null_resource" "wait_for_function" {
65+
provisioner "local-exec" {
66+
command = "sleep 60"
67+
}
68+
69+
depends_on = [module.localhost_function]
70+
}

examples/dynamic-files/outputs.tf

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
* Copyright 2019 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
output "project_id" {
18+
value = var.project_id
19+
description = "The ID of the project to which resources are applied."
20+
}
21+
22+
output "region" {
23+
value = var.region
24+
description = "The region in which resources are applied."
25+
}
26+
27+
output "function_name" {
28+
value = module.localhost_function.name
29+
description = "The name of the function created"
30+
}
31+
32+
output "random_file_string" {
33+
value = random_string.random.result
34+
description = "The content of the terraform created file in the source directory."
35+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* Copyright 2019 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
variable "project_id" {
18+
type = string
19+
description = "The ID of the project to which resources will be applied."
20+
}
21+
22+
variable "folder_id" {
23+
type = string
24+
description = "The ID of the folder to look for changes."
25+
}
26+
27+
variable "region" {
28+
type = string
29+
description = "The region in which resources will be applied."
30+
}

0 commit comments

Comments
 (0)