Skip to content
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

feat: render provider file skeleton #64

Merged
merged 21 commits into from
Apr 24, 2024

Conversation

pimielowski
Copy link
Contributor

@pimielowski pimielowski commented Apr 17, 2024

Description

Introducing skeleton for Resource and DataSource object for Terraform Provider, plus mechanism to generate file, imports etc.

Motivation and Context

Part of the #52 #59 #60 #61

How Has This Been Tested?

Local test

Screenshots (if appropriate)

image
image

Types of changes

  • New feature (non-breaking change which adds functionality)

Checklist

  • I have updated the documentation accordingly.
  • I have read the CONTRIBUTING document.
  • I have added tests to cover my changes if appropriate.
  • All new and existing tests passed.

shinmog and others added 14 commits April 13, 2024 23:42
…kage

In this update, the "array.go" and "bool.go" files from the "translate/normalized" package were deleted. These updates streamline the overall structure and improve code readability.
Removed unused functions and constants in the `names.go` file under the naming package. These include `PathNameToSdkPath`, `AsPackageName`, `SchemaNameToSdkPath`, `MakePathFrom` functions and `DelayNaming` constant. The deletion resulted in a cleaner and leaner code base.
The refactoring simplifies the usage of template functions in terraform_resource.go by utilizing camel casing and restructuring resource object. Also, methods for creating, updating, reading, and deleting of the Terraform state were introduced, preparing for future data operations and error handling. Placeholder structs and functions were added for future completion.
The code in generator.go has been modularized into more specific functions for creating a file path and writing to a file. These modifications enhance the readability of the code and conform with the single-responsibility principle. Comment placeholders were also added in codegen.go to earmark places for future integration with datasources. Moreover, the fixed class name 'ExampleResource' was replaced with a dynamic one in terraform_resource.go.
Changed the process involved in generating a Terraform resource. This specifically involved renaming the `Resource` function to `GenerateTerraformResource` for clarity, and implementing a new template string for better structuring and future modifications. This makes the code more maintainable and improves resource creation's overall robustness.
This commit introduces builds to support data sources in the Terraform code generation pathway. The data source list logic is no longer commented out, suggesting it's functioning as intended. Simultaneously, the code generator for resources has been significantly refactored. The file 'terraform_resource.go' was deleted and instead implementation for resource generation was moved into the 'terraform_provider.go' file with appropriate changes.
This update enhances the process of generating Terraform resources and data sources by refining the template generation function and introducing a new naming convention structure. It adds a new function that generates a Terraform provider file. Additionally, it expands cases and imports in the 'RenderImports' function, maximizing its utility and robustness.
@pimielowski pimielowski marked this pull request as ready for review April 19, 2024 09:18
A test has been added to ensure the successful generation of a Terraform resource using valid inputs. Additionally, a null check was added to the generateTemplate function to prevent errors when the Terraform provider's code is not initialized.
@pimielowski pimielowski self-assigned this Apr 19, 2024
@pimielowski pimielowski added the enhancement New feature or request label Apr 19, 2024
@pimielowski pimielowski changed the title feat: render resource based on spec feat: render provider file skeleton Apr 19, 2024
This commit primarily changes the process of generating Terraform providers and specs. It introduces options to skip the process for certain data types and reorganizes the generation sequence. Additionally, it updates various resource and data source templates and modifies import paths to adhere to the new generation logic. Tests are also updated to check these modifications. It streamlines the system by controlling which Terraform resources/data source entities to generate.
pkg/properties/normalized_test.go Outdated Show resolved Hide resolved
pkg/translate/golang/terraform/template.go Show resolved Hide resolved
The terraform generation parameters 'skip_generate_terraform_resource', 'skip_generate_terraform_datasource', and 'skip_generate_terraform_datasource_listing' have been refactored into a single struct 'SkipGenerateTerraform'. This change affects multiple YAML specifications and related code. This makes the terraform generation configuration more organized within the codebase.
pkg/properties/normalized_test.go Outdated Show resolved Hide resolved
pkg/commands/codegen/codegen.go Outdated Show resolved Hide resolved
pkg/generate/generator.go Outdated Show resolved Hide resolved
Copy link
Contributor

@sebastianczech sebastianczech left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

approved (regarding proposition from @michalbil about #64 (comment) , I prefer second option)

pkg/properties/normalized_test.go Outdated Show resolved Hide resolved
pkg/properties/provider_file.go Outdated Show resolved Hide resolved
pkg/translate/golang/terraform/template.go Show resolved Hide resolved

// Write logs using the tflog package
// Documentation: https://terraform.io/plugin/log
tflog.Trace(ctx, "created a resource")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You and @michalbil will need to discuss your vision and approach to logging. What types of messages get logged at what level, so there is consistency between data sources and resources.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is true, but we need to move forward right now with at least skeleton, I change it in the future :)


{{- /* Done */ -}}`

const dataSourceSingletonTemplateStr = `
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can't help but feel like these templates should be in separate files..?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So there was a topic on how to handle templates, and we agreed to store it like a piece of code in the code, that's why I created the template.go with const there. If we want to have them in separate files, we should create them in .tmpl files for each template and store them in the template folder in the terraform subdirectory, if you are ok with it I can quickly refactor the code for handling this.

pkg/translate/golang/terraform/terraform_provider_file.go Outdated Show resolved Hide resolved
pkg/properties/normalized.go Outdated Show resolved Hide resolved
specs/device/dns.yaml Outdated Show resolved Hide resolved
The Terraform provider configuration has been refactored for better organization. The 'terraform_provider_suffix' and 'skip_generate_terraform' attributes have been combined into a new attribute called 'terraform_provider_config'. This group includes the 'suffix' attribute, as well as boolean attributes for resource, datasource, and datasource listing. The code has been updated accordingly across the package, tests, and specifications.
This update simplifies the management of imports by directly using AddHashicorpImport/AddSdkImport, eliminating the need for a separate ImportManager object for each section. It also replaces null checks on Resource, Datasource, and DatasourceListing with straightforward boolean checks. Additional cleanup includes updating test cases and fixing minor discrepancies in some data structures' definitions.
@pimielowski pimielowski requested a review from shinmog April 23, 2024 10:25
pkg/properties/normalized.go Outdated Show resolved Hide resolved
specs/objects/address-group.yaml Outdated Show resolved Hide resolved
specs/objects/address.yaml Outdated Show resolved Hide resolved
specs/objects/service.yaml Outdated Show resolved Hide resolved
specs/objects/tag.yaml Outdated Show resolved Hide resolved
The Terraform configuration fields "resource", "datasource", and "datasource_listing" were renamed to "skip_resource", "skip_datasource", and "skip_datasource_listing", respectively, in the TerraformProviderConfig struct. These changes were reflected in the generate Terraform functions and tests. Additionally, the field "suffix" was updated in multiple object specification files.
The 'terraform_provider_suffix' field has been replaced with more flexible 'terraform_provider_config' dictionary. This accommodates boolean fields 'skip_resource', 'skip_datasource', and 'skip_datasource_listing' in addition to the 'suffix' field previously provided under 'terraform_provider_suffix'. The changes have been applied across all test and YAML specification files.
@pimielowski pimielowski requested a review from shinmog April 23, 2024 12:38
Copy link
Collaborator

@shinmog shinmog left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@pimielowski pimielowski merged commit 6befe3b into main Apr 24, 2024
2 checks passed
@pimielowski pimielowski deleted the feat-render-resource-based-on-spec branch April 24, 2024 07:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants