From 1b9757a5d14fabd95278156513c585be8ed47736 Mon Sep 17 00:00:00 2001 From: Oded Dwek Date: Tue, 17 Sep 2024 18:21:35 +0300 Subject: [PATCH] feat(entity): added delete_dependents --- .gitignore | 1 + docs/resources/port_entity.md | 3 ++- internal/cli/entity.go | 13 +++++++++---- port/entity/model.go | 27 ++++++++++++++------------- port/entity/resource.go | 4 ++-- 5 files changed, 28 insertions(+), 20 deletions(-) diff --git a/.gitignore b/.gitignore index d29b5682..0ba893b7 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ +.history .vscode/ .env local/ diff --git a/docs/resources/port_entity.md b/docs/resources/port_entity.md index 5c1535ea..080c76bb 100644 --- a/docs/resources/port_entity.md +++ b/docs/resources/port_entity.md @@ -24,10 +24,11 @@ Entity resource - `icon` (String) The icon of the entity - `identifier` (String) The identifier of the entity - `properties` (Attributes) The properties of the entity (see [below for nested schema](#nestedatt--properties)) -- `relations` (Attributes) The relations of the entity (see [below for nested schema](#nestedatt--relations)) - `run_id` (String) The runID of the action run that created the entity - `teams` (List of String) The teams the entity belongs to - `title` (String) The title of the entity +- `title` (String) The title of the entity +- `delete_dependents` (Boolean) On delete, also delete the entity dependents ### Read-Only diff --git a/internal/cli/entity.go b/internal/cli/entity.go index 5bbebe92..83a02102 100644 --- a/internal/cli/entity.go +++ b/internal/cli/entity.go @@ -68,15 +68,20 @@ func (c *PortClient) UpdateEntity(ctx context.Context, id string, blueprint stri return &pb.Entity, nil } -func (c *PortClient) DeleteEntity(ctx context.Context, id string, blueprint string) error { +func (c *PortClient) DeleteEntity(ctx context.Context, id string, blueprint string, deleteDependents bool) error { url := "v1/blueprints/{blueprint}/entities/{identifier}" pb := &PortBody{} - resp, err := c.Client.R(). + request := c.Client.R(). SetHeader("Accept", "application/json"). SetPathParam("blueprint", blueprint). SetPathParam("identifier", id). - SetResult(pb). - Delete(url) + SetResult(pb) + + if deleteDependents { + request.SetQueryParam("delete_dependents", "true") + } + + resp, err := request.Delete(url) if err != nil { return err } diff --git a/port/entity/model.go b/port/entity/model.go index 4aefafbd..10161cec 100644 --- a/port/entity/model.go +++ b/port/entity/model.go @@ -25,17 +25,18 @@ type RelationModel struct { } type EntityModel struct { - ID types.String `tfsdk:"id"` - Identifier types.String `tfsdk:"identifier"` - Blueprint types.String `tfsdk:"blueprint"` - Title types.String `tfsdk:"title"` - Icon types.String `tfsdk:"icon"` - RunID types.String `tfsdk:"run_id"` - CreatedAt types.String `tfsdk:"created_at"` - CreatedBy types.String `tfsdk:"created_by"` - UpdatedAt types.String `tfsdk:"updated_at"` - UpdatedBy types.String `tfsdk:"updated_by"` - Properties *EntityPropertiesModel `tfsdk:"properties"` - Teams []types.String `tfsdk:"teams"` - Relations *RelationModel `tfsdk:"relations"` + ID types.String `tfsdk:"id"` + Identifier types.String `tfsdk:"identifier"` + Blueprint types.String `tfsdk:"blueprint"` + Title types.String `tfsdk:"title"` + Icon types.String `tfsdk:"icon"` + RunID types.String `tfsdk:"run_id"` + CreatedAt types.String `tfsdk:"created_at"` + CreatedBy types.String `tfsdk:"created_by"` + UpdatedAt types.String `tfsdk:"updated_at"` + UpdatedBy types.String `tfsdk:"updated_by"` + Properties *EntityPropertiesModel `tfsdk:"properties"` + Teams []types.String `tfsdk:"teams"` + Relations *RelationModel `tfsdk:"relations"` + DeleteDependents types.Bool `tfsdk:"delete_dependents"` } diff --git a/port/entity/resource.go b/port/entity/resource.go index af62a862..b4ef19ff 100644 --- a/port/entity/resource.go +++ b/port/entity/resource.go @@ -158,7 +158,7 @@ func (r *EntityResource) Update(ctx context.Context, req resource.UpdateRequest, if isBlueprintChanged { // Delete the old entity - err := r.portClient.DeleteEntity(ctx, previousState.Identifier.ValueString(), previousState.Blueprint.ValueString()) + err := r.portClient.DeleteEntity(ctx, previousState.Identifier.ValueString(), previousState.Blueprint.ValueString(), false) if err != nil { resp.Diagnostics.AddError("failed to delete entity", err.Error()) return @@ -177,7 +177,7 @@ func (r *EntityResource) Delete(ctx context.Context, req resource.DeleteRequest, return } - err := r.portClient.DeleteEntity(ctx, state.Identifier.ValueString(), state.Blueprint.ValueString()) + err := r.portClient.DeleteEntity(ctx, state.Identifier.ValueString(), state.Blueprint.ValueString(), state.DeleteDependents.ValueBool()) if err != nil { resp.Diagnostics.AddError("failed to delete entity", err.Error())