Skip to content

support provider prefix in generated filenames #348

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -166,8 +166,8 @@ The `migrate` subcommand takes the following actions:

The generation of missing documentation is based on a number of assumptions / conventional paths.

> **NOTE:** In the following conventional paths, `<data source name>` and `<resource name>` include the provider prefix as well, but the provider prefix is **NOT** included in`<function name>`.
> For example, the data source [`caller_identity`](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/caller_identity) in the `aws` provider would have an "example" conventional path of: `examples/data-sources/aws_caller_identity/data-source.tf`
> **NOTE:** In the following conventional paths, `<data source name>`, `<resource name>`, and `<function name>` **DO NOT**include the provider prefix; if `--use-provider-prefix=true` is specified, the provider prefix is included in `<data source name>` and `<resource name>` but it is **NOT** included in`<function name>`.
> For example, the data source [`caller_identity`](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/caller_identity) in the `aws` provider would have an "example" conventional path of `examples/data-sources/caller_identity/data-source.tf` by default, and `examples/data-sources/aws_caller_identity/data-source.tf` if `--use-provider-prefix=true` is specified.

For templates:

Expand Down
5 changes: 4 additions & 1 deletion internal/cmd/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ import (
type generateCmd struct {
commonCmd

flagIgnoreDeprecated bool
flagIgnoreDeprecated bool
flagUseProviderPrefix bool

flagProviderName string
flagRenderedProviderName string
Expand Down Expand Up @@ -82,6 +83,7 @@ func (cmd *generateCmd) Flags() *flag.FlagSet {
fs.StringVar(&cmd.flagWebsiteSourceDir, "website-source-dir", "templates", "templates directory based on provider-dir")
fs.StringVar(&cmd.tfVersion, "tf-version", "", "terraform binary version to download. If not provided, will look for a terraform binary in the local environment. If not found in the environment, will download the latest version of Terraform")
fs.BoolVar(&cmd.flagIgnoreDeprecated, "ignore-deprecated", false, "don't generate documentation for deprecated resources and data-sources")
fs.BoolVar(&cmd.flagUseProviderPrefix, "use-provider-prefix", false, "include the provider name as a prefix in the filenames of all resource and data-source documents")
return fs
}

Expand Down Expand Up @@ -109,6 +111,7 @@ func (cmd *generateCmd) runInternal() error {
cmd.flagWebsiteSourceDir,
cmd.tfVersion,
cmd.flagIgnoreDeprecated,
cmd.flagUseProviderPrefix,
)
if err != nil {
return fmt.Errorf("unable to generate website: %w", err)
Expand Down
40 changes: 31 additions & 9 deletions internal/provider/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,9 @@ var (
)

type generator struct {
ignoreDeprecated bool
tfVersion string
ignoreDeprecated bool
useProviderPrefix bool
tfVersion string

// providerDir is the absolute path to the root provider directory
providerDir string
Expand All @@ -105,7 +106,7 @@ func (g *generator) warnf(format string, a ...interface{}) {
g.ui.Warn(fmt.Sprintf(format, a...))
}

func Generate(ui cli.Ui, providerDir, providerName, providersSchemaPath, renderedProviderName, renderedWebsiteDir, examplesDir, websiteTmpDir, templatesDir, tfVersion string, ignoreDeprecated bool) error {
func Generate(ui cli.Ui, providerDir, providerName, providersSchemaPath, renderedProviderName, renderedWebsiteDir, examplesDir, websiteTmpDir, templatesDir, tfVersion string, ignoreDeprecated, useProviderPrefix bool) error {
// Ensure provider directory is resolved absolute path
if providerDir == "" {
wd, err := os.Getwd()
Expand Down Expand Up @@ -148,6 +149,7 @@ func Generate(ui cli.Ui, providerDir, providerName, providersSchemaPath, rendere
examplesDir: examplesDir,
templatesDir: templatesDir,
websiteTmpDir: websiteTmpDir,
useProviderPrefix: useProviderPrefix,

ui: ui,
}
Expand Down Expand Up @@ -266,7 +268,10 @@ func (g *generator) TempTemplatesDir() string {
}

func (g *generator) generateMissingResourceTemplate(resourceName string) error {
templatePath := fmt.Sprintf(websiteResourceFile, resourceShortName(resourceName, g.providerName))
templatePath := fmt.Sprintf(websiteResourceFile, resourceName)
if !g.useProviderPrefix {
templatePath = fmt.Sprintf(websiteResourceFile, resourceShortName(resourceName, g.providerName))
}
templatePath = filepath.Join(g.TempTemplatesDir(), templatePath)
if fileExists(templatePath) {
g.infof("resource %q template exists, skipping", resourceName)
Expand All @@ -284,7 +289,10 @@ func (g *generator) generateMissingResourceTemplate(resourceName string) error {
}

for _, candidate := range websiteResourceFileStaticCandidates {
candidatePath := fmt.Sprintf(candidate, resourceShortName(resourceName, g.providerName))
candidatePath := fmt.Sprintf(candidate, resourceName)
if !g.useProviderPrefix {
candidatePath = fmt.Sprintf(candidate, resourceShortName(resourceName, g.providerName))
}
candidatePath = filepath.Join(g.TempTemplatesDir(), candidatePath)
if fileExists(candidatePath) {
g.infof("resource %q static file exists, skipping", resourceName)
Expand All @@ -302,7 +310,10 @@ func (g *generator) generateMissingResourceTemplate(resourceName string) error {
}

func (g *generator) generateMissingDataSourceTemplate(datasourceName string) error {
templatePath := fmt.Sprintf(websiteDataSourceFile, resourceShortName(datasourceName, g.providerName))
templatePath := fmt.Sprintf(websiteDataSourceFile, datasourceName)
if !g.useProviderPrefix {
templatePath = fmt.Sprintf(websiteDataSourceFile, resourceShortName(datasourceName, g.providerName))
}
templatePath = filepath.Join(g.TempTemplatesDir(), templatePath)
if fileExists(templatePath) {
g.infof("data-source %q template exists, skipping", datasourceName)
Expand All @@ -320,7 +331,10 @@ func (g *generator) generateMissingDataSourceTemplate(datasourceName string) err
}

for _, candidate := range websiteDataSourceFileStaticCandidates {
candidatePath := fmt.Sprintf(candidate, resourceShortName(datasourceName, g.providerName))
candidatePath := fmt.Sprintf(candidate, datasourceName)
if !g.useProviderPrefix {
candidatePath = fmt.Sprintf(candidate, resourceShortName(datasourceName, g.providerName))
}
candidatePath = filepath.Join(g.TempTemplatesDir(), candidatePath)
if fileExists(candidatePath) {
g.infof("data-source %q static file exists, skipping", datasourceName)
Expand Down Expand Up @@ -528,7 +542,11 @@ func (g *generator) renderStaticWebsite(providerSchema *tfjson.ProviderSchema) e
g.infof("rendering %q", rel)
switch relDir {
case "data-sources/":
resSchema, resName := resourceSchema(providerSchema.DataSourceSchemas, shortName, relFile)
// hack: if we're including the provider prefix we need to trim it out before passing to resourceSchema
resSchema, resName := resourceSchema(providerSchema.DataSourceSchemas, shortName, resourceShortName(relFile, shortName))
if !g.useProviderPrefix {
resSchema, resName = resourceSchema(providerSchema.DataSourceSchemas, shortName, relFile)
}
exampleFilePath := filepath.Join(g.ProviderExamplesDir(), "data-sources", resName, "data-source.tf")

if resSchema != nil {
Expand All @@ -545,7 +563,11 @@ func (g *generator) renderStaticWebsite(providerSchema *tfjson.ProviderSchema) e
}
g.warnf("data source entitled %q, or %q does not exist", shortName, resName)
case "resources/":
resSchema, resName := resourceSchema(providerSchema.ResourceSchemas, shortName, relFile)
// hack: if we're including the provider prefix we need to trim it out before passing to resourceSchema
resSchema, resName := resourceSchema(providerSchema.ResourceSchemas, shortName, resourceShortName(relFile, shortName))
if !g.useProviderPrefix {
resSchema, resName = resourceSchema(providerSchema.ResourceSchemas, shortName, relFile)
}
exampleFilePath := filepath.Join(g.ProviderExamplesDir(), "resources", resName, "resource.tf")
importFilePath := filepath.Join(g.ProviderExamplesDir(), "resources", resName, "import.sh")

Expand Down