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-26] feat: apis update #29

Merged
merged 1 commit 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: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"
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:resource_servers update:connections update:hooks update:rules"
audiencePath = "/api/v2/"
)

Expand Down
41 changes: 40 additions & 1 deletion internal/cli/apis.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ func apisCmd(cli *cli) *cobra.Command {
cmd.SetUsageTemplate(resourceUsageTemplate())
cmd.AddCommand(listApisCmd(cli))
cmd.AddCommand(createApiCmd(cli))
cmd.AddCommand(updateApiCmd(cli))

return cmd
}
Expand Down Expand Up @@ -42,7 +43,7 @@ Lists your existing APIs. To create one try:
return err
}

cli.renderer.ApisList(list.ResourceServers)
cli.renderer.ApiList(list.ResourceServers)
return nil
},
}
Expand Down Expand Up @@ -89,3 +90,41 @@ auth0 apis create --name myapi --identifier http://my-api

return cmd
}

func updateApiCmd(cli *cli) *cobra.Command {
var flags struct {
id string
name string
}

cmd := &cobra.Command{
Use: "update",
Short: "Update an API",
Long: `Updates an API:

auth0 apis update --id id --name myapi
`,
RunE: func(cmd *cobra.Command, args []string) error {
api := &management.ResourceServer{Name: &flags.name}
manager := &management.ResourceServerManager{Management: cli.api}

err := ansi.Spinner("Updating API", func() error {
return manager.Update(flags.id, api)
})

if err != nil {
return err
}

cli.renderer.ApiUpdate(api)
return nil
},
}

cmd.Flags().StringVarP(&flags.id, "id", "i", "", "ID of the API.")
cmd.Flags().StringVarP(&flags.name, "name", "n", "", "Name of the API.")

mustRequireFlags(cmd, "id", "name")

return cmd
}
20 changes: 11 additions & 9 deletions internal/display/apis.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,30 +20,32 @@ func (v *apiView) AsTableRow() []string {
return []string{ansi.Faint(v.ID), v.Name, v.Identifier}
}

func (r *Renderer) ApisList(apis []*management.ResourceServer) {
func (r *Renderer) ApiList(apis []*management.ResourceServer) {
r.Heading(ansi.Bold(r.Tenant), "APIs\n")

var res []View

for _, api := range apis {
res = append(res, &apiView{
ID: auth0.StringValue(api.ID),
Name: auth0.StringValue(api.Name),
Identifier: auth0.StringValue(api.Identifier),
})
res = append(res, makeView(api))
}

r.Results(res)
}

func (r *Renderer) ApiCreate(api *management.ResourceServer) {
r.Heading(ansi.Bold(r.Tenant), "API created\n")
r.Results([]View{makeView(api)})
}

func (r *Renderer) ApiUpdate(api *management.ResourceServer) {
r.Heading(ansi.Bold(r.Tenant), "API updated\n")
r.Results([]View{makeView(api)})
}

v := &apiView{
func makeView(api *management.ResourceServer) *apiView {
return &apiView{
ID: auth0.StringValue(api.ID),
Name: auth0.StringValue(api.Name),
Identifier: auth0.StringValue(api.Identifier),
}

r.Results([]View{v})
}