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 legacyAbac to google_container_cluster #261

Merged
merged 3 commits into from
Jul 31, 2017
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions google/resource_container_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,21 @@ func resourceContainerCluster() *schema.Resource {
Elem: &schema.Schema{Type: schema.TypeString},
},

"legacy_abac": {
Copy link
Contributor

Choose a reason for hiding this comment

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

Are there any other parameters here? Should we simplify this for the users into a simple toggle?
enable_legacy_abac ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, I was leaving the option open in case any others get added, but realistically I don't think they will so I went ahead and made the change.

Type: schema.TypeList,
Optional: true,
MaxItems: 1,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"enabled": {
Type: schema.TypeBool,
Required: true,
Copy link
Contributor

Choose a reason for hiding this comment

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

I suspect we want a default value here

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

},
},
},
},

"logging_service": {
Type: schema.TypeString,
Optional: true,
Expand Down Expand Up @@ -409,6 +424,12 @@ func resourceContainerClusterCreate(d *schema.ResourceData, meta interface{}) er
cluster.Description = v.(string)
}

if _, ok := d.GetOk("legacy_abac"); ok {
Copy link
Contributor

Choose a reason for hiding this comment

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

if the user specifies false, this block won't get triggered, right? So we'll never send false, if I recall correctly

Copy link
Contributor

@catsby catsby Jul 28, 2017

Choose a reason for hiding this comment

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

In addition, LegacyAbac.Enabled is a boolean that's marshaled with omitempty, so even if we do go in here, we won't actually send false unless this field is added to the LegacyAbac.ForceSendFields slice.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good point. Fixed.

cluster.LegacyAbac = &container.LegacyAbac{
Enabled: d.Get("legacy_abac.0.enabled").(bool),
}
}

if v, ok := d.GetOk("logging_service"); ok {
cluster.LoggingService = v.(string)
}
Expand Down Expand Up @@ -610,6 +631,7 @@ func resourceContainerClusterRead(d *schema.ResourceData, meta interface{}) erro
d.Set("node_version", cluster.CurrentNodeVersion)
d.Set("cluster_ipv4_cidr", cluster.ClusterIpv4Cidr)
d.Set("description", cluster.Description)
d.Set("legacy_abac", flattenLegacyAbac(cluster))
d.Set("logging_service", cluster.LoggingService)
d.Set("monitoring_service", cluster.MonitoringService)
d.Set("network", d.Get("network").(string))
Expand Down Expand Up @@ -692,6 +714,29 @@ func resourceContainerClusterUpdate(d *schema.ResourceData, meta interface{}) er

log.Printf("[INFO] GKE cluster %s locations have been updated to %v", d.Id(),
locations)

d.SetPartial("additional_zones")
}

if d.HasChange("legacy_abac.0.enabled") {
enabled := d.Get("legacy_abac.0.enabled").(bool)
req := &container.SetLegacyAbacRequest{
Enabled: enabled,
}
op, err := config.clientContainer.Projects.Zones.Clusters.LegacyAbac(project, zoneName, clusterName, req).Do()
if err != nil {
return err
}

// Wait until it's updated
waitErr := containerOperationWait(config, op, project, zoneName, "updating GKE legacy ABAC", timeoutInMinutes, 2)
if waitErr != nil {
return waitErr
}

log.Printf("[INFO] GKE cluster %s legacy ABAC has been updated to %v", d.Id(), enabled)

d.SetPartial("legacy_abac")
}

d.Partial(false)
Expand Down Expand Up @@ -755,6 +800,14 @@ func getInstanceGroupUrlsFromManagerUrls(config *Config, igmUrls []string) ([]st
return instanceGroupURLs, nil
}

func flattenLegacyAbac(c *container.Cluster) []map[string]interface{} {
return []map[string]interface{}{
{
"enabled": c.LegacyAbac.Enabled,
},
}
}

func flattenClusterNodeConfig(c *container.NodeConfig) []map[string]interface{} {
config := []map[string]interface{}{
{
Expand Down
53 changes: 53 additions & 0 deletions google/resource_container_cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,32 @@ func TestAccContainerCluster_withAdditionalZones(t *testing.T) {
})
}

func TestAccContainerCluster_withLegacyAbac(t *testing.T) {
clusterName := fmt.Sprintf("cluster-test-%s", acctest.RandString(10))

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckContainerClusterDestroy,
Steps: []resource.TestStep{
{
Config: testAccContainerCluster_withLegacyAbac(clusterName),
Check: resource.ComposeTestCheckFunc(
testAccCheckContainerCluster(
"google_container_cluster.with_legacy_abac"),
),
},
{
Config: testAccContainerCluster_updateLegacyAbac(clusterName),
Check: resource.ComposeTestCheckFunc(
testAccCheckContainerCluster(
"google_container_cluster.with_legacy_abac"),
),
},
},
})
}

func TestAccContainerCluster_withVersion(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Expand Down Expand Up @@ -291,6 +317,7 @@ func testAccCheckContainerCluster(n string) resource.TestCheckFunc {
{"description", cluster.Description},
{"endpoint", cluster.Endpoint},
{"instance_group_urls", igUrls},
{"legacy_abac.0.enabled", strconv.FormatBool(cluster.LegacyAbac.Enabled)},
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 believe this is accurately testing this as the code stands. We can't send false, so we never actually change the value. The test doesn't seem to verify that the value is now false.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yup, you're right. Fixed.

{"logging_service", cluster.LoggingService},
{"monitoring_service", cluster.MonitoringService},
{"subnetwork", cluster.Subnetwork},
Expand Down Expand Up @@ -520,6 +547,32 @@ resource "google_container_cluster" "with_additional_zones" {
}`, clusterName)
}

func testAccContainerCluster_withLegacyAbac(clusterName string) string {
return fmt.Sprintf(`
resource "google_container_cluster" "with_legacy_abac" {
name = "cluster-test-%s"
zone = "us-central1-a"
initial_node_count = 1

legacy_abac {
enabled = true
}
}`, clusterName)
}

func testAccContainerCluster_updateLegacyAbac(clusterName string) string {
return fmt.Sprintf(`
resource "google_container_cluster" "with_legacy_abac" {
name = "cluster-test-%s"
zone = "us-central1-a"
initial_node_count = 1

legacy_abac {
enabled = false
}
}`, clusterName)
}

var testAccContainerCluster_withVersion = fmt.Sprintf(`
data "google_container_engine_versions" "central1a" {
zone = "us-central1-a"
Expand Down
Loading