github.com/spinnaker/spin@v1.30.0/cmd/application/delete.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 application
    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 deleteOptions struct {
    30  	*applicationOptions
    31  }
    32  
    33  var (
    34  	deleteApplicationShort   = "Delete the specified application"
    35  	deleteApplicationLong    = "Delete the provided application --application-name: Name of the Spinnaker application to delete"
    36  	deleteApplicationExample = "usage: spin application delete [options] applicationName"
    37  )
    38  
    39  func NewDeleteCmd(appOptions *applicationOptions) *cobra.Command {
    40  	options := &deleteOptions{
    41  		appOptions,
    42  	}
    43  	cmd := &cobra.Command{
    44  		Use:     "delete",
    45  		Aliases: []string{"del"},
    46  		Short:   deleteApplicationShort,
    47  		Long:    deleteApplicationLong,
    48  		Example: deleteApplicationExample,
    49  		RunE: func(cmd *cobra.Command, args []string) error {
    50  			return deleteApplication(cmd, options, args)
    51  		},
    52  	}
    53  	return cmd
    54  }
    55  
    56  func deleteApplication(cmd *cobra.Command, options *deleteOptions, args []string) error {
    57  	applicationName, err := util.ReadArgsOrStdin(args)
    58  	if err != nil {
    59  		return err
    60  	}
    61  
    62  	appSpec := map[string]interface{}{
    63  		"type": "deleteApplication",
    64  		"application": map[string]interface{}{
    65  			"name": applicationName,
    66  		},
    67  	}
    68  
    69  	_, resp, err := options.GateClient.ApplicationControllerApi.GetApplicationUsingGET(options.GateClient.Context, applicationName, &gate.ApplicationControllerApiGetApplicationUsingGETOpts{Expand: optional.NewBool(false)})
    70  
    71  	if resp != nil && resp.StatusCode == http.StatusNotFound {
    72  		return fmt.Errorf("Attempting to delete application '%s' which does not exist, exiting...", applicationName)
    73  	}
    74  
    75  	if err != nil {
    76  		return fmt.Errorf("Encountered an error checking application existence, status code: %d\n", resp.StatusCode)
    77  	}
    78  
    79  	deleteAppTask := map[string]interface{}{
    80  		"job":         []interface{}{appSpec},
    81  		"application": applicationName,
    82  		"description": fmt.Sprintf("Delete Application: %s", applicationName),
    83  	}
    84  
    85  	taskRef, resp, err := options.GateClient.TaskControllerApi.TaskUsingPOST1(options.GateClient.Context, deleteAppTask)
    86  	if err != nil {
    87  		return err
    88  	}
    89  	if resp.StatusCode != http.StatusOK {
    90  		return fmt.Errorf("Encountered an error deleting application, status code: %d\n", resp.StatusCode)
    91  	}
    92  
    93  	err = orca_tasks.WaitForSuccessfulTask(options.GateClient, taskRef)
    94  	if err != nil {
    95  		return err
    96  	}
    97  
    98  	options.Ui.Success("Application deleted")
    99  	return nil
   100  }