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 support for google_compute_project_metadata_item #176

Merged
merged 3 commits into from
Jul 17, 2017

Conversation

selmanj
Copy link
Contributor

@selmanj selmanj commented Jul 5, 2017

This allows terraform users to manage single key/value items within the
project metadata map, rather than the entire map itself.

@selmanj
Copy link
Contributor Author

selmanj commented Jul 5, 2017

Oops, forgot to gofmt

@selmanj selmanj force-pushed the add_fine_grained_project_metadata branch 2 times, most recently from 6ebf6ec to f60453f Compare July 5, 2017 19:55
@@ -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
Copy link
Collaborator

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.

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 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).

Copy link
Collaborator

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
Copy link
Collaborator

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.

Copy link
Contributor Author

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.

@@ -286,3 +287,28 @@ func linkDiffSuppress(k, old, new string, d *schema.ResourceData) bool {
}
return false
}

func computeMetadataToMap(metadata []*compute.MetadataItems) map[string]string {
Copy link
Collaborator

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.

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 can do that for consistency's sake, but I am not sure the naming is improved.

State: schema.ImportStatePassthrough,
},

SchemaVersion: 0,
Copy link
Collaborator

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.

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 remove

@selmanj selmanj force-pushed the add_fine_grained_project_metadata branch 3 times, most recently from c4e7a03 to 407a312 Compare July 6, 2017 18:27
},

Schema: map[string]*schema.Schema{
"project": {
Copy link
Contributor

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

Copy link
Contributor Author

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 {
Copy link
Contributor

Choose a reason for hiding this comment

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

why *string?

Copy link
Contributor

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?

Copy link
Contributor Author

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?
Copy link
Contributor

Choose a reason for hiding this comment

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

which id?

Copy link
Contributor Author

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 {
Copy link
Contributor

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?

Copy link
Contributor Author

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) {
Copy link
Contributor

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)

Copy link
Contributor Author

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"
Copy link
Contributor

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 -

Copy link
Contributor Author

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"
Copy link
Contributor

Choose a reason for hiding this comment

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

: should be =

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed

@@ -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
Copy link
Contributor

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)

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 moved it to metadata.go as it works with both project metadata and instance metadata

if val.Value == nil {
continue
}
if v2, ok := m[val.Key]; ok {
Copy link
Contributor

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?

Copy link
Contributor Author

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.


idx := 0
for key, value := range m {
metadata[idx] = &compute.MetadataItems{Key: key, Value: &value}
Copy link
Contributor

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.)

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 am undone

will fix

@danawillow
Copy link
Contributor

make testacc TEST=./google TESTARGS='-run=TestAccComputeProjectMetadataItem'
==> Checking that code complies with gofmt requirements...
TF_ACC=1 go test ./google -v -run=TestAccComputeProjectMetadataItem -timeout 120m
=== RUN   TestAccComputeProjectMetadataItem_importBasic
--- PASS: TestAccComputeProjectMetadataItem_importBasic (38.07s)
=== RUN   TestAccComputeProjectMetadataItem_basic
--- PASS: TestAccComputeProjectMetadataItem_basic (22.98s)
=== RUN   TestAccComputeProjectMetadataItem_basicWithEmptyVal
--- PASS: TestAccComputeProjectMetadataItem_basicWithEmptyVal (22.58s)
=== RUN   TestAccComputeProjectMetadataItem_basicUpdate
--- PASS: TestAccComputeProjectMetadataItem_basicUpdate (36.65s)
PASS
ok  	github.com/terraform-providers/terraform-provider-google/google	120.403s

👍

This allows terraform users to manage single key/value items within the
project metadata map, rather than the entire map itself.
@selmanj selmanj force-pushed the add_fine_grained_project_metadata branch from 6ecfd95 to 0b50bd5 Compare July 17, 2017 23:28
@selmanj selmanj merged commit 236c0f5 into hashicorp:master Jul 17, 2017
@selmanj selmanj deleted the add_fine_grained_project_metadata branch July 17, 2017 23:28
danawillow added a commit to danawillow/terraform-provider-google that referenced this pull request Jul 20, 2017
danawillow added a commit that referenced this pull request Jul 20, 2017
* 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.
@selmanj selmanj restored the add_fine_grained_project_metadata branch September 17, 2017 19:49
negz pushed a commit to negz/terraform-provider-google that referenced this pull request Oct 17, 2017
* 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
negz pushed a commit to negz/terraform-provider-google that referenced this pull request Oct 17, 2017
* 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.
chrisst pushed a commit to chrisst/terraform-provider-google that referenced this pull request Nov 9, 2018
* 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
chrisst pushed a commit to chrisst/terraform-provider-google that referenced this pull request Nov 9, 2018
* 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.
luis-silva pushed a commit to luis-silva/terraform-provider-google that referenced this pull request May 21, 2019
Accommodate the new iamcredentials service in tests
luis-silva pushed a commit to luis-silva/terraform-provider-google that referenced this pull request May 21, 2019
Accommodate the new iamcredentials service in tests
@ghost
Copy link

ghost commented Mar 31, 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 👉 hashibot-feedback@hashicorp.com. Thanks!

@ghost ghost locked and limited conversation to collaborators Mar 31, 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