Skip to content

Commit

Permalink
improve terraform generation by loading the qovery terraform provider…
Browse files Browse the repository at this point in the history
… documentation
  • Loading branch information
evoxmusic committed Sep 14, 2024
1 parent 431dbb7 commit dc11f9f
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 4 deletions.
2 changes: 1 addition & 1 deletion cli/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module qovery-ai-migration
go 1.22.5

require (
github.com/Qovery/qovery-migration-ai-agent v0.0.0-20240914141434-ca2b24132660
github.com/Qovery/qovery-migration-ai-agent v0.0.0-20240914150846-431dbb7a76b4
github.com/joho/godotenv v1.5.1
github.com/schollz/progressbar/v3 v3.14.6
github.com/spf13/cobra v1.8.1
Expand Down
2 changes: 2 additions & 0 deletions cli/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ github.com/Qovery/qovery-migration-ai-agent v0.0.0-20240914141020-b7e8463abbc6 h
github.com/Qovery/qovery-migration-ai-agent v0.0.0-20240914141020-b7e8463abbc6/go.mod h1:BK4dodrSYDAJySssVnQDgzCjaR1yvonA3hNYsDigYLQ=
github.com/Qovery/qovery-migration-ai-agent v0.0.0-20240914141434-ca2b24132660 h1:/WGnhUU3BhRjGnTtvR2ne3ETPqhLAaka2fQr+iMHdAs=
github.com/Qovery/qovery-migration-ai-agent v0.0.0-20240914141434-ca2b24132660/go.mod h1:BK4dodrSYDAJySssVnQDgzCjaR1yvonA3hNYsDigYLQ=
github.com/Qovery/qovery-migration-ai-agent v0.0.0-20240914150846-431dbb7a76b4 h1:TFpTH6EqVmmwCYmKVB6lAXAKkHuLLMkrdkFHmaJPAeY=
github.com/Qovery/qovery-migration-ai-agent v0.0.0-20240914150846-431dbb7a76b4/go.mod h1:BK4dodrSYDAJySssVnQDgzCjaR1yvonA3hNYsDigYLQ=
github.com/cpuguy83/go-md2man/v2 v2.0.4/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
Expand Down
61 changes: 58 additions & 3 deletions pkg/migration/migration.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,13 @@ func generateTerraform(qoveryConfigs map[string]interface{}, destination string,

airbyteExample, err := loadTerraformExamples("evoxmusic", "qovery-airbyte", ".")
if err != nil {
return "", "", fmt.Errorf("error loading Terraform examples: %w", err)
return "", "", fmt.Errorf("error loading Airbyte Terraform example: %w", err)
}

// https://github.com/Qovery/terraform-provider-qovery/tree/main/docs
qoveryTerraformDocMarkdown, err := loadMarkdownFiles("Qovery", "terraform-provider-qovery", "main")
if err != nil {
return "", "", fmt.Errorf("error loading Qovery Terraform Provide markdown documentation: %w", err)
}

examples := append(officialExamples, airbyteExample...)
Expand All @@ -148,6 +154,11 @@ func generateTerraform(qoveryConfigs map[string]interface{}, destination string,
return "", "", fmt.Errorf("error marshaling Terraform examples: %w", err)
}

qoveryTerraformDocMarkdownJSON, err := json.Marshal(qoveryTerraformDocMarkdown)
if err != nil {
return "", "", fmt.Errorf("error marshaling Qovery Terraform Provider markdown documentation: %w", err)
}

prompt := fmt.Sprintf(`Generate a consolidated Terraform configuration for Qovery that includes all of the following apps:
%s
The configuration should be for the %s cloud provider.
Expand All @@ -159,8 +170,13 @@ Provide two separate configurations:
Format the response as a tuple of two strings with a "|||" separator: (main_tf_content|||variables_tf_content).
Don't use Buildpacks, only use Dockerfiles for build_mode.
Don't format the output by using backticks.
Do not include anything else.`,
string(configJSON), destination, string(examplesJSON), destination)
Export secrets or sensitive information from the main.tf file into the variables.tf and don't include the sensitive data.
Do not include anything else.
Try to optimize the configuration as much as possible.
Include comment to explain the configuration if needed - users are technical but can be not familiar with Terraform.
Refer to the Qovery Terraform Provider Documentation below to see all the options of the provider and how to use it:
%s`, string(configJSON), destination, string(examplesJSON), destination, string(qoveryTerraformDocMarkdownJSON))

response, err := claudeClient.Messages(prompt)
if err != nil {
Expand Down Expand Up @@ -215,6 +231,45 @@ func loadTerraformExamples(owner string, repo string, exampleDir string) ([]Terr
return examples, nil
}

func loadMarkdownFiles(owner, repo, branch string) (map[string]string, error) {
client := github.NewClient(nil)
ctx := context.Background()

result := make(map[string]string)

// Folders to search
folders := []string{"", "data-sources", "resources"}

for _, folder := range folders {
// List files in the folder
_, directoryContent, _, err := client.Repositories.GetContents(ctx, owner, repo, "docs/"+folder, &github.RepositoryContentGetOptions{Ref: branch})
if err != nil {
return nil, fmt.Errorf("error listing contents of %s: %v", folder, err)
}

for _, content := range directoryContent {
if content.GetType() == "file" && strings.HasSuffix(content.GetName(), ".md") {
// Get file content
fileContent, _, _, err := client.Repositories.GetContents(ctx, owner, repo, content.GetPath(), &github.RepositoryContentGetOptions{Ref: branch})
if err != nil {
return nil, fmt.Errorf("error getting content of %s: %v", content.GetPath(), err)
}

// Decode content
decodedContent, err := fileContent.GetContent()
if err != nil {
return nil, fmt.Errorf("error decoding content of %s: %v", content.GetPath(), err)
}

// Add to result map
result[content.GetPath()] = decodedContent
}
}
}

return result, nil
}

// parseTerraformResponse parses the Claude AI response for Terraform configurations
func parseTerraformResponse(response string) (string, string, error) {
response = strings.TrimSpace(response)
Expand Down

0 comments on commit dc11f9f

Please sign in to comment.