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

Add basic update for google_kms_crypto_key resource #1511

Merged
merged 6 commits into from
May 30, 2018
Merged

Add basic update for google_kms_crypto_key resource #1511

merged 6 commits into from
May 30, 2018

Conversation

MrSaints
Copy link
Contributor

Prior to this commit, any changes to rotation_period would
force a new resource as no Update was defined for the resource.
This commit introduces a basic Update through calling the
Patch service method. It only modifies the rotation_period,
and next_rotation_time at the moment, but this is reflective
of what is "allowed" on https://console.cloud.google.com/security/kms.

Prior to this commit, any changes to `rotation_period` would
force a new resource as no `Update` was defined for the resource.
This commit introduces a basic `Update` through calling the
`Patch` service method. It only modifies the `rotation_period`,
and `next_rotation_time` at the moment, but this is reflective
of what is "allowed" on https://console.cloud.google.com/security/kms.

log.Printf("[DEBUG] Updated CryptoKey %s", cryptoKey.Name)

d.SetId(cryptoKeyId.cryptoKeyId())
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't think this is entirely necessary (?), but I see this being done across various Terraform providers, so I suppose it can add some guarantee.

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah, unless you think it's possible that the Id could change on an update (which in this case, it can't) that doesn't really do anything. I usually don't put it in, but I also don't tell people they have to take it out.

@danawillow danawillow self-requested a review May 25, 2018 00:07
Copy link
Contributor

@danawillow danawillow left a comment

Choose a reason for hiding this comment

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

Thanks @MrSaints! This looks great- just a few comments/questions.

return err
}

key := cloudkms.CryptoKey{Purpose: "ENCRYPT_DECRYPT"}
Copy link
Contributor

Choose a reason for hiding this comment

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

Just curious- since it's a PATCH API, do we actually need to include the purpose here since it isn't changing?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good point - removing.


key := cloudkms.CryptoKey{Purpose: "ENCRYPT_DECRYPT"}

if d.HasChange("rotation_period") && d.Get("rotation_period") != "" {
Copy link
Contributor

Choose a reason for hiding this comment

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

Hmm, what should the behavior be if someone changes the rotation_period from something to nothing?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

In theory, d.Get("rotation_period") will never be an empty string due to validation.
So if we set rotation_period = "", it will fail validation, but that is the master behaviour. If we remove it completely, this block will not execute, but since we included an update mask, it will use the zero value of the struct, so it will end up setting the rotation period to Never. From my understanding, that would've been the case if it was not defined on Create.

Copy link
Contributor

Choose a reason for hiding this comment

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

Oh, you're totally right- I missed the update mask. Thanks!

cryptoKeyId.cryptoKeyId(),
&key,
).
UpdateMask("rotation_period,next_rotation_time").
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: We don't have any line-length limitations here, so we tend to go for the "just put it all on one line" style. If that makes you cringe, maybe just put it on two lines or something like that.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

👍


log.Printf("[DEBUG] Updated CryptoKey %s", cryptoKey.Name)

d.SetId(cryptoKeyId.cryptoKeyId())
Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah, unless you think it's possible that the Id could change on an update (which in this case, it can't) that doesn't really do anything. I usually don't put it in, but I also don't tell people they have to take it out.

@@ -111,6 +112,46 @@ func resourceKmsCryptoKeyCreate(d *schema.ResourceData, meta interface{}) error
return resourceKmsCryptoKeyRead(d, meta)
}

func resourceKmsCryptoKeyUpdate(d *schema.ResourceData, meta interface{}) error {
Copy link
Contributor

Choose a reason for hiding this comment

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

Mind adding a test (or adding a step into an existing test) in resource_kms_crypto_key_test.go that does an update?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Will do

MrSaints added 3 commits May 25, 2018 10:03
We are only patching the `rotation_period`, and `next_rotation_time`,
so that value will not be affected.
- Test change in rotation period
- Test removal of rotation period
@MrSaints
Copy link
Contributor Author

Cheers for the CR @danawillow. I've made the relevant changes.

@danawillow
Copy link
Contributor

The test looks like it's failing:

=== RUN   TestAccKmsCryptoKey_rotation
--- FAIL: TestAccKmsCryptoKey_rotation (60.70s)
    testing.go:513: Step 2 error: Check failed: Check 2/2 error: Failed to parse NextRotationTime timestamp: parsing time "" as "2006-01-02T15:04:05.999999999Z07:00": cannot parse "" as "2006"
FAIL

@MrSaints
Copy link
Contributor Author

@danawillow I'll have a look. Strange that Travis CI is passing. Are those tests executed in Travis CI?

@danawillow
Copy link
Contributor

No, Travis just does unit tests. Since the acceptance tests create actual cloud resources, we want to be in a bit more control of when they get run.

@MrSaints
Copy link
Contributor Author

Unfortunately, I can't currently run the acceptance tests locally, but I've pushed a quick fix for it. Considering NextRotationTime can be empty, I reckon it is not too ridiculous to shortcircuit early.

@danawillow
Copy link
Contributor

Thanks, it's passing now!

I pushed a quick change to your branch to get rid of the ForceNew line altogether (we usually don't bother setting it to false. I figured this was faster than another round of back-and-forth just for the one thing). Merging now, thanks for the contribution!

@danawillow danawillow merged commit b4be2fa into hashicorp:master May 30, 2018
@MrSaints
Copy link
Contributor Author

Thanks a lot @danawillow! I greatly appreciate it 💯

@MrSaints MrSaints deleted the resource-kms-crypto-key-add-update branch May 30, 2018 01:09
@ghost
Copy link

ghost commented Nov 18, 2018

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 and limited conversation to collaborators Nov 18, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants