github.com/m3db/m3@v1.5.0/src/cmd/tools/m3ctl/yaml/peeker.go (about)

     1  // Copyright (c) 2020 Uber Technologies, Inc.
     2  //
     3  // Permission is hereby granted, free of charge, to any person obtaining a copy
     4  // of this software and associated documentation files (the "Software"), to deal
     5  // in the Software without restriction, including without limitation the rights
     6  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     7  // copies of the Software, and to permit persons to whom the Software is
     8  // furnished to do so, subject to the following conditions:
     9  //
    10  // The above copyright notice and this permission notice shall be included in
    11  // all copies or substantial portions of the Software.
    12  //
    13  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    14  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    15  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    16  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    17  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    18  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    19  // THE SOFTWARE.
    20  
    21  package yaml
    22  
    23  import (
    24  	"fmt"
    25  
    26  	"github.com/ghodss/yaml"
    27  	"github.com/gogo/protobuf/proto"
    28  
    29  	"github.com/m3db/m3/src/cmd/tools/m3ctl/placements"
    30  	"github.com/m3db/m3/src/query/generated/proto/admin"
    31  )
    32  
    33  // peek into the yaml to see what it is expected to be
    34  //
    35  // returns the url path, proto.Message, and error
    36  func peeker(data []byte) (string, proto.Message, error) {
    37  	type peeker struct {
    38  		Operation string
    39  		Service   string
    40  	}
    41  	peek := &peeker{}
    42  
    43  	// this really does nothing more than unpack into the above
    44  	// private type to take a peek at Operation
    45  	// its just a peek
    46  	if err := yaml.Unmarshal(data, &peek); err != nil {
    47  		return "", nil, err
    48  	}
    49  
    50  	// now the payload is of known type
    51  	// unmarshal it and return the proto.Message
    52  	if peek.Service == "" {
    53  		peek.Service = "m3db"
    54  	}
    55  	placementPath := placements.DefaultPath + peek.Service + "/placement"
    56  
    57  	switch peek.Operation {
    58  	case opCreate:
    59  		payload := struct{ Request admin.DatabaseCreateRequest }{}
    60  		if err := yaml.Unmarshal(data, &payload); err != nil {
    61  			return "", nil, err
    62  		}
    63  		return dbcreatePath, &payload.Request, nil
    64  	case opInit:
    65  		payload := struct{ Request admin.PlacementInitRequest }{}
    66  		if err := yaml.Unmarshal(data, &payload); err != nil {
    67  			return "", nil, err
    68  		}
    69  		return fmt.Sprintf("%s/init", placementPath), &payload.Request, nil
    70  	case opReplace:
    71  		payload := struct{ Request admin.PlacementReplaceRequest }{}
    72  		if err := yaml.Unmarshal(data, &payload); err != nil {
    73  			return "", nil, err
    74  		}
    75  		return fmt.Sprintf("%s/replace", placementPath), &payload.Request, nil
    76  	case opNewNode:
    77  		payload := struct{ Request admin.PlacementInitRequest }{}
    78  		if err := yaml.Unmarshal(data, &payload); err != nil {
    79  			return "", nil, err
    80  		}
    81  		return placementPath, &payload.Request, nil
    82  	default:
    83  		return "", nil, fmt.Errorf("unknown operation specified in the yaml")
    84  	}
    85  
    86  }