github.com/spinnaker/spin@v1.30.0/cmd/canary/canary-config/delete.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  	"github.com/spf13/cobra"
    22  
    23  	gate "github.com/spinnaker/spin/gateapi"
    24  	"github.com/spinnaker/spin/util"
    25  )
    26  
    27  type deleteOptions struct {
    28  	*canaryConfigOptions
    29  }
    30  
    31  const (
    32  	deleteCanaryConfigShort = "Delete the provided canary config"
    33  	deleteCanaryConfigLong  = "Delete the provided canary config"
    34  )
    35  
    36  func NewDeleteCmd(canaryConfigOptions *canaryConfigOptions) *cobra.Command {
    37  	options := &deleteOptions{
    38  		canaryConfigOptions: canaryConfigOptions,
    39  	}
    40  	cmd := &cobra.Command{
    41  		Use:     "delete",
    42  		Aliases: []string{"del"},
    43  		Short:   deleteCanaryConfigShort,
    44  		Long:    deleteCanaryConfigLong,
    45  		RunE: func(cmd *cobra.Command, args []string) error {
    46  			return deleteCanaryConfig(cmd, options, args)
    47  		},
    48  	}
    49  
    50  	return cmd
    51  }
    52  
    53  func deleteCanaryConfig(cmd *cobra.Command, options *deleteOptions, args []string) error {
    54  	id, err := util.ReadArgsOrStdin(args)
    55  	if err != nil {
    56  		return err
    57  	}
    58  
    59  	resp, err := options.GateClient.V2CanaryConfigControllerApi.DeleteCanaryConfigUsingDELETE(
    60  		options.GateClient.Context, id, &gate.V2CanaryConfigControllerApiDeleteCanaryConfigUsingDELETEOpts{})
    61  	if err != nil {
    62  		return err
    63  	}
    64  
    65  	if resp.StatusCode != http.StatusOK {
    66  		return fmt.Errorf(
    67  			"Encountered an error deleting canary config, status code: %d\n", resp.StatusCode)
    68  	}
    69  
    70  	options.Ui.Success(fmt.Sprintf("Canary config %s deleted", id))
    71  	return nil
    72  }