Skip to content

Commit

Permalink
Add delete_behavior to sync resources
Browse files Browse the repository at this point in the history
  • Loading branch information
nmanoogian committed Jun 24, 2024
1 parent 42b65f7 commit 934e3fe
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 additions & 2 deletions doppler/resource_sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"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"
)

type SyncDataBuilderFunc = func(d *schema.ResourceData) SyncData
Expand Down Expand Up @@ -35,6 +36,13 @@ func (builder ResourceSyncBuilder) Build() *schema.Resource {
Required: true,
ForceNew: true,
},
"delete_behavior": {
Description: "The behavior to be performed on the secrets in the sync target when this resource is deleted or recreated. Either `leave_in_target` (default) or `delete_from_target`.",
Type: schema.TypeString,
Optional: true,
// Implicitly defaults to "leave_in_target" but not defined here to avoid state migration
ValidateFunc: validation.StringInSlice([]string{"leave_in_target", "delete_from_target"}, false),
},
}

for name, subschema := range builder.DataSchema {
Expand All @@ -45,6 +53,7 @@ func (builder ResourceSyncBuilder) Build() *schema.Resource {
return &schema.Resource{
CreateContext: builder.CreateContextFunc(),
ReadContext: builder.ReadContextFunc(),
UpdateContext: resourceSyncUpdate,
DeleteContext: builder.DeleteContextFunc(),
Schema: resourceSchema,
}
Expand Down Expand Up @@ -102,6 +111,13 @@ func (builder ResourceSyncBuilder) ReadContextFunc() schema.ReadContextFunc {
}
}

func resourceSyncUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
var diags diag.Diagnostics
// This function must be specified in order to update `delete_behavior` but no API operations are required.
// All other fields require `ForceNew`.
return diags
}

func (builder ResourceSyncBuilder) DeleteContextFunc() schema.DeleteContextFunc {

return func(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
Expand All @@ -112,8 +128,8 @@ func (builder ResourceSyncBuilder) DeleteContextFunc() schema.DeleteContextFunc
slug := d.Id()
config := d.Get("config").(string)
project := d.Get("project").(string)
// In the future, we can support this as a param on the sync
deleteFromTarget := false
// NOTE: `delete_behavior` might be null, this logic will treat that as `leave_in_target`
deleteFromTarget := d.Get("delete_behavior").(string) == "delete_from_target"
if err := client.DeleteSync(ctx, slug, deleteFromTarget, config, project); err != nil {
return diag.FromErr(err)
}
Expand Down

0 comments on commit 934e3fe

Please sign in to comment.