From cd3b286a999708772210b72420694aad138480bf Mon Sep 17 00:00:00 2001 From: Modular Magician Date: Wed, 26 Aug 2020 17:33:07 +0000 Subject: [PATCH] Add project overrides to gke services (#3892) * project override header added * project override header added for project service calls * PR comments implemented * indentation corrected Signed-off-by: Modular Magician --- .changelog/3892.txt | 6 + google-beta/resource_container_cluster.go | 145 +++++++++++++++--- .../resource_container_cluster_test.go | 3 + google-beta/resource_container_node_pool.go | 122 +++++++++++---- .../resource_container_node_pool_test.go | 3 + .../resource_google_project_service.go | 15 +- .../resource_google_project_service_test.go | 3 + 7 files changed, 237 insertions(+), 60 deletions(-) create mode 100644 .changelog/3892.txt diff --git a/.changelog/3892.txt b/.changelog/3892.txt new file mode 100644 index 0000000000..b2ad5c99e6 --- /dev/null +++ b/.changelog/3892.txt @@ -0,0 +1,6 @@ +```release-note:enhancement + +container : added project override support to `google_container_cluster` and `google_container_nodepool` +servicemanagement : added project override support to `google_project_service` + +``` diff --git a/google-beta/resource_container_cluster.go b/google-beta/resource_container_cluster.go index aa24391929..7cd67aaeed 100644 --- a/google-beta/resource_container_cluster.go +++ b/google-beta/resource_container_cluster.go @@ -1359,7 +1359,11 @@ func resourceContainerClusterCreate(d *schema.ResourceData, meta interface{}) er parent := fmt.Sprintf("projects/%s/locations/%s", project, location) var op *containerBeta.Operation err = retry(func() error { - op, err = config.clientContainerBeta.Projects.Locations.Clusters.Create(parent, req).Do() + clusterCreateCall := config.clientContainerBeta.Projects.Locations.Clusters.Create(parent, req) + if config.UserProjectOverride { + clusterCreateCall.Header().Add("X-Goog-User-Project", project) + } + op, err = clusterCreateCall.Do() return err }) if err != nil { @@ -1384,7 +1388,11 @@ func resourceContainerClusterCreate(d *schema.ResourceData, meta interface{}) er // leaving default case to ensure this is non blocking } // Try a GET on the cluster so we can see the state in debug logs. This will help classify error states. - _, getErr := config.clientContainerBeta.Projects.Locations.Clusters.Get(containerClusterFullName(project, location, clusterName)).Do() + clusterGetCall := config.clientContainerBeta.Projects.Locations.Clusters.Get(containerClusterFullName(project, location, clusterName)) + if config.UserProjectOverride { + clusterGetCall.Header().Add("X-Goog-User-Project", project) + } + _, getErr := clusterGetCall.Do() if getErr != nil { log.Printf("[WARN] Cluster %s was created in an error state and not found", clusterName) d.SetId("") @@ -1406,7 +1414,11 @@ func resourceContainerClusterCreate(d *schema.ResourceData, meta interface{}) er if d.Get("remove_default_node_pool").(bool) { parent := fmt.Sprintf("%s/nodePools/%s", containerClusterFullName(project, location, clusterName), "default-pool") err = retry(func() error { - op, err = config.clientContainerBeta.Projects.Locations.Clusters.NodePools.Delete(parent).Do() + clusterNodePoolDeleteCall := config.clientContainerBeta.Projects.Locations.Clusters.NodePools.Delete(parent) + if config.UserProjectOverride { + clusterNodePoolDeleteCall.Header().Add("X-Goog-User-Project", project) + } + op, err = clusterNodePoolDeleteCall.Do() return err }) if err != nil { @@ -1462,7 +1474,12 @@ func resourceContainerClusterRead(d *schema.ResourceData, meta interface{}) erro clusterName := d.Get("name").(string) name := containerClusterFullName(project, location, clusterName) - cluster, err := config.clientContainerBeta.Projects.Locations.Clusters.Get(name).Do() + clusterGetCall := config.clientContainerBeta.Projects.Locations.Clusters.Get(name) + if config.UserProjectOverride { + clusterGetCall.Header().Add("X-Goog-User-Project", project) + } + + cluster, err := clusterGetCall.Do() if err != nil { return handleNotFoundError(err, d, fmt.Sprintf("Container Cluster %q", d.Get("name").(string))) } @@ -1611,7 +1628,11 @@ func resourceContainerClusterUpdate(d *schema.ResourceData, meta interface{}) er updateFunc := func(req *containerBeta.UpdateClusterRequest, updateDescription string) func() error { return func() error { name := containerClusterFullName(project, location, clusterName) - op, err := config.clientContainerBeta.Projects.Locations.Clusters.Update(name, req).Do() + clusterUpdateCall := config.clientContainerBeta.Projects.Locations.Clusters.Update(name, req) + if config.UserProjectOverride { + clusterUpdateCall.Header().Add("X-Goog-User-Project", project) + } + op, err := clusterUpdateCall.Do() if err != nil { return err } @@ -1730,7 +1751,11 @@ func resourceContainerClusterUpdate(d *schema.ResourceData, meta interface{}) er updateF := func() error { log.Println("[DEBUG] updating release_channel") name := containerClusterFullName(project, location, clusterName) - op, err := config.clientContainerBeta.Projects.Locations.Clusters.Update(name, req).Do() + clusterUpdateCall := config.clientContainerBeta.Projects.Locations.Clusters.Update(name, req) + if config.UserProjectOverride { + clusterUpdateCall.Header().Add("X-Goog-User-Project", project) + } + op, err := clusterUpdateCall.Do() if err != nil { return err } @@ -1764,7 +1789,11 @@ func resourceContainerClusterUpdate(d *schema.ResourceData, meta interface{}) er updateF := func() error { log.Println("[DEBUG] updating enable_intranode_visibility") name := containerClusterFullName(project, location, clusterName) - op, err := config.clientContainerBeta.Projects.Locations.Clusters.Update(name, req).Do() + clusterUpdateCall := config.clientContainerBeta.Projects.Locations.Clusters.Update(name, req) + if config.UserProjectOverride { + clusterUpdateCall.Header().Add("X-Goog-User-Project", project) + } + op, err := clusterUpdateCall.Do() if err != nil { return err } @@ -1794,7 +1823,11 @@ func resourceContainerClusterUpdate(d *schema.ResourceData, meta interface{}) er updateF := func() error { log.Println("[DEBUG] updating default_snat_status") name := containerClusterFullName(project, location, clusterName) - op, err := config.clientContainerBeta.Projects.Locations.Clusters.Update(name, req).Do() + clusterUpdateCall := config.clientContainerBeta.Projects.Locations.Clusters.Update(name, req) + if config.UserProjectOverride { + clusterUpdateCall.Header().Add("X-Goog-User-Project", project) + } + op, err := clusterUpdateCall.Do() if err != nil { return err } @@ -1822,7 +1855,11 @@ func resourceContainerClusterUpdate(d *schema.ResourceData, meta interface{}) er updateF := func() error { name := containerClusterFullName(project, location, clusterName) - op, err := config.clientContainerBeta.Projects.Locations.Clusters.SetMaintenancePolicy(name, req).Do() + clusterSetMaintenancePolicyCall := config.clientContainerBeta.Projects.Locations.Clusters.SetMaintenancePolicy(name, req) + if config.UserProjectOverride { + clusterSetMaintenancePolicyCall.Header().Add("X-Goog-User-Project", project) + } + op, err := clusterSetMaintenancePolicyCall.Do() if err != nil { return err @@ -1901,7 +1938,11 @@ func resourceContainerClusterUpdate(d *schema.ResourceData, meta interface{}) er updateF := func() error { log.Println("[DEBUG] updating enable_legacy_abac") name := containerClusterFullName(project, location, clusterName) - op, err := config.clientContainerBeta.Projects.Locations.Clusters.SetLegacyAbac(name, req).Do() + clusterSetLegacyAbacCall := config.clientContainerBeta.Projects.Locations.Clusters.SetLegacyAbac(name, req) + if config.UserProjectOverride { + clusterSetLegacyAbacCall.Header().Add("X-Goog-User-Project", project) + } + op, err := clusterSetLegacyAbacCall.Do() if err != nil { return err } @@ -1934,7 +1975,11 @@ func resourceContainerClusterUpdate(d *schema.ResourceData, meta interface{}) er DesiredLoggingService: logging, }, } - op, err := config.clientContainerBeta.Projects.Locations.Clusters.Update(name, req).Do() + clusterUpdateCall := config.clientContainerBeta.Projects.Locations.Clusters.Update(name, req) + if config.UserProjectOverride { + clusterUpdateCall.Header().Add("X-Goog-User-Project", project) + } + op, err := clusterUpdateCall.Do() if err != nil { return err } @@ -1962,7 +2007,11 @@ func resourceContainerClusterUpdate(d *schema.ResourceData, meta interface{}) er updateF := func() error { log.Println("[DEBUG] updating network_policy") name := containerClusterFullName(project, location, clusterName) - op, err := config.clientContainerBeta.Projects.Locations.Clusters.SetNetworkPolicy(name, req).Do() + clusterSetNetworkPolicyCall := config.clientContainerBeta.Projects.Locations.Clusters.SetNetworkPolicy(name, req) + if config.UserProjectOverride { + clusterSetNetworkPolicyCall.Header().Add("X-Goog-User-Project", project) + } + op, err := clusterSetNetworkPolicyCall.Do() if err != nil { return err } @@ -2075,7 +2124,11 @@ func resourceContainerClusterUpdate(d *schema.ResourceData, meta interface{}) er updateF := func() error { name := containerClusterFullName(project, location, clusterName) - op, err := config.clientContainerBeta.Projects.Locations.Clusters.Update(name, req).Do() + clusterUpdateCall := config.clientContainerBeta.Projects.Locations.Clusters.Update(name, req) + if config.UserProjectOverride { + clusterUpdateCall.Header().Add("X-Goog-User-Project", project) + } + op, err := clusterUpdateCall.Do() if err != nil { return err } @@ -2112,7 +2165,11 @@ func resourceContainerClusterUpdate(d *schema.ResourceData, meta interface{}) er updateF := func() error { name := containerClusterFullName(project, location, clusterName) - op, err := config.clientContainerBeta.Projects.Locations.Clusters.SetMasterAuth(name, req).Do() + clusterSetMasterAuthCall := config.clientContainerBeta.Projects.Locations.Clusters.SetMasterAuth(name, req) + if config.UserProjectOverride { + clusterSetMasterAuthCall.Header().Add("X-Goog-User-Project", project) + } + op, err := clusterSetMasterAuthCall.Do() if err != nil { return err } @@ -2160,7 +2217,11 @@ func resourceContainerClusterUpdate(d *schema.ResourceData, meta interface{}) er updateF := func() error { name := containerClusterFullName(project, location, clusterName) - op, err := config.clientContainerBeta.Projects.Locations.Clusters.Update(name, req).Do() + clusterUpdateCall := config.clientContainerBeta.Projects.Locations.Clusters.Update(name, req) + if config.UserProjectOverride { + clusterUpdateCall.Header().Add("X-Goog-User-Project", project) + } + op, err := clusterUpdateCall.Do() if err != nil { return err } @@ -2185,7 +2246,11 @@ func resourceContainerClusterUpdate(d *schema.ResourceData, meta interface{}) er updateF := func() error { name := containerClusterFullName(project, location, clusterName) - op, err := config.clientContainerBeta.Projects.Locations.Clusters.Update(name, req).Do() + clusterUpdateCall := config.clientContainerBeta.Projects.Locations.Clusters.Update(name, req) + if config.UserProjectOverride { + clusterUpdateCall.Header().Add("X-Goog-User-Project", project) + } + op, err := clusterUpdateCall.Do() if err != nil { return err } @@ -2238,7 +2303,11 @@ func resourceContainerClusterUpdate(d *schema.ResourceData, meta interface{}) er } updateF := func() error { name := containerClusterFullName(project, location, clusterName) - op, err := config.clientContainerBeta.Projects.Locations.Clusters.SetResourceLabels(name, req).Do() + clusterSetResourceLabelsCall := config.clientContainerBeta.Projects.Locations.Clusters.SetResourceLabels(name, req) + if config.UserProjectOverride { + clusterSetResourceLabelsCall.Header().Add("X-Goog-User-Project", project) + } + op, err := clusterSetResourceLabelsCall.Do() if err != nil { return err } @@ -2257,7 +2326,11 @@ func resourceContainerClusterUpdate(d *schema.ResourceData, meta interface{}) er if d.HasChange("remove_default_node_pool") && d.Get("remove_default_node_pool").(bool) { name := fmt.Sprintf("%s/nodePools/%s", containerClusterFullName(project, location, clusterName), "default-pool") - op, err := config.clientContainerBeta.Projects.Locations.Clusters.NodePools.Delete(name).Do() + clusterNodePoolDeleteCall := config.clientContainerBeta.Projects.Locations.Clusters.NodePools.Delete(name) + if config.UserProjectOverride { + clusterNodePoolDeleteCall.Header().Add("X-Goog-User-Project", project) + } + op, err := clusterNodePoolDeleteCall.Do() if err != nil { if !isGoogleApiErrorWithCode(err, 404) { return errwrap.Wrapf("Error deleting default node pool: {{err}}", err) @@ -2281,7 +2354,11 @@ func resourceContainerClusterUpdate(d *schema.ResourceData, meta interface{}) er updateF := func() error { name := containerClusterFullName(project, location, clusterName) - op, err := config.clientContainerBeta.Projects.Locations.Clusters.Update(name, req).Do() + clusterUpdateCall := config.clientContainerBeta.Projects.Locations.Clusters.Update(name, req) + if config.UserProjectOverride { + clusterUpdateCall.Header().Add("X-Goog-User-Project", project) + } + op, err := clusterUpdateCall.Do() if err != nil { return err } @@ -2307,7 +2384,11 @@ func resourceContainerClusterUpdate(d *schema.ResourceData, meta interface{}) er updateF := func() error { log.Println("[DEBUG] updating cluster_telemetry") name := containerClusterFullName(project, location, clusterName) - op, err := config.clientContainerBeta.Projects.Locations.Clusters.Update(name, req).Do() + clusterUpdateCall := config.clientContainerBeta.Projects.Locations.Clusters.Update(name, req) + if config.UserProjectOverride { + clusterUpdateCall.Header().Add("X-Goog-User-Project", project) + } + op, err := clusterUpdateCall.Do() if err != nil { return err } @@ -2364,7 +2445,11 @@ func resourceContainerClusterDelete(d *schema.ResourceData, meta interface{}) er count++ name := containerClusterFullName(project, location, clusterName) - op, err = config.clientContainerBeta.Projects.Locations.Clusters.Delete(name).Do() + clusterDeleteCall := config.clientContainerBeta.Projects.Locations.Clusters.Delete(name) + if config.UserProjectOverride { + clusterDeleteCall.Header().Add("X-Goog-User-Project", project) + } + op, err = clusterDeleteCall.Do() if err != nil { log.Printf("[WARNING] Cluster is still not ready to delete, retrying %s", clusterName) @@ -2415,7 +2500,11 @@ func cleanFailedContainerCluster(d *schema.ResourceData, meta interface{}) error fullName := containerClusterFullName(project, location, clusterName) log.Printf("[DEBUG] Cleaning up failed GKE cluster %s", d.Get("name").(string)) - op, err := config.clientContainerBeta.Projects.Locations.Clusters.Delete(fullName).Do() + clusterDeleteCall := config.clientContainerBeta.Projects.Locations.Clusters.Delete(fullName) + if config.UserProjectOverride { + clusterDeleteCall.Header().Add("X-Goog-User-Project", project) + } + op, err := clusterDeleteCall.Do() if err != nil { return handleNotFoundError(err, d, fmt.Sprintf("Container Cluster %q", d.Get("name").(string))) } @@ -2441,7 +2530,11 @@ var containerClusterRestingStates = RestingStates{ func containerClusterAwaitRestingState(config *Config, project, location, clusterName string, timeout time.Duration) (state string, err error) { err = resource.Retry(timeout, func() *resource.RetryError { name := containerClusterFullName(project, location, clusterName) - cluster, gErr := config.clientContainerBeta.Projects.Locations.Clusters.Get(name).Do() + clusterGetCall := config.clientContainerBeta.Projects.Locations.Clusters.Get(name) + if config.UserProjectOverride { + clusterGetCall.Header().Add("X-Goog-User-Project", project) + } + cluster, gErr := clusterGetCall.Do() if gErr != nil { return resource.NonRetryableError(gErr) } @@ -2605,7 +2698,11 @@ func expandMaintenancePolicy(d *schema.ResourceData, meta interface{}) *containe location, _ := getLocation(d, config) clusterName := d.Get("name").(string) name := containerClusterFullName(project, location, clusterName) - cluster, _ := config.clientContainerBeta.Projects.Locations.Clusters.Get(name).Do() + clusterGetCall := config.clientContainerBeta.Projects.Locations.Clusters.Get(name) + if config.UserProjectOverride { + clusterGetCall.Header().Add("X-Goog-User-Project", project) + } + cluster, _ := clusterGetCall.Do() resourceVersion := "" // If the cluster doesn't exist or if there is a read error of any kind, we will pass in an empty // resourceVersion. If there happens to be a change to maintenance policy, we will fail at that diff --git a/google-beta/resource_container_cluster_test.go b/google-beta/resource_container_cluster_test.go index d5f144269a..283df09591 100644 --- a/google-beta/resource_container_cluster_test.go +++ b/google-beta/resource_container_cluster_test.go @@ -3494,6 +3494,9 @@ resource "google_container_cluster" "with_ip_allocation_policy" { func testAccContainerCluster_withResourceUsageExportConfig(clusterName, datasetId, enableMetering string) string { return fmt.Sprintf(` +provider "google" { + user_project_override = true +} resource "google_bigquery_dataset" "default" { dataset_id = "%s" description = "gke resource usage dataset tests" diff --git a/google-beta/resource_container_node_pool.go b/google-beta/resource_container_node_pool.go index aa1d2f2bad..b3e74e7779 100644 --- a/google-beta/resource_container_node_pool.go +++ b/google-beta/resource_container_node_pool.go @@ -296,8 +296,11 @@ func resourceContainerNodePoolCreate(d *schema.ResourceData, meta interface{}) e var operation *containerBeta.Operation err = resource.Retry(timeout, func() *resource.RetryError { - operation, err = config.clientContainerBeta. - Projects.Locations.Clusters.NodePools.Create(nodePoolInfo.parent(), req).Do() + clusterNodePoolsCreateCall := config.clientContainerBeta.Projects.Locations.Clusters.NodePools.Create(nodePoolInfo.parent(), req) + if config.UserProjectOverride { + clusterNodePoolsCreateCall.Header().Add("X-Goog-User-Project", nodePoolInfo.project) + } + operation, err = clusterNodePoolsCreateCall.Do() if err != nil { if isFailedPreconditionError(err) { @@ -330,7 +333,7 @@ func resourceContainerNodePoolCreate(d *schema.ResourceData, meta interface{}) e return err } - state, err := containerNodePoolAwaitRestingState(config, d.Id(), d.Timeout(schema.TimeoutCreate)) + state, err := containerNodePoolAwaitRestingState(config, d.Id(), nodePoolInfo.project, d.Timeout(schema.TimeoutCreate)) if err != nil { return err } @@ -351,7 +354,11 @@ func resourceContainerNodePoolRead(d *schema.ResourceData, meta interface{}) err name := getNodePoolName(d.Id()) - nodePool, err := config.clientContainerBeta.Projects.Locations.Clusters.NodePools.Get(nodePoolInfo.fullyQualifiedName(name)).Do() + clusterNodePoolsGetCall := config.clientContainerBeta.Projects.Locations.Clusters.NodePools.Get(nodePoolInfo.fullyQualifiedName(name)) + if config.UserProjectOverride { + clusterNodePoolsGetCall.Header().Add("X-Goog-User-Project", nodePoolInfo.project) + } + nodePool, err := clusterNodePoolsGetCall.Do() if err != nil { return handleNotFoundError(err, d, fmt.Sprintf("NodePool %q from cluster %q", name, nodePoolInfo.cluster)) } @@ -373,14 +380,13 @@ func resourceContainerNodePoolRead(d *schema.ResourceData, meta interface{}) err func resourceContainerNodePoolUpdate(d *schema.ResourceData, meta interface{}) error { config := meta.(*Config) - nodePoolInfo, err := extractNodePoolInformation(d, config) if err != nil { return err } name := getNodePoolName(d.Id()) - _, err = containerNodePoolAwaitRestingState(config, nodePoolInfo.fullyQualifiedName(name), d.Timeout(schema.TimeoutUpdate)) + _, err = containerNodePoolAwaitRestingState(config, nodePoolInfo.fullyQualifiedName(name), nodePoolInfo.project, d.Timeout(schema.TimeoutUpdate)) if err != nil { return err } @@ -391,7 +397,7 @@ func resourceContainerNodePoolUpdate(d *schema.ResourceData, meta interface{}) e } d.Partial(false) - _, err = containerNodePoolAwaitRestingState(config, nodePoolInfo.fullyQualifiedName(name), d.Timeout(schema.TimeoutUpdate)) + _, err = containerNodePoolAwaitRestingState(config, nodePoolInfo.fullyQualifiedName(name), nodePoolInfo.project, d.Timeout(schema.TimeoutUpdate)) if err != nil { return err } @@ -401,7 +407,6 @@ func resourceContainerNodePoolUpdate(d *schema.ResourceData, meta interface{}) e func resourceContainerNodePoolDelete(d *schema.ResourceData, meta interface{}) error { config := meta.(*Config) - nodePoolInfo, err := extractNodePoolInformation(d, config) if err != nil { return err @@ -409,7 +414,7 @@ func resourceContainerNodePoolDelete(d *schema.ResourceData, meta interface{}) e name := getNodePoolName(d.Id()) - _, err = containerNodePoolAwaitRestingState(config, nodePoolInfo.fullyQualifiedName(name), d.Timeout(schema.TimeoutDelete)) + _, err = containerNodePoolAwaitRestingState(config, nodePoolInfo.fullyQualifiedName(name), nodePoolInfo.project, d.Timeout(schema.TimeoutDelete)) if err != nil { return err } @@ -422,8 +427,11 @@ func resourceContainerNodePoolDelete(d *schema.ResourceData, meta interface{}) e var operation *containerBeta.Operation err = resource.Retry(timeout, func() *resource.RetryError { - operation, err = config.clientContainerBeta. - Projects.Locations.Clusters.NodePools.Delete(nodePoolInfo.fullyQualifiedName(name)).Do() + clusterNodePoolsDeleteCall := config.clientContainerBeta.Projects.Locations.Clusters.NodePools.Delete(nodePoolInfo.fullyQualifiedName(name)) + if config.UserProjectOverride { + clusterNodePoolsDeleteCall.Header().Add("X-Goog-User-Project", nodePoolInfo.project) + } + operation, err = clusterNodePoolsDeleteCall.Do() if err != nil { if isFailedPreconditionError(err) { @@ -458,15 +466,18 @@ func resourceContainerNodePoolDelete(d *schema.ResourceData, meta interface{}) e func resourceContainerNodePoolExists(d *schema.ResourceData, meta interface{}) (bool, error) { config := meta.(*Config) - nodePoolInfo, err := extractNodePoolInformation(d, config) if err != nil { return false, err } name := getNodePoolName(d.Id()) + clusterNodePoolsGetCall := config.clientContainerBeta.Projects.Locations.Clusters.NodePools.Get(nodePoolInfo.fullyQualifiedName(name)) + if config.UserProjectOverride { + clusterNodePoolsGetCall.Header().Add("X-Goog-User-Project", nodePoolInfo.project) + } + _, err = clusterNodePoolsGetCall.Do() - _, err = config.clientContainerBeta.Projects.Locations.Clusters.NodePools.Get(nodePoolInfo.fullyQualifiedName(name)).Do() if err != nil { if err = handleNotFoundError(err, d, fmt.Sprintf("Container NodePool %s", name)); err == nil { return false, nil @@ -490,7 +501,12 @@ func resourceContainerNodePoolStateImporter(d *schema.ResourceData, meta interfa d.SetId(id) - if _, err := containerNodePoolAwaitRestingState(config, d.Id(), d.Timeout(schema.TimeoutCreate)); err != nil { + project, err := getProject(d, config) + if err != nil { + return nil, err + } + + if _, err := containerNodePoolAwaitRestingState(config, d.Id(), project, d.Timeout(schema.TimeoutCreate)); err != nil { return nil, err } @@ -657,7 +673,6 @@ func flattenNodePool(d *schema.ResourceData, config *Config, np *containerBeta.N func nodePoolUpdate(d *schema.ResourceData, meta interface{}, nodePoolInfo *NodePoolInformation, prefix string, timeout time.Duration) error { config := meta.(*Config) - name := d.Get(prefix + "name").(string) lockKey := nodePoolInfo.lockKey() @@ -685,7 +700,11 @@ func nodePoolUpdate(d *schema.ResourceData, meta interface{}, nodePoolInfo *Node } updateF := func() error { - op, err := config.clientContainerBeta.Projects.Locations.Clusters.Update(nodePoolInfo.parent(), req).Do() + clusterUpdateCall := config.clientContainerBeta.Projects.Locations.Clusters.Update(nodePoolInfo.parent(), req) + if config.UserProjectOverride { + clusterUpdateCall.Header().Add("X-Goog-User-Project", nodePoolInfo.project) + } + op, err := clusterUpdateCall.Do() if err != nil { return err } @@ -719,7 +738,11 @@ func nodePoolUpdate(d *schema.ResourceData, meta interface{}, nodePoolInfo *Node } updateF := func() error { - op, err := config.clientContainerBeta.Projects.Locations.Clusters.Update(nodePoolInfo.parent(), req).Do() + clusterUpdateCall := config.clientContainerBeta.Projects.Locations.Clusters.Update(nodePoolInfo.parent(), req) + if config.UserProjectOverride { + clusterUpdateCall.Header().Add("X-Goog-User-Project", nodePoolInfo.project) + } + op, err := clusterUpdateCall.Do() if err != nil { return err } @@ -748,8 +771,12 @@ func nodePoolUpdate(d *schema.ResourceData, meta interface{}, nodePoolInfo *Node req.ForceSendFields = []string{"WorkloadMetadataConfig"} } updateF := func() error { - op, err := config.clientContainerBeta.Projects.Locations.Clusters.NodePools. - Update(nodePoolInfo.fullyQualifiedName(name), req).Do() + clusterNodePoolsUpdateCall := config.clientContainerBeta.Projects.Locations.Clusters.NodePools.Update(nodePoolInfo.fullyQualifiedName(name), req) + if config.UserProjectOverride { + clusterNodePoolsUpdateCall.Header().Add("X-Goog-User-Project", nodePoolInfo.project) + } + op, err := clusterNodePoolsUpdateCall.Do() + if err != nil { return err } @@ -780,8 +807,11 @@ func nodePoolUpdate(d *schema.ResourceData, meta interface{}, nodePoolInfo *Node req.ForceSendFields = []string{"KubeletConfig"} } updateF := func() error { - op, err := config.clientContainerBeta.Projects.Locations.Clusters.NodePools. - Update(nodePoolInfo.fullyQualifiedName(name), req).Do() + clusterNodePoolsUpdateCall := config.clientContainerBeta.Projects.Locations.Clusters.NodePools.Update(nodePoolInfo.fullyQualifiedName(name), req) + if config.UserProjectOverride { + clusterNodePoolsUpdateCall.Header().Add("X-Goog-User-Project", nodePoolInfo.project) + } + op, err := clusterNodePoolsUpdateCall.Do() if err != nil { return err } @@ -811,8 +841,11 @@ func nodePoolUpdate(d *schema.ResourceData, meta interface{}, nodePoolInfo *Node req.ForceSendFields = []string{"LinuxNodeConfig"} } updateF := func() error { - op, err := config.clientContainerBeta.Projects.Locations.Clusters.NodePools. - Update(nodePoolInfo.fullyQualifiedName(name), req).Do() + clusterNodePoolsUpdateCall := config.clientContainerBeta.Projects.Locations.Clusters.NodePools.Update(nodePoolInfo.fullyQualifiedName(name), req) + if config.UserProjectOverride { + clusterNodePoolsUpdateCall.Header().Add("X-Goog-User-Project", nodePoolInfo.project) + } + op, err := clusterNodePoolsUpdateCall.Do() if err != nil { return err } @@ -844,7 +877,11 @@ func nodePoolUpdate(d *schema.ResourceData, meta interface{}, nodePoolInfo *Node NodeCount: newSize, } updateF := func() error { - op, err := config.clientContainerBeta.Projects.Locations.Clusters.NodePools.SetSize(nodePoolInfo.fullyQualifiedName(name), req).Do() + clusterNodePoolsSetSizeCall := config.clientContainerBeta.Projects.Locations.Clusters.NodePools.SetSize(nodePoolInfo.fullyQualifiedName(name), req) + if config.UserProjectOverride { + clusterNodePoolsSetSizeCall.Header().Add("X-Goog-User-Project", nodePoolInfo.project) + } + op, err := clusterNodePoolsSetSizeCall.Do() if err != nil { return err @@ -882,8 +919,11 @@ func nodePoolUpdate(d *schema.ResourceData, meta interface{}, nodePoolInfo *Node } updateF := func() error { - op, err := config.clientContainerBeta.Projects.Locations. - Clusters.NodePools.SetManagement(nodePoolInfo.fullyQualifiedName(name), req).Do() + clusterNodePoolsSetManagementCall := config.clientContainerBeta.Projects.Locations.Clusters.NodePools.SetManagement(nodePoolInfo.fullyQualifiedName(name), req) + if config.UserProjectOverride { + clusterNodePoolsSetManagementCall.Header().Add("X-Goog-User-Project", nodePoolInfo.project) + } + op, err := clusterNodePoolsSetManagementCall.Do() if err != nil { return err @@ -913,8 +953,11 @@ func nodePoolUpdate(d *schema.ResourceData, meta interface{}, nodePoolInfo *Node NodeVersion: d.Get(prefix + "version").(string), } updateF := func() error { - op, err := config.clientContainerBeta.Projects. - Locations.Clusters.NodePools.Update(nodePoolInfo.fullyQualifiedName(name), req).Do() + clusterNodePoolsUpdateCall := config.clientContainerBeta.Projects.Locations.Clusters.NodePools.Update(nodePoolInfo.fullyQualifiedName(name), req) + if config.UserProjectOverride { + clusterNodePoolsUpdateCall.Header().Add("X-Goog-User-Project", nodePoolInfo.project) + } + op, err := clusterNodePoolsUpdateCall.Do() if err != nil { return err @@ -943,7 +986,11 @@ func nodePoolUpdate(d *schema.ResourceData, meta interface{}, nodePoolInfo *Node Locations: convertStringSet(d.Get(prefix + "node_locations").(*schema.Set)), } updateF := func() error { - op, err := config.clientContainerBeta.Projects.Locations.Clusters.NodePools.Update(nodePoolInfo.fullyQualifiedName(name), req).Do() + clusterNodePoolsUpdateCall := config.clientContainerBeta.Projects.Locations.Clusters.NodePools.Update(nodePoolInfo.fullyQualifiedName(name), req) + if config.UserProjectOverride { + clusterNodePoolsUpdateCall.Header().Add("X-Goog-User-Project", nodePoolInfo.project) + } + op, err := clusterNodePoolsUpdateCall.Do() if err != nil { return err @@ -976,7 +1023,11 @@ func nodePoolUpdate(d *schema.ResourceData, meta interface{}, nodePoolInfo *Node UpgradeSettings: upgradeSettings, } updateF := func() error { - op, err := config.clientContainerBeta.Projects.Locations.Clusters.NodePools.Update(nodePoolInfo.fullyQualifiedName(name), req).Do() + clusterNodePoolsUpdateCall := config.clientContainerBeta.Projects.Locations.Clusters.NodePools.Update(nodePoolInfo.fullyQualifiedName(name), req) + if config.UserProjectOverride { + clusterNodePoolsUpdateCall.Header().Add("X-Goog-User-Project", nodePoolInfo.project) + } + op, err := clusterNodePoolsUpdateCall.Do() if err != nil { return err @@ -1013,11 +1064,16 @@ var containerNodePoolRestingStates = RestingStates{ "ERROR": ErrorState, } -// takes in a config object, full node pool name, and the current CRUD action timeout +// takes in a config object, full node pool name, project name and the current CRUD action timeout // returns a state with no error if the state is a resting state, and the last state with an error otherwise -func containerNodePoolAwaitRestingState(config *Config, name string, timeout time.Duration) (state string, err error) { +func containerNodePoolAwaitRestingState(config *Config, name string, project string, timeout time.Duration) (state string, err error) { + err = resource.Retry(timeout, func() *resource.RetryError { - nodePool, gErr := config.clientContainerBeta.Projects.Locations.Clusters.NodePools.Get(name).Do() + clusterNodePoolsGetCall := config.clientContainerBeta.Projects.Locations.Clusters.NodePools.Get(name) + if config.UserProjectOverride { + clusterNodePoolsGetCall.Header().Add("X-Goog-User-Project", project) + } + nodePool, gErr := clusterNodePoolsGetCall.Do() if gErr != nil { return resource.NonRetryableError(gErr) } diff --git a/google-beta/resource_container_node_pool_test.go b/google-beta/resource_container_node_pool_test.go index a58bbe47b2..d73962d231 100644 --- a/google-beta/resource_container_node_pool_test.go +++ b/google-beta/resource_container_node_pool_test.go @@ -871,6 +871,9 @@ func testAccCheckContainerNodePoolDestroyProducer(t *testing.T) func(s *terrafor func testAccContainerNodePool_basic(cluster, np string) string { return fmt.Sprintf(` +provider "google" { + user_project_override = true +} resource "google_container_cluster" "cluster" { name = "%s" location = "us-central1-a" diff --git a/google-beta/resource_google_project_service.go b/google-beta/resource_google_project_service.go index e8d0cbbe29..4bc861078c 100644 --- a/google-beta/resource_google_project_service.go +++ b/google-beta/resource_google_project_service.go @@ -157,7 +157,12 @@ func resourceGoogleProjectServiceRead(d *schema.ResourceData, meta interface{}) } // Verify project for services still exists - p, err := config.clientResourceManager.Projects.Get(project).Do() + projectGetCall := config.clientResourceManager.Projects.Get(project) + if config.UserProjectOverride { + projectGetCall.Header().Add("X-Goog-User-Project", project) + } + p, err := projectGetCall.Do() + if err == nil && p.LifecycleState == "DELETE_REQUESTED" { // Construct a 404 error for handleNotFoundError err = &googleapi.Error{ @@ -222,9 +227,13 @@ func resourceGoogleProjectServiceUpdate(d *schema.ResourceData, meta interface{} func disableServiceUsageProjectService(service, project string, d *schema.ResourceData, config *Config, disableDependentServices bool) error { err := retryTimeDuration(func() error { name := fmt.Sprintf("projects/%s/services/%s", project, service) - sop, err := config.clientServiceUsage.Services.Disable(name, &serviceusage.DisableServiceRequest{ + servicesDisableCall := config.clientServiceUsage.Services.Disable(name, &serviceusage.DisableServiceRequest{ DisableDependentServices: disableDependentServices, - }).Do() + }) + if config.UserProjectOverride { + servicesDisableCall.Header().Add("X-Goog-User-Project", project) + } + sop, err := servicesDisableCall.Do() if err != nil { return err } diff --git a/google-beta/resource_google_project_service_test.go b/google-beta/resource_google_project_service_test.go index 958502dcc9..dd21670c8b 100644 --- a/google-beta/resource_google_project_service_test.go +++ b/google-beta/resource_google_project_service_test.go @@ -187,6 +187,9 @@ func testAccCheckProjectService(t *testing.T, services []string, pid string, exp func testAccProjectService_basic(services []string, pid, name, org string) string { return fmt.Sprintf(` +provider "google" { + user_project_override = true +} resource "google_project" "acceptance" { project_id = "%s" name = "%s"