Skip to content

Commit

Permalink
Add project overrides to gke services (#3892) (#7114)
Browse files Browse the repository at this point in the history
* project override header added

* project override header added for project service calls

* PR comments implemented

* indentation corrected

Signed-off-by: Modular Magician <[email protected]>
  • Loading branch information
modular-magician authored Aug 26, 2020
1 parent cf269e7 commit 4d55cd1
Show file tree
Hide file tree
Showing 7 changed files with 201 additions and 50 deletions.
6 changes: 6 additions & 0 deletions .changelog/3892.txt
Original file line number Diff line number Diff line change
@@ -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`

```
121 changes: 101 additions & 20 deletions google/resource_container_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -1183,7 +1183,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 {
Expand All @@ -1208,7 +1212,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("")
Expand All @@ -1230,7 +1238,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 {
Expand Down Expand Up @@ -1286,7 +1298,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)))
}
Expand Down Expand Up @@ -1420,7 +1437,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
}
Expand Down Expand Up @@ -1539,7 +1560,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
}
Expand Down Expand Up @@ -1567,7 +1592,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
Expand Down Expand Up @@ -1646,7 +1675,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
}
Expand Down Expand Up @@ -1679,7 +1712,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
}
Expand Down Expand Up @@ -1707,7 +1744,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
}
Expand Down Expand Up @@ -1820,7 +1861,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
}
Expand Down Expand Up @@ -1857,7 +1902,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
}
Expand Down Expand Up @@ -1905,7 +1954,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
}
Expand Down Expand Up @@ -1958,7 +2011,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
}
Expand All @@ -1977,7 +2034,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)
Expand All @@ -2001,7 +2062,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
}
Expand Down Expand Up @@ -2054,7 +2119,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)
Expand Down Expand Up @@ -2105,7 +2174,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)))
}
Expand All @@ -2131,7 +2204,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)
}
Expand Down Expand Up @@ -2251,7 +2328,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
Expand Down
3 changes: 3 additions & 0 deletions google/resource_container_cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2784,6 +2784,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"
Expand Down
Loading

0 comments on commit 4d55cd1

Please sign in to comment.