-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathcommand_describe.go
More file actions
96 lines (78 loc) · 3.01 KB
/
command_describe.go
File metadata and controls
96 lines (78 loc) · 3.01 KB
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package apikey
import (
"time"
"github.com/spf13/cobra"
apikeysv2 "github.com/confluentinc/ccloud-sdk-go-v2/apikeys/v2"
pcmd "github.com/confluentinc/cli/v4/pkg/cmd"
"github.com/confluentinc/cli/v4/pkg/config"
"github.com/confluentinc/cli/v4/pkg/errors"
"github.com/confluentinc/cli/v4/pkg/featureflags"
"github.com/confluentinc/cli/v4/pkg/output"
)
type out struct {
IsCurrent bool `human:"Current,omitempty" serialized:"is_current,omitempty"`
Key string `human:"Key" serialized:"key"`
Description string `human:"Description" serialized:"description"`
Owner string `human:"Owner" serialized:"owner"`
OwnerEmail string `human:"Owner Email" serialized:"owner_email"`
ResourceType string `human:"Resource Type" serialized:"resource_type"`
Resource string `human:"Resource" serialized:"resource"`
Created string `human:"Created" serialized:"created"`
}
func (c *command) newDescribeCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "describe <id>",
Short: "Describe an API key.",
Args: cobra.ExactArgs(1),
ValidArgsFunction: pcmd.NewValidArgsFunction(c.validArgs),
RunE: c.describe,
}
pcmd.AddOutputFlag(cmd)
return cmd
}
func (c *command) describe(cmd *cobra.Command, args []string) error {
c.setKeyStoreIfNil()
apiKey, httpResp, err := c.V2Client.GetApiKey(args[0])
if err != nil {
return errors.CatchApiKeyForbiddenAccessError(err, getOperation, httpResp)
}
var ownerId string
var email string
if apiKey.Spec.HasOwner() {
allUsers, err := c.getAllUsers()
if err != nil {
return err
}
resourceIdToUserIdMap := mapResourceIdToUserId(allUsers)
usersMap := getUsersMap(allUsers)
serviceAccounts, err := c.V2Client.ListIamServiceAccounts(nil)
if err != nil {
return err
}
serviceAccountsMap := getServiceAccountsMap(serviceAccounts)
ownerId = apiKey.Spec.Owner.GetId()
auditLogServiceAccountId := c.getAuditLogServiceAccountId()
email = c.getEmail(ownerId, auditLogServiceAccountId, resourceIdToUserIdMap, usersMap, serviceAccountsMap)
}
resources := []apikeysv2.ObjectReference{apiKey.Spec.GetResource()}
multiClusterResources := apiKey.Spec.GetResources()
// Check if multicluster keys are enabled, and if so check the resources field
if featureflags.Manager.BoolVariation("cli.multicluster-api-keys.enable", c.Context, config.CliLaunchDarklyClient, true, false) && len(multiClusterResources) > 0 {
resources = multiClusterResources
}
list := output.NewList(cmd)
// Note that if more resource types are added with no logical clusters, then additional logic
// needs to be added here to determine the resource type.
for _, resource := range resources {
list.Add(&out{
Key: apiKey.GetId(),
Description: apiKey.Spec.GetDescription(),
Owner: ownerId,
OwnerEmail: email,
ResourceType: getResourceType(resource),
Resource: getResourceId(resource.GetId()),
Created: apiKey.Metadata.GetCreatedAt().Format(time.RFC3339),
})
}
return list.Print()
}