-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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 support for google_compute_project_metadata_item #176
Add support for google_compute_project_metadata_item #176
Conversation
Oops, forgot to |
6ebf6ec
to
f60453f
Compare
@@ -8,7 +8,10 @@ description: |- | |||
|
|||
# google\_compute\_project\_metadata | |||
|
|||
Manages metadata common to all instances for a project in GCE. | |||
Manages metadata common to all instances for a project in GCE. If you |
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.
What if we state that this resource authoritatively manages all of the project's metadata before we explain google_compute_project_metadata_item
as an alternative? That way it will be more clear what the potential gotchas with using this resource are, and how the alternative solves them.
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 almost documented exactly this, but unfortunately there's a bug right now in how google_compute_project_metadata
works; right now it works similar to this PR, except upon delete (which deletes the entire map). We have a work item to go fix this so that it manages the entire metadata, but until then I'm hesitant to document that behavior (hence the current carefully worded description).
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.
SGTM then 👍
Manages metadata common to all instances for a project in GCE. | ||
Manages metadata common to all instances for a project in GCE. If you | ||
want to manage only single key/value pairs within the project metadata | ||
rather than the entire map, then use |
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.
Is it being a map an implementation detail? We describe it as a series below in the metadata
field docs, for example.
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.
Hmm, good point. I see in the documentation about it being a set of metadata field docs, but nowhere about it being a map. Will update.
google/provider.go
Outdated
@@ -286,3 +287,28 @@ func linkDiffSuppress(k, old, new string, d *schema.ResourceData) bool { | |||
} | |||
return false | |||
} | |||
|
|||
func computeMetadataToMap(metadata []*compute.MetadataItems) map[string]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.
We can name these functions flattenComputeMetadata (computeMetadataToMap) and expandComputeMetadata (mapToComputeMetadata) to match similar functions like in google_compute_backend_service
.
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 can do that for consistency's sake, but I am not sure the naming is improved.
State: schema.ImportStatePassthrough, | ||
}, | ||
|
||
SchemaVersion: 0, |
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.
We don't need to set a SchemaVersion until we perform a migration.
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.
Will remove
c4e7a03
to
407a312
Compare
}, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"project": { |
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.
nit: typically we put all required elements before all optional ones
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.
Fixed
return nil | ||
} | ||
|
||
func updateComputeCommonInstanceMetadata(config *Config, projectID string, key string, beforeVal *string, afterVal *string) error { |
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.
why *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.
Oh is it because of differentiating between unset and set to the empty string? If so can you add a comment for clarity?
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.
Yes; will do
(ps do we have a maybe type or something better than *string
)?
return fmt.Errorf("Error loading project '%s': %s", projectID, err) | ||
} | ||
|
||
// Should the ID contain the project name? |
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.
which id?
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.
This was supposed to have a TODO/NOTE in front of it but i foolishly forgot here - sorry, removing.
// Asked to set no value and we didn't find one - we're done | ||
return nil | ||
} | ||
if beforeVal != nil { |
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.
So the only example I can think of for us actually reaching this case is on a race condition where the key is deleted between the refresh and the apply. Either way, I'm not sure we necessarily need to error on it- if this is an update we can just add the new value, and if it's a delete we can just exit. Is there a different case you can think of?
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.
When I wrote this, it wasn't clear to me when a refresh occurred. Does it always occur before a write?
If so, then the race condition you listed above is the only one I can think of. If that's not worth checking then I'm not sure it's worth enforcing a beforeVal
at all, right?
}) | ||
} | ||
|
||
func TestAccComputeProjectMetadataItem_basicImport(t *testing.T) { |
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 don't have a good justification for this, but we tend to put import tests in their own file (and for consistency's sake it would be called [...]_importBasic)
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.
OK, will do
--- | ||
layout: "google" | ||
page_title: "Google: google_compute_project_metadata_item" | ||
sidebar_current: "docs-google-compute-project-metadata_item" |
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 think that last _
should be a -
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.
AGH, fixed
|
||
```hcl | ||
resource "google_compute_project_metadata_item" "default" { | ||
key: "my_metadata" |
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.
:
should be =
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.
Fixed
google/provider.go
Outdated
@@ -297,3 +298,39 @@ func convertStringArr(ifaceArr []interface{}) []string { | |||
} | |||
return arr | |||
} | |||
|
|||
// flattenComputeMetadata transforms a list of MetadataItems (as returned via the GCP client) into a simple map from key |
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.
This probably belongs either in the resource file itself or in metadata.go (generally flatten/expand functions are in the same file as the rest of the resource though)
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 moved it to metadata.go as it works with both project metadata and instance metadata
google/provider.go
Outdated
if val.Value == nil { | ||
continue | ||
} | ||
if v2, ok := m[val.Key]; ok { |
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.
nit: should v2
just be v
?
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.
Probably, although val
should have a better name too. Will fix names.
google/provider.go
Outdated
|
||
idx := 0 | ||
for key, value := range m { | ||
metadata[idx] = &compute.MetadataItems{Key: key, Value: &value} |
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.
idx++
? (you can also do metadata = append(metadata, &compute.MetadataItems{Key: key, Value: &value})
. I think since the backing array won't need to be resized, it should have the same performance but I'm not 100% sure.)
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 am undone
will fix
407a312
to
f7b6c8f
Compare
f7b6c8f
to
6ecfd95
Compare
👍 |
This allows terraform users to manage single key/value items within the project metadata map, rather than the entire map itself.
6ecfd95
to
0b50bd5
Compare
* Revert "Add additional fingerprint error to check for when updating metadata (#221)" This reverts commit 4c8f62e. * Revert "Fix bug where range variable is improperly dereferenced (#217)" This reverts commit 8f75c1c. * Revert "Add support for google_compute_project_metadata_item (#176)" This reverts commit 236c0f5.
* Add support for google_compute_project_metadata_item This allows terraform users to manage single key/value items within the project metadata map, rather than the entire map itself. * Update CHANGELOG.md * Add details about import
* Revert "Add additional fingerprint error to check for when updating metadata (hashicorp#221)" This reverts commit 4c8f62e. * Revert "Fix bug where range variable is improperly dereferenced (hashicorp#217)" This reverts commit 8f75c1c. * Revert "Add support for google_compute_project_metadata_item (hashicorp#176)" This reverts commit 236c0f5.
* Add support for google_compute_project_metadata_item This allows terraform users to manage single key/value items within the project metadata map, rather than the entire map itself. * Update CHANGELOG.md * Add details about import
* Revert "Add additional fingerprint error to check for when updating metadata (hashicorp#221)" This reverts commit 4c8f62e. * Revert "Fix bug where range variable is improperly dereferenced (hashicorp#217)" This reverts commit 8f75c1c. * Revert "Add support for google_compute_project_metadata_item (hashicorp#176)" This reverts commit 236c0f5.
Accommodate the new iamcredentials service in tests
Accommodate the new iamcredentials service in tests
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 👉 hashibot-feedback@hashicorp.com. Thanks! |
This allows terraform users to manage single key/value items within the
project metadata map, rather than the entire map itself.