Skip to content

Commit

Permalink
feat(cluster-connection): add docs
Browse files Browse the repository at this point in the history
  • Loading branch information
Dmitry Kropachev committed Feb 20, 2024
1 parent 0df4034 commit ffa1f8b
Show file tree
Hide file tree
Showing 5 changed files with 169 additions and 14 deletions.
74 changes: 74 additions & 0 deletions docs/resources/cluster_connection.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
---
page_title: "scylladbcloud_cluster_connection Resource - terraform-provider-scylladbcloud"
subcategory: ""
description: |-
---

# scylladbcloud_cluster_connection (Resource)



## Example of AWS Transit Gateway VPC Attachment Cluster Connection

```terraform
# Create AWS TGW VPC Attachment Connection for the specified cluster.
resource "scylladbcloud_cluster_connection" "aws_tgw" {
cluster_id = 1337
name = "aws-tgw-test"
cidrlist = ["10.201.0.0/16"]
type = "AWS_TGW_ATTACHMENT"
datacenter = "AWS_US_EAST_1"
status = "ACTIVE"
data = {
tgwid = "tgw-08461afa1119f390a"
ramarn = "arn:aws:ram:us-east-1:043400831220:resource-share/be3b0395-1782-47cb-9ae4-6d3517c6a721"
}
}
output "scylladbcloud_cluster_connection_id" {
value = scylladbcloud_cluster_connection.aws_tgw.id
}
```

<!-- schema generated by tfplugindocs -->
## Schema

### Required

- `cidrlist` (List of String) List of CIDRs to route to the cluster connection
- `cluster_id` (Number) Cluster ID
- `data` (Map of String) Connection Data
- `datacenter` (String) Cluster datacenter name
- `type` (String) Connection Type

### Optional

