-
Notifications
You must be signed in to change notification settings - Fork 43
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
Handle code blocks in import section #2382
Open
andrzejressel
wants to merge
2
commits into
pulumi:master
Choose a base branch
from
andrzejressel:feature/fix_code_in_imports
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1166,6 +1166,42 @@ func flattenListAttributeKey(attribute string) string { | |
return strings.ReplaceAll(attribute, ".0", "") | ||
} | ||
|
||
func parseImportShellLine(section, token string) string { | ||
if strings.Contains(section, "terraform import") { | ||
// First, remove the `$` | ||
section := strings.Replace(section, "$ ", "", -1) | ||
// Next, remove `terraform import` from the codeblock | ||
section = strings.Replace(section, "terraform import ", "", -1) | ||
importString := "" | ||
parts := strings.Split(section, " ") | ||
for i, p := range parts { | ||
switch i { | ||
case 0: | ||
if !isBlank(p) { | ||
// split the string on . and take the last item | ||
// this gets the identifier broken from the tf resource | ||
ids := strings.Split(p, ".") | ||
name := ids[len(ids)-1] | ||
importString = fmt.Sprintf("%s %s", importString, name) | ||
} | ||
default: | ||
if !isBlank(p) { | ||
importString = fmt.Sprintf("%s %s", importString, p) | ||
} | ||
} | ||
} | ||
var tok string | ||
if token != "" { | ||
tok = token | ||
} else { | ||
tok = "MISSING_TOK" | ||
} | ||
importCommand := fmt.Sprintf("$ pulumi import %s%s", tok, importString) | ||
return importCommand | ||
} | ||
return section | ||
} | ||
|
||
func (p *tfMarkdownParser) parseImports(subsection []string) { | ||
var token string | ||
if p.info != nil && p.info.GetTok() != "" { | ||
|
@@ -1206,65 +1242,68 @@ func (p *tfMarkdownParser) parseImports(subsection []string) { | |
} | ||
|
||
var importDocString string | ||
for _, section := range subsection { | ||
var i = 0 | ||
|
||
for i < len(subsection) { | ||
section := subsection[i] | ||
trimmedSection := strings.TrimSpace(section) | ||
if strings.Contains(section, "**NOTE:") || strings.Contains(section, "**Please Note:") || | ||
strings.Contains(section, "**Note:**") { | ||
// This is a Terraform import specific comment that we don't need to parse or include in our docs | ||
i++ | ||
continue | ||
} | ||
|
||
// Skip another redundant comment | ||
if strings.Contains(section, "Import is supported using the following syntax") { | ||
i++ | ||
continue | ||
} | ||
|
||
// Remove the shell comment characters to avoid writing this line as a Markdown H1: | ||
section = strings.TrimPrefix(section, "# ") | ||
|
||
// There are multiple variations of codeblocks for import syntax | ||
section = strings.Replace(section, "```shell", "", -1) | ||
section = strings.Replace(section, "```sh", "", -1) | ||
section = strings.Replace(section, "```", "", -1) | ||
|
||
if strings.Contains(section, "terraform import") { | ||
// First, remove the `$` | ||
section := strings.Replace(section, "$ ", "", -1) | ||
// Next, remove `terraform import` from the codeblock | ||
section = strings.Replace(section, "terraform import ", "", -1) | ||
importString := "" | ||
parts := strings.Split(section, " ") | ||
for i, p := range parts { | ||
switch i { | ||
case 0: | ||
if !isBlank(p) { | ||
// split the string on . and take the last item | ||
// this gets the identifier broken from the tf resource | ||
ids := strings.Split(p, ".") | ||
name := ids[len(ids)-1] | ||
importString = fmt.Sprintf("%s %s", importString, name) | ||
} | ||
default: | ||
if !isBlank(p) { | ||
importString = fmt.Sprintf("%s %s", importString, p) | ||
} | ||
// Handle terraform blocks - pass them whole without changes | ||
if trimmedSection == "```terraform" || trimmedSection == "```hcl" { | ||
for { | ||
section = subsection[i] | ||
trimmedSection := strings.TrimSpace(section) | ||
importDocString = importDocString + section + "\n" | ||
i++ | ||
if trimmedSection == "```" { | ||
break | ||
} | ||
} | ||
var tok string | ||
if token != "" { | ||
tok = token | ||
} else { | ||
tok = "MISSING_TOK" | ||
} | ||
importCommand := fmt.Sprintf("$ pulumi import %s%s\n", tok, importString) | ||
importDetails := "```sh\n" + importCommand + "```\n\n" | ||
importDocString = importDocString + importDetails | ||
} else { | ||
if !isBlank(section) { | ||
// Ensure every section receives a line break. | ||
section = section + "\n\n" | ||
importDocString = importDocString + section | ||
importDocString = importDocString + "\n" | ||
continue | ||
} | ||
|
||
// Handle script blocks - replace terraform import with pulumi import | ||
if strings.HasPrefix(trimmedSection, "```") { | ||
initial := true | ||
for { | ||
section = subsection[i] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This line panics when running this code against pulumi-gcp. |
||
trimmedSection := strings.TrimSpace(section) | ||
if initial { | ||
// ``` and ```shell are removed when converting to programming language comment | ||
importDocString = importDocString + "```sh" + "\n" | ||
} else { | ||
importDocString = importDocString + parseImportShellLine(section, token) + "\n" | ||
} | ||
i++ | ||
if trimmedSection == "```" && !initial { | ||
break | ||
} | ||
initial = false | ||
} | ||
importDocString = importDocString + "\n" | ||
continue | ||
} | ||
|
||
if !isBlank(section) { | ||
// Ensure every section receives a line break. | ||
section = section + "\n\n" | ||
importDocString = importDocString + section | ||
} | ||
|
||
i++ | ||
} | ||
|
||
if len(importDocString) > 0 { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
Import is supported using the following syntax by providing the `id`: | ||
|
||
```shell | ||
#!/bin/bash | ||
terraform import docker_container.foo id | ||
``` | ||
|
||
### Example | ||
|
||
Assuming you created a `container` as follows | ||
|
||
```shell | ||
#!/bin/bash | ||
docker run --name foo -p8080:80 -d nginx | ||
# prints the container ID | ||
9a550c0f0163d39d77222d3efd58701b625d47676c25c686c95b5b92d1cba6fd | ||
``` | ||
|
||
you provide the definition for the resource as follows | ||
|
||
```terraform | ||
resource "docker_container" "foo" { | ||
name = "foo" | ||
image = "nginx" | ||
|
||
ports { | ||
internal = "80" | ||
external = "8080" | ||
} | ||
} | ||
``` | ||
|
||
then the import command is as follows | ||
|
||
```shell | ||
#!/bin/bash | ||
terraform import docker_container.foo 9a550c0f0163d39d77222d3efd58701b625d47676c25c686c95b5b92d1cba6fd | ||
``` |
39 changes: 39 additions & 0 deletions
39
pkg/tfgen/test_data/parse-imports/docker_container-expected.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
## Import | ||
|
||
```sh | ||
#!/bin/bash | ||
$ pulumi import docker:index/container:Container foo id | ||
``` | ||
|
||
### Example | ||
|
||
Assuming you created a `container` as follows | ||
|
||
```sh | ||
#!/bin/bash | ||
docker run --name foo -p8080:80 -d nginx | ||
# prints the container ID | ||
9a550c0f0163d39d77222d3efd58701b625d47676c25c686c95b5b92d1cba6fd | ||
``` | ||
|
||
you provide the definition for the resource as follows | ||
|
||
```terraform | ||
guineveresaenger marked this conversation as resolved.
Show resolved
Hide resolved
|
||
resource "docker_container" "foo" { | ||
name = "foo" | ||
image = "nginx" | ||
|
||
ports { | ||
internal = "80" | ||
external = "8080" | ||
} | ||
} | ||
``` | ||
|
||
then the import command is as follows | ||
|
||
```sh | ||
#!/bin/bash | ||
$ pulumi import docker:index/container:Container foo 9a550c0f0163d39d77222d3efd58701b625d47676c25c686c95b5b92d1cba6fd | ||
``` | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
Import is supported using the following syntax by providing the `id`: | ||
|
||
```shell | ||
#!/bin/bash | ||
terraform import docker_container.foo id | ||
``` | ||
|
||
### Example | ||
|
||
Assuming you created a `container` as follows | ||
|
||
```shell | ||
#!/bin/bash | ||
docker run --name foo -p8080:80 -d nginx | ||
# prints the container ID | ||
9a550c0f0163d39d77222d3efd58701b625d47676c25c686c95b5b92d1cba6fd | ||
``` | ||
|
||
you provide the definition for the resource as follows | ||
|
||
```terraform | ||
resource "docker_container" "foo" { | ||
name = "foo" | ||
image = "nginx" | ||
|
||
ports { | ||
internal = "80" | ||
external = "8080" | ||
} | ||
} | ||
``` | ||
|
||
then the import command is as follows | ||
|
||
```shell | ||
#!/bin/bash | ||
terraform import docker_container.foo 9a550c0f0163d39d77222d3efd58701b625d47676c25c686c95b5b92d1cba6fd | ||
``` |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please use https://github.com/pulumi/pulumi-terraform-bridge/blob/master/pkg/tfgen/docs.go#L2353 for this check - it contains the current known detection patterns for TF code. 🙏