From 84d824226907541a3047abe6e846e01e55c2581f Mon Sep 17 00:00:00 2001 From: emily Date: Wed, 29 May 2019 22:59:15 +0000 Subject: [PATCH] Fixes for nested query Signed-off-by: Modular Magician --- ...e_compute_backend_bucket_signed_url_key.go | 36 +++++--- ..._compute_backend_service_signed_url_key.go | 36 +++++--- google-beta/resource_resource_manager_lien.go | 88 +++++-------------- 3 files changed, 65 insertions(+), 95 deletions(-) diff --git a/google-beta/resource_compute_backend_bucket_signed_url_key.go b/google-beta/resource_compute_backend_bucket_signed_url_key.go index 4c1ab5add2..229637f1c0 100644 --- a/google-beta/resource_compute_backend_bucket_signed_url_key.go +++ b/google-beta/resource_compute_backend_bucket_signed_url_key.go @@ -256,32 +256,40 @@ func flattenNestedComputeBackendBucketSignedUrlKey(d *schema.ResourceData, meta return nil, nil } - // Final nested resource is either a list of resources we need to filter - // or just the resource itself, which we return. switch v.(type) { case []interface{}: break case map[string]interface{}: - return v.(map[string]interface{}), nil + // Construct list out of single nested resource + v = []interface{}{v} default: - return nil, fmt.Errorf("invalid value for cdnPolicy.signedUrlKeyNames: %v", v) + return nil, fmt.Errorf("expected list or map for value cdnPolicy.signedUrlKeyNames. Actual value: %v", v) } + expectedName, err := expandComputeBackendBucketSignedUrlKeyName(d.Get("name"), d, meta.(*Config)) + if err != nil { + return nil, err + } + + // Search list for this resource. items := v.([]interface{}) - for _, vRaw := range items { - // If only an id is given in parent resource, - // construct a resource map for that id KV pair. - item := map[string]interface{}{"keyName": vRaw} - itemIdV, err := expandComputeBackendBucketSignedUrlKeyName(d.Get("name"), d, meta.(*Config)) - if err != nil { - return nil, err + for _, itemRaw := range items { + if itemRaw == nil { + continue } - actualIdV := flattenComputeBackendBucketSignedUrlKeyName(item["keyName"], d) - log.Printf("[DEBUG] Checking if item's keyName (%#v) is equal to resource's (%#v)", itemIdV, actualIdV) - if !reflect.DeepEqual(itemIdV, actualIdV) { + // List response only contains the ID - construct a response object. + item := map[string]interface{}{ + "keyName": itemRaw, + } + + itemName := flattenComputeBackendBucketSignedUrlKeyName(item["keyName"], d) + if !reflect.DeepEqual(itemName, expectedName) { + log.Printf("[DEBUG] Skipping item with keyName= %#v, looking for %#v)", itemName, expectedName) continue } + log.Printf("[DEBUG] Found item for resource %q: %#v)", d.Id(), item) return item, nil } + return nil, nil } diff --git a/google-beta/resource_compute_backend_service_signed_url_key.go b/google-beta/resource_compute_backend_service_signed_url_key.go index 25bdf6b114..c997ee2304 100644 --- a/google-beta/resource_compute_backend_service_signed_url_key.go +++ b/google-beta/resource_compute_backend_service_signed_url_key.go @@ -256,32 +256,40 @@ func flattenNestedComputeBackendServiceSignedUrlKey(d *schema.ResourceData, meta return nil, nil } - // Final nested resource is either a list of resources we need to filter - // or just the resource itself, which we return. switch v.(type) { case []interface{}: break case map[string]interface{}: - return v.(map[string]interface{}), nil + // Construct list out of single nested resource + v = []interface{}{v} default: - return nil, fmt.Errorf("invalid value for cdnPolicy.signedUrlKeyNames: %v", v) + return nil, fmt.Errorf("expected list or map for value cdnPolicy.signedUrlKeyNames. Actual value: %v", v) } + expectedName, err := expandComputeBackendServiceSignedUrlKeyName(d.Get("name"), d, meta.(*Config)) + if err != nil { + return nil, err + } + + // Search list for this resource. items := v.([]interface{}) - for _, vRaw := range items { - // If only an id is given in parent resource, - // construct a resource map for that id KV pair. - item := map[string]interface{}{"keyName": vRaw} - itemIdV, err := expandComputeBackendServiceSignedUrlKeyName(d.Get("name"), d, meta.(*Config)) - if err != nil { - return nil, err + for _, itemRaw := range items { + if itemRaw == nil { + continue } - actualIdV := flattenComputeBackendServiceSignedUrlKeyName(item["keyName"], d) - log.Printf("[DEBUG] Checking if item's keyName (%#v) is equal to resource's (%#v)", itemIdV, actualIdV) - if !reflect.DeepEqual(itemIdV, actualIdV) { + // List response only contains the ID - construct a response object. + item := map[string]interface{}{ + "keyName": itemRaw, + } + + itemName := flattenComputeBackendServiceSignedUrlKeyName(item["keyName"], d) + if !reflect.DeepEqual(itemName, expectedName) { + log.Printf("[DEBUG] Skipping item with keyName= %#v, looking for %#v)", itemName, expectedName) continue } + log.Printf("[DEBUG] Found item for resource %q: %#v)", d.Id(), item) return item, nil } + return nil, nil } diff --git a/google-beta/resource_resource_manager_lien.go b/google-beta/resource_resource_manager_lien.go index cb22d7fd74..22e5876518 100644 --- a/google-beta/resource_resource_manager_lien.go +++ b/google-beta/resource_resource_manager_lien.go @@ -18,8 +18,6 @@ import ( "fmt" "log" "reflect" - "strconv" - "strings" "time" "github.com/hashicorp/terraform/helper/schema" @@ -290,84 +288,40 @@ func flattenNestedResourceManagerLien(d *schema.ResourceData, meta interface{}, return nil, nil } - // Final nested resource is either a list of resources we need to filter - // or just the resource itself, which we return. switch v.(type) { case []interface{}: break case map[string]interface{}: - return v.(map[string]interface{}), nil + // Construct list out of single nested resource + v = []interface{}{v} default: - return nil, fmt.Errorf("invalid value for liens: %v", v) + return nil, fmt.Errorf("expected list or map for value liens. Actual value: %v", v) } + expectedName := d.Get("name") + + // Search list for this resource. items := v.([]interface{}) - for _, vRaw := range items { - item := vRaw.(map[string]interface{}) - itemIdV := d.Get("name") - actualIdV := flattenResourceManagerLienName(item["name"], d) - log.Printf("[DEBUG] Checking if item's name (%#v) is equal to resource's (%#v)", itemIdV, actualIdV) - if !reflect.DeepEqual(itemIdV, actualIdV) { + for _, itemRaw := range items { + if itemRaw == nil { continue } - return item, nil - } - return nil, nil -} + item := itemRaw.(map[string]interface{}) -func resourceResourceManagerLienDecoder(d *schema.ResourceData, meta interface{}, res map[string]interface{}) (map[string]interface{}, error) { - // The problem we're trying to solve here is that this property is a Project, - // and there are a lot of ways to specify a Project, including the ID vs - // Number, which is something that we can't address in a diffsuppress. - // Since we can't enforce a particular method of entering the project, - // we're just going to have to use whatever the user entered, whether - // it's project/projectName, project/12345, projectName, or 12345. - // The normal behavior of this method would be 'return res' - and that's - // what we'll fall back to if any of our conditions aren't met. Those - // conditions are: - // 1) if the new or old values contain '/', the prefix of that is 'projects'. - // 2) if either is non-numeric, a project with that ID exists. - // 3) the project IDs represented by both the new and old values are the same. - config := meta.(*Config) - new := res["parent"].(string) - old := d.Get("parent").(string) - if strings.HasPrefix(new, "projects/") { - new = strings.Split(new, "/")[1] - } - if strings.HasPrefix(old, "projects/") { - old = strings.Split(old, "/")[1] - } - log.Printf("[DEBUG] Trying to figure out whether to use %s or %s", old, new) - // If there's still a '/' in there, the value must not be a project ID. - if strings.Contains(old, "/") || strings.Contains(new, "/") { - return res, nil - } - // If 'old' isn't entirely numeric, let's assume it's a project ID. - // If it's a project ID - var oldProjId int64 - var newProjId int64 - if oldVal, err := strconv.ParseInt(old, 10, 64); err == nil { - log.Printf("[DEBUG] The old value was a real number: %d", oldVal) - oldProjId = oldVal - } else { - pOld, err := config.clientResourceManager.Projects.Get(old).Do() + // Decode list item before comparing. + item, err := resourceResourceManagerLienDecoder(d, meta, item) if err != nil { - return res, nil + return nil, err } - oldProjId = pOld.ProjectNumber - } - if newVal, err := strconv.ParseInt(new, 10, 64); err == nil { - log.Printf("[DEBUG] The new value was a real number: %d", newVal) - newProjId = newVal - } else { - pNew, err := config.clientResourceManager.Projects.Get(new).Do() - if err != nil { - return res, nil + + itemName := flattenResourceManagerLienName(item["name"], d) + if !reflect.DeepEqual(itemName, expectedName) { + log.Printf("[DEBUG] Skipping item with name= %#v, looking for %#v)", itemName, expectedName) + continue } - newProjId = pNew.ProjectNumber - } - if newProjId == oldProjId { - res["parent"] = d.Get("parent") + log.Printf("[DEBUG] Found item for resource %q: %#v)", d.Id(), item) + return item, nil } - return res, nil + + return nil, nil }