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

Vault cluster 'tier' added as an optional input #144

Merged
merged 12 commits into from
Jun 15, 2021
4 changes: 3 additions & 1 deletion docs/resources/vault_cluster.md
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
```

Expand All @@ -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`, `standard_small`, `standard_medium`, `standard_large`.

### Optional

Expand All @@ -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.
Expand Down
23 changes: 13 additions & 10 deletions internal/provider/resource_vault_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package provider
import (
"context"
"log"
"strings"
"time"

sharedmodels "github.com/hashicorp/hcp-sdk-go/clients/cloud-shared/v1/models"
Expand Down Expand Up @@ -56,6 +57,16 @@ func resourceVaultCluster() *schema.Resource {
ForceNew: true,
ValidateDiagFunc: validateSlugID,
},
"tier": {
Description: "Tier of the HCP Vault cluster. Valid options for tiers - `development`, `standard_small`, `standard_medium`, `standard_large`",
waxb marked this conversation as resolved.
Show resolved Hide resolved
Type: schema.TypeString,
Required: true,
waxb marked this conversation as resolved.
Show resolved Hide resolved
ForceNew: true,
ValidateDiagFunc: validateVaultClusterTier,
DiffSuppressFunc: func(_, old, new string, _ *schema.ResourceData) bool {
return strings.ToLower(old) == strings.ToLower(new)
},
},
// optional fields
"public_endpoint": {
Description: "Denotes that the cluster has a public endpoint. Defaults to false.",
Expand All @@ -72,12 +83,6 @@ 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.",
Type: schema.TypeString,
Computed: true,
},
"organization_id": {
Description: "The ID of the organization this HCP Vault cluster is located in.",
Type: schema.TypeString,
Expand Down Expand Up @@ -169,17 +174,15 @@ 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

log.Printf("[INFO] Creating Vault cluster (%s)", clusterID)

vaultCuster := &vaultmodels.HashicorpCloudVault20201125InputCluster{
Config: &vaultmodels.HashicorpCloudVault20201125InputClusterConfig{
VaultConfig: &vaultmodels.HashicorpCloudVault20201125VaultConfig{
InitialVersion: vaultVersion,
},
Tier: tier,
//TODO: HashicorpCloudVault20201125Tier still hardcoded as in consul
waxb marked this conversation as resolved.
Show resolved Hide resolved
Tier: vaultmodels.HashicorpCloudVault20201125Tier(strings.Replace(strings.ToUpper(d.Get("tier").(string)), "_", "", -1)),
NetworkConfig: &vaultmodels.HashicorpCloudVault20201125InputNetworkConfig{
NetworkID: hvn.ID,
PublicIpsEnabled: publicEndpoint,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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" {
Expand Down
5 changes: 3 additions & 2 deletions internal/provider/resource_vault_cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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" {
Expand All @@ -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"),
Expand Down Expand Up @@ -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"),
Expand Down
21 changes: 21 additions & 0 deletions internal/provider/validators.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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
waxb marked this conversation as resolved.
Show resolved Hide resolved
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
}