From 8f02b409b32c277ddf597fd125359f48b91eb94f Mon Sep 17 00:00:00 2001 From: Rita Zerrizuela Date: Mon, 25 Jan 2021 17:22:44 -0300 Subject: [PATCH 1/2] feat: apis create --- internal/auth/auth.go | 2 +- internal/cli/apis.go | 52 +++++++++++++++++++++++++++++++++++++++- internal/display/apis.go | 12 ++++++++++ 3 files changed, 64 insertions(+), 2 deletions(-) diff --git a/internal/auth/auth.go b/internal/auth/auth.go index ae922ed96..413f94654 100644 --- a/internal/auth/auth.go +++ b/internal/auth/auth.go @@ -33,7 +33,7 @@ const ( deviceCodeEndpoint = "https://auth0.auth0.com/oauth/device/code" oauthTokenEndpoint = "https://auth0.auth0.com/oauth/token" // TODO(jfatta) extend the scope as we extend the CLI: - scope = "openid create:actions create:clients create:connections create:hooks create:rules delete:actions delete:clients delete:connections delete:hooks delete:rules read:actions read:clients read:connections read:hooks read:logs read:rules update:actions update:clients update:connections update:hooks update:rules" + scope = "openid create:actions create:clients create:resource_servers create:connections create:hooks create:rules delete:actions delete:clients delete:connections delete:hooks delete:rules read:actions read:clients read:resource_servers read:connections read:hooks read:logs read:rules update:actions update:clients update:connections update:hooks update:rules" audiencePath = "/api/v2/" ) diff --git a/internal/cli/apis.go b/internal/cli/apis.go index ea51856a6..8d282f56e 100644 --- a/internal/cli/apis.go +++ b/internal/cli/apis.go @@ -1,7 +1,9 @@ package cli import ( + "github.com/auth0/auth0-cli/internal/ansi" "github.com/spf13/cobra" + "gopkg.in/auth0.v5/management" ) func apisCmd(cli *cli) *cobra.Command { @@ -12,6 +14,7 @@ func apisCmd(cli *cli) *cobra.Command { cmd.SetUsageTemplate(resourceUsageTemplate()) cmd.AddCommand(listApisCmd(cli)) + cmd.AddCommand(createApiCmd(cli)) return cmd } @@ -26,7 +29,14 @@ Lists your existing APIs. To create one try: $ auth0 apis create `, RunE: func(cmd *cobra.Command, args []string) error { - list, err := cli.api.ResourceServer.List() + var list *management.ResourceServerList + + err := ansi.Spinner("Getting APIs", func() error { + var err error + + list, err = cli.api.ResourceServer.List() + return err + }) if err != nil { return err @@ -39,3 +49,43 @@ Lists your existing APIs. To create one try: return cmd } + +func createApiCmd(cli *cli) *cobra.Command { + var flags struct { + name string + identifier string + } + + cmd := &cobra.Command{ + Use: "create", + Short: "Create a new API", + Long: `Creates a new API: + +auth0 apis create --name myapi --identifier http://my-api +`, + RunE: func(cmd *cobra.Command, args []string) error { + api := &management.ResourceServer{ + Name: &flags.name, + Identifier: &flags.identifier, + } + + err := ansi.Spinner("Creating API", func() error { + return cli.api.ResourceServer.Create(api) + }) + + if err != nil { + return err + } + + cli.renderer.ApiCreate(api) + return nil + }, + } + + cmd.Flags().StringVarP(&flags.name, "name", "n", "", "Name of the API.") + cmd.Flags().StringVarP(&flags.identifier, "identifier", "i", "", "Identifier of the API.") + + mustRequireFlags(cmd, "name", "identifier") + + return cmd +} diff --git a/internal/display/apis.go b/internal/display/apis.go index 208db8ecc..9d95ad424 100644 --- a/internal/display/apis.go +++ b/internal/display/apis.go @@ -35,3 +35,15 @@ func (r *Renderer) ApisList(apis []*management.ResourceServer) { r.Results(res) } + +func (r *Renderer) ApiCreate(api *management.ResourceServer) { + r.Heading(ansi.Bold(r.Tenant), "API created\n") + + v := &apiView{ + ID: auth0.StringValue(api.ID), + Name: auth0.StringValue(api.Name), + Identifier: auth0.StringValue(api.Identifier), + } + + r.Results([]View{v}) +} From 7cf6c83410084461ac916452f9cc3d90729549de Mon Sep 17 00:00:00 2001 From: Rita Zerrizuela Date: Mon, 25 Jan 2021 17:42:14 -0300 Subject: [PATCH 2/2] Format properly --- internal/cli/apis.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/cli/apis.go b/internal/cli/apis.go index 8d282f56e..e06b3ea15 100644 --- a/internal/cli/apis.go +++ b/internal/cli/apis.go @@ -65,7 +65,7 @@ auth0 apis create --name myapi --identifier http://my-api `, RunE: func(cmd *cobra.Command, args []string) error { api := &management.ResourceServer{ - Name: &flags.name, + Name: &flags.name, Identifier: &flags.identifier, }