vitess.io/vitess@v0.16.2/go/vt/vtctl/cells_aliases.go (about)

     1  /*
     2  Copyright 2019 The Vitess Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package vtctl
    18  
    19  import (
    20  	"context"
    21  	"fmt"
    22  	"strings"
    23  
    24  	"github.com/spf13/pflag"
    25  
    26  	"vitess.io/vitess/go/vt/wrangler"
    27  
    28  	topodatapb "vitess.io/vitess/go/vt/proto/topodata"
    29  	vtctldatapb "vitess.io/vitess/go/vt/proto/vtctldata"
    30  )
    31  
    32  // This file contains the CellsAliases command group for vtctl.
    33  
    34  const cellsAliasesGroupName = "CellsAliases"
    35  
    36  func init() {
    37  	addCommandGroup(cellsAliasesGroupName)
    38  
    39  	addCommand(cellsAliasesGroupName, command{
    40  		name:   "AddCellsAlias",
    41  		method: commandAddCellsAlias,
    42  		params: "[--cells <cell,cell2...>] <alias>",
    43  		help:   "Defines a group of cells within which replica/rdonly traffic can be routed across cells. Between cells that are not in the same group (alias), only primary traffic can be routed.",
    44  	})
    45  
    46  	addCommand(cellsAliasesGroupName, command{
    47  		name:   "UpdateCellsAlias",
    48  		method: commandUpdateCellsAlias,
    49  		params: "[--cells <cell,cell2,...>] <alias>",
    50  		help:   "Updates the content of a CellsAlias with the provided parameters. If a value is empty, it is not updated. The CellsAlias will be created if it doesn't exist.",
    51  	})
    52  
    53  	addCommand(cellsAliasesGroupName, command{
    54  		name:   "DeleteCellsAlias",
    55  		method: commandDeleteCellsAlias,
    56  		params: "<alias>",
    57  		help:   "Deletes the CellsAlias for the provided alias.",
    58  	})
    59  
    60  	addCommand(cellsAliasesGroupName, command{
    61  		name:   "GetCellsAliases",
    62  		method: commandGetCellsAliases,
    63  		params: "",
    64  		help:   "Lists all the cells for which we have a CellsAlias object.",
    65  	})
    66  }
    67  
    68  func commandAddCellsAlias(ctx context.Context, wr *wrangler.Wrangler, subFlags *pflag.FlagSet, args []string) error {
    69  	cells := subFlags.StringSlice("cells", nil, "The list of cell names that are members of this alias.")
    70  	if err := subFlags.Parse(args); err != nil {
    71  		return err
    72  	}
    73  	if subFlags.NArg() != 1 {
    74  		return fmt.Errorf("the <alias> argument is required for the AddCellsAlias command")
    75  	}
    76  
    77  	for i, cell := range *cells {
    78  		(*cells)[i] = strings.TrimSpace(cell)
    79  	}
    80  
    81  	alias := subFlags.Arg(0)
    82  	_, err := wr.VtctldServer().AddCellsAlias(ctx, &vtctldatapb.AddCellsAliasRequest{
    83  		Name:  alias,
    84  		Cells: *cells,
    85  	})
    86  	return err
    87  }
    88  
    89  func commandUpdateCellsAlias(ctx context.Context, wr *wrangler.Wrangler, subFlags *pflag.FlagSet, args []string) error {
    90  	cells := subFlags.StringSlice("cells", nil, "The list of cell names that are members of this alias.")
    91  	if err := subFlags.Parse(args); err != nil {
    92  		return err
    93  	}
    94  	if subFlags.NArg() != 1 {
    95  		return fmt.Errorf("the <alias> argument is required for the UpdateCellsAlias command")
    96  	}
    97  
    98  	for i, cell := range *cells {
    99  		(*cells)[i] = strings.TrimSpace(cell)
   100  	}
   101  
   102  	alias := subFlags.Arg(0)
   103  	_, err := wr.VtctldServer().UpdateCellsAlias(ctx, &vtctldatapb.UpdateCellsAliasRequest{
   104  		Name: alias,
   105  		CellsAlias: &topodatapb.CellsAlias{
   106  			Cells: *cells,
   107  		},
   108  	})
   109  	return err
   110  }
   111  
   112  func commandDeleteCellsAlias(ctx context.Context, wr *wrangler.Wrangler, subFlags *pflag.FlagSet, args []string) error {
   113  	if err := subFlags.Parse(args); err != nil {
   114  		return err
   115  	}
   116  	if subFlags.NArg() != 1 {
   117  		return fmt.Errorf("the <alias> argument is required for the DeleteCellsAlias command")
   118  	}
   119  	alias := subFlags.Arg(0)
   120  
   121  	_, err := wr.VtctldServer().DeleteCellsAlias(ctx, &vtctldatapb.DeleteCellsAliasRequest{
   122  		Name: alias,
   123  	})
   124  	return err
   125  }
   126  
   127  func commandGetCellsAliases(ctx context.Context, wr *wrangler.Wrangler, subFlags *pflag.FlagSet, args []string) error {
   128  	if err := subFlags.Parse(args); err != nil {
   129  		return err
   130  	}
   131  	if subFlags.NArg() != 0 {
   132  		return fmt.Errorf("GetCellsAliases command takes no parameter")
   133  	}
   134  	aliases, err := wr.TopoServer().GetCellsAliases(ctx, true /*strongRead*/)
   135  	if err != nil {
   136  		return err
   137  	}
   138  	return printJSON(wr.Logger(), aliases)
   139  }