Skip to content

Commit

Permalink
feat(fctl): delete metadata (#532)
Browse files Browse the repository at this point in the history
  • Loading branch information
gfyrag authored Sep 26, 2023
1 parent 05e07c6 commit 29571f9
Show file tree
Hide file tree
Showing 20 changed files with 291 additions and 21 deletions.
86 changes: 86 additions & 0 deletions components/fctl/cmd/ledger/accounts/delete_metadata.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package accounts

import (
"github.com/formancehq/fctl/cmd/ledger/internal"
fctl "github.com/formancehq/fctl/pkg"
"github.com/formancehq/formance-sdk-go/pkg/models/operations"
"github.com/pterm/pterm"
"github.com/spf13/cobra"
)

type DeleteMetadataStore struct {
Success bool `json:"success"`
}
type DeleteMetadataController struct {
store *DeleteMetadataStore
}

var _ fctl.Controller[*DeleteMetadataStore] = (*DeleteMetadataController)(nil)

func NewDefaultDeleteMetadataStore() *DeleteMetadataStore {
return &DeleteMetadataStore{}
}

func NewDeleteMetadataController() *DeleteMetadataController {
return &DeleteMetadataController{
store: NewDefaultDeleteMetadataStore(),
}
}

func NewDeleteMetadataCommand() *cobra.Command {
return fctl.NewCommand("delete-metadata <address> [<key>...]",
fctl.WithShortDescription("Delete metadata on account"),
fctl.WithAliases("dm", "del-meta"),
fctl.WithConfirmFlag(),
fctl.WithArgs(cobra.MinimumNArgs(2)),
fctl.WithController[*DeleteMetadataStore](NewDeleteMetadataController()),
)
}

func (c *DeleteMetadataController) GetStore() *DeleteMetadataStore {
return c.store
}

func (c *DeleteMetadataController) Run(cmd *cobra.Command, args []string) (fctl.Renderable, error) {

cfg, err := fctl.GetConfig(cmd)
if err != nil {
return nil, err
}

organizationID, err := fctl.ResolveOrganizationID(cmd, cfg)
if err != nil {
return nil, err
}

stack, err := fctl.ResolveStack(cmd, cfg, organizationID)
if err != nil {
return nil, err
}

ledgerClient, err := fctl.NewStackClient(cmd, cfg, stack)
if err != nil {
return nil, err
}

if !fctl.CheckStackApprobation(cmd, stack, "You are about to set a metadata on account %s", args[0]) {
return nil, fctl.ErrMissingApproval
}

response, err := ledgerClient.Ledger.DeleteAccountMetadata(cmd.Context(), operations.DeleteAccountMetadataRequest{
Address: args[0],
Key: args[1],
Ledger: fctl.GetString(cmd, internal.LedgerFlag),
})
if err != nil {
return nil, err
}

c.store.Success = (response.StatusCode % 200) < 100
return c, nil
}

func (c *DeleteMetadataController) Render(cmd *cobra.Command, args []string) error {
pterm.Success.WithWriter(cmd.OutOrStdout()).Printfln("Metadata deleted!")
return nil
}
1 change: 1 addition & 0 deletions components/fctl/cmd/ledger/accounts/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ func NewLedgerAccountsCommand() *cobra.Command {
NewListCommand(),
NewShowCommand(),
NewSetMetadataCommand(),
NewDeleteMetadataCommand(),
),
)
}
3 changes: 1 addition & 2 deletions components/fctl/cmd/ledger/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package ledger

import (
"fmt"

fctl "github.com/formancehq/fctl/pkg"
"github.com/pterm/pterm"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -77,7 +76,7 @@ func (c *ListController) Run(cmd *cobra.Command, args []string) (fctl.Renderable
return nil, fmt.Errorf("unexpected status code: %d", response.StatusCode)
}

c.store.Ledgers = response.ConfigInfoResponse.Data.Config.Storage.Ledgers
c.store.Ledgers = response.ConfigInfoResponse.Config.Storage.Ledgers

return c, nil
}
Expand Down
8 changes: 4 additions & 4 deletions components/fctl/cmd/ledger/serverinfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,10 @@ func (c *ServerInfoController) Run(cmd *cobra.Command, args []string) (fctl.Rend
return nil, fmt.Errorf("unexpected status code: %d", response.StatusCode)
}

c.store.Server = response.ConfigInfoResponse.Data.Server
c.store.Version = response.ConfigInfoResponse.Data.Version
c.store.StorageDriver = response.ConfigInfoResponse.Data.Config.Storage.Driver
c.store.Ledgers = response.ConfigInfoResponse.Data.Config.Storage.Ledgers
c.store.Server = response.ConfigInfoResponse.Server
c.store.Version = response.ConfigInfoResponse.Version
c.store.StorageDriver = response.ConfigInfoResponse.Config.Storage.Driver
c.store.Ledgers = response.ConfigInfoResponse.Config.Storage.Ledgers

