github.com/spinnaker/spin@v1.30.0/cmd/pipeline/save.go (about)

     1  // Copyright (c) 2018, Google, 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 pipeline
    16  
    17  import (
    18  	"fmt"
    19  	"io/ioutil"
    20  	"net/http"
    21  
    22  	"github.com/spf13/cobra"
    23  
    24  	gate "github.com/spinnaker/spin/gateapi"
    25  	"github.com/spinnaker/spin/util"
    26  )
    27  
    28  type saveOptions struct {
    29  	*PipelineOptions
    30  	output       string
    31  	pipelineFile string
    32  }
    33  
    34  var (
    35  	savePipelineShort = "Save the provided pipeline"
    36  	savePipelineLong  = "Save the provided pipeline"
    37  )
    38  
    39  func NewSaveCmd(pipelineOptions *PipelineOptions) *cobra.Command {
    40  	options := &saveOptions{
    41  		PipelineOptions: pipelineOptions,
    42  	}
    43  	cmd := &cobra.Command{
    44  		Use:     "save",
    45  		Aliases: []string{},
    46  		Short:   savePipelineShort,
    47  		Long:    savePipelineLong,
    48  		RunE: func(cmd *cobra.Command, args []string) error {
    49  			return savePipeline(cmd, options)
    50  		},
    51  	}
    52  
    53  	cmd.PersistentFlags().StringVarP(&options.pipelineFile, "file", "f", "", "path to the pipeline file")
    54  
    55  	return cmd
    56  }
    57  
    58  func savePipeline(cmd *cobra.Command, options *saveOptions) error {
    59  	pipelineJson, err := util.ParseJsonFromFileOrStdin(options.pipelineFile, false)
    60  	if err != nil {
    61  		return err
    62  	}
    63  	valid := true
    64  	if _, exists := pipelineJson["name"]; !exists {
    65  		options.Ui.Error("Required pipeline key 'name' missing...\n")
    66  		valid = false
    67  	}
    68  
    69  	if _, exists := pipelineJson["application"]; !exists {
    70  		options.Ui.Error("Required pipeline key 'application' missing...\n")
    71  		valid = false
    72  	}
    73  
    74  	if template, exists := pipelineJson["template"]; exists && len(template.(map[string]interface{})) > 0 {
    75  		if _, exists := pipelineJson["schema"]; !exists {
    76  			options.Ui.Error("Required pipeline key 'schema' missing for templated pipeline...\n")
    77  			valid = false
    78  		}
    79  		pipelineJson["type"] = "templatedPipeline"
    80  	}
    81  
    82  	if !valid {
    83  		return fmt.Errorf("Submitted pipeline is invalid: %s\n", pipelineJson)
    84  	}
    85  
    86  	application := pipelineJson["application"].(string)
    87  	pipelineName := pipelineJson["name"].(string)
    88  
    89  	foundPipeline, queryResp, _ := options.GateClient.ApplicationControllerApi.GetPipelineConfigUsingGET(options.GateClient.Context, application, pipelineName)
    90  	switch queryResp.StatusCode {
    91  	case http.StatusOK:
    92  		// pipeline found, let's use Spinnaker's known Pipeline ID, otherwise we'll get one created for us
    93  		if len(foundPipeline) > 0 {
    94  			pipelineJson["id"] = foundPipeline["id"].(string)
    95  		}
    96  	case http.StatusNotFound:
    97  		// pipeline doesn't exists, let's create a new one
    98  	default:
    99  		b, _ := ioutil.ReadAll(queryResp.Body)
   100  		return fmt.Errorf("unhandled response %d: %s", queryResp.StatusCode, b)
   101  	}
   102  
   103  	// TODO: support option passing in and remove nil in below call
   104  	opt := &gate.PipelineControllerApiSavePipelineUsingPOSTOpts{}
   105  	saveResp, err := options.GateClient.PipelineControllerApi.SavePipelineUsingPOST(options.GateClient.Context, pipelineJson, opt)
   106  	if err != nil {
   107  		return err
   108  	}
   109  	if saveResp.StatusCode != http.StatusOK {
   110  		return fmt.Errorf("Encountered an error saving pipeline, status code: %d\n", saveResp.StatusCode)
   111  	}
   112  
   113  	options.Ui.Success("Pipeline save succeeded")
   114  	return nil
   115  }