Skip to content

Commit

Permalink
Merge pull request #29 from auth0/feature/apis-update
Browse files Browse the repository at this point in the history
[A0CLI-26] feat: apis update
  • Loading branch information
Widcket authored Jan 25, 2021
2 parents d85ff3c + 4a12c4c commit 46d2521
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 11 deletions.
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})
}

0 comments on commit 46d2521

Please sign in to comment.