-
Notifications
You must be signed in to change notification settings - Fork 20
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: use a custom diff + new id
attr on record attrs to allow using them with for_each
#27
Conversation
@wgebis any chance of a review? |
Hello there! Thanks for your contribution! Could you provide unit test impl in order to validate if modified resources work with |
👋 hiya! I'm not that experienced with writing tests for Terraform providers, and it also seems like the I'm happy to explore options, but would need some help :) |
@G-Rath I have just pushed to master upgrade of go, terraform sdk and Mailgun API, could you sync your branch with master and run |
…g them with `for_each`
@wgebis here you go:
I've also confirmed it worked using Route53
|
I just realized that it isn’t backward compatible 😔. Is any chance to achieve it via TypeList? |
@wgebis not as far as I know (and I did try a few ways before going with this one to get the smallest change possible). Given that youre on v0.x doing a semi-breaking change should be ok so long as it's documented right? |
Right, but i’m wondering if it possible to upgrade your hcl resource from List to Set without touching external resources. Do you have any idea how to do this? |
e.g. how to convert resources via
|
I don't know off the top of my head, but you'll always be able to I actually feel like it's not stable to use a |
Yes it's true, but it could be super annoying that after provider upgrade you have to refactor a lot of objects. So let's validate other possibilities. OK, so give me a feedback how |
I'm aware of limitation of |
BTW: From my perspective I recommend to keep all stuff related to Mailgun domain in the module e.g. below. It's super clean and might be used easily reused. An example of a module definition with CF as DNS provider:
usage:
|
It's not good to rely on I've confirmed you can easily move the resources with
You do have to figure out which index in the list holds what unique item (which is why using a set is a better type here):
|
Thanks for info. I restored you branch to my repo and will make some more tests locally. #35 |
Just added you as a collaborator, feel free to make changes in this repo directly. |
For now leave it for me, I will make some local tests. If I decide to go step further with this PR, some description about dealing with braking changes will be necessary to put in the resource documentation, so I will let you know. |
I have one question in terms of your snipped:
Why just not use e.g. md5(record.value) instead of computed Still this workaround is necessary for
|
It makes it a bit easier to identify the resources at a glance, e.g.
vs
and
The information is technically still in the state elsewhere (i.e. the ID of the |
@wgebis have you had a chance to look back over this? it'd be great to get landed and released |
@G-Rath I'm not sure it's worth of breaking the provider backward compatibility. You can handle This PR includes a logic where a core is in fact calculating the IDs of resources, don't you think? |
@wgebis using
Gives
While you might be able to use
I'm not sure what you mean by this, but the fact that the provider is calculating the ID of resources is the key part of this - that provides Terraform with a set of static values that it can use in the static plan phase, and then fill out the rest in the dynamic compute/apply phase. But look what if we just introduced this as new attributes i.e. I do think in the long-run though the current list-based attributes should be deprecated and replaced with the set version because its more accurate and fits with expectations based on other providers (i.e. this all started with us assuming we can use the same structure as we can for resources like |
Hi!
Yep, I got the same result when tested code from this PR. It's because total objects for for_each elements are now known before creation
Yeah, it definitely make sense 👍. So, Feel free to make these changes, you should have permissions to push directly to this repo to a new branch. 👍 |
Because of the way Terraform works, you currently cannot use
sending_records
orreceiving_records
in afor_each
as they're an arbitrary list set by the API response.Attempting to do so will cause an error:
This is annoying because it means we have to manually setup DNS records in e.g. CloudFlare despite having a Terraform provider that lets us create DNS records 😭
There are two requirements that must be meet for Terraform to be able to plan the above:
That is what this PR attempts to do, working off the fact that we know the number of records that we'll be provided by the API (3, for
sending_records
), and what those record names actually are... almost.The edge case we have to handle is that one of the three records (the
_domainkey
record) we expect has a random prefix to it, which means we cannot usename
itself as the field to compute the stableid
for the Set, since that will result in at best an unstable diff.We work around by adding a new
id
attribute to thesending_records
map, which is effectively a normalised version of each records name - which is to say for two of the three records it is their name, but for the_domainkey
record we drop the random prefix.This allows us to do this:
This was shamelessly ripped off the
aws_acm_certificate
resource, which does almost the same thing using thedomain_name
(related PR hashicorp/terraform-provider-aws#14199)I'm opening this as a draft for now as I'm cleaning up the code and still have to apply it to the receiving records attribute, but wanted to try and get a review going especially becauseI don't know how well I'll be able to actually write tests for this 😬I can say that I've tested this a bunch locally, and I believe if MailGun were to suddenly add a new record that shouldn't cause the provider to fall over as a result of this change - especially since we're always setting an ID, so if anything breaks it would be
for_each
s which are already broken right now anyway so this would still be better than what happens currently 😅