Skip to content

Commit

Permalink
feat(entity): added delete_dependents
Browse files Browse the repository at this point in the history
  • Loading branch information
odeddoek committed Sep 17, 2024
1 parent f2571a4 commit 1b9757a
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 20 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.history
.vscode/
.env
local/
Expand Down
3 changes: 2 additions & 1 deletion docs/resources/port_entity.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
13 changes: 9 additions & 4 deletions internal/cli/entity.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
27 changes: 14 additions & 13 deletions port/entity/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}
4 changes: 2 additions & 2 deletions port/entity/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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())
Expand Down

0 comments on commit 1b9757a

Please sign in to comment.