-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
Give addresses beta support, add internal addresses #594
Conversation
8f20cb0
to
29924b4
Compare
Ping. Is there anything I can do to help get this merged? :) |
29924b4
to
b9c8a56
Compare
project, region, addr).Do() | ||
v0BetaAddress := &computeBeta.Address{ | ||
Name: d.Get("name").(string), | ||
AddressType: d.Get("address_type").(string), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Echoing @rosbo's comment: we should explicitly document (or use Default
to set) the default value here.
Source: https://github.com/terraform-providers/terraform-provider-google/pull/488/files#r142192254
google/resource_compute_address.go
Outdated
return handleNotFoundError(err, d, fmt.Sprintf("Address %q", d.Get("name").(string))) | ||
} | ||
|
||
err = Convert(v0BetaAddr, addr) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@rosbo mentioned we don't actually need to convert here: https://github.com/terraform-providers/terraform-provider-google/pull/488/files#r142192681
b9c8a56
to
5b7a769
Compare
@paddycarver Thanks for the review! Please take another look - comments should be addressed. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like we're almost there, @negz! Thanks for all the work on this. It looks like a couple tests are failing now. I think we need:
- to set address_type on read, so import works and we can detect drift
- to convert the self_link to v1, so the import test passes and we have consistent self_links: https://github.com/terraform-providers/terraform-provider-google/blob/57449a88a24bdfa070b0e3dbb6da99d82b092678/google/resource_compute_instance.go#L971
5b7a769
to
0b184c4
Compare
@paddycarver Whoops, my mistake! Should have run all the tests. I believe I've corrected everything you mentioned, but I added a new test
It seems as if we're trying to use the Any idea how I can get this test to pass? |
Update See an example here: |
0b184c4
to
1e2ca95
Compare
@rosbo Your suggested change makes sense, thanks. I found that the test still failed after setting the default value for Without having dug into the mechanics of how Terraform imports work I had assumed something under the hood was triggering a read subsequent to the import function being called to determine the resource's ID, but it seems that's not the case? I believe all integration tests matching |
I'm :/ about explicitly calling Read, as that should be unnecessary, and is a bit of a weird situation--it should be automatically Read after calling Import, anyways, I believe? I looked into this some, and can't explain the behaviour--without your fix, I can't find anything that would set |
@paddycarver Thanks for following up - glad I wasn't just missing something obvious. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I found why the additional call to the read method inside the import method makes the test pass.
The read method takes a *schema.ResourceData
as an argument often called d
. d
stores, among other things, the attributes values and the id. The read method IS called automatically after the import method is called. However, the ResourceData passed as an argument to the read method has an empty attributes map and only the id is set. Inside the read method, we check whether we should use the v1 or the beta api. Since the attribute address_type
is not set (remember, the attributes map in d
is empty), it calls the v1 API. The v1 API does not set the address_type
and it therefor defaults to EXTERNAL
.
The extra call to read inside the import method means that the read method is now called twice. The second time the read method is called the attributes values are set (the first call to read sets them). However, the address_type
was set to EXTERNAL
so it shouldn't trigger the beta API right? That is correct, the reason why the beta API is called is because of a bug introduced in this PR. The address
field is marked as a beta field. The field is computed. If the user does not specify an address
field, we put the value generated by the server. This means address
is always set which means that the beta API is always used even if the user does not set the address
field.
A few things to fix:
- the address field should not be marked as
beta
. The field is present in the v1 API. - open question: How to detect that the import method should use the beta API... I need to think more about a solution for this.
Does that mean that import is broken for any beta resource?
Yes, but running terraform apply fixes it. However, if the beta field is ForceNew
, this is bad because the newly imported resource gets recreated!
Let's say you want to import the following resource:
resource "google_compute_address" "test" {
name = "test"
address_type = "INTERNAL"
subnetwork = "https://www.googleapis.com/compute/v1/projects/my-project/regions/us-central1/subnetworks/default"
}
When you run terraform import google_compute_address.test test
terraform show
hasaddress_type
set toEXTERNAL
(this is wrong!)terraform plan
shows a diff.terraform apply
will fix the diff by either calling update or create (depending on ForceNew value for the field) and the state should be properly set afterwards.
func TestAccComputeAddress_importInternal(t *testing.T) { | ||
t.Parallel() | ||
|
||
resourceName := "google_compute_address.internal_with_subnet" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
testAccComputeAddress_internal
creates 3 internal addresses.
I would add 2 extra test steps below to test that the import worked properly for the resource google_compute_address.internal
and internal_with_subnet_and_address
too. These checks are cheap and the resources gets created anyway as of now.
Update the `google_compute_address` resource to take advantage of differing API versions. Use this new functionality to support internal reserved IP addresses. Fixes hashicorp#366.
Note I decided to delete rather than generalise the DEBUG logline. I suspect it was left in by mistake.
This avoids naming collisions when running multiple tests in parallel.
In order to inform Terraform to use the beta API when the address_type is set to any value beside EXTERNAL.
This allows us to correctly import beta addresses
85fca29
to
ab28b39
Compare
@rosbo I've removed Given that we've discovered imports are broken for any beta resources, how would you feel merging this PR (perhaps without the internal import acceptance tests?) and addressing the beta import issue separately? |
This sounds reasonable to me. Please comment out the import acceptance tests and a quick comment explaining why the test is commented-out. Thanks |
Thanks for filling the new issue :) |
Pending resolution of hashicorp#694
@rosbo I've commented out the offending tests:
|
) <!-- This change is generated by MagicModules. --> Original Author: @rileykarson
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! |
This PR (hopefully) ties together the existing work in #488. Specifically it:
I haven't contributed to Terraform before, but I'm pretty sure this works as expected: