|
1 | 1 | package servicelog |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "encoding/json" |
4 | 5 | "fmt" |
| 6 | + slv1 "github.com/openshift-online/ocm-sdk-go/servicelogs/v1" |
5 | 7 | "os" |
| 8 | + "time" |
6 | 9 |
|
7 | | - "github.com/openshift-online/ocm-cli/pkg/arguments" |
8 | 10 | "github.com/openshift-online/ocm-cli/pkg/dump" |
9 | | - sdk "github.com/openshift-online/ocm-sdk-go" |
10 | | - cmv1 "github.com/openshift-online/ocm-sdk-go/clustersmgmt/v1" |
11 | | - "github.com/openshift/osdctl/pkg/utils" |
12 | | - log "github.com/sirupsen/logrus" |
13 | 11 | "github.com/spf13/cobra" |
14 | | - cmdutil "k8s.io/kubectl/pkg/cmd/util" |
| 12 | +) |
| 13 | + |
| 14 | +const ( |
| 15 | + AllMessagesFlag = "all-messages" |
| 16 | + AllMessagesShortFlag = "A" |
| 17 | + InternalFlag = "internal" |
| 18 | + InternalShortFlag = "i" |
15 | 19 | ) |
16 | 20 |
|
17 | 21 | // listCmd represents the list command |
18 | 22 | var listCmd = &cobra.Command{ |
19 | | - Use: "list [flags] [options] cluster-identifier", |
20 | | - Short: "gets all servicelog messages for a given cluster", |
21 | | - Args: cobra.ArbitraryArgs, |
22 | | - SilenceErrors: true, |
23 | | - Run: func(cmd *cobra.Command, args []string) { |
24 | | - cmdutil.CheckErr(complete(cmd, args)) |
25 | | - cmdutil.CheckErr(run(cmd, args[0])) |
26 | | - }, |
27 | | -} |
28 | | - |
29 | | -func complete(cmd *cobra.Command, args []string) error { |
30 | | - if len(args) == 0 { |
31 | | - err := cmd.Help() |
| 23 | + Use: "list [flags] [options] cluster-identifier", |
| 24 | + Short: "gets all servicelog messages for a given cluster", |
| 25 | + Args: cobra.ExactArgs(1), |
| 26 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 27 | + allMessages, err := cmd.Flags().GetBool(AllMessagesFlag) |
32 | 28 | if err != nil { |
33 | | - return fmt.Errorf("error calling cmd.Help(): %w", err) |
| 29 | + return fmt.Errorf("failed to get flag `--%v`/`-%v`, %w", AllMessagesFlag, AllMessagesShortFlag, err) |
| 30 | + } |
34 | 31 |
|
| 32 | + internalOnly, err := cmd.Flags().GetBool(InternalFlag) |
| 33 | + if err != nil { |
| 34 | + return fmt.Errorf("failed to get flag `--%v`/`-%v`, %w", InternalFlag, InternalShortFlag, err) |
35 | 35 | } |
36 | | - return fmt.Errorf("cluster-identifier was not provided. please provide a cluster id, UUID, or name") |
37 | | - } |
38 | 36 |
|
39 | | - if len(args) != 1 { |
40 | | - log.Infof("Too many arguments. Expected 1 got %d", len(args)) |
41 | | - } |
| 37 | + return ListServiceLogs(args[0], allMessages, internalOnly) |
| 38 | + }, |
| 39 | +} |
42 | 40 |
|
43 | | - return nil |
| 41 | +func init() { |
| 42 | + // define flags |
| 43 | + listCmd.Flags().BoolP(AllMessagesFlag, AllMessagesShortFlag, false, "Toggle if we should see all of the messages or only SRE-P specific ones") |
| 44 | + listCmd.Flags().BoolP(InternalFlag, InternalShortFlag, false, "Toggle if we should see internal messages") |
44 | 45 | } |
45 | 46 |
|
46 | | -func run(cmd *cobra.Command, clusterID string) error { |
47 | | - response, err := FetchServiceLogs(clusterID) |
| 47 | +func ListServiceLogs(clusterID string, allMessages bool, internalOnly bool) error { |
| 48 | + response, err := FetchServiceLogs(clusterID, allMessages, internalOnly) |
48 | 49 | if err != nil { |
49 | | - // If the response has errored, likely the input was bad, so show usage |
50 | | - helpErr := cmd.Help() |
51 | | - if helpErr != nil { |
52 | | - return helpErr |
53 | | - } |
54 | | - return err |
| 50 | + return fmt.Errorf("failed to fetch service logs: %w", err) |
55 | 51 | } |
56 | 52 |
|
57 | | - err = dump.Pretty(os.Stdout, response.Bytes()) |
58 | | - if err != nil { |
59 | | - // If outputing the data errored, there's likely an internal error, so just return the error |
60 | | - return err |
| 53 | + if err = printServiceLogResponse(response); err != nil { |
| 54 | + return fmt.Errorf("failed to print service logs: %w", err) |
61 | 55 | } |
| 56 | + |
62 | 57 | return nil |
63 | 58 | } |
64 | 59 |
|
65 | | -var serviceLogListAllMessagesFlag = false |
66 | | -var serviceLogListInternalOnlyFlag = false |
67 | | - |
68 | | -func FetchServiceLogs(clusterID string) (*sdk.Response, error) { |
69 | | - // Create OCM client to talk to cluster API |
70 | | - ocmClient, err := utils.CreateConnection() |
71 | | - if err != nil { |
72 | | - return nil, err |
| 60 | +func printServiceLogResponse(response *slv1.ClustersClusterLogsListResponse) error { |
| 61 | + entryViews := logEntryToView(response.Items().Slice()) |
| 62 | + view := LogEntryResponseView{ |
| 63 | + Items: entryViews, |
| 64 | + Kind: "ClusterLogList", |
| 65 | + Page: response.Page(), |
| 66 | + Size: response.Size(), |
| 67 | + Total: response.Total(), |
73 | 68 | } |
74 | | - defer func() { |
75 | | - if err := ocmClient.Close(); err != nil { |
76 | | - fmt.Printf("Cannot close the ocmClient (possible memory leak): %q", err) |
77 | | - } |
78 | | - }() |
79 | 69 |
|
80 | | - // Use the OCM client to retrieve clusters |
81 | | - clusters := utils.GetClusters(ocmClient, []string{clusterID}) |
82 | | - if len(clusters) != 1 { |
83 | | - return nil, fmt.Errorf("GetClusters expected to return 1 cluster, got: %d", len(clusters)) |
| 70 | + viewBytes, err := json.Marshal(view) |
| 71 | + if err != nil { |
| 72 | + return fmt.Errorf("failed to marshal response for output: %w", err) |
84 | 73 | } |
85 | | - cluster := clusters[0] |
86 | 74 |
|
87 | | - // Now get the SLs for the cluster |
88 | | - return utils.SendRequest(CreateListSLRequest(ocmClient, cluster, serviceLogListAllMessagesFlag, serviceLogListInternalOnlyFlag)) |
| 75 | + return dump.Pretty(os.Stdout, viewBytes) |
89 | 76 | } |
90 | 77 |
|
91 | | -func init() { |
92 | | - // define required flags |
93 | | - listCmd.Flags().BoolVarP(&serviceLogListAllMessagesFlag, "all-messages", "A", serviceLogListAllMessagesFlag, "Toggle if we should see all of the messages or only SRE-P specific ones") |
94 | | - listCmd.Flags().BoolVarP(&serviceLogListInternalOnlyFlag, "internal", "i", serviceLogListInternalOnlyFlag, "Toggle if we should see internal messages") |
| 78 | +type LogEntryResponseView struct { |
| 79 | + Items []*LogEntryView `json:"items"` |
| 80 | + Kind string `json:"kind"` |
| 81 | + Page int `json:"page"` |
| 82 | + Size int `json:"size"` |
| 83 | + Total int `json:"total"` |
95 | 84 | } |
96 | 85 |
|
97 | | -func CreateListSLRequest(ocmClient *sdk.Connection, cluster *cmv1.Cluster, allMessages bool, internalMessages bool) *sdk.Request { |
98 | | - // Create and populate the request: |
99 | | - request := ocmClient.Get() |
100 | | - err := arguments.ApplyPathArg(request, targetAPIPath) |
101 | | - if err != nil { |
102 | | - log.Fatalf("Can't parse API path '%s': %v\n", targetAPIPath, err) |
103 | | - } |
104 | | - var empty []string |
105 | | - |
106 | | - // prefer cluster external over cluster internal ID |
107 | | - var formatMessage string |
108 | | - if cluster.ExternalID() != "" { |
109 | | - formatMessage = fmt.Sprintf(`search=cluster_uuid = '%s'`, cluster.ExternalID()) |
110 | | - } else { |
111 | | - formatMessage = fmt.Sprintf(`search=cluster_id = '%s'`, cluster.ID()) |
112 | | - } |
| 86 | +type LogEntryView struct { |
| 87 | + ClusterID string `json:"cluster_id"` |
| 88 | + ClusterUUID string `json:"cluster_uuid"` |
| 89 | + CreatedAt time.Time `json:"created_at"` |
| 90 | + CreatedBy string `json:"created_by"` |
| 91 | + Description string `json:"description"` |
| 92 | + EventStreamID string `json:"event_stream_id"` |
| 93 | + Href string `json:"href"` |
| 94 | + ID string `json:"id"` |
| 95 | + InternalOnly bool `json:"internal_only"` |
| 96 | + Kind string `json:"kind"` |
| 97 | + LogType string `json:"log_type"` |
| 98 | + ServiceName string `json:"service_name"` |
| 99 | + Severity string `json:"severity"` |
| 100 | + Summary string `json:"summary"` |
| 101 | + Timestamp time.Time `json:"timestamp"` |
| 102 | + Username string `json:"username"` |
| 103 | +} |
113 | 104 |
|
114 | | - if !allMessages { |
115 | | - formatMessage += ` and service_name = 'SREManualAction'` |
116 | | - } |
117 | | - if internalMessages { |
118 | | - formatMessage += ` and internal_only = 'true'` |
| 105 | +func logEntryToView(entries []*slv1.LogEntry) []*LogEntryView { |
| 106 | + entryViews := make([]*LogEntryView, 0, len(entries)) |
| 107 | + for _, entry := range entries { |
| 108 | + entryView := &LogEntryView{ |
| 109 | + ClusterID: entry.ClusterID(), |
| 110 | + ClusterUUID: entry.ClusterUUID(), |
| 111 | + CreatedAt: entry.CreatedAt(), |
| 112 | + CreatedBy: entry.CreatedBy(), |
| 113 | + Description: entry.Description(), |
| 114 | + EventStreamID: entry.EventStreamID(), |
| 115 | + Href: entry.HREF(), |
| 116 | + ID: entry.ID(), |
| 117 | + InternalOnly: entry.InternalOnly(), |
| 118 | + Kind: entry.Kind(), |
| 119 | + LogType: string(entry.LogType()), |
| 120 | + ServiceName: entry.ServiceName(), |
| 121 | + Severity: string(entry.Severity()), |
| 122 | + Summary: entry.Summary(), |
| 123 | + Timestamp: entry.Timestamp(), |
| 124 | + Username: entry.Username(), |
| 125 | + } |
| 126 | + entryViews = append(entryViews, entryView) |
119 | 127 | } |
120 | | - arguments.ApplyParameterFlag(request, []string{formatMessage}) |
121 | | - arguments.ApplyHeaderFlag(request, empty) |
122 | | - return request |
| 128 | + return entryViews |
123 | 129 | } |
0 commit comments