Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[A0CLI-3] feat: apis create #23

Merged
merged 2 commits into from
Jan 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion internal/auth/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -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/"
)

Expand Down
52 changes: 51 additions & 1 deletion internal/cli/apis.go
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -12,6 +14,7 @@ func apisCmd(cli *cli) *cobra.Command {

cmd.SetUsageTemplate(resourceUsageTemplate())
cmd.AddCommand(listApisCmd(cli))
cmd.AddCommand(createApiCmd(cli))

return cmd
}
Expand All @@ -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
Expand All @@ -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
}
12 changes: 12 additions & 0 deletions internal/display/apis.go
Original file line number Diff line number Diff line change
Expand Up @@ -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})
}