Skip to content

Commit

Permalink
cmd: Add policy list handler
Browse files Browse the repository at this point in the history
Signed-off-by: arekkas <[email protected]>
  • Loading branch information
arekkas authored and aeneasr committed Nov 11, 2018
1 parent 9ee0664 commit a290619
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 0 deletions.
23 changes: 23 additions & 0 deletions cmd/client/handler_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,29 @@ func (h *PolicyHandler) GetPolicy(cmd *cobra.Command, args []string) {
fmt.Printf("%s\n", formatResponse(p))
}

func (h *PolicyHandler) ListPolicy(cmd *cobra.Command, args []string) {
m := h.newPolicyManager(cmd)
if len(args) == 0 {
fmt.Print(cmd.UsageString())
return
}

var offset int64
var all []keto.Policy
const limit = 100
for {
policies, response, err := m.ListPolicies(limit, offset)
checkResponse(response, err, http.StatusOK)
if len(policies) == 0 {
break
}
offset = offset + int64(len(policies))
all = append(all, policies...)
}

fmt.Printf("%s\n", formatResponse(all))
}

func (h *PolicyHandler) DeletePolicy(cmd *cobra.Command, args []string) {
m := h.newPolicyManager(cmd)
if len(args) == 0 {
Expand Down
45 changes: 45 additions & 0 deletions cmd/policies_list.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Copyright © 2018 NAME HERE <EMAIL ADDRESS>
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package cmd

import (
"github.com/spf13/cobra"
)

// policiesListCmd represents the export command
var policiesListCmd = &cobra.Command{
Use: "list",
Short: "List all policies",
Long: `This command lists all policies.
Example:
keto policies list
`,
Run: cmdHandler.Policies.ListPolicy,
}

func init() {
policiesGetCmd.AddCommand(policiesListCmd)

// Here you will define your flags and configuration settings.

// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// policiesListCmd.PersistentFlags().String("foo", "", "A help for foo")

// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// policiesListCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}
2 changes: 2 additions & 0 deletions cmd/root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ func TestExecute(t *testing.T) {
{args: []string{"policies", "subjects", "remove", "foobar", "ken", "tracy", "--endpoint", fmt.Sprintf("http://127.0.0.1:%d", port)}},
{args: []string{"policies", "get", "foobar", "--endpoint", fmt.Sprintf("http://127.0.0.1:%d", port)}},
{args: []string{"policies", "delete", "foobar", "--endpoint", fmt.Sprintf("http://127.0.0.1:%d", port)}},
{args: []string{"policies", "list", "--endpoint", fmt.Sprintf("http://127.0.0.1:%d", port)}},
{args: []string{"policies", "list", "--endpoint", fmt.Sprintf("http://127.0.0.1:%d", port)}},
{args: []string{"warden", "authorize", "subject", "--subject", "foo", "--action", "bar", "--resource", "baz", "--endpoint", fmt.Sprintf("http://127.0.0.1:%d", port)}},
{args: []string{"help", "migrate", "sql"}},
{args: []string{"help", "migrate", "hydra"}},
Expand Down

0 comments on commit a290619

Please sign in to comment.