github.com/spinnaker/spin@v1.30.0/cmd/canary/canary-config/save.go (about)

     1  // Copyright (c) 2019, Waze, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //   http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  //   Unless required by applicable law or agreed to in writing, software
    10  //   distributed under the License is distributed on an "AS IS" BASIS,
    11  //   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  //   See the License for the specific language governing permissions and
    13  //   limitations under the License.
    14  
    15  package canary_config
    16  
    17  import (
    18  	"fmt"
    19  	"net/http"
    20  
    21  	gate "github.com/spinnaker/spin/gateapi"
    22  
    23  	"github.com/spf13/cobra"
    24  
    25  	"github.com/spinnaker/spin/util"
    26  )
    27  
    28  type saveOptions struct {
    29  	*canaryConfigOptions
    30  	output       string
    31  	templateFile string
    32  }
    33  
    34  const (
    35  	saveTemplateShort = "Save the provided canary config"
    36  	saveTemplateLong  = "Save the provided canary config"
    37  )
    38  
    39  func NewSaveCmd(canaryConfigOptions *canaryConfigOptions) *cobra.Command {
    40  	options := &saveOptions{
    41  		canaryConfigOptions: canaryConfigOptions,
    42  	}
    43  	cmd := &cobra.Command{
    44  		Use:     "save",
    45  		Aliases: []string{},
    46  		Short:   saveTemplateShort,
    47  		Long:    saveTemplateLong,
    48  		RunE: func(cmd *cobra.Command, args []string) error {
    49  			return saveCanaryConfig(cmd, options)
    50  		},
    51  	}
    52  
    53  	cmd.PersistentFlags().StringVarP(&options.templateFile, "file",
    54  		"f", "", "path to the canary config file")
    55  
    56  	return cmd
    57  }
    58  
    59  func saveCanaryConfig(cmd *cobra.Command, options *saveOptions) error {
    60  	templateJson, err := util.ParseJsonFromFileOrStdin(options.templateFile, false)
    61  	if err != nil {
    62  		return err
    63  	}
    64  
    65  	if _, exists := templateJson["id"]; !exists {
    66  		options.Ui.Error("Required canary config key 'id' missing...\n")
    67  		return fmt.Errorf("Submitted canary config is invalid: %s\n", templateJson)
    68  	}
    69  
    70  	templateId := templateJson["id"].(string)
    71  
    72  	_, resp, queryErr := options.GateClient.V2CanaryConfigControllerApi.GetCanaryConfigUsingGET(
    73  		options.GateClient.Context, templateId, &gate.V2CanaryConfigControllerApiGetCanaryConfigUsingGETOpts{})
    74  
    75  	var saveResp *http.Response
    76  	var saveErr error
    77  	switch resp.StatusCode {
    78  	case http.StatusOK:
    79  		_, saveResp, saveErr = options.GateClient.V2CanaryConfigControllerApi.UpdateCanaryConfigUsingPUT(
    80  			options.GateClient.Context, templateJson, templateId, &gate.V2CanaryConfigControllerApiUpdateCanaryConfigUsingPUTOpts{})
    81  	case http.StatusNotFound:
    82  		_, saveResp, saveErr = options.GateClient.V2CanaryConfigControllerApi.CreateCanaryConfigUsingPOST(
    83  			options.GateClient.Context, templateJson, &gate.V2CanaryConfigControllerApiCreateCanaryConfigUsingPOSTOpts{})
    84  	default:
    85  		if queryErr != nil {
    86  			return queryErr
    87  		}
    88  		return fmt.Errorf(
    89  			"Encountered an unexpected status code %d querying canary config with id %s\n",
    90  			resp.StatusCode, templateId)
    91  	}
    92  
    93  	if saveErr != nil {
    94  		return saveErr
    95  	}
    96  
    97  	if saveResp.StatusCode != http.StatusOK {
    98  		return fmt.Errorf(
    99  			"Encountered an error saving canary config %v, status code: %d\n",
   100  			templateJson, saveResp.StatusCode)
   101  	}
   102  
   103  	options.Ui.Success("Canary config save succeeded")
   104  	return nil
   105  }