From 89a1ec42026600176be2843b77727b352030e327 Mon Sep 17 00:00:00 2001 From: dave-pollock <20267387+dave-pollock@users.noreply.github.com> Date: Sun, 2 Oct 2022 12:31:47 +1100 Subject: [PATCH] Allow updating connection certificate (#40) * Allow updating connection certificate * Remove calculated flag and add description * Update docs * docs fix * Add id field back to docs --- VERSION | 2 +- docs/resources/connection.md | 4 ++-- pkg/looker/resource_connection.go | 25 ++++++++----------------- pkg/looker/util.go | 10 ++++++++++ 4 files changed, 21 insertions(+), 20 deletions(-) diff --git a/VERSION b/VERSION index 5c5cbb3..021abec 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -0.8.8 \ No newline at end of file +0.8.9 \ No newline at end of file diff --git a/docs/resources/connection.md b/docs/resources/connection.md index a6edc7a..12130ab 100644 --- a/docs/resources/connection.md +++ b/docs/resources/connection.md @@ -55,10 +55,10 @@ resource "looker_connection" "snowflake_connection" { ### Optional - `after_connect_statements` (String) -- `certificate` (String, Sensitive) +- `certificate` (String, Sensitive) Base64 encoded certificate body for server authentication (when appropriate for the dialect). Due to limitations in the Looker API, changes made outside of Terraform cannot be detected. - `db_timezone` (String) - `disable_context_comment` (Boolean) -- `file_type` (String) +- `file_type` (String) Certificate key file type (.json or .p12). - `id` (String) The ID of this resource. - `jdbc_additional_params` (String) - `maintenance_cron` (String) diff --git a/pkg/looker/resource_connection.go b/pkg/looker/resource_connection.go index f9d39df..54860c4 100644 --- a/pkg/looker/resource_connection.go +++ b/pkg/looker/resource_connection.go @@ -52,22 +52,19 @@ func resourceConnection() *schema.Resource { }, }, "certificate": { - Type: schema.TypeString, - Optional: true, - ForceNew: true, - Sensitive: true, - DiffSuppressFunc: func(k, old, new string, d *schema.ResourceData) bool { - return d.Id() != "" - }, + Type: schema.TypeString, + Description: "Base64 encoded certificate body for server authentication (when " + + "appropriate for the dialect). Due to limitations in the Looker " + + "API, changes made outside of Terraform cannot be detected.", + Optional: true, + Sensitive: true, + StateFunc: hash, }, "file_type": { Type: schema.TypeString, + Description: "Certificate key file type (.json or .p12).", Optional: true, - ForceNew: true, ValidateFunc: validation.StringInSlice([]string{".json", ".p12"}, false), - DiffSuppressFunc: func(k, old, new string, d *schema.ResourceData) bool { - return d.Id() != "" - }, }, "database": { Type: schema.TypeString, @@ -480,12 +477,6 @@ func flattenConnection(connection apiclient.DBConnection, d *schema.ResourceData if err := d.Set("password", connection.Password); err != nil { return err } - if err := d.Set("certificate", connection.Certificate); err != nil { - return err - } - if err := d.Set("file_type", connection.FileType); err != nil { - return err - } if err := d.Set("database", connection.Database); err != nil { return err } diff --git a/pkg/looker/util.go b/pkg/looker/util.go index 68ef49e..0724350 100644 --- a/pkg/looker/util.go +++ b/pkg/looker/util.go @@ -1,6 +1,8 @@ package looker import ( + "crypto/sha256" + "encoding/hex" "fmt" "strings" @@ -51,3 +53,11 @@ func flattenStringListToSet(strings []string) *schema.Set { // } // return ints // } + +func hash(val interface{}) string { + if val == nil || val.(string) == "" { + return "" + } + sha := sha256.Sum256([]byte(val.(string))) + return hex.EncodeToString(sha[:]) +}