Skip to content

feat: Generate terraform datasource structs #120

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

Merged

Conversation

kklimonda-cl
Copy link
Contributor

Description

This commit adds generation of Terraform datasource structures.

@kklimonda-cl kklimonda-cl requested a review from michalbil July 25, 2024 13:53
@kklimonda-cl kklimonda-cl changed the base branch from feat-render-resource-second-part to feat-tf-locations-rendering July 25, 2024 13:56
Base automatically changed from feat-tf-locations-rendering to feat-render-resource-second-part July 29, 2024 07:27
@kklimonda-cl kklimonda-cl force-pushed the feat-generate-datasource-structs branch from 905abdc to b8fc6f5 Compare July 29, 2024 07:42
@kklimonda-cl kklimonda-cl merged commit ccf9ac2 into feat-render-resource-second-part Jul 29, 2024
2 checks passed
@kklimonda-cl kklimonda-cl deleted the feat-generate-datasource-structs branch July 29, 2024 15:20
migara pushed a commit that referenced this pull request Aug 8, 2024
* Update codegen config and add SDK imports

The codegen config.yaml has been updated to include file sections for SDK and Terraform Provider. Additional SDK imports have been added to handle resources with hashicorp/terraform-plugin-framework. Also, commenting out the code block invoking `GenerateTerraformDataSource` is part of this commit. Moreover, a new `tools.go` file has been created under the assets/provider directory.

* Update code generation and add provider configuration

Updated code generation for both Terraform resources and data sources. Added configuration details for the Terraform provider. Also, other minor modifications which include adding new files, file renaming, and updating imports in several files have been made as well.

* Remove unused dependencies

* Refactor code to improve code clarity and naming conventions

Various changes were committed across multiple files to improve the code base's cleanliness and readability. Main changes included refactoring naming conventions in the 'funcs.go' file, providing clearer and more descriptive names. Moreover, an unused import in 'tfid.go' was cleaned up, and other minor reformatting actions were performed to enhance code alignment and consistency.

* Extend example to create device group

* Extend template and template stack spec

* Add TfidStruct function and location encoding

A TfidStruct function was added to generate tfid structures through a function in terraform_provider/template.go. This function includes both Name and Location fields in the tfid struct. Also, EncodeLocation and DecodeLocation functions were introduced in provider_internal/tools.go to handle encoding and decoding of Locationer objects. These changes pave the way for better structure generation and data handling.

* Enhance Terraform provider translation and model generation

* Refactor Terraform provider template generation code

The updated code improves the generation of nested struct templates and condenses redundant code in the GenerateTerraformResource function. It introduces a helper function to create nested structures for handling parameter specifications. The revised process improves code efficiency and readability.

* Added debugging messages and refactored nested structure handling in terraform_provider

This commit adds several debugging messages to improve visibility during the nested structure handling in terraform_provider. It also refactors the handling of 'Params' and 'OneOf' attributes of the SpecParam object to reduce duplicated code and optimize operations.

* Refactor code for terraform provider and add related tests

This commit includes significant refactoring of the terraform provider related code for better readability and maintainability. This includes changes in the template structure, addition of utility functions and adjusting the existing functions accordingly. It also includes the introduction of unit tests for these functionalities.

* Extend Terraform provider translation with location attributes

Code changes extend the current functionality of the Terraform provider translation used for generating commit messages. New parameters were added mainly to handle attributes related to resource location, including device group, vsys, and shared. These changes facilitate better management of provider resources. Additional import statements were also added as required to support the new implementation. The ParamToModel function was renamed to ParamToModelBasic for clarity and consistency. A new helper function was introduced to combine two existing maps. Code was adjusted where necessary to accommodate these changes, to maintain logical consistency and coherence.

* Enhance code formatting and adjust schema descriptions

This commit improves code readability by implementing automated code formatting in the generation process. It also enhances schema descriptions by enclosing variables in backticks for clarity. Additionally, support for item length in provider parameters has been added. Several import statements were also included for newly introduced packages.

* Refactor code for template and file processing in generator

Code related to generating templates and writing to files has been refactored to improve readability and efficiency. Redundant code has been consolidated into new helper functions processTemplate and writeFormattedContentToFile, reducing complexity and improving maintainability. Comment descriptions have also been corrected for clarity and consistency.

* Update generator and provider to support dynamic struct creation

The update involves changes in the generator code and the terraform provider to support dynamic creation of location structs for varying types. The generated struct will depend on the fields of the type passed to the generator. The code also modifies client type from `*pango.XmlApiClient` to `*pango.Client` across multiple files. This update enhances flexibility and coherence of the code.

* Remove unused dependencies from go.mod and go.sum

The commit involved the removal of unused dependencies from our go.mod and go.sum files. This cleanup makes the project lighter and reduces the potential attack surface given by unused packages. It also potentially improves build times and makes dependency management simpler.

* Improve structure templates and format for Terraform provider code generation

This commit optimizes the code generation component for Terraform providers. Changes include injecting additional functions to handle resource creation, reading, updating, and deletion. The handling of Go types to Terraform types has been moved to a separate helper function. Code formatting has been temporarily commented out, and will be reinstated after tests.

* Refactor template execution and enhance Terraform provider generation

This update refactors the execution of various code templates for readability and maintainability. It also enriches the Terraform provider file generation mechanism to better handle different resource types and specifications. Furthermore, the update ensures that the generated Go code is formatted correctly, and it fine-tunes the logic for copying assets based on the command type.

* Update struct types and functions in terraform provider

Multiple struct types are renamed and added to translate structs in the `terraform_provider` package. The changes affect the `struct.go`, `terraform_provider_file.go` and `template.go` files. Also added instructions in `README.md` file for using the provider locally. Changes also include debugging information in `terraform_provider_file.go` and function renaming/updates in several places in the code.

* Fixed schema template

* Remove duplicated attribute 'location' from schemas.

We are already including location via ResourceSchemaLocationAttribute
function so there is no need to list it separately.

* Fix generated device_group.go in terraform provider

* Fix failing checks, adjust log messages

* Update to pango.Client

* Fix compilation errors

Partially done by disabling unsupported features

* Dependencies update

* Remove obsolete provider assets

* Some initial work on nested spec copy

* Fix generation of CopyFromTerraformToPango functions

* Rename templates consts

* Fix how we get SDK package name

* Fixes to the nested function calls

* Handle nested lists of objects when copying from tf to pango

* Generate structs for lists of objects

* Implement CopyToPango() on all terraform models and objects

This replaces existing CopyFromTerraformToPango* functions.

* Fix copying of lists with object item types

* Add an incomplete implementations of CopyFromPango functions

* Update go.mod and go.sum

* Fix tests for terraform struct generation

* feat: Added generation of location-related code across all terraform templates (#118)

* feat: Generate Terraform DataSource structures (#120)

* Create missing code for handling NTP, DNS and other configuration

* Add more support for copying lists between pango and terraform

* Properly create local state when reading from PAN-OS

* Add support for updating resources

* Generate SpecMatches and Update code for configs

* Update terraform codegen for configs to make update work

* Make encrypted values working correctly for the "solo" case

* Implement reading of DataSource objects

* Generate default values for resource schema

* Fix lists of nested attributes

* When checking if generated file is empty, ignore white spaces

* Fix errors reported by golangci-lint

* Fix unmarshaling/marshaling cycle tests

---------

Co-authored-by: Sebastian Czech <sczech@paloaltonetworks.com>
Co-authored-by: michalbil <mbilski@paloaltonetworks.com>
Co-authored-by: Krzysztof Klimonda <kklimonda@paloaltonetworks.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants