Skip to content

Commit

Permalink
data-api: Auth provider delete command (#93)
Browse files Browse the repository at this point in the history
* feat: base cmd

* feat: typo in test func name

* feat: changie

* feat: remove comment
  • Loading branch information
tbwiss authored Nov 6, 2024
1 parent b42a4c0 commit 112f70d
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .changes/unreleased/Added-20241104-080849.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
kind: Added
body: GraphQL Data API authentication provider delete command
time: 2024-11-04T08:08:49.404096+01:00
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ func NewCmd(cfg *clicfg.Config) *cobra.Command {

cmd.AddCommand(NewListCmd(cfg))
cmd.AddCommand(NewGetCmd(cfg))
cmd.AddCommand(NewDeleteCmd(cfg))

return cmd
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package authprovider

import (
"fmt"
"net/http"

"github.com/neo4j/cli/common/clicfg"
"github.com/neo4j/cli/neo4j-cli/aura/internal/api"
"github.com/neo4j/cli/neo4j-cli/aura/internal/output"
"github.com/spf13/cobra"
)

func NewDeleteCmd(cfg *clicfg.Config) *cobra.Command {
var (
instanceId string
dataApiId string
)

cmd := &cobra.Command{
Use: "delete <id>",
Short: "Delete a GraphQL Data API authentication provider",
Long: "Deletes a GraphQL Data API authentication provider. This action can not be undone.",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
cmd.SilenceUsage = true
path := fmt.Sprintf("/instances/%s/data-apis/graphql/%s/auth-providers/%s", instanceId, dataApiId, args[0])

resBody, statusCode, err := api.MakeRequest(cfg, path, &api.RequestConfig{
Method: http.MethodDelete,
})
if err != nil {
return err
}

// NOTE: delete should not return OK (200), it always returns 202, checking both just in case
if statusCode == http.StatusAccepted || statusCode == http.StatusOK {
output.PrintBody(cmd, cfg, resBody, []string{"id", "name", "type", "enabled", "url"})
}
return nil
},
}

cmd.Flags().StringVar(&instanceId, "instance-id", "", "The ID of the instance to delete the Data API for")
cmd.MarkFlagRequired("instance-id")

cmd.Flags().StringVar(&dataApiId, "data-api-id", "", "The ID of the GraphQL Data API to delete the Authentication provider for")
cmd.MarkFlagRequired("data-api-id")

return cmd
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package authprovider_test

import (
"fmt"
"net/http"
"testing"

"github.com/neo4j/cli/neo4j-cli/aura/internal/test/testutils"
)

func TestDeleteAuthProvider(t *testing.T) {
helper := testutils.NewAuraTestHelper(t)
defer helper.Close()

helper.SetConfigValue("aura.beta-enabled", true)

instanceId := "2f49c2b3"
dataApiId := "a342b824"
authProviderId := "87d46b4b-3bfb-4ad2-8dac-0e95cf72d39f"
mockHandler := helper.NewRequestHandlerMock(fmt.Sprintf("/v1/instances/%s/data-apis/graphql/%s/auth-providers/%s", instanceId, dataApiId, authProviderId), http.StatusAccepted, `{
"data": {
"id": "87d46b4b-3bfb-4ad2-8dac-0e95cf72d39f",
"name": "test-key",
"type": "jwks",
"enabled": true,
"url": "https://test.com/.well-known/jwks.json"
}
}`)

helper.ExecuteCommand(fmt.Sprintf("data-api graphql auth-provider delete %s --output json --instance-id %s --data-api-id %s", authProviderId, instanceId, dataApiId))

mockHandler.AssertCalledTimes(1)
mockHandler.AssertCalledWithMethod(http.MethodDelete)

helper.AssertOutJson(`{
"data": {
"enabled": true,
"id": "87d46b4b-3bfb-4ad2-8dac-0e95cf72d39f",
"name": "test-key",
"type": "jwks",
"url": "https://test.com/.well-known/jwks.json"
}
}
`)
}

0 comments on commit 112f70d

Please sign in to comment.