Skip to content

Commit

Permalink
Add deletion_protection to Database and Streaming resources (#207)
Browse files Browse the repository at this point in the history
  • Loading branch information
emerkle826 authored Feb 16, 2023
1 parent cbd3246 commit c77b833
Show file tree
Hide file tree
Showing 9 changed files with 81 additions and 18 deletions.
1 change: 1 addition & 0 deletions docs/resources/database.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ resource "astra_database" "example" {

### Optional

- `deletion_protection` (Boolean) Whether or not to allow Terraform to destroy the instance. Unless this field is set to false in Terraform state, a `terraform destroy` or `terraform apply` command that deletes the instance will fail. Defaults to `true`.
- `timeouts` (Block, Optional) (see [below for nested schema](#nestedblock--timeouts))

### Read-Only
Expand Down
4 changes: 4 additions & 0 deletions docs/resources/streaming_sink.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ resource "astra_streaming_sink" "streaming_sink-1" {
- `tenant_name` (String) Streaming tenant name.
- `topic` (String) Streaming tenant topic.

### Optional

- `deletion_protection` (Boolean) Whether or not to allow Terraform to destroy this streaming sink. Unless this field is set to false in Terraform state, a `terraform destroy` or `terraform apply` command that deletes the instance will fail. Defaults to `true`.

### Read-Only

- `id` (String) The ID of this resource.
Expand Down
4 changes: 4 additions & 0 deletions docs/resources/streaming_tenant.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ resource "astra_streaming_tenant" "streaming_tenant-1" {
- `topic` (String) Streaming tenant topic.
- `user_email` (String) User email for tenant.

### Optional

- `deletion_protection` (Boolean) Whether or not to allow Terraform to destroy this tenant. Unless this field is set to false in Terraform state, a `terraform destroy` or `terraform apply` command that deletes the instance will fail. Defaults to `true`.

### Read-Only

- `broker_service_url` (String) The Pulsar Binary Protocol URL used for production and consumption of messages.
Expand Down
4 changes: 4 additions & 0 deletions docs/resources/streaming_topic.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ description: |-
- `tenant_name` (String) Streaming tenant name.
- `topic` (String) Streaming tenant topic.

### Optional

- `deletion_protection` (Boolean) Whether or not to allow Terraform to destroy this streaming topic. Unless this field is set to false in Terraform state, a `terraform destroy` or `terraform apply` command that deletes the instance will fail. Defaults to `true`.

### Read-Only

- `id` (String) The ID of this resource.
Expand Down
16 changes: 10 additions & 6 deletions internal/provider/resource_database.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,13 @@ func resourceDatabase() *schema.Resource {
Type: schema.TypeString,
},
},

// Optional
"deletion_protection": {
Description: "Whether or not to allow Terraform to destroy the instance. Unless this field is set to false in Terraform state, a `terraform destroy` or `terraform apply` command that deletes the instance will fail. Defaults to `true`.",
Type: schema.TypeBool,
Optional: true,
Default: true,
},
// Computed
"owner_id": {
Description: "The owner id.",
Expand Down Expand Up @@ -258,6 +264,9 @@ func resourceDatabaseRead(ctx context.Context, resourceData *schema.ResourceData
}

func resourceDatabaseDelete(ctx context.Context, resourceData *schema.ResourceData, meta interface{}) diag.Diagnostics {
if protectedFromDelete(resourceData) {
return diag.Errorf("\"deletion_protection\" must be explicitly set to \"false\" in order to destroy astra_database")
}
client := meta.(astraClients).astraClient.(*astra.ClientWithResponses)


Expand Down Expand Up @@ -356,11 +365,6 @@ func resourceDatabaseUpdate(ctx context.Context, resourceData *schema.ResourceDa
databaseID := resourceData.Id()
cloudProvider := resourceData.Get("cloud_provider").(string)

if resourceData.HasChangeExcept("regions") {
// only region changes supported at the moment
return diag.Errorf("Update of Database resource only supported for fields: %s", "regions")
}

if resourceData.HasChange("regions") {
// get regions to add and delete
regionsToAdd, regionsToDelete := getRegionUpdates(resourceData.GetChange("regions"))
Expand Down
24 changes: 19 additions & 5 deletions internal/provider/resource_streaming_sink.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@ import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"regexp"
"strings"

"github.com/datastax/astra-client-go/v2/astra"
astrastreaming "github.com/datastax/astra-client-go/v2/astra-streaming"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
"io/ioutil"
"regexp"
"strings"
)

func resourceStreamingSink() *schema.Resource {
Expand All @@ -21,6 +22,7 @@ func resourceStreamingSink() *schema.Resource {
CreateContext: resourceStreamingSinkCreate,
ReadContext: resourceStreamingSinkRead,
DeleteContext: resourceStreamingSinkDelete,
UpdateContext: resourceStreamingSinkUpdate,

Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
Expand Down Expand Up @@ -99,14 +101,26 @@ func resourceStreamingSink() *schema.Resource {
Required: true,
ForceNew: true,
},
// Optional
"deletion_protection": {
Description: "Whether or not to allow Terraform to destroy this streaming sink. Unless this field is set to false in Terraform state, a `terraform destroy` or `terraform apply` command that deletes the instance will fail. Defaults to `true`.",
Type: schema.TypeBool,
Optional: true,
Default: true,
},
},
}
}


func resourceStreamingSinkUpdate(ctx context.Context, resourceData *schema.ResourceData, meta interface{}) diag.Diagnostics {
// In-place update not supported. This is only here to support deletion_protection
return nil
}

func resourceStreamingSinkDelete(ctx context.Context, resourceData *schema.ResourceData, meta interface{}) diag.Diagnostics {
//TODO: call delete endpoint
if protectedFromDelete(resourceData) {
return diag.Errorf("\"deletion_protection\" must be explicitly set to \"false\" in order to destroy astra_streaming_sink")
}

streamingClient := meta.(astraClients).astraStreamingClient.(*astrastreaming.ClientWithResponses)
client := meta.(astraClients).astraClient.(*astra.ClientWithResponses)
Expand Down
19 changes: 16 additions & 3 deletions internal/provider/resource_streaming_tenant.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ func resourceStreamingTenant() *schema.Resource {
CreateContext: resourceStreamingTenantCreate,
ReadContext: resourceStreamingTenantRead,
DeleteContext: resourceStreamingTenantDelete,
UpdateContext: resourceStreamingTenantUpdate,

Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
Expand Down Expand Up @@ -66,6 +67,13 @@ func resourceStreamingTenant() *schema.Resource {
ForceNew: true,
ValidateFunc: validation.StringMatch(regexp.MustCompile("^.{2,}"), "name must be atleast 2 characters"),
},
// Optional
"deletion_protection": {
Description: "Whether or not to allow Terraform to destroy this tenant. Unless this field is set to false in Terraform state, a `terraform destroy` or `terraform apply` command that deletes the instance will fail. Defaults to `true`.",
Type: schema.TypeBool,
Optional: true,
Default: true,
},
// Computed
"broker_service_url": {
Description: "The Pulsar Binary Protocol URL used for production and consumption of messages.",
Expand Down Expand Up @@ -107,9 +115,6 @@ func resourceStreamingTenant() *schema.Resource {
}
}




type OrgId struct {
ID string `json:"id"`
}
Expand All @@ -135,7 +140,15 @@ type StreamingClusters []struct {
Email string `json:"Email"`
}

func resourceStreamingTenantUpdate(ctx context.Context, resourceData *schema.ResourceData, meta interface{}) diag.Diagnostics {
// In-place update not supported. This is only here to support deletion_protection
return nil
}

func resourceStreamingTenantDelete(ctx context.Context, resourceData *schema.ResourceData, meta interface{}) diag.Diagnostics {
if protectedFromDelete(resourceData) {
return diag.Errorf("\"deletion_protection\" must be explicitly set to \"false\" in order to destroy astra_streaming_tenant")
}
streamingClient := meta.(astraClients).astraStreamingClient.(*astrastreaming.ClientWithResponses)
client := meta.(astraClients).astraClient.(*astra.ClientWithResponses)

Expand Down
23 changes: 19 additions & 4 deletions internal/provider/resource_streaming_topic.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@ import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"regexp"
"strings"

"github.com/datastax/astra-client-go/v2/astra"
astrastreaming "github.com/datastax/astra-client-go/v2/astra-streaming"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
"io/ioutil"
"regexp"
"strings"
)

func resourceStreamingTopic() *schema.Resource {
Expand All @@ -21,6 +22,7 @@ func resourceStreamingTopic() *schema.Resource {
CreateContext: resourceStreamingTopicCreate,
ReadContext: resourceStreamingTopicRead,
DeleteContext: resourceStreamingTopicDelete,
UpdateContext: resourceStreamingTopicUpdate,

Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
Expand Down Expand Up @@ -62,13 +64,26 @@ func resourceStreamingTopic() *schema.Resource {
Required: true,
ForceNew: true,
},
// Optional
"deletion_protection": {
Description: "Whether or not to allow Terraform to destroy this streaming topic. Unless this field is set to false in Terraform state, a `terraform destroy` or `terraform apply` command that deletes the instance will fail. Defaults to `true`.",
Type: schema.TypeBool,
Optional: true,
Default: true,
},
},
}
}


func resourceStreamingTopicUpdate(ctx context.Context, resourceData *schema.ResourceData, meta interface{}) diag.Diagnostics {
// In-place update not supported. This is only here to support deletion_protection
return nil
}

func resourceStreamingTopicDelete(ctx context.Context, resourceData *schema.ResourceData, meta interface{}) diag.Diagnostics {
if protectedFromDelete(resourceData) {
return diag.Errorf("\"deletion_protection\" must be explicitly set to \"false\" in order to destroy astra_streaming_topic")
}
streamingClient := meta.(astraClients).astraStreamingClient.(*astrastreaming.ClientWithResponses)
client := meta.(astraClients).astraClient.(*astra.ClientWithResponses)
streamingClientv3 := meta.(astraClients).astraStreamingClientv3
Expand Down
4 changes: 4 additions & 0 deletions internal/provider/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,7 @@ func keyFromStrings(s []string) string {
sort.Strings(ss)
return fmt.Sprintf("%x", sha256.Sum256([]byte(strings.Join(ss, "|"))))
}

func protectedFromDelete(resourceData *schema.ResourceData) bool {
return resourceData.Get("deletion_protection").(bool)
}

0 comments on commit c77b833

Please sign in to comment.