-
Notifications
You must be signed in to change notification settings - Fork 112
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(servicelog): list current servicelogs
- refactor `osdctl servicelog post`
- Loading branch information
1 parent
bdec0a4
commit 84931a0
Showing
9 changed files
with
278 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package servicelog | ||
|
||
import ( | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func NewCmdServiceLog() *cobra.Command { | ||
var servicelogCmd = &cobra.Command{ | ||
Use: "servicelog", | ||
Short: "OCM/Hive Service log", | ||
Run: func(cmd *cobra.Command, args []string) { | ||
cmd.Help() | ||
}, | ||
} | ||
|
||
// Add subcommands | ||
servicelogCmd.AddCommand(listCmd) // servicelog list | ||
servicelogCmd.AddCommand(postCmd) // servicelog post | ||
|
||
return servicelogCmd | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package servicelog | ||
|
||
import ( | ||
"strings" | ||
|
||
"github.com/openshift-online/ocm-cli/pkg/ocm" | ||
sdk "github.com/openshift-online/ocm-sdk-go" | ||
log "github.com/sirupsen/logrus" | ||
) | ||
|
||
var ( | ||
templateParams, userParameterNames, userParameterValues []string | ||
isURL bool | ||
HTMLBody []byte | ||
) | ||
|
||
const ( | ||
// in case you want to see the swagger code gen, you can look at | ||
// https://api.openshift.com/?urls.primaryName=Service%20logs#/default/post_api_service_logs_v1_cluster_logs | ||
targetAPIPath = "/api/service_logs/v1/cluster_logs" | ||
) | ||
|
||
func createConnection() *sdk.Connection { | ||
connection, err := ocm.NewConnection().Build() | ||
if err != nil { | ||
if strings.Contains(err.Error(), "Not logged in, run the") { | ||
log.Fatalf("Failed to create OCM connection: Authetication error, run the 'ocm login' command first.") | ||
} | ||
log.Fatalf("Failed to create OCM connection: %v", err) | ||
} | ||
return connection | ||
} | ||
|
||
func sendRequest(request *sdk.Request) *sdk.Response { | ||
response, err := request.Send() | ||
if err != nil { | ||
log.Fatalf("Can't send request: %v", err) | ||
} | ||
return response | ||
} | ||
|
||
func check(response *sdk.Response, dir string) { | ||
status := response.Status() | ||
|
||
body := response.Bytes() | ||
|
||
if status < 400 { | ||
validateGoodResponse(body) | ||
log.Info("Message has been successfully sent") | ||
|
||
} else { | ||
validateBadResponse(body) | ||
cleanup(dir) | ||
log.Fatalf("Failed to post message because of %q", BadReply.Reason) | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
package servicelog | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"os" | ||
"strings" | ||
|
||
"github.com/openshift-online/ocm-cli/pkg/arguments" | ||
"github.com/openshift-online/ocm-cli/pkg/dump" | ||
sdk "github.com/openshift-online/ocm-sdk-go" | ||
"github.com/openshift/osdctl/internal/servicelog" | ||
log "github.com/sirupsen/logrus" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
// listCmd represents the list command | ||
var listCmd = &cobra.Command{ | ||
Use: "list", | ||
Short: "gets all servicelog messages for a given cluster", | ||
// validate only clusterid is provided | ||
Args: cobra.ExactArgs(1), | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
clusterId := args[0] | ||
|
||
// Create an OCM client to talk to the cluster API | ||
// the user has to be logged in (e.g. 'ocm login') | ||
ocmClient := createConnection() | ||
defer func() { | ||
if err := ocmClient.Close(); err != nil { | ||
log.Errorf("Cannot close the ocmClient (possible memory leak): %q", err) | ||
} | ||
}() | ||
|
||
// Use the OCM client to create the POST request | ||
request := createClusterRequest(ocmClient, clusterId) | ||
response := sendRequest(request) | ||
clusterExternalId, err := extractExternalIdFromResponse(response) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// send it as logservice and validate the response | ||
request = createListRequest(ocmClient, clusterExternalId, serviceLogListAllMessagesFlag) | ||
response = sendRequest(request) | ||
|
||
err = dump.Pretty(os.Stdout, response.Bytes()) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
}, | ||
} | ||
|
||
var serviceLogListAllMessagesFlag = false | ||
|
||
const listServiceLogAPIPath = "/api/service_logs/v1/clusters/%s/cluster_logs" | ||
|
||
func init() { | ||
// define required flags | ||
listCmd.Flags().BoolVarP(&serviceLogListAllMessagesFlag, "all-messages", "A", serviceLogListAllMessagesFlag, "Toggle if we should see all of the messages or only SRE-P specific ones") | ||
} | ||
|
||
func extractExternalIdFromResponse(response *sdk.Response) (string, error) { | ||
status := response.Status() | ||
body := response.Bytes() | ||
|
||
if status >= 400 { | ||
validateBadResponse(body) | ||
log.Fatalf("Failed to list message because of %q", BadReply.Reason) | ||
return "", nil | ||
} | ||
|
||
validateGoodResponse(body) | ||
clusterListGoodReply := servicelog.ClusterListGoodReply{} | ||
err := json.Unmarshal(body, &clusterListGoodReply) | ||
if err != nil { | ||
err = fmt.Errorf("cannot parse good clusterlist response: %w", err) | ||
return "", err | ||
} | ||
|
||
if clusterListGoodReply.Total != 1 || len(clusterListGoodReply.Items) != 1 { | ||
return "", fmt.Errorf("could not find an exact match for the clustername") | ||
} | ||
|
||
return clusterListGoodReply.Items[0].ExternalID, nil | ||
} | ||
|
||
func createClusterRequest(ocmClient *sdk.Connection, clusterId string) *sdk.Request { | ||
|
||
searchString := fmt.Sprintf(`search=display_name like '%[1]s' or name like '%[1]s' or id like '%[1]s' or external_id like '%[1]s'`, clusterId) | ||
searchString = strings.TrimSpace(searchString) | ||
request := ocmClient.Get() | ||
err := arguments.ApplyPathArg(request, "/api/clusters_mgmt/v1/clusters/") | ||
if err != nil { | ||
log.Fatalf("Can't parse API path '%s': %v\n", targetAPIPath, err) | ||
} | ||
|
||
arguments.ApplyParameterFlag(request, []string{searchString}) | ||
|
||
return request | ||
} | ||
|
||
func createListRequest(ocmClient *sdk.Connection, clusterId string, allMessages bool) *sdk.Request { | ||
// Create and populate the request: | ||
request := ocmClient.Get() | ||
err := arguments.ApplyPathArg(request, targetAPIPath) | ||
if err != nil { | ||
log.Fatalf("Can't parse API path '%s': %v\n", targetAPIPath, err) | ||
} | ||
var empty []string | ||
|
||
formatMessage := fmt.Sprintf(`search=cluster_uuid = '%s'`, clusterId) | ||
if !allMessages { | ||
formatMessage += ` and service_name = 'SREManualAction'` | ||
} | ||
arguments.ApplyParameterFlag(request, []string{formatMessage}) | ||
arguments.ApplyHeaderFlag(request, empty) | ||
return request | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.