Skip to content

Commit c4c7d86

Browse files
rileykarsonmodular-magician
authored andcommitted
Add interpolation of Terraform examples and migrate Address to it. (#423)
Merged PR #423.
1 parent fec5e1c commit c4c7d86

File tree

8 files changed

+85
-27
lines changed

8 files changed

+85
-27
lines changed

build/terraform

products/compute/terraform.yaml

+11-26
Original file line numberDiff line numberDiff line change
@@ -15,32 +15,17 @@
1515
overrides: !ruby/object:Provider::ResourceOverrides
1616
Address: !ruby/object:Provider::Terraform::ResourceOverride
1717
id_format: "{{project}}/{{region}}/{{name}}"
18-
examples: |
19-
```hcl
20-
resource "google_compute_address" "default" {
21-
name = "my-address"
22-
}
23-
```
24-
```hcl
25-
resource "google_compute_network" "default" {
26-
name = "my-network"
27-
}
28-
29-
resource "google_compute_subnetwork" "default" {
30-
name = "my-subnet"
31-
ip_cidr_range = "10.0.0.0/16"
32-
region = "us-central1"
33-
network = "${google_compute_network.default.self_link}"
34-
}
35-
36-
resource "google_compute_address" "internal_with_subnet_and_address" {
37-
name = "my-internal-address"
38-
subnetwork = "${google_compute_subnetwork.default.self_link}"
39-
address_type = "INTERNAL"
40-
address = "10.0.42.42"
41-
region = "us-central1"
42-
}
43-
```
18+
example:
19+
- !ruby/object:Provider::Terraform::Examples
20+
name: "address_basic"
21+
vars_documentation:
22+
address_name: "my-address"
23+
- !ruby/object:Provider::Terraform::Examples
24+
name: "address_with_subnetwork"
25+
vars_documentation:
26+
address_name: "my-internal-address"
27+
network_name: "my-network"
28+
subnetwork_name: "my-subnet"
4429
properties:
4530
address: !ruby/object:Provider::Terraform::PropertyOverride
4631
default_from_api: true

provider/terraform/custom_code.rb

+34
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
# limitations under the License.
1313

1414
require 'api/object'
15+
require 'compile/core'
1516
require 'provider/abstract_core'
1617
require 'provider/property_override'
1718

@@ -42,6 +43,39 @@ def validate
4243
end
4344
end
4445

46+
# Generates configs to be shown as examples in docs from a template
47+
class Examples < Api::Object
48+
include Compile::Core
49+
50+
# The name of the example in lower snake_case.
51+
# Generally takes the form of the resource name followed by some detail
52+
# about the specific test. For example, "address_with_subnetwork".
53+
# The template for the example is expected at the path
54+
# "templates/terraform/examples/{{name}}.tf.erb"
55+
attr_reader :name
56+
57+
# vars_documentation is a Hash from template variable names to output
58+
# variable names
59+
attr_reader :vars_documentation
60+
61+
def config_documentation
62+
body = lines(compile_file(
63+
vars_documentation,
64+
"templates/terraform/examples/#{name}.tf.erb"
65+
))
66+
lines(compile_file(
67+
{ content: body },
68+
'templates/terraform/examples/base_configs/documentation.tf.erb'
69+
))
70+
end
71+
72+
def validate
73+
super
74+
check_property :name, String
75+
check_property :vars_documentation, Hash
76+
end
77+
end
78+
4579
# Inserts custom code into terraform resources.
4680
class CustomCode < Api::Object
4781
# Collection of fields allowed in the CustomCode section for

provider/terraform/resource_override.rb

+8
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,13 @@ module OverrideProperties
3131
# resource.
3232
attr_reader :mutex
3333

34+
# Deprecated - examples in documentation
35+
# TODO(rileykarson): Remove examples and replace them with new examples
3436
attr_reader :examples
37+
38+
# New examples in documentation - will take the "examples" name when
39+
# old-style examples are gone.
40+
attr_reader :example
3541
end
3642

3743
# A class to control overridden properties on terraform.yaml in lieu of
@@ -50,6 +56,8 @@ def validate
5056
check_property :id_format, String
5157

5258
check_optional_property :examples, String
59+
check_optional_property :example, Array
60+
5361
check_optional_property :custom_code, Provider::Terraform::CustomCode
5462
check_optional_property :docs, Provider::Terraform::Docs
5563
check_property :import_format, Array
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
resource "google_compute_address" "default" {
2+
name = "<%= ctx["address_name"] %>"
3+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
resource "google_compute_network" "default" {
2+
name = "<%= ctx["network_name"] %>"
3+
}
4+
5+
resource "google_compute_subnetwork" "default" {
6+
name = "<%= ctx["subnetwork_name"] %>"
7+
ip_cidr_range = "10.0.0.0/16"
8+
region = "us-central1"
9+
network = "${google_compute_network.default.self_link}"
10+
}
11+
12+
resource "google_compute_address" "internal_with_subnet_and_address" {
13+
name = "<%= ctx["address_name"] %>"
14+
subnetwork = "${google_compute_subnetwork.default.self_link}"
15+
address_type = "INTERNAL"
16+
address = "10.0.42.42"
17+
region = "us-central1"
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```hcl
2+
<%= ctx[:content] -%>
3+
```

templates/terraform/resource.html.markdown.erb

+7
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,13 @@ To get more information about <%= object.name -%>, see:
7575
<%= "\n" + object.examples -%>
7676
7777
<% end -%>
78+
<% unless object.example.nil? -%>
79+
## Example Usage
80+
81+
<% object.example.each do |example| -%>
82+
<%= example.config_documentation -%>
83+
<%- end %>
84+
<%- end -%>
7885
## Argument Reference
7986
8087
The following arguments are supported:

0 commit comments

Comments
 (0)