Skip to content

Commit f713cb3

Browse files
authored
Update Test Generation: resource doc + OiCS (#15629)
1 parent 695ac92 commit f713cb3

File tree

6 files changed

+180
-2
lines changed

6 files changed

+180
-2
lines changed

mmv1/provider/terraform_oics.go

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ func (toics TerraformOiCS) GenerateObject(object api.Resource, outputFolder, res
7575
}
7676
}
7777

78-
func (toics TerraformOiCS) GenerateResource(object api.Resource, templateData TemplateData, outputFolder string, generateCode, generateDocs bool) {
78+
func (toics TerraformOiCS) GenerateResourceLegacy(object api.Resource, templateData TemplateData, outputFolder string, generateCode, generateDocs bool) {
7979
if !generateDocs {
8080
return
8181
}
@@ -119,6 +119,60 @@ func (toics TerraformOiCS) GenerateResource(object api.Resource, templateData Te
119119
}
120120
}
121121

122+
func (toics TerraformOiCS) GenerateResource(object api.Resource, templateData TemplateData, outputFolder string, generateCode, generateDocs bool) {
123+
if object.Samples != nil && object.Examples != nil {
124+
log.Fatalf("Both Samples and Examples block exist in %v", object.Name)
125+
}
126+
if object.Examples != nil {
127+
toics.GenerateResourceLegacy(object, templateData, outputFolder, generateCode, generateDocs)
128+
return
129+
}
130+
131+
if !generateDocs {
132+
return
133+
}
134+
135+
for _, sample := range object.TestSamples() {
136+
for _, step := range sample.NewConfigFuncs {
137+
if len(step.TestEnvVars) > 0 {
138+
continue
139+
}
140+
141+
step.SetOiCSHCLText()
142+
143+
targetFolder := path.Join(outputFolder, step.Name)
144+
145+
if err := os.MkdirAll(targetFolder, os.ModePerm); err != nil {
146+
log.Println(fmt.Errorf("error creating oics example directory %v: %v", targetFolder, err))
147+
}
148+
149+
oicsExampleTemplatePath := "templates/terraform/samples/base_configs/oics_example_file.tf.tmpl"
150+
oicsExampleTemplates := []string{
151+
oicsExampleTemplatePath,
152+
}
153+
templateData.GenerateFile(path.Join(targetFolder, "main.tf"), oicsExampleTemplatePath, step, false, oicsExampleTemplates...)
154+
155+
tutorialTemplatePath := "templates/terraform/samples/base_configs/tutorial.md.tmpl"
156+
tutorialTemplates := []string{
157+
tutorialTemplatePath,
158+
}
159+
templateData.GenerateFile(path.Join(targetFolder, "tutorial.md"), tutorialTemplatePath, step, false, tutorialTemplates...)
160+
161+
backingTemplatePath := "templates/terraform/samples/base_configs/example_backing_file.tf.tmpl"
162+
backingTemplates := []string{
163+
backingTemplatePath,
164+
}
165+
templateData.GenerateFile(path.Join(targetFolder, "backing_file.tf"), backingTemplatePath, step, false, backingTemplates...)
166+
167+
motdTemplatePath := "templates/terraform/samples/static/motd.tmpl"
168+
motdTemplates := []string{
169+
motdTemplatePath,
170+
}
171+
templateData.GenerateFile(path.Join(targetFolder, "motd"), motdTemplatePath, step, false, motdTemplates...)
172+
}
173+
}
174+
}
175+
122176
func (toics TerraformOiCS) CompileCommonFiles(outputFolder string, products []*api.Product, overridePath string) {
123177

124178
}

mmv1/templates/terraform/resource.html.markdown.tmpl

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ To get more information about {{$.Name}}, see:
4141
* [{{$title}}]({{$link}})
4242
{{- end }}
4343
{{- end }}
44-
{{- if gt (len $.Examples) 0}}
44+
{{- if or (gt (len $.Examples) 0) (gt (len $.Samples) 0) }}
4545
{{ "" }}
4646
{{- end }}
4747
{{- else }}
@@ -82,6 +82,28 @@ values will be stored in the raw state as plain text: {{ $.SensitivePropsToStrin
8282
{{- end }}
8383
{{- end }}
8484

85+
{{- if $.Samples }}
86+
{{- range $sample := $.Samples }}
87+
{{- range $step := $sample.Steps }}
88+
{{- if not $step.ExcludeDocs }}
89+
{{- if not (or $sample.ExcludeTest $step.TestEnvVars) }}
90+
<div class = "oics-button" style="float: right; margin: 0 0 -15px">
91+
<a href="{{ $step.OiCSLink }}" target="_blank">
92+
<img alt="Open in Cloud Shell" src="//gstatic.com/cloudssh/images/open-btn.svg" style="max-height: 44px; margin: 32px auto; max-width: 100%;">
93+
</a>
94+
</div>
95+
{{- end }}
96+
## Example Usage - {{ title (camelize $step.Name "upper" )}}
97+
98+
99+
```hcl
100+
{{ $step.DocumentationHCLText -}}
101+
```
102+
{{- end }}
103+
{{- end }}
104+
{{- end }}
105+
{{- end }}
106+
85107
## Argument Reference
86108

87109
The following arguments are supported:
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# This file has some scaffolding to make sure that names are unique and that
2+
# a region and zone are selected when you try to create your Terraform resources.
3+
4+
locals {
5+
name_suffix = "${random_pet.suffix.id}"
6+
}
7+
8+
resource "random_pet" "suffix" {
9+
length = 2
10+
}
11+
12+
provider "google" {
13+
region = "us-central1"
14+
zone = "us-central1-c"
15+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{{ $.OicsHCLText -}}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
# {{ title (camelize $.Name "upper") }} - Terraform
2+
3+
## Setup
4+
5+
<walkthrough-author name="[email protected]" analyticsId="UA-125550242-1" tutorialName="{{ underscore $.Name }}" repositoryUrl="https://github.com/terraform-google-modules/docs-examples"></walkthrough-author>
6+
7+
Welcome to Terraform in Google Cloud Shell! We need you to let us know what project you'd like to use with Terraform.
8+
9+
<walkthrough-project-billing-setup></walkthrough-project-billing-setup>
10+
11+
Terraform provisions real GCP resources, so anything you create in this session will be billed against this project.
12+
13+
## Terraforming!
14+
15+
Let's use {{"{{"}}project-id{{"}}"}} with Terraform! Click the Cloud Shell icon below to copy the command
16+
to your shell, and then run it from the shell by pressing Enter/Return. Terraform will pick up
17+
the project name from the environment variable.
18+
19+
```bash
20+
export GOOGLE_CLOUD_PROJECT={{"{{"}}project-id{{"}}"}}
21+
```
22+
23+
After that, let's get Terraform started. Run the following to pull in the providers.
24+
25+
```bash
26+
terraform init
27+
```
28+
29+
With the providers downloaded and a project set, you're ready to use Terraform. Go ahead!
30+
31+
```bash
32+
terraform apply
33+
```
34+
35+
Terraform will show you what it plans to do, and prompt you to accept. Type "yes" to accept the plan.
36+
37+
```bash
38+
yes
39+
```
40+
41+
42+
## Post-Apply
43+
44+
### Editing your config
45+
46+
Now you've provisioned your resources in GCP! If you run a "plan", you should see no changes needed.
47+
48+
```bash
49+
terraform plan
50+
```
51+
52+
So let's make a change! Try editing a number, or appending a value to the name in the editor. Then,
53+
run a 'plan' again.
54+
55+
```bash
56+
terraform plan
57+
```
58+
59+
Afterwards you can run an apply, which implicitly does a plan and shows you the intended changes
60+
at the 'yes' prompt.
61+
62+
```bash
63+
terraform apply
64+
```
65+
66+
```bash
67+
yes
68+
```
69+
70+
## Cleanup
71+
72+
Run the following to remove the resources Terraform provisioned:
73+
74+
```bash
75+
terraform destroy
76+
```
77+
```bash
78+
yes
79+
```
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
===
2+
3+
These examples use real resources that will be billed to the
4+
Google Cloud Platform project you use - so make sure that you
5+
run "terraform destroy" before quitting!
6+
7+
===

0 commit comments

Comments
 (0)