- `name` (String) Cluster Connection Name
- `status` (String) Connection Status
- `timeouts` (Block, Optional) (see [below for nested schema](#nestedblock--timeouts))

### Read-Only

- `external_id` (String) ID of the cloud resource that represents connection
- `id` (String) Cluster connection ID
- `region` (String) Region name

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

Optional:

- `create` (String)
- `delete` (String)
- `update` (String)

## Import

Import is supported using the following syntax:

```shell
# An cluster connection can be imported by specifying the numeric identifier.
terraform import scylladbcloud_cluster_connection.example 123
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Create AWS TGW VPC Attachment Connection for the specified cluster.

resource "scylladbcloud_cluster_connection" "aws_tgw" {
cluster_id = 1337
name = "aws-tgw-test"
cidrlist = ["10.201.0.0/16"]
type = "AWS_TGW_ATTACHMENT"
datacenter = "AWS_US_EAST_1"
status = "ACTIVE"
data = {
tgwid = "tgw-08461afa1119f390a"
ramarn = "arn:aws:ram:us-east-1:043400831220:resource-share/be3b0395-1782-47cb-9ae4-6d3517c6a721"
}
}

output "scylladbcloud_cluster_connection_id" {
value = scylladbcloud_cluster_connection.aws_tgw.id
}
2 changes: 2 additions & 0 deletions examples/resources/scylladbcloud_cluster_connection/import.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# An cluster connection can be imported by specifying the numeric identifier.
terraform import scylladbcloud_cluster_connection.example 123
67 changes: 53 additions & 14 deletions internal/provider/cluster_connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ package provider

import (
"context"
"errors"
"fmt"
"strconv"
"strings"
"time"

"github.com/hashicorp/go-cty/cty"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
Expand All @@ -24,12 +26,23 @@ const (
func ResourceClusterConnection() *schema.Resource {
return &schema.Resource{
CreateContext: resourceClusterConnectionCreate,
ReadContext: resourceClusterConnectionRead,
ReadContext: func(ctx context.Context, data *schema.ResourceData, i interface{}) diag.Diagnostics {
err := resourceClusterConnectionRead(ctx, data, i)
if err != nil {
if errors.Is(err, errNotFound) {
data.SetId("")
_ = data.Set("cluster_id", 0)
return nil
}
return diag.FromErr(err)
}
return nil
},
UpdateContext: resourceClusterConnectionUpdate,
DeleteContext: resourceClusterConnectionDelete,

Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
StateContext: resourceClusterConnectionImport,
},

Timeouts: &schema.ResourceTimeout{
Expand Down Expand Up @@ -94,6 +107,18 @@ func ResourceClusterConnection() *schema.Resource {
Description: "Connection Data",
Required: true,
Type: schema.TypeMap,
ValidateDiagFunc: func(i interface{}, s cty.Path) diag.Diagnostics {
nonLowerCasedKeys := make([]string, 0)
for k := range i.(map[string]interface{}) {
if k != strings.ToLower(k) {
nonLowerCasedKeys = append(nonLowerCasedKeys, k)
}
}
if len(nonLowerCasedKeys) > 0 {
return diag.Errorf("data keys must be lowercase: %s", strings.Join(nonLowerCasedKeys, ","))
}
return nil
},
Elem: &schema.Schema{
Required: true,
Computed: true,
Expand Down Expand Up @@ -168,7 +193,9 @@ func resourceClusterConnectionCreate(ctx context.Context, d *schema.ResourceData
return nil
}

func resourceClusterConnectionRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
var errNotFound = errors.New("not found")

func resourceClusterConnectionRead(ctx context.Context, d *schema.ResourceData, meta interface{}) error {
var (
c = meta.(*scylla.Client)
connIDStr = d.Id()
Expand All @@ -181,20 +208,20 @@ func resourceClusterConnectionRead(ctx context.Context, d *schema.ResourceData,

connectionID, err := strconv.ParseInt(connIDStr, 10, 64)
if err != nil {
return diag.Errorf("failed to parse connection id %q: %s", connIDStr, err)
return fmt.Errorf("failed to parse connection id %q: %s", connIDStr, err)
}

clusterID := int64(d.Get("cluster_id").(int))

cluster, connection, err := getClusterAndConnection(ctx, c, clusterID, connectionID)
if err != nil {
return diag.Errorf(err.Error())
return err
}

if connection == nil || cluster == nil {
d.SetId("")
_ = d.Set("cluster_id", 0)
return nil
if connection == nil {
return errors.Join(errNotFound, errors.New("error reading cluster connection"))
}
if cluster == nil {
return errors.Join(errNotFound, errors.New("error reading cluster"))
}

for id := range cluster.Datacenters {
Expand All @@ -205,9 +232,7 @@ func resourceClusterConnectionRead(ctx context.Context, d *schema.ResourceData,
}

if dc == nil {
d.SetId("")
_ = d.Set("cluster_id", 0)
return nil
return errors.Join(errNotFound, errors.New("error reading cluster datacenter"))
}

_ = d.Set("datacenter", dc.Name)
Expand All @@ -218,6 +243,8 @@ func resourceClusterConnectionRead(ctx context.Context, d *schema.ResourceData,
_ = d.Set("name", connection.Name)
_ = d.Set("data", convertFromData(connection.Data))
_ = d.Set("type", connection.Type)
_ = d.Set("status", connection.Status)
d.SetId(strconv.FormatInt(connection.ID, 10))
return nil
}

Expand Down Expand Up @@ -282,6 +309,14 @@ func resourceClusterConnectionDelete(ctx context.Context, d *schema.ResourceData
return nil
}

func resourceClusterConnectionImport(ctx context.Context, d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
err := resourceClusterConnectionRead(ctx, d, meta)
if err != nil {
return nil, err
}
return []*schema.ResourceData{d}, nil
}

func convertData(mapVal map[string]interface{}) map[string]string {
out := make(map[string]string, len(mapVal))
for key, val := range mapVal {
Expand All @@ -293,7 +328,7 @@ func convertData(mapVal map[string]interface{}) map[string]string {
func convertFromData(mapVal map[string]string) map[string]interface{} {
out := make(map[string]interface{}, len(mapVal))
for key, val := range mapVal {
out[key] = val
out[strings.ToLower(key)] = val
}
return out
}
Expand Down Expand Up @@ -357,6 +392,10 @@ func getClusterAndConnection(ctx context.Context, c *scylla.Client, clusterID, c
cluster = &clusters[i]
connection, err = c.GetClusterConnection(ctx, cluster.ID, connectionID)
if err == nil {
cluster, err = c.GetCluster(ctx, cluster.ID)
if err != nil {
return nil, nil, fmt.Errorf("error reading cluster %d: %s", clusterID, err)
}
return cluster, connection, nil
}
if !scylla.IsNotFound(err) && !scylla.IsClusterConnectionDeletedErr(err) {
Expand Down
22 changes: 22 additions & 0 deletions templates/resources/cluster_connection.md.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
---
page_title: "{{.Name}} {{.Type}} - {{.ProviderName}}"
subcategory: ""
description: |-
{{ .Description | plainmarkdown | trimspace | prefixlines " " }}
---

# {{.Name}} ({{.Type}})

{{ .Description | trimspace }}

## Example of AWS Transit Gateway VPC Attachment Cluster Connection

{{ tffile (printf "examples/resources/%s/aws-tgw-vpc-attachment.tf" .Name)}}

{{ .SchemaMarkdown | trimspace }}

## Import

Import is supported using the following syntax:

{{ codefile "shell" (printf "examples/resources/%s/import.sh" .Name)}}

0 comments on commit ffa1f8b

Please sign in to comment.