github.com/spinnaker/spin@v1.30.0/cmd/pipeline-template/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_template 16 17 import ( 18 "fmt" 19 "net/http" 20 21 "github.com/antihax/optional" 22 "github.com/spf13/cobra" 23 24 orca_tasks "github.com/spinnaker/spin/cmd/orca-tasks" 25 gate "github.com/spinnaker/spin/gateapi" 26 "github.com/spinnaker/spin/util" 27 ) 28 29 type saveOptions struct { 30 *pipelineTemplateOptions 31 output string 32 templateFile string 33 tag string 34 } 35 36 var ( 37 saveTemplateShort = "Save the provided pipeline template" 38 saveTemplateLong = "Save the provided pipeline template" 39 ) 40 41 func NewSaveCmd(pipelineTemplateOptions *pipelineTemplateOptions) *cobra.Command { 42 options := &saveOptions{ 43 pipelineTemplateOptions: pipelineTemplateOptions, 44 } 45 cmd := &cobra.Command{ 46 Use: "save", 47 Aliases: []string{}, 48 Short: saveTemplateShort, 49 Long: saveTemplateLong, 50 RunE: func(cmd *cobra.Command, args []string) error { 51 return savePipelineTemplate(cmd, options) 52 }, 53 } 54 55 cmd.PersistentFlags().StringVarP(&options.templateFile, "file", 56 "f", "", "path to the pipeline template file") 57 cmd.PersistentFlags().StringVar(&options.tag, "tag", "", 58 "(optional) specific tag to tag pipeline template with") 59 60 return cmd 61 } 62 63 func savePipelineTemplate(cmd *cobra.Command, options *saveOptions) error { 64 templateJson, err := util.ParseJsonFromFileOrStdin(options.templateFile, false) 65 if err != nil { 66 return err 67 } 68 69 valid := true 70 if _, exists := templateJson["id"]; !exists { 71 options.Ui.Error("Required pipeline template key 'id' missing...\n") 72 valid = false 73 } 74 if _, exists := templateJson["schema"]; !exists { 75 options.Ui.Error("Required pipeline template key 'schema' missing...\n") 76 valid = false 77 } 78 if !valid { 79 return fmt.Errorf("Submitted pipeline is invalid: %s\n", templateJson) 80 } 81 82 templateId := templateJson["id"].(string) 83 84 getQueryParam := &gate.V2PipelineTemplatesControllerApiGetUsingGET2Opts{} 85 if options.tag != "" { 86 getQueryParam.Tag = optional.NewString(options.tag) 87 } else if tag, exists := templateJson["tag"]; exists && tag.(string) != "" { 88 getQueryParam.Tag = optional.NewString(tag.(string)) 89 } 90 91 _, resp, queryErr := options.GateClient.V2PipelineTemplatesControllerApi.GetUsingGET2(options.GateClient.Context, templateId, getQueryParam) 92 93 var saveResp *http.Response 94 var saveRet map[string]interface{} 95 var saveErr error 96 97 switch resp.StatusCode { 98 case http.StatusOK: 99 opt := &gate.V2PipelineTemplatesControllerApiUpdateUsingPOST1Opts{} 100 if options.tag != "" { 101 opt.Tag = optional.NewString(options.tag) 102 } 103 104 saveRet, saveResp, saveErr = options.GateClient.V2PipelineTemplatesControllerApi.UpdateUsingPOST1(options.GateClient.Context, templateId, templateJson, opt) 105 case http.StatusNotFound: 106 opt := &gate.V2PipelineTemplatesControllerApiCreateUsingPOST1Opts{} 107 if options.tag != "" { 108 opt.Tag = optional.NewString(options.tag) 109 } 110 111 saveRet, saveResp, saveErr = options.GateClient.V2PipelineTemplatesControllerApi.CreateUsingPOST1(options.GateClient.Context, templateJson, opt) 112 default: 113 if queryErr != nil { 114 return queryErr 115 } 116 return fmt.Errorf("Encountered an unexpected status code %d querying pipeline template with id %s\n", 117 resp.StatusCode, templateId) 118 } 119 120 if saveErr != nil { 121 return saveErr 122 } 123 124 if saveResp.StatusCode != http.StatusAccepted && saveResp.StatusCode != http.StatusOK { 125 return fmt.Errorf("Encountered an error saving pipeline template %v, status code: %d\n", 126 templateJson, 127 saveResp.StatusCode) 128 } 129 130 if len(saveRet) > 0 { 131 taskSucceeded := orca_tasks.TaskSucceeded(saveRet) 132 if !taskSucceeded { 133 return fmt.Errorf("Encountered an error with saving pipeline template %v", saveRet) 134 } 135 } 136 137 options.Ui.Success("Pipeline template save succeeded") 138 return nil 139 }