pulumi/pkg/codegen/docs
Justin Van Patten 4e9e017cd2
[codegen/python] Rename conflicting ResourceArgs classes (#7171)
Python resource constructor overloads were recently added that accept a
`<Resource>Args` class for input properties, as an alternative to the
other constructor overload that accepts keyword arguments. The name of
the new args class is the name of the resource concatenated with an
`Args` suffix.

Some providers (e.g. Kubernetes, Azure Native, and Google Native) have
input types with the same name as resources in the same module, which
results in two different `<Resource>Args` classes in the same module.

When you try to use the new args class with the constructor, e.g.:

```python
pulumi_kubernetes.storage.v1.StorageClass(
            resource_name='string',
            args=pulumi_kubernetes.storage.v1.StorageClassArgs(...),
            opts=pulumi.ResourceOptions(...),
)
```

You run into an error, because
`pulumi_kubernetes.storage.v1.StorageClassArgs` is actually referring to
the existing input type rather than the intended `StorageClassArgs`
class for the constructor arguments.

Having the duplicate classes hasn't broken existing usage of the input
type because we "export" all the input types for a module _after_ all
the resources and resource args classes are exported, so the input type
just ends up "overwriting" the duplicate resource args class.

Other languages don't have this problem because the input type is either
in it's own module/namespace (e.g. Node.js and .NET) or a different name
is used for the input type (Go). But with Python, the input types and
resources are all available in the same module.

To address this for Python, when there is an input type in the same
module with the same name as the resource, the args class for the
resource will be emitted as `<Resource>InitArgs` instead of
`<Resource>Args`.
2021-06-10 10:41:49 -07:00
..
templates Remove leading and trailing whitespace in resource properties (#6959) 2021-05-04 17:59:30 -07:00
.gitignore Update schema-based docs generator (#4035) 2020-03-09 10:35:20 -07:00
bundler.go Updates to resource doc generator for generating docs for Functions (#4055) 2020-03-11 17:58:12 -07:00
examples.go [breaking] Changing the version of go.mod in sdk / pkg to be v3 2021-04-14 19:32:18 +01:00
gen.go [codegen/python] Rename conflicting ResourceArgs classes (#7171) 2021-06-10 10:41:49 -07:00
gen_function.go [docgen] Fix horizontal scroll in python resource docs (#6801) 2021-04-17 15:33:23 -07:00
gen_kubernetes.go [breaking] Changing the version of go.mod in sdk / pkg to be v3 2021-04-14 19:32:18 +01:00
gen_test.go [codegen/python] Rename conflicting ResourceArgs classes (#7171) 2021-06-10 10:41:49 -07:00
README.md Add a note to a README about regenerating test data (#6970) 2021-05-05 12:59:37 -07:00
utils.go [breaking] Changing the version of go.mod in sdk / pkg to be v3 2021-04-14 19:32:18 +01:00
utils_test.go Update schema-based docs generator (#4035) 2020-03-09 10:35:20 -07:00

Docs generator

This generator generates resource-level docs by utilizing the Pulumi schema.

Crash course on templates

The templates use Go's built-in html/template package to process templates with data. The driver for this doc generator (e.g. tfbridge for TF-based providers) then persists each file from memory onto the disk as .md files.

Although we are using the html/template package, it has the same exact interface as the text/template package, except for some HTML specific things. Therefore, all of the functions available in the text/template package are also available with the html/template package.

  • Data can be injected using {{.PropertyName}}.
  • Nested properties can be accessed using the dot notation, i.e. {{.Property1.Property2}}.
  • Templates can inject other templates using the {{template "template_name"}} directive.
    • For this to work, you will need to first define the named template using {{define "template_name"}}.
  • You can pass data to nested templates by simply passing an argument after the template's name.
  • To remove whitespace from injected values, use the - in the template tags.
    • For example, {{if .SomeBool}} some text {{- else}} some other text {{- end}}. Note the use of - to eliminate whitespace from the enclosing text.
    • Read more here.
  • To render un-encoded content use the custom global function htmlSafe.
    • Note: This should only be used if you know for sure you are not injecting any user-generated content, as it by-passes the HTML encoding.
  • To print regular strings, that share the same syntax as the Go templating engine, use the built-in global function print function.
    • For example, if you need to render {{% md %}}, you will instead need to do {{print "{{% md %}}"}}.

Learn more from here: https://curtisvermeeren.github.io/2017/09/14/Golang-Templates-Cheatsheet

Modifying templates and updating tests

We run tests that validate our template-rendering output. If you need to make change that produces a set of Markdown files that differs from the set that we use in our tests (see codegen/internal/test/testdata/**/*.md), your pull-request checks will fail, and to get them to pass, you'll need to modify the test data to match the output produced by your change.

For minor diffs, you can just update the test files manually and include those updates with your PR. But for large diffs, you may want to regenerate the full set. To do that, from the root of the repo, run:

PULUMI_ACCEPT=true pushd pkg/codegen/docs && go test . && popd

bundler.go

This file contains a main function and is part of the main package. We run it using the go generate command (see the Makefile and the starting comment in pkg/codegen/gen.go).

This file is ignored using a +build ignore comment at the top of the file, so it is not ignored during a go build ....

packaged.go

A file generated by bundler.go that contains formatted byte strings, that represent the string templates from the ./templates/ folder. This file is also git-ignored as it is intended to only be generated by the docs repo and is not used during runtime of the main Pulumi CLI. In fact, this whole package is not used during the runtime of the CLI itself.

go:generate

Read more here.

go:generate is a special code comment that can be used to run custom commands by simply running go generate <package>, which then scans for go:generate comments in all sources in the package <package>. It also serves as a way to document, that a certain file relies on a command to have been executed before it can be used.