blob: 46abf25b88a7a0d5f930a5da45a60d4b4f9a026a [file] [log] [blame]
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
package rack
import (
ufsAPI "infra/unifiedfleet/api/v1/rpc"
ufsUtil "infra/unifiedfleet/app/util"
// GetRackCmd get rack by given name.
var GetRackCmd = &subcommands.Command{
UsageLine: "rack ...",
ShortDesc: "Get rack details by filters",
LongDesc: `Get rack details by filters
shivas get rack name1 name2
shivas get rack -n 10
shivas get rack -zone atl97 -state serving
Gets the rack and prints the output in the user-specified format.`,
CommandRun: func() subcommands.CommandRun {
c := &getRack{}
c.authFlags.Register(&c.Flags, site.DefaultAuthOptions)
c.Flags.IntVar(&c.pageSize, "n", 0, cmdhelp.ListPageSizeDesc)
c.Flags.BoolVar(&c.keysOnly, "keys", false, cmdhelp.KeysOnlyText)
c.Flags.Var(flag.StringSlice(&c.zones), "zone", "Name(s) of a zone to filter by. Can be specified multiple times."+cmdhelp.ZoneFilterHelpText)
c.Flags.Var(flag.StringSlice(&c.tags), "tag", "Name(s) of a tag to filter by. Can be specified multiple times.")
c.Flags.Var(flag.StringSlice(&c.states), "state", "Name(s) of a state to filter by. Can be specified multiple times."+cmdhelp.StateFilterHelpText)
return c
type getRack struct {
authFlags authcli.Flags
envFlags site.EnvFlags
commonFlags site.CommonFlags
outputFlags site.OutputFlags
// Filters
zones []string
tags []string
states []string
pageSize int
keysOnly bool
func (c *getRack) Run(a subcommands.Application, args []string, env subcommands.Env) int {
if err := c.innerRun(a, args, env); err != nil {
cmdlib.PrintError(a, err)
return 1
return 0
func (c *getRack) innerRun(a subcommands.Application, args []string, env subcommands.Env) error {
ctx := cli.GetContext(a, c, env)
ns, err := c.envFlags.Namespace()
if err != nil {
return err
ctx = utils.SetupContext(ctx, ns)
hc, err := cmdlib.NewHTTPClient(ctx, &c.authFlags)
if err != nil {
return err
e := c.envFlags.Env()
ic := ufsAPI.NewFleetPRPCClient(&prpc.Client{
C: hc,
Host: e.UnifiedFleetService,
Options: site.DefaultPRPCOptions,
if c.commonFlags.Verbose() {
fmt.Printf("Using UnifiedFleet service %s\n", e.UnifiedFleetService)
emit := !utils.NoEmitMode(c.outputFlags.NoEmit())
full := utils.FullMode(c.outputFlags.Full())
var res []proto.Message
if len(args) > 0 {
res = utils.ConcurrentGet(ctx, ic, args, c.getSingle)
} else {
res, err = utils.BatchList(ctx, ic, listRacks, c.formatFilters(), c.pageSize, c.keysOnly, full)
if err != nil {
return err
return utils.PrintEntities(ctx, ic, res, utils.PrintRacksJSON, printRackFull, printRackNormal,
c.outputFlags.JSON(), emit, full, c.outputFlags.Tsv(), c.keysOnly)
func (c *getRack) formatFilters() []string {
filters := make([]string, 0)
filters = utils.JoinFilters(filters, utils.PrefixFilters(ufsUtil.ZoneFilterName, c.zones)...)
filters = utils.JoinFilters(filters, utils.PrefixFilters(ufsUtil.TagFilterName, c.tags)...)
filters = utils.JoinFilters(filters, utils.PrefixFilters(ufsUtil.StateFilterName, c.states)...)
return filters
func (c *getRack) getSingle(ctx context.Context, ic ufsAPI.FleetClient, name string) (proto.Message, error) {
return ic.GetRack(ctx, &ufsAPI.GetRackRequest{
Name: ufsUtil.AddPrefix(ufsUtil.RackCollection, name),
func listRacks(ctx context.Context, ic ufsAPI.FleetClient, pageSize int32, pageToken, filter string, keysOnly, full bool) ([]proto.Message, string, error) {
req := &ufsAPI.ListRacksRequest{
PageSize: pageSize,
PageToken: pageToken,
Filter: filter,
KeysOnly: keysOnly,
Full: full,
res, err := ic.ListRacks(ctx, req)
if err != nil {
return nil, "", err
protos := make([]proto.Message, len(res.GetRacks()))
for i, m := range res.GetRacks() {
protos[i] = m
return protos, res.GetNextPageToken(), nil
func printRackFull(ctx context.Context, ic ufsAPI.FleetClient, msgs []proto.Message, tsv bool) error {
return printRackNormal(msgs, tsv, false)
func printRackNormal(entities []proto.Message, tsv, keysOnly bool) error {
if len(entities) == 0 {
return nil
if tsv {
utils.PrintTSVRacks(entities, keysOnly)
return nil
utils.PrintTableTitle(utils.RackTitle, tsv, keysOnly)
utils.PrintRacks(entities, keysOnly)
return nil