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..e06b3ea15 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}) +}