-
Notifications
You must be signed in to change notification settings - Fork 48
/
openapi_terraform_template_html.go
429 lines (410 loc) · 20.3 KB
/
openapi_terraform_template_html.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
package openapiterraformdocsgenerator
import "fmt"
// TableOfContentsTmpl contains the template used to render the table of contents as HTML formatted for Zendesk
var TableOfContentsTmpl = `<p dir="ltr">
This guide lists the configuration for '{{.ProviderName}}' Terraform provider
resources that can be managed using
<a href="https://www.hashicorp.com/blog/announcing-terraform-0-12/" target="_self">Terraform v0.12</a>.
</p>
{{- if .ProviderNotes}}
{{range .ProviderNotes -}}
<p><span class="wysiwyg-color-red">*Note: {{.}}</span></p>
{{end}}
{{- end -}}
<ul>
<li>
<a href="#provider_installation" target="_self">Provider Installation</a>
</li>
{{if or .ProviderConfiguration.Regions .ProviderConfiguration.ConfigProperties }}
<li>
<a href="#provider_configuration" target="_self">Provider Configuration</a>
<ul>
<li>
<a href="#provider_configuration_example_usage" target="_self">Example Usage</a>
</li>
<li>
<a href="#provider_configuration_arguments_reference" target="_self">Arguments Reference</a>
</li>
</ul>
</li>
{{end}}
<li>
<a href="#provider_resources" target="_self">Provider Resources</a>
<ul>
{{range .ProviderResources.Resources -}}
<li><a href="#{{.Name}}" target="_self">{{$.ProviderName}}_{{.Name}}</a></li>
{{- end}}
</ul>
</li>
<li>
<a href="#provider_datasources" target="_self">Data Sources (using resource id)</a>
<ul>
{{range .DataSources.DataSourceInstances -}}
<li><a href="#{{.Name}}" target="_self">{{$.ProviderName}}_{{.Name}}</a></li>
{{- end}}
</ul>
</li>
<li>
<a href="#provider_datasources_filters" target="_self">Data Sources (using filters)</a>
<ul>
{{range .DataSources.DataSources -}}
<li><a href="#{{.Name}}_datasource" target="_self">{{$.ProviderName}}_{{.Name}}</a></li>
{{- end}}
</ul>
</li>
{{ if .ShowSpecialTermsDefinitions }}
<li>
<a href="#special_terms_definitions" target="_self">Special Terms Definitions</a>
<ul>
{{ if .ProviderResources.ContainsResourcesWithSecretProperties }}
<li>
<a href="#special_terms_definitions_sensitive_property" target="_self">Sensitive Property</a>
</li>
{{end}}
</ul>
</li>
{{end}}
</ul>`
// ProviderInstallationTmpl contains the template used to render the TerraformProviderDocumentation.ProviderInstallation struct as HTML formatted for Zendesk
var ProviderInstallationTmpl = `<h2 id="provider_installation">Provider Installation</h2>
<p>
In order to provision '{{.ProviderName}}' Terraform resources, you need to first install the '{{.ProviderName}}'
Terraform plugin by running the following command (you must be running Terraform >= 0.12):
</p>
<pre>{{.Example}}</pre>
<p>
<span>{{.Other}}</span>
</p>
<pre dir="ltr">
{{- if .OtherCommand -}}
{{- .OtherCommand -}}
{{- end}}
➜ ~ terraform init && terraform plan
</pre>`
// ProviderConfigurationTmpl contains the template used to render the TerraformProviderDocumentation.ProviderConfiguration struct as HTML formatted for Zendesk
var ProviderConfigurationTmpl = `{{if or .Regions .ConfigProperties}}
<h2 id="provider_configuration">Provider Configuration</h2>
<h4 id="provider_configuration_example_usage" dir="ltr">Example Usage</h4>
<pre>
<span>provider </span><span>"{{.ProviderName}}" </span>{
{{- range .ConfigProperties}}
<span> {{.Name}} </span>= <span>"..."</span>
{{- end}}
<span>}</span>
</pre>
{{- end}}
{{if .Regions }}
<p>Using the default region ({{index .Regions 0}}):</p>
<pre>
<span>provider </span><span>"{{.ProviderName}}" </span>{
<span> # Resources using this default provider will be created in the '{{index .Regions 0}}' region<br> ...<br></span>}
</pre>
{{ if gt (len .Regions) 1 -}}
<p>Using a specific region ({{index .Regions 1}}):</p>
<pre>
<span>provider </span><span>"{{.ProviderName}}" </span>{
<span> alias </span>= <span>"{{index .Regions 1}}"</span>
<span> region </span>= <span>"{{index .Regions 1}}"<br> ...<br></span>}
<br>resource<span>"{{.ProviderName}}_resource" "my_resource" {</span>
<span> provider = "{{.ProviderName}}.{{index .Regions 1}}"<br> ...<br>}</span>
</pre>
{{- end }}
<h4 id="provider_configuration_arguments_reference" dir="ltr">Arguments Reference</h4>
<p dir="ltr">The following arguments are supported:</p>
<ul dir="ltr">
{{- range .ConfigProperties -}}
{{- $required := "Optional" -}}
{{- if .Required -}}
{{- $required = "Required" -}}
{{end}}
<li><span>{{.Name}} [{{.Type}}] - ({{$required}}) {{.Description}}.</span></li></li>
{{- end -}}
{{if .Regions }}
<li>
region [string] - (Optional) The region location to be used ({{.Regions}}). If region isn't specified, the default is "{{index .Regions 0}}".
</li>
{{end}}
</ul>
{{end}}`
// ProviderResourcesTmpl contains the template used to render the TerraformProviderDocumentation.ProviderResources struct as HTML formatted for Zendesk
var ProviderResourcesTmpl = fmt.Sprintf(`{{define "resource_example"}}
{{- if .Required}}
{{if eq .Type "string" -}}
<span>{{.Name}} </span>= <span>"{{.Name}}"</span>
{{- else if eq .Type "integer" -}}
<span>{{.Name}} </span>= <span>1234</span>
{{- else if eq .Type "boolean" -}}
<span>{{.Name}} </span>= <span>true</span>
{{- else if eq .Type "number" -}}
<span>{{.Name}} </span>= <span>12.95</span>
{{- else if and (eq .Type "list") (eq .ArrayItemsType "string") -}}
<span>{{.Name}} </span>= <span>["{{.Name}}1", "{{.Name}}2"]</span>
{{- else if and (eq .Type "list") (eq .ArrayItemsType "integer") -}}
<span>{{.Name}} </span>= <span>[1234, 4567]</span>
{{- else if and (eq .Type "list") (eq .ArrayItemsType "boolean") -}}
<span>{{.Name}} </span>= <span>[true, false]</span>
{{- else if and (eq .Type "list") (eq .ArrayItemsType "number") -}}
<span>{{.Name}} </span>= <span>[12.36, 99.45]</span>
{{- else -}}
{{- if or (eq .Type "object") (and (eq .Type "list") (eq .ArrayItemsType "object")) -}}
<span>{{.Name}} </span><span>{</span>
{{- range .Schema}}
{{template "resource_example" .}}
{{- end}}
<span>}</span>
{{- end -}}
{{- end -}}
{{- end -}}
{{end}}
%s
%s
<h2 id="provider_resources">Provider Resources</h2>
{{if not .Resources}}
<p>No resources are supported at the moment.</p>
{{- end -}}
{{range .Resources -}}
{{ $resource := . }}
<h3 id="{{.Name}}" dir="ltr">{{$.ProviderName}}_{{.Name}}</h3>
{{if ne .Description "" -}}
<p>{{.Description}}</p>
{{- end}}
{{- if .KnownIssues}}
<p>If you experience any issues using this resource, please check the <a href="#resource_{{.Name}}_known_issues" target="_self">Known Issues</a> section to see if there is a fix/workaround.</p>
{{end -}}
<h4 id="resource_{{.Name}}_example_usage" dir="ltr">Example usage</h4>
{{- if .ExampleUsage}}
{{- range .ExampleUsage}}
{{- if .Title}}
<p>{{.Title}}</p>
{{- end}}
<pre>
{{- .Example}}
</pre>
{{- end}}
{{- else}}
<pre>
<span>resource </span><span>"{{$.ProviderName}}_{{$resource.Name}}" "my_{{$resource.Name}}"</span>{
{{- range $resource.Properties -}}
{{template "resource_example" .}}
{{- end}}
<span>}</span>
</pre>
{{- end}}
<h4 id="resource_{{.Name}}_arguments_reference" dir="ltr">Arguments Reference</h4>
<p dir="ltr">The following arguments are supported:</p>
{{if $resource.Properties -}}
<ul dir="ltr">
{{- range $resource.Properties -}}
{{- template "resource_argument_reference" . -}}
{{- end}}
</ul>
{{- end}}
{{- $object_property_name := "" -}}
{{- range $resource.Properties -}}
{{- if (eq .Type "object")}}
{{ $object_property_name = .Name }}
{{end}}
{{- end -}}
{{- if ne $object_property_name "" -}}
<p><span class="wysiwyg-color-red">* </span>Note: Object type properties are internally represented (in the state file) as a list of one elem due to <a href="https://github.com/hashicorp/terraform-plugin-sdk/issues/155#issuecomment-489699737" target="_blank">Terraform SDK's limitation for supporting complex object types</a>. Please index on the first elem of the array to reference the object values (eg: {{$.ProviderName}}_{{.Name}}.my_{{.Name}}.<b>{{$object_property_name}}[0]</b>.object_property)</p>
{{- end -}}
{{range .ArgumentsReference.Notes}}
<p><span class="wysiwyg-color-red">*Note: {{.}}</span></p>
{{end}}
<h4 id="resource_{{.Name}}_attributes_reference" dir="ltr">Attributes Reference</h4>
<p dir="ltr">In addition to all arguments above, the following attributes are exported:</p>
{{if $resource.Properties -}}
<ul dir="ltr">
{{- range $resource.Properties -}}
{{- template "resource_attribute_reference" . -}}
{{- end}}
</ul>
{{- end -}}
{{- $object_property_name := "" -}}
{{- range $resource.Properties -}}
{{- if (eq .Type "object")}}
{{ $object_property_name = .Name }}
{{end}}
{{- end -}}
{{- if ne $object_property_name "" -}}
<p><span class="wysiwyg-color-red">* </span>Note: Object type properties are internally represented (in the state file) as a list of one elem due to <a href="https://github.com/hashicorp/terraform-plugin-sdk/issues/155#issuecomment-489699737" target="_blank">Terraform SDK's limitation for supporting complex object types</a>. Please index on the first elem of the array to reference the object values (eg: {{$.ProviderName}}_{{.Name}}.my_{{.Name}}.<b>{{$object_property_name}}[0]</b>.object_property)</p>
{{- end -}}
<h4 id="resource_{{.Name}}_import" dir="ltr">Import</h4>
<p dir="ltr">
{{.Name}} resources can be imported using the <code>id</code> {{if ne $resource.BuildImportIDsExample "id"}}. This is a sub-resource so the parent resource IDs (<code>{{$resource.ParentProperties}}</code>) are required to be able to retrieve an instance of this resource{{end}}, e.g:
</p>
<pre dir="ltr">$ terraform import {{$.ProviderName}}_{{.Name}}.my_{{.Name}} {{$resource.BuildImportIDsExample}}</pre>
<p dir="ltr">
<strong>Note</strong>: In order for the import to work, the '{{$.ProviderName}}' terraform
provider must be <a href="#provider_installation" target="_self">properly installed</a>. Read more about Terraform import usage <a href="https://www.terraform.io/docs/import/usage.html" target="_blank" rel="noopener noreferrer">here</a>.
</p>
{{if .KnownIssues}}
<h4 id="resource_{{.Name}}_known_issues" dir="ltr">Known Issues</h4>
{{range .KnownIssues}}
<p><i>{{.Title}}</i></p>
<p>{{.Description}}</p>
{{- range .Examples}}
{{- if .Title}}
<p>{{.Title}}</p>
{{- end}}
<pre>{{.Example}}</pre>
{{- end}}
{{end}}
{{- end}}
{{end}} {{/* END range .Resources */}}`, ArgumentReferenceTmpl, AttributeReferenceTmpl)
// ArgumentReferenceTmpl contains the definition used in resources to render the arguments
var ArgumentReferenceTmpl = `{{- define "resource_argument_reference" -}}
{{- $required := "Optional" -}}
{{- if .Required -}}
{{- $required = "Required" -}}
{{end}}
{{- if or .Required (and (not .Required) (not .Computed)) .IsOptionalComputed -}}
<li>{{if eq .Type "object"}}<span class="wysiwyg-color-red">*</span>{{end}} {{.Name}} [{{.Type}} {{- if eq .Type "list" }} of {{.ArrayItemsType}}s{{- end -}}] {{- if .IsSensitive -}}(<a href="#special_terms_definitions_sensitive_property" target="_self">sensitive</a>){{- end}} - ({{$required}}) {{if .IsParent}}The {{.Name}} that this resource belongs to{{else}}{{.Description}}{{end}}
{{- if or (eq .Type "object") (eq .ArrayItemsType "object")}}. The following properties compose the object schema
:<ul dir="ltr">
{{- range .Schema}}
{{- template "resource_argument_reference" .}}
{{- end}}
</ul>
{{ end -}}
</li>
{{end}}
{{- end -}}`
// AttributeReferenceTmpl contains the definition used in resources to render the attributes
var AttributeReferenceTmpl = `{{- define "resource_attribute_reference" -}}
{{- if or .Computed .ContainsComputedSubProperties -}}
{{- if and .Schema (not .ContainsComputedSubProperties) -}}{{- /* objects or arrays of objects that DO NOT have computed props are ignored since they will be documented in the arguments section */ -}}
{{- else -}}
<li>{{if eq .Type "object"}}<span class="wysiwyg-color-red">*</span>{{end}} {{.Name}} [{{.Type}} {{- if eq .Type "list" }} of {{.ArrayItemsType}}s{{- end -}}] {{ if .IsSensitive }}(<a href="#special_terms_definitions_sensitive_property" target="_self">sensitive</a>) {{end -}}{{- if .Description }}- {{.Description}} {{- end -}}
{{- if or (eq .Type "object") (eq .ArrayItemsType "object")}} The following properties compose the object schema:
<ul dir="ltr">
{{- range .Schema}}
{{- template "resource_attribute_reference" .}}
{{- end}}
</ul>
{{ end -}}
</li>
{{end}}
{{- end}}
{{- end -}}`
// DataSourcesTmpl contains the template used to render the TerraformProviderDocumentation.DataSources struct as HTML formatted for Zendesk
var DataSourcesTmpl = fmt.Sprintf(`%s
<h2 id="provider_datasources">Data Sources (using resource id)</h2>
{{if not .DataSourceInstances}}
No data sources using resource id are supported at the moment.
{{- end -}}
{{range .DataSourceInstances -}}
{{ $datasource := . }}
<h3 id="{{.Name}}" dir="ltr">{{$.ProviderName}}_{{.Name}}</h3>
{{if ne .Description "" -}}
<p>{{.Description}}</p>
{{else}}
<p>Retrieve an existing resource using it's ID</p>
{{- end}}
<h4 id="datasource_{{.Name}}_example_usage" dir="ltr">Example usage</h4>
<pre><span>data </span><span>"{{$.ProviderName}}_{{$datasource.Name}}" "my_{{$datasource.Name}}"</span>{
id = "existing_resource_id"
<span>}</span></pre>
<h4 id="datasource_{{.Name}}_arguments_reference" dir="ltr">Arguments Reference</h4>
<p dir="ltr">The following arguments are supported:</p>
<ul dir="ltr">
<li>id - (Required) ID of the existing resource to retrieve</li>
</ul>
<h4 id="datasource_{{.Name}}_attributes_reference" dir="ltr">Attributes Reference</h4>
<p dir="ltr">In addition to all arguments above, the following attributes are exported:</p>
{{if $datasource.Properties -}}
<ul dir="ltr">
{{- range $datasource.Properties -}}
{{- template "resource_attribute_reference" . -}}
{{- end}}
</ul>
{{- end}}
{{- $object_property_name := "" -}}
{{- range $datasource.Properties -}}
{{- if (eq .Type "object")}}
{{ $object_property_name = .Name }}
{{end}}
{{- end -}}
{{- if ne $object_property_name "" -}}
<p><span class="wysiwyg-color-red">* </span>Note: Object type properties are internally represented (in the state file) as a list of one elem due to <a href="https://github.com/hashicorp/terraform-plugin-sdk/issues/155#issuecomment-489699737" target="_blank">Terraform SDK's limitation for supporting complex object types</a>. Please index on the first elem of the array to reference the object values (eg: {{$.ProviderName}}_{{.Name}}.my_{{.Name}}.<b>{{$object_property_name}}[0]</b>.object_property)</p>
{{- end -}}
{{end}} {{/* END range .DataSourceInstances */}}
<h2 id="provider_datasources_filters">Data Sources (using filters)</h2>
{{if not .DataSources}}
No data sources using filters are supported at the moment.
{{- end -}}
{{range .DataSources -}}
{{ $datasource := . }}
<h3 id="{{.Name}}_datasource" dir="ltr">{{$.ProviderName}}_{{.Name}} (filters)</h3>
{{if ne .Description "" -}}
<p>{{.Description}}</p>
{{else}}
<p>The {{.Name}} data source allows you to retrieve an already existing {{.Name}} resource using filters. Refer to the arguments section to learn more about how to configure the filters.</p>
{{- end}}
<h4 id="datasource_{{.Name}}_example_usage" dir="ltr">Example usage</h4>
<pre>
<span>data </span><span>"{{$.ProviderName}}_{{$datasource.Name}}" "my_{{$datasource.Name}}"</span>{
<span>filter </span><span>{</span>
<span>name </span>= <span>"property name to filter by, see docs below for more info about available filter name options"</span>
<span>values </span>= <span>["filter value"]</span>
<span>}</span>
<span>}</span></pre>
<h4 id="datasource_{{.Name}}_arguments_reference" dir="ltr">Arguments Reference</h4>
<p dir="ltr">The following arguments are supported:</p>
{{if $datasource.Properties -}}
<ul dir="ltr">
<li>filter - (Required) Object containing two properties.</li>
<ul>
<li>name [string]: the name should match one of the properties to filter by. The following property names are supported:
{{range $datasource.Properties}}
{{if or (eq .Type "string") (eq .Type "integer") (eq .Type "number") (eq .Type "boolean")}}
<span>{{.Name}}, </span>
{{end}}
{{end}}
</li>
<li>values [array of string]: Values to filter by (only one value is supported at the moment).</li>
</ul>
</ul>
{{- end}}
<p dir="ltr"><b>Note: </b>If more or less than a single match is returned by the search, Terraform will fail. Ensure that your search is specific enough to return a single result only.</p>
<h4 id="datasource_{{.Name}}_attributes_reference" dir="ltr">Attributes Reference</h4>
<p dir="ltr">In addition to all arguments above, the following attributes are exported:</p>
{{if $datasource.Properties -}}
<ul dir="ltr">
{{- range $datasource.Properties -}}
{{if .Computed -}}
{{- template "resource_attribute_reference" . -}}
{{- end}}
{{- end}}
</ul>
{{- end}}
{{- $object_property_name := "" -}}
{{- range $datasource.Properties -}}
{{- if (eq .Type "object")}}
{{ $object_property_name = .Name }}
{{end}}
{{- end -}}
{{- if ne $object_property_name ""}}
<p><span class="wysiwyg-color-red">* </span>Note: Object type properties are internally represented (in the state file) as a list of one elem due to <a href="https://github.com/hashicorp/terraform-plugin-sdk/issues/155#issuecomment-489699737" target="_blank">Terraform SDK's limitation for supporting complex object types</a>. Please index on the first elem of the array to reference the object values (eg: {{$.ProviderName}}_{{.Name}}.my_{{.Name}}.<b>{{$object_property_name}}[0]</b>.object_property)</p>
{{- end -}}
{{end}} {{/* END range .DataSources */}}`, AttributeReferenceTmpl)
// SpecialTermsTmpl contains the template used to render the special terms definitions section as HTML formatted for Zendesk
var SpecialTermsTmpl = `{{ if .ShowSpecialTermsDefinitions }}
<h2 id="special_terms_definitions">Special Terms Definitions</h2>
<p>
This section describes specific terms used throughout this document to clarify their meaning in the context of Terraform.
</p>
{{ if .ProviderResources.ContainsResourcesWithSecretProperties }}
<h3 id="special_terms_definitions_sensitive_property">
<span style="font-weight:400">Sensitive Property</span>
</h3>
<p>
<span style="font-weight:400">The ‘{{.ProviderName}}’ Terraform plugin treats secret properties as ‘sensitive’. As per </span><a href="https://github.com/hashicorp/terraform-plugin-sdk/blob/9f0df37a8fdb2627ae32db6ceaf7f036d89b6768/helper/schema/schema.go#L245" target="_self">Terraform documentation</a><span style="font-weight:400">, this means the following:</span>
</p>
<pre><span style="font-weight:400">// Sensitive ensures that the attribute's value does not get displayed <br>// in logs or regular output. It should be used for passwords or other <br>// secret fields. Future versions of Terraform may encrypt these values.</span></pre>
<p>
<span style="font-weight:400">Please note that even though the secret values don’t get displayed in the logs or regular output, the state file will still store the secrets. </span><span style="font-weight:400">As per <a href="https://www.terraform.io/docs/state/sensitive-data.html">Terraform’s official recommendations</a> on how to treat Sensitive Data in State, if your state file may contain sensitive information always treat the State itself as sensitive data.</span>
</p>
<p> </p>
{{end}}
{{end}}`