This repository has been archived by the owner on Nov 16, 2020. It is now read-only.
/
iam_delete_serviceaccount.go
71 lines (60 loc) · 1.92 KB
/
iam_delete_serviceaccount.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
///////////////////////////////////////////////////////////////////////
// Copyright (c) 2017 VMware, Inc. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
///////////////////////////////////////////////////////////////////////
package cmd
import (
"context"
"fmt"
"io"
"github.com/spf13/cobra"
"github.com/vmware/dispatch/pkg/client"
"github.com/vmware/dispatch/pkg/api/v1"
"github.com/vmware/dispatch/pkg/dispatchcli/i18n"
)
var (
deleteServiceAccountLong = i18n.T(`Delete a dispatch service account`)
// TODO: add examples
deleteServiceAccountExample = i18n.T(``)
)
// NewCmdIamDeleteServiceAccount creates command for delete service accounts
func NewCmdIamDeleteServiceAccount(out, errOut io.Writer) *cobra.Command {
cmd := &cobra.Command{
Use: i18n.T("serviceaccount SERVICE_ACCOUNT_NAME"),
Short: i18n.T("Delete serviceaccount"),
Long: deleteServiceAccountLong,
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
c := identityManagerClient()
err := deleteServiceAccount(out, errOut, cmd, args, c)
CheckErr(err)
},
}
return cmd
}
// CallDeleteServiceAccount makes the API call to delete ServiceAccount
func CallDeleteServiceAccount(c client.IdentityClient) ModelAction {
return func(s interface{}) error {
serviceAccountModel := s.(*v1.ServiceAccount)
deleted, err := c.DeleteServiceAccount(context.TODO(), "", *serviceAccountModel.Name)
if err != nil {
return err
}
*serviceAccountModel = *deleted
return nil
}
}
func deleteServiceAccount(out, errOut io.Writer, cmd *cobra.Command, args []string, c client.IdentityClient) error {
serviceAccountModel := v1.ServiceAccount{
Name: &args[0],
}
err := CallDeleteServiceAccount(c)(&serviceAccountModel)
if err != nil {
return err
}
if w, err := formatOutput(out, false, serviceAccountModel); w {
return err
}
fmt.Fprintf(out, "Deleted ServiceAccount: %s\n", *serviceAccountModel.Name)
return nil
}