Skip to content

Commit

Permalink
Handle GKE cluster client cert settings correctly
Browse files Browse the repository at this point in the history
Signed-off-by: Modular Magician <[email protected]>
  • Loading branch information
rileykarson authored and modular-magician committed May 31, 2019
1 parent 56ea59c commit 76188a2
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 43 deletions.
73 changes: 30 additions & 43 deletions google-beta/resource_container_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -377,19 +377,22 @@ func resourceContainerCluster() *schema.Resource {
Optional: true,
},

// Ideally, this would be Optional (and not Computed).
// In past versions (incl. 2.X series) of the provider
// though, being unset was considered identical to set
// and the issue_client_certificate value being true.
"client_certificate_config": {
Type: schema.TypeList,
MaxItems: 1,
Optional: true,
DiffSuppressFunc: masterAuthClientCertCfgSuppress,
ForceNew: true,
Type: schema.TypeList,
MaxItems: 1,
Optional: true,
Computed: true,
ForceNew: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"issue_client_certificate": {
Type: schema.TypeBool,
Required: true,
ForceNew: true,
DiffSuppressFunc: masterAuthClientCertCfgSuppress,
Type: schema.TypeBool,
Required: true,
ForceNew: true,
},
},
},
Expand Down Expand Up @@ -1920,16 +1923,17 @@ func expandMasterAuth(configured interface{}) *containerBeta.MasterAuth {
Username: masterAuth["username"].(string),
Password: masterAuth["password"].(string),
}
if _, ok := masterAuth["client_certificate_config"]; ok {
if len(masterAuth["client_certificate_config"].([]interface{})) > 0 {

if v, ok := masterAuth["client_certificate_config"]; ok {
if len(v.([]interface{})) > 0 {
clientCertificateConfig := masterAuth["client_certificate_config"].([]interface{})[0].(map[string]interface{})
if _, ok := clientCertificateConfig["issue_client_certificate"]; ok {
result.ClientCertificateConfig = &containerBeta.ClientCertificateConfig{
IssueClientCertificate: clientCertificateConfig["issue_client_certificate"].(bool),
}

result.ClientCertificateConfig = &containerBeta.ClientCertificateConfig{
IssueClientCertificate: clientCertificateConfig["issue_client_certificate"].(bool),
}
}
}

return result
}

Expand Down Expand Up @@ -2206,11 +2210,18 @@ func flattenMasterAuth(ma *containerBeta.MasterAuth) []map[string]interface{} {
"cluster_ca_certificate": ma.ClusterCaCertificate,
},
}
if len(ma.ClientCertificate) == 0 {
masterAuth[0]["client_certificate_config"] = []map[string]interface{}{
{"issue_client_certificate": false},
}

// No version of the GKE API returns the client_certificate_config value.
// Instead, we need to infer whether or not it was set based on the
// client cert being returned from the API or not.
// Previous versions of the provider didn't record anything in state when
// a client cert was enabled, only setting the block when it was false.
masterAuth[0]["client_certificate_config"] = []map[string]interface{}{
{
"issue_client_certificate": len(ma.ClientCertificate) != 0,
},
}

return masterAuth
}

Expand Down Expand Up @@ -2344,30 +2355,6 @@ func cidrOrSizeDiffSuppress(k, old, new string, d *schema.ResourceData) bool {
return strings.HasPrefix(new, "/") && strings.HasSuffix(old, new)
}

// We want to suppress diffs for empty or default client certificate configs, i.e:
// [{ "issue_client_certificate": true}] --> []
// [] -> [{ "issue_client_certificate": true}]
func masterAuthClientCertCfgSuppress(k, old, new string, r *schema.ResourceData) bool {
var clientConfig map[string]interface{}
if v, ok := r.GetOk("master_auth"); ok {
masterAuths := v.([]interface{})
masterAuth := masterAuths[0].(map[string]interface{})
cfgs := masterAuth["client_certificate_config"].([]interface{})
if len(cfgs) > 0 {
clientConfig = cfgs[0].(map[string]interface{})
}
}

if strings.HasSuffix(k, "client_certificate_config.#") && old == "0" && new == "1" {
// nil --> { "issue_client_certificate": true }
if issueCert, ok := clientConfig["issue_client_certificate"]; ok {
return issueCert.(bool)
}
}

return strings.HasSuffix(k, ".issue_client_certificate") && old == "" && new == "true"
}

// We want to suppress diffs for empty/disabled private cluster config.
func containerClusterPrivateClusterConfigSuppress(k, old, new string, d *schema.ResourceData) bool {
o, n := d.GetChange("private_cluster_config.0.enable_private_endpoint")
Expand Down
1 change: 1 addition & 0 deletions google-beta/resource_sql_database_instance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/hashicorp/terraform/helper/acctest"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"

sqladmin "google.golang.org/api/sqladmin/v1beta4"
)

Expand Down

0 comments on commit 76188a2

Please sign in to comment.