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

HCS-1760: Add Consul versions data source #63

Merged
merged 4 commits into from
Feb 18, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
38 changes: 38 additions & 0 deletions docs/data-sources/consul_versions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
---
page_title: "hcp_consul_versions Data Source - terraform-provider-hcp"
subcategory: ""
description: |-
The Consul versions data source provides the Consul versions supported by HCP.
---

# Data Source `hcp_consul_versions`

The Consul versions data source provides the Consul versions supported by HCP.

## Example Usage

```terraform
data "hcp_consul_versions" "default" {}
```

## Schema

### Optional

- **id** (String) The ID of this resource.
- **timeouts** (Block, Optional) (see [below for nested schema](#nestedblock--timeouts))

### Read-only

- **available** (List of String) The Consul versions available on HCP.
- **preview** (List of String) The preview versions of Consul available on HCP.
- **recommended** (String) The recommended Consul version for HCP clusters.

<a id="nestedblock--timeouts"></a>
### Nested Schema for `timeouts`

Optional:

- **default** (String)


1 change: 1 addition & 0 deletions examples/data-sources/hcp_consul_versions/data-source.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
data "hcp_consul_versions" "default" {}
21 changes: 18 additions & 3 deletions internal/clients/consul_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,9 @@ func CreateConsulCluster(ctx context.Context, client *Client, loc *sharedmodels.
return resp.Payload, nil
}

// GetAvailableHCPConsulVersions gets the list of available Consul versions that HCP supports.
func GetAvailableHCPConsulVersions(ctx context.Context, loc *sharedmodels.HashicorpCloudLocationLocation, client *Client) ([]*consulmodels.HashicorpCloudConsul20200826Version, error) {
// GetAvailableHCPConsulVersionsForLocation gets the list of available Consul versions that HCP supports for
// the provided location.
func GetAvailableHCPConsulVersionsForLocation(ctx context.Context, loc *sharedmodels.HashicorpCloudLocationLocation, client *Client) ([]*consulmodels.HashicorpCloudConsul20200826Version, error) {
p := consul_service.NewListVersionsParams()
p.Context = ctx
p.LocationProjectID = loc.ProjectID
Expand All @@ -128,6 +129,20 @@ func GetAvailableHCPConsulVersions(ctx context.Context, loc *sharedmodels.Hashic
return resp.Payload.Versions, nil
}

// GetAvailableHCPConsulVersions gets the list of available Consul versions that HCP supports.
func GetAvailableHCPConsulVersions(ctx context.Context, client *Client) ([]*consulmodels.HashicorpCloudConsul20200826Version, error) {
p := consul_service.NewListVersions2Params()
p.Context = ctx

resp, err := client.Consul.ListVersions2(p, nil)

if err != nil {
return nil, err
}

return resp.Payload.Versions, nil
}

// DeleteConsulCluster will make a call to the Consul service to initiate the delete Consul
// cluster workflow.
func DeleteConsulCluster(ctx context.Context, client *Client, loc *sharedmodels.HashicorpCloudLocationLocation,
Expand All @@ -147,7 +162,7 @@ func DeleteConsulCluster(ctx context.Context, client *Client, loc *sharedmodels.
return deleteResp.Payload, nil
}

// GetAvailableHCPConsulVersions gets the list of available Consul versions that HCP supports.
// GetAvailableHCPConsulVersionsForLocation gets the list of available Consul versions that HCP supports.
func ListConsulUpgradeVersions(ctx context.Context, client *Client, loc *sharedmodels.HashicorpCloudLocationLocation,
alexmunda marked this conversation as resolved.
Show resolved Hide resolved
clusterID string) ([]*consulmodels.HashicorpCloudConsul20200826Version, error) {

Expand Down
96 changes: 96 additions & 0 deletions internal/provider/data_source_consul_versions.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package provider

import (
"context"
"fmt"
"time"

consulmodels "github.com/hashicorp/hcp-sdk-go/clients/cloud-consul-service/preview/2020-08-26/models"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"

"github.com/hashicorp/terraform-provider-hcp/internal/clients"
)

// defaultConsulVersionsTimeoutDuration is the default timeout
// for reading the agent Helm config.
var defaultConsulVersionsTimeoutDuration = time.Minute * 5

// dataSourceConsulVersions is the data source for the Consul versions supported by HCP.
func dataSourceConsulVersions() *schema.Resource {
return &schema.Resource{
Description: "The Consul versions data source provides the Consul versions supported by HCP.",
Timeouts: &schema.ResourceTimeout{
Default: &defaultConsulVersionsTimeoutDuration,
},
ReadContext: dataSourceConsulVersionsRead,
Schema: map[string]*schema.Schema{
// Computed outputs
"recommended": {
Description: "The recommended Consul version for HCP clusters.",
Type: schema.TypeString,
Computed: true,
},
"available": {
Description: "The Consul versions available on HCP.",
Type: schema.TypeList,
Elem: &schema.Schema{
Type: schema.TypeString,
},
Computed: true,
},
"preview": {
Description: "The preview versions of Consul available on HCP.",
Type: schema.TypeList,
Elem: &schema.Schema{
Type: schema.TypeString,
},
Computed: true,
},
},
}
}

// dataSourceConsulVersionsRead is the func to implement reading of the
// supported Consul versions on HCP.
func dataSourceConsulVersionsRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
availableConsulVersions, err := clients.GetAvailableHCPConsulVersions(ctx, meta.(*clients.Client))
if err != nil || len(availableConsulVersions) == 0 {
return diag.Errorf("error fetching available HCP Consul versions: %v", err)
}

var recommendedVersion string
availableVersions := make([]string, 0)
previewVersions := make([]string, 0)

for _, v := range availableConsulVersions {
switch v.Status {
case consulmodels.HashicorpCloudConsul20200826VersionStatusRECOMMENDED:
recommendedVersion = v.Version
availableVersions = append(availableVersions, v.Version)
case consulmodels.HashicorpCloudConsul20200826VersionStatusAVAILABLE:
availableVersions = append(availableVersions, v.Version)
case consulmodels.HashicorpCloudConsul20200826VersionStatusPREVIEW:
previewVersions = append(previewVersions, v.Version)
}
}

err = d.Set("recommended", recommendedVersion)
if err != nil {
return diag.FromErr(err)
}

err = d.Set("available", availableVersions)
if err != nil {
return diag.FromErr(err)
}

err = d.Set("preview", previewVersions)
if err != nil {
return diag.FromErr(err)
}

d.SetId(fmt.Sprintf("recommended/%s/available_len/%d/preview_len/%d", recommendedVersion, len(availableVersions), len(previewVersions)))
alexmunda marked this conversation as resolved.
Show resolved Hide resolved

return nil
}
1 change: 1 addition & 0 deletions internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ func New() func() *schema.Provider {
"hcp_consul_agent_helm_config": dataSourceConsulAgentHelmConfig(),
"hcp_consul_agent_kubernetes_secret": dataSourceConsulAgentKubernetesSecret(),
"hcp_consul_cluster": dataSourceConsulCluster(),
"hcp_consul_versions": dataSourceConsulVersions(),
"hcp_hvn": dataSourceHvn(),
},
ResourcesMap: map[string]*schema.Resource{
Expand Down
2 changes: 1 addition & 1 deletion internal/provider/resource_consul_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ func resourceConsulClusterCreate(ctx context.Context, d *schema.ResourceData, me
}

// fetch available version from HCP
availableConsulVersions, err := clients.GetAvailableHCPConsulVersions(ctx, loc, client)
availableConsulVersions, err := clients.GetAvailableHCPConsulVersionsForLocation(ctx, loc, client)
if err != nil || availableConsulVersions == nil {
return diag.Errorf("error fetching available HCP Consul versions: %v", err)
}
Expand Down