Skip to content

Commit

Permalink
Add ability to List/Get AMRs to tctl (#47388)
Browse files Browse the repository at this point in the history
  • Loading branch information
strideynet authored Oct 9, 2024
1 parent 8492e07 commit de64686
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
30 changes: 30 additions & 0 deletions tool/tctl/common/collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (
"github.com/gravitational/trace"

"github.com/gravitational/teleport/api/constants"
accessmonitoringrulesv1pb "github.com/gravitational/teleport/api/gen/proto/go/teleport/accessmonitoringrules/v1"
autoupdatev1pb "github.com/gravitational/teleport/api/gen/proto/go/teleport/autoupdate/v1"
crownjewelv1 "github.com/gravitational/teleport/api/gen/proto/go/teleport/crownjewel/v1"
dbobjectv1 "github.com/gravitational/teleport/api/gen/proto/go/teleport/dbobject/v1"
Expand Down Expand Up @@ -1864,3 +1865,32 @@ func (c *autoUpdateVersionCollection) writeText(w io.Writer, verbose bool) error
_, err := t.AsBuffer().WriteTo(w)
return trace.Wrap(err)
}

type accessMonitoringRuleCollection struct {
items []*accessmonitoringrulesv1pb.AccessMonitoringRule
}

func (c *accessMonitoringRuleCollection) resources() []types.Resource {
r := make([]types.Resource, 0, len(c.items))
for _, resource := range c.items {
r = append(r, types.Resource153ToLegacy(resource))
}
return r
}

// writeText formats the user tasks into a table and writes them into w.
// If verbose is disabled, labels column can be truncated to fit into the console.
func (c *accessMonitoringRuleCollection) writeText(w io.Writer, verbose bool) error {
var rows [][]string
for _, item := range c.items {
labels := common.FormatLabels(item.GetMetadata().GetLabels(), verbose)
rows = append(rows, []string{item.Metadata.GetName(), labels})
}
headers := []string{"Name", "Labels"}
t := asciitable.MakeTable(headers, rows...)

// stable sort by name.
t.SortRowsBy([]int{0}, true)
_, err := t.AsBuffer().WriteTo(w)
return trace.Wrap(err)
}
24 changes: 24 additions & 0 deletions tool/tctl/common/resource_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import (
apiclient "github.com/gravitational/teleport/api/client"
"github.com/gravitational/teleport/api/client/proto"
apidefaults "github.com/gravitational/teleport/api/defaults"
accessmonitoringrulesv1pb "github.com/gravitational/teleport/api/gen/proto/go/teleport/accessmonitoringrules/v1"
autoupdatev1pb "github.com/gravitational/teleport/api/gen/proto/go/teleport/autoupdate/v1"
clusterconfigpb "github.com/gravitational/teleport/api/gen/proto/go/teleport/clusterconfig/v1"
crownjewelv1 "github.com/gravitational/teleport/api/gen/proto/go/teleport/crownjewel/v1"
Expand Down Expand Up @@ -3080,6 +3081,29 @@ func (rc *ResourceCommand) getCollection(ctx context.Context, client *authclient
return nil, trace.Wrap(err)
}
return &autoUpdateVersionCollection{version}, nil
case types.KindAccessMonitoringRule:
if rc.ref.Name != "" {
rule, err := client.AccessMonitoringRuleClient().GetAccessMonitoringRule(ctx, rc.ref.Name)
if err != nil {
return nil, trace.Wrap(err)
}
return &accessMonitoringRuleCollection{items: []*accessmonitoringrulesv1pb.AccessMonitoringRule{rule}}, nil
}

var rules []*accessmonitoringrulesv1pb.AccessMonitoringRule
nextToken := ""
for {
resp, token, err := client.AccessMonitoringRuleClient().ListAccessMonitoringRules(ctx, 0, nextToken)
if err != nil {
return nil, trace.Wrap(err)
}
rules = append(rules, resp...)
if token == "" {
break
}
nextToken = token
}
return &accessMonitoringRuleCollection{items: rules}, nil
}
return nil, trace.BadParameter("getting %q is not supported", rc.ref.String())
}
Expand Down

0 comments on commit de64686

Please sign in to comment.