return c, nil
}
Expand Down
93 changes: 93 additions & 0 deletions components/fctl/cmd/ledger/transactions/delete_metadata.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package transactions

import (
"github.com/formancehq/fctl/cmd/ledger/internal"
fctl "github.com/formancehq/fctl/pkg"
"github.com/formancehq/formance-sdk-go/pkg/models/operations"
"github.com/pterm/pterm"
"github.com/spf13/cobra"
)

type DeleteMetadataStore struct {
Success bool `json:"success"`
}
type DeleteMetadataController struct {
store *DeleteMetadataStore
}

var _ fctl.Controller[*DeleteMetadataStore] = (*DeleteMetadataController)(nil)

func NewDefaultDeleteMetadataStore() *DeleteMetadataStore {
return &DeleteMetadataStore{}
}

func NewDeleteMetadataController() *DeleteMetadataController {
return &DeleteMetadataController{
store: NewDefaultDeleteMetadataStore(),
}
}

func NewDeleteMetadataCommand() *cobra.Command {
return fctl.NewCommand("delete-metadata <transaction-id> [<key>...]",
fctl.WithShortDescription("Delete metadata on transaction"),
fctl.WithAliases("dm", "del-meta"),
fctl.WithConfirmFlag(),
fctl.WithValidArgs("last"),
fctl.WithArgs(cobra.MinimumNArgs(2)),
fctl.WithController[*DeleteMetadataStore](NewDeleteMetadataController()),
)
}

func (c *DeleteMetadataController) GetStore() *DeleteMetadataStore {
return c.store
}

func (c *DeleteMetadataController) Run(cmd *cobra.Command, args []string) (fctl.Renderable, error) {

cfg, err := fctl.GetConfig(cmd)
if err != nil {
return nil, err
}

organizationID, err := fctl.ResolveOrganizationID(cmd, cfg)
if err != nil {
return nil, err
}

stack, err := fctl.ResolveStack(cmd, cfg, organizationID)
if err != nil {
return nil, err
}

ledgerClient, err := fctl.NewStackClient(cmd, cfg, stack)
if err != nil {
return nil, err
}

transactionID, err := internal.TransactionIDOrLastN(cmd.Context(), ledgerClient,
fctl.GetString(cmd, internal.LedgerFlag), args[0])
if err != nil {
return nil, err
}

if !fctl.CheckStackApprobation(cmd, stack, "You are about to set a metadata on transaction %d", transactionID) {
return nil, fctl.ErrMissingApproval
}

response, err := ledgerClient.Ledger.DeleteTransactionMetadata(cmd.Context(), operations.DeleteTransactionMetadataRequest{
ID: transactionID,
Key: args[1],
Ledger: fctl.GetString(cmd, internal.LedgerFlag),
})
if err != nil {
return nil, err
}

c.store.Success = (response.StatusCode % 200) < 100
return c, nil
}

func (c *DeleteMetadataController) Render(cmd *cobra.Command, args []string) error {
pterm.Success.WithWriter(cmd.OutOrStdout()).Printfln("Metadata deleted!")
return nil
}
1 change: 1 addition & 0 deletions components/fctl/cmd/ledger/transactions/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ func NewLedgerTransactionsCommand() *cobra.Command {
NewRevertCommand(),
NewShowCommand(),
NewSetMetadataCommand(),
NewDeleteMetadataCommand(),
),
)
}
1 change: 1 addition & 0 deletions components/fctl/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ require (
github.com/cenkalti/backoff/v4 v4.2.1 // indirect
github.com/containerd/console v1.0.4-0.20230313162750-1ae8d489ac81 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/ericlagergren/decimal v0.0.0-20221120152707-495c53812d05 // indirect
github.com/fatih/color v1.15.0 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/google/uuid v1.3.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions components/fctl/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46t
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/ericlagergren/decimal v0.0.0-20221120152707-495c53812d05 h1:S92OBrGuLLZsyM5ybUzgc/mPjIYk2AZqufieooe98uw=
github.com/ericlagergren/decimal v0.0.0-20221120152707-495c53812d05/go.mod h1:M9R1FoZ3y//hwwnJtO51ypFGwm8ZfpxPT/ZLtO1mcgQ=
github.com/fatih/color v1.15.0 h1:kOqh6YHBtK8aywxGerMG2Eq3H6Qgoqeo13Bk2Mv/nBs=
github.com/fatih/color v1.15.0/go.mod h1:0h5ZqXfHYED7Bhv2ZJamyIOUej9KtShiJESRwBDUSsw=
github.com/formancehq/cobra v0.0.0-20221112160629-60a6d6d55ef9 h1:vix/rzvBgwdc0/4HRnEGFUXmqMJdU1LwIZbdXsm4qT4=
Expand Down
4 changes: 2 additions & 2 deletions components/ledger/internal/api/v1/controllers_info_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,13 @@ func TestGetLedgerInfo(t *testing.T) {
Version: "1",
Name: "init",
State: "ready",
Date: time.Now().Add(-2 * time.Minute).Round(time.Second),
Date: time.Now().Add(-2 * time.Minute).Round(time.Second).UTC(),
},
{
Version: "2",
Name: "fix",
State: "ready",
Date: time.Now().Add(-time.Minute).Round(time.Second),
Date: time.Now().Add(-time.Minute).Round(time.Second).UTC(),
},
}

