From 4ac662452c1c81511463581a5fa0e1d44bff446a Mon Sep 17 00:00:00 2001 From: "Wax, Bence" Date: Thu, 10 Jun 2021 18:22:28 +0200 Subject: [PATCH 01/12] Tier added as a required input --- internal/provider/resource_vault_cluster.go | 26 +++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/internal/provider/resource_vault_cluster.go b/internal/provider/resource_vault_cluster.go index f1d79566e..da251fce4 100644 --- a/internal/provider/resource_vault_cluster.go +++ b/internal/provider/resource_vault_cluster.go @@ -56,6 +56,13 @@ func resourceVaultCluster() *schema.Resource { ForceNew: true, ValidateDiagFunc: validateSlugID, }, + "tier": { + Description: "Tier of the HCP Vault cluster. Valid options for tiers - `development`, `small`, `medium`, `large`", + Type: schema.TypeString, + Required: true, + ForceNew: true, + ValidateDiagFunc: validateSlugID, + }, // optional fields "public_endpoint": { Description: "Denotes that the cluster has a public endpoint. Defaults to false.", @@ -72,9 +79,8 @@ func resourceVaultCluster() *schema.Resource { ForceNew: true, }, // computed outputs - // TODO: once more tiers are supported and can be changed by users, make this a required input. "tier": { - Description: "The tier that the HCP Vault cluster will be provisioned as. Only 'development' is available at this time.", + Description: "The tier that the HCP Vault cluster will be provisioned as.", Type: schema.TypeString, Computed: true, }, @@ -169,8 +175,20 @@ func resourceVaultClusterCreate(ctx context.Context, d *schema.ResourceData, met publicEndpoint := d.Get("public_endpoint").(bool) - // TODO: Tier is hard-coded for now, but eventually will be required input on the resource. - tier := vaultmodels.HashicorpCloudVault20201125TierDEV + //tiers from here: https://github.com/hashicorp/hcp-sdk-go/blob/f7f0dd4b49fd46758c2d2c9a151fe3a70e5e271e/clients/cloud-vault-service/preview/2020-11-25/models/hashicorp_cloud_vault20201125_tier.go#L21 + t, err := d.Get("tier").(string) + switch t { + case "development": + tier := vaultmodels.HashicorpCloudVault20201125TierDEV + case "small": + tier := vaultmodels.HashicorpCloudVault20201125TierSTANDARDSMALL + case "medium": + tier := vaultmodels.HashicorpCloudVault20201125TierSTANDARDMEDIUM + case "large": + tier := vaultmodels.HashicorpCloudVault20201125TierSTANDARDLARGE + default: + return diag.Errorf("unable to create Vault cluster (%s): %s is an invalid tier. Valid tiers are 'development', 'small', 'medium', 'large'", clusterID, err) + } log.Printf("[INFO] Creating Vault cluster (%s)", clusterID) From 5b3f239aa760306b936dd30d1d89ce3b5bc67e4f Mon Sep 17 00:00:00 2001 From: "Wax, Bence" Date: Thu, 10 Jun 2021 18:34:12 +0200 Subject: [PATCH 02/12] Docu updated --- docs/resources/vault_cluster.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/docs/resources/vault_cluster.md b/docs/resources/vault_cluster.md index 5a45ef647..4227cae45 100644 --- a/docs/resources/vault_cluster.md +++ b/docs/resources/vault_cluster.md @@ -23,6 +23,7 @@ resource "hcp_hvn" "example" { resource "hcp_vault_cluster" "example" { cluster_id = "vault-cluster" hvn_id = hcp_hvn.example.hvn_id + tier = "small" } ``` @@ -33,6 +34,7 @@ resource "hcp_vault_cluster" "example" { - **cluster_id** (String) The ID of the HCP Vault cluster. - **hvn_id** (String) The ID of the HVN this HCP Vault cluster is associated to. +- **tier** (String) Tier of the HCP Vault cluster. Valid options for tiers - `development`, `small`, `medium`, `large`. ### Optional @@ -49,7 +51,7 @@ resource "hcp_vault_cluster" "example" { - **organization_id** (String) The ID of the organization this HCP Vault cluster is located in. - **project_id** (String) The ID of the project this HCP Vault cluster is located in. - **region** (String) The region where the HCP Vault cluster is located. -- **tier** (String) The tier that the HCP Vault cluster will be provisioned as. Only 'development' is available at this time. +- **tier** (String) The tier that the HCP Vault cluster will be provisioned as. - **vault_private_endpoint_url** (String) The private URL for the Vault cluster. - **vault_public_endpoint_url** (String) The public URL for the Vault cluster. This will be empty if `public_endpoint` is `false`. - **vault_version** (String) The Vault version of the cluster. From 6b6d6b303afad5b9269e0ac6d7857aa53c6f78ec Mon Sep 17 00:00:00 2001 From: "Wax, Bence" Date: Fri, 11 Jun 2021 10:04:21 +0200 Subject: [PATCH 03/12] tier explicit output removed; 'Computed: true' for tier input; accepted tiers got 'standard_' prefix --- docs/resources/vault_cluster.md | 2 +- internal/provider/resource_vault_cluster.go | 15 ++++++--------- 2 files changed, 7 insertions(+), 10 deletions(-) diff --git a/docs/resources/vault_cluster.md b/docs/resources/vault_cluster.md index 4227cae45..a227a8fa3 100644 --- a/docs/resources/vault_cluster.md +++ b/docs/resources/vault_cluster.md @@ -34,7 +34,7 @@ resource "hcp_vault_cluster" "example" { - **cluster_id** (String) The ID of the HCP Vault cluster. - **hvn_id** (String) The ID of the HVN this HCP Vault cluster is associated to. -- **tier** (String) Tier of the HCP Vault cluster. Valid options for tiers - `development`, `small`, `medium`, `large`. +- **tier** (String) Tier of the HCP Vault cluster. Valid options for tiers - `development`, `standard_small`, `standard_medium`, `standard_large`. ### Optional diff --git a/internal/provider/resource_vault_cluster.go b/internal/provider/resource_vault_cluster.go index da251fce4..9c6bbac57 100644 --- a/internal/provider/resource_vault_cluster.go +++ b/internal/provider/resource_vault_cluster.go @@ -57,8 +57,9 @@ func resourceVaultCluster() *schema.Resource { ValidateDiagFunc: validateSlugID, }, "tier": { - Description: "Tier of the HCP Vault cluster. Valid options for tiers - `development`, `small`, `medium`, `large`", + Description: "Tier of the HCP Vault cluster. Valid options for tiers - `development`, `standard_small`, `standard_medium`, `standard_large`", Type: schema.TypeString, + Computed: true, Required: true, ForceNew: true, ValidateDiagFunc: validateSlugID, @@ -79,11 +80,6 @@ func resourceVaultCluster() *schema.Resource { ForceNew: true, }, // computed outputs - "tier": { - Description: "The tier that the HCP Vault cluster will be provisioned as.", - Type: schema.TypeString, - Computed: true, - }, "organization_id": { Description: "The ID of the organization this HCP Vault cluster is located in.", Type: schema.TypeString, @@ -176,15 +172,16 @@ func resourceVaultClusterCreate(ctx context.Context, d *schema.ResourceData, met publicEndpoint := d.Get("public_endpoint").(bool) //tiers from here: https://github.com/hashicorp/hcp-sdk-go/blob/f7f0dd4b49fd46758c2d2c9a151fe3a70e5e271e/clients/cloud-vault-service/preview/2020-11-25/models/hashicorp_cloud_vault20201125_tier.go#L21 + //TODO: it is less hardcoded now, but a patch to introduce `size` would be ideal after {vault,consule}models update to bring consistency between {vault,consul}_cluster code t, err := d.Get("tier").(string) switch t { case "development": tier := vaultmodels.HashicorpCloudVault20201125TierDEV - case "small": + case "standard_small": tier := vaultmodels.HashicorpCloudVault20201125TierSTANDARDSMALL - case "medium": + case "standard_medium": tier := vaultmodels.HashicorpCloudVault20201125TierSTANDARDMEDIUM - case "large": + case "standard_large": tier := vaultmodels.HashicorpCloudVault20201125TierSTANDARDLARGE default: return diag.Errorf("unable to create Vault cluster (%s): %s is an invalid tier. Valid tiers are 'development', 'small', 'medium', 'large'", clusterID, err) From cc3b094a3bab1812c7d1f921fd063e92ef16fdc5 Mon Sep 17 00:00:00 2001 From: "Wax, Bence" Date: Fri, 11 Jun 2021 10:26:16 +0200 Subject: [PATCH 04/12] fmt --- internal/provider/resource_vault_cluster.go | 24 ++++++++++----------- 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/internal/provider/resource_vault_cluster.go b/internal/provider/resource_vault_cluster.go index 9c6bbac57..2d000e74a 100644 --- a/internal/provider/resource_vault_cluster.go +++ b/internal/provider/resource_vault_cluster.go @@ -59,7 +59,7 @@ func resourceVaultCluster() *schema.Resource { "tier": { Description: "Tier of the HCP Vault cluster. Valid options for tiers - `development`, `standard_small`, `standard_medium`, `standard_large`", Type: schema.TypeString, - Computed: true, + Computed: true, Required: true, ForceNew: true, ValidateDiagFunc: validateSlugID, @@ -175,17 +175,17 @@ func resourceVaultClusterCreate(ctx context.Context, d *schema.ResourceData, met //TODO: it is less hardcoded now, but a patch to introduce `size` would be ideal after {vault,consule}models update to bring consistency between {vault,consul}_cluster code t, err := d.Get("tier").(string) switch t { - case "development": - tier := vaultmodels.HashicorpCloudVault20201125TierDEV - case "standard_small": - tier := vaultmodels.HashicorpCloudVault20201125TierSTANDARDSMALL - case "standard_medium": - tier := vaultmodels.HashicorpCloudVault20201125TierSTANDARDMEDIUM - case "standard_large": - tier := vaultmodels.HashicorpCloudVault20201125TierSTANDARDLARGE - default: - return diag.Errorf("unable to create Vault cluster (%s): %s is an invalid tier. Valid tiers are 'development', 'small', 'medium', 'large'", clusterID, err) - } + case "development": + tier := vaultmodels.HashicorpCloudVault20201125TierDEV + case "standard_small": + tier := vaultmodels.HashicorpCloudVault20201125TierSTANDARDSMALL + case "standard_medium": + tier := vaultmodels.HashicorpCloudVault20201125TierSTANDARDMEDIUM + case "standard_large": + tier := vaultmodels.HashicorpCloudVault20201125TierSTANDARDLARGE + default: + return diag.Errorf("unable to create Vault cluster (%s): %s is an invalid tier. Valid tiers are 'development', 'small', 'medium', 'large'", clusterID, err) + } log.Printf("[INFO] Creating Vault cluster (%s)", clusterID) From e723c662b8288e7606dd06f0f887ffac59a4cc89 Mon Sep 17 00:00:00 2001 From: "Wax, Bence" Date: Fri, 11 Jun 2021 14:06:02 +0200 Subject: [PATCH 05/12] switch ditched; tests added; validator added --- internal/provider/resource_vault_cluster.go | 26 +++++-------------- ...resource_vault_cluster_admin_token_test.go | 3 ++- .../provider/resource_vault_cluster_test.go | 5 ++-- internal/provider/validators.go | 21 +++++++++++++++ 4 files changed, 33 insertions(+), 22 deletions(-) diff --git a/internal/provider/resource_vault_cluster.go b/internal/provider/resource_vault_cluster.go index 2d000e74a..1842da4b6 100644 --- a/internal/provider/resource_vault_cluster.go +++ b/internal/provider/resource_vault_cluster.go @@ -3,6 +3,7 @@ package provider import ( "context" "log" + "strings" "time" sharedmodels "github.com/hashicorp/hcp-sdk-go/clients/cloud-shared/v1/models" @@ -59,10 +60,12 @@ func resourceVaultCluster() *schema.Resource { "tier": { Description: "Tier of the HCP Vault cluster. Valid options for tiers - `development`, `standard_small`, `standard_medium`, `standard_large`", Type: schema.TypeString, - Computed: true, Required: true, ForceNew: true, - ValidateDiagFunc: validateSlugID, + ValidateDiagFunc: validateVaultClusterTier, + DiffSuppressFunc: func(_, old, new string, _ *schema.ResourceData) bool { + return strings.ToLower(old) == strings.ToLower(new) + }, }, // optional fields "public_endpoint": { @@ -171,22 +174,6 @@ func resourceVaultClusterCreate(ctx context.Context, d *schema.ResourceData, met publicEndpoint := d.Get("public_endpoint").(bool) - //tiers from here: https://github.com/hashicorp/hcp-sdk-go/blob/f7f0dd4b49fd46758c2d2c9a151fe3a70e5e271e/clients/cloud-vault-service/preview/2020-11-25/models/hashicorp_cloud_vault20201125_tier.go#L21 - //TODO: it is less hardcoded now, but a patch to introduce `size` would be ideal after {vault,consule}models update to bring consistency between {vault,consul}_cluster code - t, err := d.Get("tier").(string) - switch t { - case "development": - tier := vaultmodels.HashicorpCloudVault20201125TierDEV - case "standard_small": - tier := vaultmodels.HashicorpCloudVault20201125TierSTANDARDSMALL - case "standard_medium": - tier := vaultmodels.HashicorpCloudVault20201125TierSTANDARDMEDIUM - case "standard_large": - tier := vaultmodels.HashicorpCloudVault20201125TierSTANDARDLARGE - default: - return diag.Errorf("unable to create Vault cluster (%s): %s is an invalid tier. Valid tiers are 'development', 'small', 'medium', 'large'", clusterID, err) - } - log.Printf("[INFO] Creating Vault cluster (%s)", clusterID) vaultCuster := &vaultmodels.HashicorpCloudVault20201125InputCluster{ @@ -194,7 +181,8 @@ func resourceVaultClusterCreate(ctx context.Context, d *schema.ResourceData, met VaultConfig: &vaultmodels.HashicorpCloudVault20201125VaultConfig{ InitialVersion: vaultVersion, }, - Tier: tier, + //TODO: HashicorpCloudVault20201125Tier still hardcoded as in consul + Tier: vaultmodels.HashicorpCloudVault20201125Tier(strings.Replace(strings.ToUpper(d.Get("tier").(string)), "_", "", -1)), NetworkConfig: &vaultmodels.HashicorpCloudVault20201125InputNetworkConfig{ NetworkID: hvn.ID, PublicIpsEnabled: publicEndpoint, diff --git a/internal/provider/resource_vault_cluster_admin_token_test.go b/internal/provider/resource_vault_cluster_admin_token_test.go index 8dd7db596..396e7e06e 100644 --- a/internal/provider/resource_vault_cluster_admin_token_test.go +++ b/internal/provider/resource_vault_cluster_admin_token_test.go @@ -14,10 +14,11 @@ resource "hcp_hvn" "test" { cloud_provider = "aws" region = "us-west-2" } - + resource "hcp_vault_cluster" "test" { cluster_id = "test-vault-cluster" hvn_id = hcp_hvn.test.hvn_id + tier = "standard_small" } resource "hcp_vault_cluster_admin_token" "test" { diff --git a/internal/provider/resource_vault_cluster_test.go b/internal/provider/resource_vault_cluster_test.go index 636ff5e15..2f11a1cf9 100644 --- a/internal/provider/resource_vault_cluster_test.go +++ b/internal/provider/resource_vault_cluster_test.go @@ -20,6 +20,7 @@ resource "hcp_hvn" "test" { resource "hcp_vault_cluster" "test" { cluster_id = "test-vault-cluster" hvn_id = hcp_hvn.test.hvn_id + tier = "standard_small" } data "hcp_vault_cluster" "test" { @@ -45,7 +46,7 @@ func TestAccVaultCluster(t *testing.T) { testAccCheckVaultClusterExists(resourceName), resource.TestCheckResourceAttr(resourceName, "cluster_id", "test-vault-cluster"), resource.TestCheckResourceAttr(resourceName, "hvn_id", "test-hvn"), - resource.TestCheckResourceAttr(resourceName, "tier", "DEV"), + resource.TestCheckResourceAttr(resourceName, "tier", "standard_small"), resource.TestCheckResourceAttr(resourceName, "cloud_provider", "aws"), resource.TestCheckResourceAttr(resourceName, "region", "us-west-2"), resource.TestCheckResourceAttr(resourceName, "public_endpoint", "false"), @@ -79,7 +80,7 @@ func TestAccVaultCluster(t *testing.T) { testAccCheckVaultClusterExists(resourceName), resource.TestCheckResourceAttr(resourceName, "cluster_id", "test-vault-cluster"), resource.TestCheckResourceAttr(resourceName, "hvn_id", "test-hvn"), - resource.TestCheckResourceAttr(resourceName, "tier", "DEV"), + resource.TestCheckResourceAttr(resourceName, "tier", "standard_small"), resource.TestCheckResourceAttr(resourceName, "cloud_provider", "aws"), resource.TestCheckResourceAttr(resourceName, "region", "us-west-2"), resource.TestCheckResourceAttr(resourceName, "public_endpoint", "false"), diff --git a/internal/provider/validators.go b/internal/provider/validators.go index 5f2ee5044..7fdf67252 100644 --- a/internal/provider/validators.go +++ b/internal/provider/validators.go @@ -8,6 +8,7 @@ import ( "github.com/go-openapi/strfmt" "github.com/hashicorp/go-cty/cty" consulmodels "github.com/hashicorp/hcp-sdk-go/clients/cloud-consul-service/preview/2021-02-04/models" + vaultmodels "github.com/hashicorp/hcp-sdk-go/clients/cloud-vault-service/preview/2020-11-25/models" "github.com/hashicorp/terraform-plugin-sdk/v2/diag" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" ) @@ -148,3 +149,23 @@ func validateConsulClusterSize(v interface{}, path cty.Path) diag.Diagnostics { return diagnostics } + +func validateVaultClusterTier(v interface{}, path cty.Path) diag.Diagnostics { + var diagnostics diag.Diagnostics + + // TODO: Update the validation once vaultmodels got consistent with consulmodels + err := vaultmodels.HashicorpCloudVault20201125Tier(strings.ToUpper(v.(string))).Validate(strfmt.Default) + if err != nil { + enumList := regexp.MustCompile(`\[.*\]`).FindString(err.Error()) + expectedEnumList := strings.ToLower(enumList) + msg := fmt.Sprintf("expected %v to be one of: %v", v, expectedEnumList) + diagnostics = append(diagnostics, diag.Diagnostic{ + Severity: diag.Error, + Summary: msg, + Detail: msg + " (value is case-insensitive).", + AttributePath: path, + }) + } + + return diagnostics +} From b13d8db1f2d6dfe8b69a6170551a58ef106cf084 Mon Sep 17 00:00:00 2001 From: "Wax, Bence" Date: Fri, 11 Jun 2021 19:46:12 +0200 Subject: [PATCH 06/12] underscore and check case --- internal/provider/resource_vault_cluster.go | 2 +- internal/provider/resource_vault_cluster_test.go | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/internal/provider/resource_vault_cluster.go b/internal/provider/resource_vault_cluster.go index 1842da4b6..026365b67 100644 --- a/internal/provider/resource_vault_cluster.go +++ b/internal/provider/resource_vault_cluster.go @@ -182,7 +182,7 @@ func resourceVaultClusterCreate(ctx context.Context, d *schema.ResourceData, met InitialVersion: vaultVersion, }, //TODO: HashicorpCloudVault20201125Tier still hardcoded as in consul - Tier: vaultmodels.HashicorpCloudVault20201125Tier(strings.Replace(strings.ToUpper(d.Get("tier").(string)), "_", "", -1)), + Tier: vaultmodels.HashicorpCloudVault20201125Tier(strings.ToUpper(d.Get("tier").(string))), NetworkConfig: &vaultmodels.HashicorpCloudVault20201125InputNetworkConfig{ NetworkID: hvn.ID, PublicIpsEnabled: publicEndpoint, diff --git a/internal/provider/resource_vault_cluster_test.go b/internal/provider/resource_vault_cluster_test.go index 2f11a1cf9..43e5bc95d 100644 --- a/internal/provider/resource_vault_cluster_test.go +++ b/internal/provider/resource_vault_cluster_test.go @@ -46,7 +46,7 @@ func TestAccVaultCluster(t *testing.T) { testAccCheckVaultClusterExists(resourceName), resource.TestCheckResourceAttr(resourceName, "cluster_id", "test-vault-cluster"), resource.TestCheckResourceAttr(resourceName, "hvn_id", "test-hvn"), - resource.TestCheckResourceAttr(resourceName, "tier", "standard_small"), + resource.TestCheckResourceAttr(resourceName, "tier", "STANDARD_SMALL"), resource.TestCheckResourceAttr(resourceName, "cloud_provider", "aws"), resource.TestCheckResourceAttr(resourceName, "region", "us-west-2"), resource.TestCheckResourceAttr(resourceName, "public_endpoint", "false"), @@ -80,7 +80,7 @@ func TestAccVaultCluster(t *testing.T) { testAccCheckVaultClusterExists(resourceName), resource.TestCheckResourceAttr(resourceName, "cluster_id", "test-vault-cluster"), resource.TestCheckResourceAttr(resourceName, "hvn_id", "test-hvn"), - resource.TestCheckResourceAttr(resourceName, "tier", "standard_small"), + resource.TestCheckResourceAttr(resourceName, "tier", "STANDARD_SMALL"), resource.TestCheckResourceAttr(resourceName, "cloud_provider", "aws"), resource.TestCheckResourceAttr(resourceName, "region", "us-west-2"), resource.TestCheckResourceAttr(resourceName, "public_endpoint", "false"), From 9ed7b4e66d66b6e40d8975288381963991318db2 Mon Sep 17 00:00:00 2001 From: "Wax, Bence" Date: Fri, 11 Jun 2021 19:52:59 +0200 Subject: [PATCH 07/12] Compute added, required -> false --- internal/provider/resource_vault_cluster.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/internal/provider/resource_vault_cluster.go b/internal/provider/resource_vault_cluster.go index 026365b67..2660ab539 100644 --- a/internal/provider/resource_vault_cluster.go +++ b/internal/provider/resource_vault_cluster.go @@ -60,8 +60,9 @@ func resourceVaultCluster() *schema.Resource { "tier": { Description: "Tier of the HCP Vault cluster. Valid options for tiers - `development`, `standard_small`, `standard_medium`, `standard_large`", Type: schema.TypeString, - Required: true, + Required: false, ForceNew: true, + Computed: true, ValidateDiagFunc: validateVaultClusterTier, DiffSuppressFunc: func(_, old, new string, _ *schema.ResourceData) bool { return strings.ToLower(old) == strings.ToLower(new) From 867c9ca3ce4acc4cd63a7419e46ba97937976e26 Mon Sep 17 00:00:00 2001 From: "Wax, Bence" Date: Fri, 11 Jun 2021 19:57:27 +0200 Subject: [PATCH 08/12] go generate --- docs/resources/vault_cluster.md | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/docs/resources/vault_cluster.md b/docs/resources/vault_cluster.md index a227a8fa3..596823681 100644 --- a/docs/resources/vault_cluster.md +++ b/docs/resources/vault_cluster.md @@ -23,7 +23,6 @@ resource "hcp_hvn" "example" { resource "hcp_vault_cluster" "example" { cluster_id = "vault-cluster" hvn_id = hcp_hvn.example.hvn_id - tier = "small" } ``` @@ -34,7 +33,6 @@ resource "hcp_vault_cluster" "example" { - **cluster_id** (String) The ID of the HCP Vault cluster. - **hvn_id** (String) The ID of the HVN this HCP Vault cluster is associated to. -- **tier** (String) Tier of the HCP Vault cluster. Valid options for tiers - `development`, `standard_small`, `standard_medium`, `standard_large`. ### Optional @@ -51,7 +49,7 @@ resource "hcp_vault_cluster" "example" { - **organization_id** (String) The ID of the organization this HCP Vault cluster is located in. - **project_id** (String) The ID of the project this HCP Vault cluster is located in. - **region** (String) The region where the HCP Vault cluster is located. -- **tier** (String) The tier that the HCP Vault cluster will be provisioned as. +- **tier** (String) Tier of the HCP Vault cluster. Valid options for tiers - `development`, `standard_small`, `standard_medium`, `standard_large` - **vault_private_endpoint_url** (String) The private URL for the Vault cluster. - **vault_public_endpoint_url** (String) The public URL for the Vault cluster. This will be empty if `public_endpoint` is `false`. - **vault_version** (String) The Vault version of the cluster. From 03d51582190fe224ec80253eeb8586a9fe3021ff Mon Sep 17 00:00:00 2001 From: "Wax, Bence" Date: Fri, 11 Jun 2021 20:38:31 +0200 Subject: [PATCH 09/12] removed commend, Required -> Optional --- internal/provider/resource_vault_cluster.go | 2 +- internal/provider/validators.go | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/internal/provider/resource_vault_cluster.go b/internal/provider/resource_vault_cluster.go index 2660ab539..e68fcdf46 100644 --- a/internal/provider/resource_vault_cluster.go +++ b/internal/provider/resource_vault_cluster.go @@ -60,7 +60,7 @@ func resourceVaultCluster() *schema.Resource { "tier": { Description: "Tier of the HCP Vault cluster. Valid options for tiers - `development`, `standard_small`, `standard_medium`, `standard_large`", Type: schema.TypeString, - Required: false, + Optional: true, ForceNew: true, Computed: true, ValidateDiagFunc: validateVaultClusterTier, diff --git a/internal/provider/validators.go b/internal/provider/validators.go index 7fdf67252..a4df78b1b 100644 --- a/internal/provider/validators.go +++ b/internal/provider/validators.go @@ -153,7 +153,6 @@ func validateConsulClusterSize(v interface{}, path cty.Path) diag.Diagnostics { func validateVaultClusterTier(v interface{}, path cty.Path) diag.Diagnostics { var diagnostics diag.Diagnostics - // TODO: Update the validation once vaultmodels got consistent with consulmodels err := vaultmodels.HashicorpCloudVault20201125Tier(strings.ToUpper(v.(string))).Validate(strfmt.Default) if err != nil { enumList := regexp.MustCompile(`\[.*\]`).FindString(err.Error()) From 0031714345e1ac68cb3efa807bec631ea016f381 Mon Sep 17 00:00:00 2001 From: "Wax, Bence" Date: Fri, 11 Jun 2021 21:31:27 +0200 Subject: [PATCH 10/12] go generate --- docs/resources/vault_cluster.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/resources/vault_cluster.md b/docs/resources/vault_cluster.md index 596823681..ea649b25c 100644 --- a/docs/resources/vault_cluster.md +++ b/docs/resources/vault_cluster.md @@ -39,6 +39,7 @@ resource "hcp_vault_cluster" "example" { - **id** (String) The ID of this resource. - **min_vault_version** (String) The minimum Vault version to use when creating the cluster. If not specified, it is defaulted to the version that is currently recommended by HCP. - **public_endpoint** (Boolean) Denotes that the cluster has a public endpoint. Defaults to false. +- **tier** (String) Tier of the HCP Vault cluster. Valid options for tiers - `development`, `standard_small`, `standard_medium`, `standard_large` - **timeouts** (Block, Optional) (see [below for nested schema](#nestedblock--timeouts)) ### Read-Only @@ -49,7 +50,6 @@ resource "hcp_vault_cluster" "example" { - **organization_id** (String) The ID of the organization this HCP Vault cluster is located in. - **project_id** (String) The ID of the project this HCP Vault cluster is located in. - **region** (String) The region where the HCP Vault cluster is located. -- **tier** (String) Tier of the HCP Vault cluster. Valid options for tiers - `development`, `standard_small`, `standard_medium`, `standard_large` - **vault_private_endpoint_url** (String) The private URL for the Vault cluster. - **vault_public_endpoint_url** (String) The public URL for the Vault cluster. This will be empty if `public_endpoint` is `false`. - **vault_version** (String) The Vault version of the cluster. From 78231f7c2fe9d265d427f65f5c611d91923e693a Mon Sep 17 00:00:00 2001 From: "Wax, Bence" Date: Mon, 14 Jun 2021 19:31:05 +0200 Subject: [PATCH 11/12] internal tier attribute translation from 'development' to 'dev' --- internal/provider/resource_vault_cluster.go | 17 +++++++++++++---- .../provider/resource_vault_cluster_test.go | 6 +++--- internal/provider/validators.go | 7 ++++++- 3 files changed, 22 insertions(+), 8 deletions(-) diff --git a/internal/provider/resource_vault_cluster.go b/internal/provider/resource_vault_cluster.go index e68fcdf46..7fcfaa56d 100644 --- a/internal/provider/resource_vault_cluster.go +++ b/internal/provider/resource_vault_cluster.go @@ -64,7 +64,7 @@ func resourceVaultCluster() *schema.Resource { ForceNew: true, Computed: true, ValidateDiagFunc: validateVaultClusterTier, - DiffSuppressFunc: func(_, old, new string, _ *schema.ResourceData) bool { + DiffSuppressFunc: func(_, old, new string, d *schema.ResourceData) bool { return strings.ToLower(old) == strings.ToLower(new) }, }, @@ -177,13 +177,18 @@ func resourceVaultClusterCreate(ctx context.Context, d *schema.ResourceData, met log.Printf("[INFO] Creating Vault cluster (%s)", clusterID) + clusterTier := strings.ToUpper(d.Get("tier").(string)) + //TODO: remove this when API will be 'DEVELOPMENT' + if strings.ToLower(clusterTier) == "development" { + clusterTier = "DEV" + } + vaultCuster := &vaultmodels.HashicorpCloudVault20201125InputCluster{ Config: &vaultmodels.HashicorpCloudVault20201125InputClusterConfig{ VaultConfig: &vaultmodels.HashicorpCloudVault20201125VaultConfig{ InitialVersion: vaultVersion, }, - //TODO: HashicorpCloudVault20201125Tier still hardcoded as in consul - Tier: vaultmodels.HashicorpCloudVault20201125Tier(strings.ToUpper(d.Get("tier").(string))), + Tier: vaultmodels.HashicorpCloudVault20201125Tier(clusterTier), NetworkConfig: &vaultmodels.HashicorpCloudVault20201125InputNetworkConfig{ NetworkID: hvn.ID, PublicIpsEnabled: publicEndpoint, @@ -214,6 +219,7 @@ func resourceVaultClusterCreate(ctx context.Context, d *schema.ResourceData, met // Get the created Vault cluster. cluster, err := clients.GetVaultClusterByID(ctx, client, loc, payload.ClusterID) + if err != nil { return diag.Errorf("unable to retrieve Vault cluster (%s): %v", payload.ClusterID, err) } @@ -322,7 +328,10 @@ func setVaultClusterResourceData(d *schema.ResourceData, cluster *vaultmodels.Ha if err := d.Set("region", cluster.Location.Region.Region); err != nil { return err } - + //TODO: remove this when API will be 'DEVELOPMENT' + if cluster.Config.Tier == "DEV" { + cluster.Config.Tier = "DEVELOPMENT" + } if err := d.Set("tier", cluster.Config.Tier); err != nil { return err } diff --git a/internal/provider/resource_vault_cluster_test.go b/internal/provider/resource_vault_cluster_test.go index 43e5bc95d..154523434 100644 --- a/internal/provider/resource_vault_cluster_test.go +++ b/internal/provider/resource_vault_cluster_test.go @@ -20,7 +20,7 @@ resource "hcp_hvn" "test" { resource "hcp_vault_cluster" "test" { cluster_id = "test-vault-cluster" hvn_id = hcp_hvn.test.hvn_id - tier = "standard_small" + tier = "development" } data "hcp_vault_cluster" "test" { @@ -46,7 +46,7 @@ func TestAccVaultCluster(t *testing.T) { testAccCheckVaultClusterExists(resourceName), resource.TestCheckResourceAttr(resourceName, "cluster_id", "test-vault-cluster"), resource.TestCheckResourceAttr(resourceName, "hvn_id", "test-hvn"), - resource.TestCheckResourceAttr(resourceName, "tier", "STANDARD_SMALL"), + resource.TestCheckResourceAttr(resourceName, "tier", "DEVELOPMENT"), resource.TestCheckResourceAttr(resourceName, "cloud_provider", "aws"), resource.TestCheckResourceAttr(resourceName, "region", "us-west-2"), resource.TestCheckResourceAttr(resourceName, "public_endpoint", "false"), @@ -80,7 +80,7 @@ func TestAccVaultCluster(t *testing.T) { testAccCheckVaultClusterExists(resourceName), resource.TestCheckResourceAttr(resourceName, "cluster_id", "test-vault-cluster"), resource.TestCheckResourceAttr(resourceName, "hvn_id", "test-hvn"), - resource.TestCheckResourceAttr(resourceName, "tier", "STANDARD_SMALL"), + resource.TestCheckResourceAttr(resourceName, "tier", "DEVELOPMENT"), resource.TestCheckResourceAttr(resourceName, "cloud_provider", "aws"), resource.TestCheckResourceAttr(resourceName, "region", "us-west-2"), resource.TestCheckResourceAttr(resourceName, "public_endpoint", "false"), diff --git a/internal/provider/validators.go b/internal/provider/validators.go index a4df78b1b..b5bb5bd11 100644 --- a/internal/provider/validators.go +++ b/internal/provider/validators.go @@ -153,10 +153,15 @@ func validateConsulClusterSize(v interface{}, path cty.Path) diag.Diagnostics { func validateVaultClusterTier(v interface{}, path cty.Path) diag.Diagnostics { var diagnostics diag.Diagnostics + //TODO: remove this when API will be 'DEVELOPMENT' + if strings.ToLower(v.(string)) == "development" { + v = "dev" + } + err := vaultmodels.HashicorpCloudVault20201125Tier(strings.ToUpper(v.(string))).Validate(strfmt.Default) if err != nil { enumList := regexp.MustCompile(`\[.*\]`).FindString(err.Error()) - expectedEnumList := strings.ToLower(enumList) + expectedEnumList := strings.ToLower(strings.Replace(enumList, "DEV", "DEVELOPMENT", 1)) //TODO: strings.ToLower(enumList) when API updated (DEV -> DEVELOPMENT) msg := fmt.Sprintf("expected %v to be one of: %v", v, expectedEnumList) diagnostics = append(diagnostics, diag.Diagnostic{ Severity: diag.Error, From 060384faf0e0820d9c7d00b3cd114919ffd1caad Mon Sep 17 00:00:00 2001 From: "Wax, Bence" Date: Tue, 15 Jun 2021 10:57:31 +0200 Subject: [PATCH 12/12] tier: development -> dev --- docs/resources/vault_cluster.md | 2 +- internal/provider/resource_vault_cluster.go | 17 ++++------------- .../provider/resource_vault_cluster_test.go | 6 +++--- internal/provider/validators.go | 9 ++------- 4 files changed, 10 insertions(+), 24 deletions(-) diff --git a/docs/resources/vault_cluster.md b/docs/resources/vault_cluster.md index ea649b25c..9b998dcbd 100644 --- a/docs/resources/vault_cluster.md +++ b/docs/resources/vault_cluster.md @@ -39,7 +39,7 @@ resource "hcp_vault_cluster" "example" { - **id** (String) The ID of this resource. - **min_vault_version** (String) The minimum Vault version to use when creating the cluster. If not specified, it is defaulted to the version that is currently recommended by HCP. - **public_endpoint** (Boolean) Denotes that the cluster has a public endpoint. Defaults to false. -- **tier** (String) Tier of the HCP Vault cluster. Valid options for tiers - `development`, `standard_small`, `standard_medium`, `standard_large` +- **tier** (String) Tier of the HCP Vault cluster. Valid options for tiers - `dev`, `standard_small`, `standard_medium`, `standard_large`. - **timeouts** (Block, Optional) (see [below for nested schema](#nestedblock--timeouts)) ### Read-Only diff --git a/internal/provider/resource_vault_cluster.go b/internal/provider/resource_vault_cluster.go index 7fcfaa56d..e83b4d325 100644 --- a/internal/provider/resource_vault_cluster.go +++ b/internal/provider/resource_vault_cluster.go @@ -58,13 +58,13 @@ func resourceVaultCluster() *schema.Resource { ValidateDiagFunc: validateSlugID, }, "tier": { - Description: "Tier of the HCP Vault cluster. Valid options for tiers - `development`, `standard_small`, `standard_medium`, `standard_large`", + Description: "Tier of the HCP Vault cluster. Valid options for tiers - `dev`, `standard_small`, `standard_medium`, `standard_large`.", Type: schema.TypeString, Optional: true, ForceNew: true, Computed: true, ValidateDiagFunc: validateVaultClusterTier, - DiffSuppressFunc: func(_, old, new string, d *schema.ResourceData) bool { + DiffSuppressFunc: func(_, old, new string, _ *schema.ResourceData) bool { return strings.ToLower(old) == strings.ToLower(new) }, }, @@ -177,18 +177,12 @@ func resourceVaultClusterCreate(ctx context.Context, d *schema.ResourceData, met log.Printf("[INFO] Creating Vault cluster (%s)", clusterID) - clusterTier := strings.ToUpper(d.Get("tier").(string)) - //TODO: remove this when API will be 'DEVELOPMENT' - if strings.ToLower(clusterTier) == "development" { - clusterTier = "DEV" - } - vaultCuster := &vaultmodels.HashicorpCloudVault20201125InputCluster{ Config: &vaultmodels.HashicorpCloudVault20201125InputClusterConfig{ VaultConfig: &vaultmodels.HashicorpCloudVault20201125VaultConfig{ InitialVersion: vaultVersion, }, - Tier: vaultmodels.HashicorpCloudVault20201125Tier(clusterTier), + Tier: vaultmodels.HashicorpCloudVault20201125Tier(strings.ToUpper(d.Get("tier").(string))), NetworkConfig: &vaultmodels.HashicorpCloudVault20201125InputNetworkConfig{ NetworkID: hvn.ID, PublicIpsEnabled: publicEndpoint, @@ -328,10 +322,7 @@ func setVaultClusterResourceData(d *schema.ResourceData, cluster *vaultmodels.Ha if err := d.Set("region", cluster.Location.Region.Region); err != nil { return err } - //TODO: remove this when API will be 'DEVELOPMENT' - if cluster.Config.Tier == "DEV" { - cluster.Config.Tier = "DEVELOPMENT" - } + if err := d.Set("tier", cluster.Config.Tier); err != nil { return err } diff --git a/internal/provider/resource_vault_cluster_test.go b/internal/provider/resource_vault_cluster_test.go index 154523434..f44653381 100644 --- a/internal/provider/resource_vault_cluster_test.go +++ b/internal/provider/resource_vault_cluster_test.go @@ -20,7 +20,7 @@ resource "hcp_hvn" "test" { resource "hcp_vault_cluster" "test" { cluster_id = "test-vault-cluster" hvn_id = hcp_hvn.test.hvn_id - tier = "development" + tier = "dev" } data "hcp_vault_cluster" "test" { @@ -46,7 +46,7 @@ func TestAccVaultCluster(t *testing.T) { testAccCheckVaultClusterExists(resourceName), resource.TestCheckResourceAttr(resourceName, "cluster_id", "test-vault-cluster"), resource.TestCheckResourceAttr(resourceName, "hvn_id", "test-hvn"), - resource.TestCheckResourceAttr(resourceName, "tier", "DEVELOPMENT"), + resource.TestCheckResourceAttr(resourceName, "tier", "DEV"), resource.TestCheckResourceAttr(resourceName, "cloud_provider", "aws"), resource.TestCheckResourceAttr(resourceName, "region", "us-west-2"), resource.TestCheckResourceAttr(resourceName, "public_endpoint", "false"), @@ -80,7 +80,7 @@ func TestAccVaultCluster(t *testing.T) { testAccCheckVaultClusterExists(resourceName), resource.TestCheckResourceAttr(resourceName, "cluster_id", "test-vault-cluster"), resource.TestCheckResourceAttr(resourceName, "hvn_id", "test-hvn"), - resource.TestCheckResourceAttr(resourceName, "tier", "DEVELOPMENT"), + resource.TestCheckResourceAttr(resourceName, "tier", "DEV"), resource.TestCheckResourceAttr(resourceName, "cloud_provider", "aws"), resource.TestCheckResourceAttr(resourceName, "region", "us-west-2"), resource.TestCheckResourceAttr(resourceName, "public_endpoint", "false"), diff --git a/internal/provider/validators.go b/internal/provider/validators.go index b5bb5bd11..b5b35c84c 100644 --- a/internal/provider/validators.go +++ b/internal/provider/validators.go @@ -153,16 +153,11 @@ func validateConsulClusterSize(v interface{}, path cty.Path) diag.Diagnostics { func validateVaultClusterTier(v interface{}, path cty.Path) diag.Diagnostics { var diagnostics diag.Diagnostics - //TODO: remove this when API will be 'DEVELOPMENT' - if strings.ToLower(v.(string)) == "development" { - v = "dev" - } - err := vaultmodels.HashicorpCloudVault20201125Tier(strings.ToUpper(v.(string))).Validate(strfmt.Default) if err != nil { enumList := regexp.MustCompile(`\[.*\]`).FindString(err.Error()) - expectedEnumList := strings.ToLower(strings.Replace(enumList, "DEV", "DEVELOPMENT", 1)) //TODO: strings.ToLower(enumList) when API updated (DEV -> DEVELOPMENT) - msg := fmt.Sprintf("expected %v to be one of: %v", v, expectedEnumList) + expectedEnumList := strings.ToLower(enumList) + msg := fmt.Sprintf("expected '%v' to be one of: %v", v, expectedEnumList) diagnostics = append(diagnostics, diag.Diagnostic{ Severity: diag.Error, Summary: msg,