This repository has been archived by the owner on Nov 16, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 58
/
delete_api.go
80 lines (69 loc) · 2.07 KB
/
delete_api.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
///////////////////////////////////////////////////////////////////////
// Copyright (c) 2017 VMware, Inc. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
///////////////////////////////////////////////////////////////////////
package cmd
import (
"encoding/json"
"fmt"
"io"
"golang.org/x/net/context"
"github.com/spf13/cobra"
apiclient "github.com/vmware/dispatch/pkg/api-manager/gen/client/endpoint"
"github.com/vmware/dispatch/pkg/api/v1"
"github.com/vmware/dispatch/pkg/dispatchcli/cmd/utils"
"github.com/vmware/dispatch/pkg/dispatchcli/i18n"
)
var (
deleteAPILong = i18n.T(`Delete api.`)
// TODO: add examples
deleteAPIExample = i18n.T(``)
)
// NewCmdDeleteAPI creates command responsible for deleting API.
func NewCmdDeleteAPI(out io.Writer, errOut io.Writer) *cobra.Command {
cmd := &cobra.Command{
Use: "api API_NAME",
Short: i18n.T("Delete API"),
Long: deleteAPILong,
Example: deleteAPIExample,
Args: cobra.ExactArgs(1),
Aliases: []string{"apis"},
Run: func(cmd *cobra.Command, args []string) {
err := deleteAPI(out, errOut, cmd, args)
CheckErr(err)
},
}
cmd.Flags().StringVarP(&cmdFlagApplication, "application", "a", "", "filter by application")
return cmd
}
func deleteAPI(out, errOut io.Writer, cmd *cobra.Command, args []string) error {
client := apiManagerClient()
params := &apiclient.DeleteAPIParams{
Context: context.Background(),
API: args[0],
Tags: []string{},
}
utils.AppendApplication(¶ms.Tags, cmdFlagApplication)
resp, err := client.Endpoint.DeleteAPI(params, GetAuthInfoWriter())
if err != nil {
return formatAPIError(err, params)
}
return formatDeleteAPIOutput(out, false, []*v1.API{resp.Payload})
}
func formatDeleteAPIOutput(out io.Writer, list bool, apis []*v1.API) error {
if dispatchConfig.JSON {
encoder := json.NewEncoder(out)
encoder.SetIndent("", " ")
if list {
return encoder.Encode(apis)
}
return encoder.Encode(apis[0])
}
for _, a := range apis {
_, err := fmt.Fprintf(out, "Deleted api: %s\n", *a.Name)
if err != nil {
return err
}
}
return nil
}