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

SDK: Strongly-Typed Data Sources & Resources #9421

Merged
merged 14 commits into from
Nov 25, 2020
Merged

Conversation

tombuildsstuff
Copy link
Contributor

This PR introduces an SDK for strongly-typed Data Sources and Resources - allowing the use of native Go types for creating Terraform Resources.


A quick note to passers-by: this is more of a prototype than ready for production usage, but will likely form the foundation for Terraform Resources going forward.


There's several components to this Pull Request:

  1. Introduction of "Typed" Data Sources and Resources

    • Introduces the "DataSource" and "Resource" interfaces which remove the ambiguous use of interface{} in favour of typed objects - meaning we can rely on higher-level types rather than casting in every resource.
    • A shim layer exists between the Terraform Plugin SDK and these resources, meaning we can lean on each as it's useful (for example, Tooling can build on top of the "DataSource" and "Resource" interfaces, rather than assuming the implementation)
    • This allows a significant amount of code reduction - introducing some Conventions to do so (for example, automatically calling Read after Create/Update, ensuring that Contexts are automatically configured wrapped with a deadline)
  2. Introduction of Encode and Decode functions - to enable encoding/decoding values from the Terraform State from/to a native Go struct

    • Ultimately this allows for hcl tags to be defined twice (in the schema and the Go struct) - rather than 5/6 times at present (one time per schema, and once per CRUD method)
    • This will enable us (in a follow up PR/commit) to enable unit testing to catch type/serialization errors by confirming that the hcl values in the struct tags exist in the schema
    • This also allows us to lean more heavily on the compiler and tooling (such as refactoring tooling) rather than find+replacing magic-strings.
  3. Fixing up the Provider Registration and associated Tooling to support both Typed (new) and Untyped (existing) resources

Notably the Typed Resources which are being introduced enable falling back to mutating ResourceData directly (for example to call HasChanges in an update function) - so this is a wrapper rather than an outright replacement.

For the purposes of completeness I've reimplemented the Resource Group resource using this new SDK (you can find it in the Readme, I've pulled it out of the history) and the tests for that resource pass 👍🏻


As mentioned above, at this point in time this is still a prototype, so whilst there's a bunch of tests there's almost certainly some bugs too - as such new functionality should still be built using the Terraform Plugin SDK directly at this time.

As such whilst we don't plan to accept external PR's using this new SDK at this time - we're hoping to enable doing so in the coming weeks - at the same time we'll look to add more documentation to solve #2688 too.

tombuildsstuff and others added 7 commits November 23, 2020 13:46
This commit introduces support for strongly-typed Data Sources and Resources.

Ultimately this allows us to use native Go objects rather than `d.Get` and `d.Set`
to parse values from/back to the State. This also allows the Data Sources and
Resources themselves to become Typed - meaning we can lean more heavily on the
compiler and unit tests to catch errors.

Co-authored-by: Matthew Frahry <[email protected]>
This allows them to be mixed and matched

Co-authored-by: Matthew Frahry <[email protected]>
Copy link
Member

@jackofallops jackofallops left a comment

Choose a reason for hiding this comment

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

Hi @tombuildsstuff / @mbfrahry - Couple really minor comments, but otherwise LGTM 👍

azurerm/internal/sdk/resource_encode.go Outdated Show resolved Hide resolved
@tombuildsstuff tombuildsstuff merged commit 32fc497 into master Nov 25, 2020
@tombuildsstuff tombuildsstuff deleted the f-resource-wrapper branch November 25, 2020 18:26
@ghost
Copy link

ghost commented Nov 27, 2020

This has been released in version 2.38.0 of the provider. Please see the Terraform documentation on provider versioning or reach out if you need any assistance upgrading. As an example:

provider "azurerm" {
    version = "~> 2.38.0"
}
# ... other configuration ...

@ghost
Copy link

ghost commented Dec 26, 2020

I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues.

If you feel this issue should be reopened, we encourage creating a new issue linking back to this one for added context. If you feel I made an error 🤖 🙉 , please reach out to my human friends 👉 [email protected]. Thanks!

@ghost ghost locked as resolved and limited conversation to collaborators Dec 26, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants