Skip to content

Commit

Permalink
Add missing permissions commands
Browse files Browse the repository at this point in the history
Signed-off-by: Matt Lord <[email protected]>
  • Loading branch information
mattlord committed Dec 27, 2024
1 parent 0e3a8e1 commit ec59343
Show file tree
Hide file tree
Showing 14 changed files with 2,445 additions and 1,185 deletions.
111 changes: 111 additions & 0 deletions go/cmd/vtctldclient/command/permissions.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
/*
Copyright 2024 The Vitess Authors.
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 command

import (
"fmt"
"strings"

"github.com/spf13/cobra"

"vitess.io/vitess/go/cmd/vtctldclient/cli"
"vitess.io/vitess/go/vt/topo/topoproto"

vtctldatapb "vitess.io/vitess/go/vt/proto/vtctldata"
)

var (
// GetPermissions makes a GetPermissions gRPC call to a vtctld.
GetPermissions = &cobra.Command{
Use: "GetPermissions <tablet_alias>",
Short: "Displays the permissions for a tablet.",
DisableFlagsInUseLine: true,
Args: cobra.ExactArgs(1),
RunE: commandGetPermissions,
}
ValidatePermissionsShard = &cobra.Command{
Use: "ValidatePermissionsShard <keyspace/shard>",
Short: "Validates that the permissions on primary match all the replicas.",
DisableFlagsInUseLine: true,
Args: cobra.ExactArgs(1),
RunE: commandValidatePermissionsShard,
}
ValidatePermissionsKeyspace = &cobra.Command{
Use: "ValidatePermissionsKeyspace <keyspace name>",
Short: "Validates that the permissions on primary of the first shard match those of all of the other tablets in the keyspace.",
DisableFlagsInUseLine: true,
Args: cobra.ExactArgs(1),
RunE: commandValidatePermissionsKeyspace,
}
)

func commandGetPermissions(cmd *cobra.Command, args []string) error {
alias, err := topoproto.ParseTabletAlias(cmd.Flags().Arg(0))
if err != nil {
return err
}

cli.FinishedParsing(cmd)

resp, err := client.GetPermissions(commandCtx, &vtctldatapb.GetPermissionsRequest{
TabletAlias: alias,
})
if err != nil {
return err
}
p, err := cli.MarshalJSON(resp.Permissions)
if err != nil {
return err
}
fmt.Printf("%s\n", p)

return nil
}

func commandValidatePermissionsKeyspace(cmd *cobra.Command, args []string) error {
keyspace := cmd.Flags().Arg(0)

cli.FinishedParsing(cmd)

_, err := client.ValidatePermissionsKeyspace(commandCtx, &vtctldatapb.ValidatePermissionsKeyspaceRequest{
Keyspace: keyspace,
})

return err
}

func commandValidatePermissionsShard(cmd *cobra.Command, args []string) error {
keyspace, shard, ok := strings.Cut(cmd.Flags().Arg(0), "/")
if !ok {
return fmt.Errorf("invalid '<keyspace>/<shard>': %v", cmd.Flags().Arg(0))
}

cli.FinishedParsing(cmd)

_, err := client.ValidatePermissionsKeyspace(commandCtx, &vtctldatapb.ValidatePermissionsKeyspaceRequest{
Keyspace: keyspace,
Shards: []string{shard},
})

return err
}

func init() {
Root.AddCommand(GetPermissions)
Root.AddCommand(ValidatePermissionsKeyspace)
Root.AddCommand(ValidatePermissionsShard)
}
9 changes: 1 addition & 8 deletions go/cmd/vtctldclient/command/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,7 @@ var validateSchemaKeyspaceOptions = struct {
IncludeViews bool
SkipNoPrimary bool
IncludeVSchema bool
Shard string
}{}

func commandValidateSchemaKeyspace(cmd *cobra.Command, args []string) error {
Expand Down Expand Up @@ -383,14 +384,6 @@ func commandValidateSchemaKeyspace(cmd *cobra.Command, args []string) error {
return nil
}

var validateSchemaShardOptions = struct {
Shard string
ExcludeTables []string
IncludeViews bool
SkipNoPrimary bool
IncludeVSchema bool
}{}

func commandValidateSchemaShard(cmd *cobra.Command, args []string) error {
cli.FinishedParsing(cmd)

Expand Down
32 changes: 0 additions & 32 deletions go/cmd/vtctldclient/command/tablets.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,14 +92,6 @@ Note: hook names may not contain slash (/) characters.
Args: cobra.ExactArgs(1),
RunE: commandGetFullStatus,
}
// GetPermissions makes a GetPermissions gRPC call to a vtctld.
GetPermissions = &cobra.Command{
Use: "GetPermissions <tablet_alias>",
Short: "Displays the permissions for a tablet.",
DisableFlagsInUseLine: true,
Args: cobra.ExactArgs(1),
RunE: commandGetPermissions,
}
// GetTablet makes a GetTablet gRPC call to a vtctld.
GetTablet = &cobra.Command{
Use: "GetTablet <alias>",
Expand Down Expand Up @@ -380,29 +372,6 @@ func commandGetFullStatus(cmd *cobra.Command, args []string) error {
return nil
}

func commandGetPermissions(cmd *cobra.Command, args []string) error {
alias, err := topoproto.ParseTabletAlias(cmd.Flags().Arg(0))
if err != nil {
return err
}

cli.FinishedParsing(cmd)

resp, err := client.GetPermissions(commandCtx, &vtctldatapb.GetPermissionsRequest{
TabletAlias: alias,
})
if err != nil {
return err
}
p, err := cli.MarshalJSON(resp.Permissions)
if err != nil {
return err
}
fmt.Printf("%s\n", p)

return nil
}

func commandGetTablet(cmd *cobra.Command, args []string) error {
aliasStr := cmd.Flags().Arg(0)
alias, err := topoproto.ParseTabletAlias(aliasStr)
Expand Down Expand Up @@ -685,7 +654,6 @@ func init() {

Root.AddCommand(ExecuteHook)
Root.AddCommand(GetFullStatus)
Root.AddCommand(GetPermissions)
Root.AddCommand(GetTablet)

GetTablets.Flags().StringSliceVarP(&getTabletsOptions.TabletAliasStrings, "tablet-alias", "t", nil, "List of tablet aliases to filter by.")
Expand Down
Loading

0 comments on commit ec59343

Please sign in to comment.