Expand Down
5 changes: 2 additions & 3 deletions components/ledger/internal/api/v2/controllers_transactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"fmt"
"math/big"
"net/http"
"strconv"

ledger "github.com/formancehq/ledger/internal"
"github.com/formancehq/ledger/internal/engine/command"
Expand Down Expand Up @@ -230,8 +229,8 @@ func postTransactionMetadata(w http.ResponseWriter, r *http.Request) {
func deleteTransactionMetadata(w http.ResponseWriter, r *http.Request) {
l := LedgerFromContext(r.Context())

transactionID, err := strconv.ParseUint(chi.URLParam(r, "id"), 10, 64)
if err != nil {
transactionID, ok := big.NewInt(0).SetString(chi.URLParam(r, "id"), 10)
if !ok {
ResponseError(w, r, errorsutil.NewError(command.ErrValidation,
errors.New("invalid transaction ID")))
return
Expand Down
2 changes: 1 addition & 1 deletion components/ledger/internal/bus/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func newEventRevertedTransaction(tx RevertedTransaction) EventMessage {
type DeletedMetadata struct {
Ledger string `json:"ledger"`
TargetType string `json:"targetType"`
TargetID any `json:"targetID"`
TargetID any `json:"targetId"`
Key string `json:"key"`
}

Expand Down
2 changes: 1 addition & 1 deletion components/ledger/internal/engine/command/commander.go
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ func (commander *Commander) DeleteMetadata(ctx context.Context, parameters Param
}
log = ledger.NewDeleteMetadataLog(at, ledger.DeleteMetadataLogPayload{
TargetType: ledger.MetaTargetTypeTransaction,
TargetID: targetID.(uint64),
TargetID: targetID.(*big.Int),
Key: key,
})
case ledger.MetaTargetTypeAccount:
Expand Down
75 changes: 72 additions & 3 deletions components/ledger/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,40 @@ paths:
schema:
$ref: '#/components/schemas/ErrorResponse'

/v2/{ledger}/accounts/{address}/metadata/{key}:
delete:
description: Delete metadata by key
operationId: deleteAccountMetadata
tags:
- Ledger
- Transactions
summary: Delete metadata by key
parameters:
- name: ledger
in: path
description: Name of the ledger.
required: true
schema:
type: string
example: ledger001
- name: address
in: path
description: Account address
required: true
schema:
type: string
- name: key
in: path
description: The key to remove.
required: true
schema:
type: string
example: foo
responses:
2XX:
description: Key deleted
content: {}

/v2/{ledger}/stats:
get:
tags:
Expand Down Expand Up @@ -563,6 +597,43 @@ paths:
schema:
$ref: '#/components/schemas/ErrorResponse'

/v2/{ledger}/transactions/{id}/metadata/{key}:
delete:
description: Delete metadata by key
operationId: deleteTransactionMetadata
summary: Delete metadata by key
tags:
- Ledger
- Transactions
parameters:
- name: ledger
in: path
description: Name of the ledger.
required: true
schema:
type: string
example: ledger001
- name: id
in: path
description: Transaction ID.
required: true
schema:
type: integer
format: int64
minimum: 0
example: 1234
- name: key
in: path
required: true
description: The key to remove.
schema:
type: string
example: foo
responses:
2XX:
description: Key deleted
content: { }

/v2/{ledger}/transactions/{id}/revert:
post:
tags:
Expand Down Expand Up @@ -1145,9 +1216,7 @@ components:
- data

ConfigInfoResponse:
properties:
data:
$ref: '#/components/schemas/ConfigInfo'
$ref: '#/components/schemas/ConfigInfo'

Volume:
type: object
Expand Down
Loading

1 comment on commit 29571f9

@vercel
Copy link

@vercel vercel bot commented on 29571f9 Sep 26, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.