Skip to content

Commit

Permalink
Merge pull request #174 from ramonbutter/addClusterIDToPost
Browse files Browse the repository at this point in the history
OSD-9325: add clusterID to servicelog post payload
  • Loading branch information
openshift-merge-robot authored Dec 22, 2021
2 parents 36e1706 + ad6e70a commit db3f15c
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 30 deletions.
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -302,9 +302,9 @@ osdctl servicelog list ${CLUSTERID} --all-messages
#### Post servicelogs

```bash
EXTERNAL_ID= # an external id for a cluster
CLUSTER_ID= # the unique cluster name, or internal, external id for a cluster
TEMPLATE= # file or url in which the template exists in
osdctl servicelog post --param=CLUSTER_UUID=${EXTERNAL_ID} --template=${TEMPLATE} --dry-run
osdctl servicelog post ${CLUSTER_ID} --template=${TEMPLATE} --dry-run

QUERIES_HERE= # queries that can be run on ocm's `clusters` resoruce
TEMPLATE= # file or url in which the template exists in
Expand All @@ -319,14 +319,14 @@ EOF
TEMPLATE= # file or url in which the template exists in
osdctl servicelog post --template=${TEMPLATE} --query-file=query_file.txt --dry-run

EXTERNAL_ID= # an external id for a cluster
ANOTHER_EXTERNAL_ID= # similar, but shows how to have multiple clusters as input
CLUSTER_ID= # the unique cluster name, or internal, external id for a cluster
ANOTHER_CLUSTER_ID= # similar, but shows how to have multiple clusters as input
# clusters_list.json will have the custom list of clusters to iterate on
cat << EOF > clusters_list.json
{
"clusters": [
"${EXTERNAL_ID}",
"${ANOTHER_EXTERNAL_ID}"
"${CLUSTER_ID}",
"${ANOTHER_CLUSTER_ID}"
]
}
EOF
Expand Down
43 changes: 26 additions & 17 deletions cmd/servicelog/post.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,22 @@ var postCmd = &cobra.Command{
readFilterFile() // parse the ocm filters in file provided via '-f' flag
readTemplate() // parse the given JSON template provided via '-t' flag

if len(args) < 1 && filtersFromFile == "" && clustersFile == "" {
log.Fatalf("No cluster identifier has been found.")
}

var queries []string
for _, clusterIds := range args {
queries = append(queries, generateQuery(clusterIds))
}

if len(queries) > 0 {
if len(filterParams) > 0 {
log.Warnf("A cluster identifier was passed with the '-q' flag. This will apply logical AND between the search query and the cluster given, potentially resulting in no matches")
}
filterParams = append(filterParams, strings.Join(queries, " or "))
}

// For every '-p' flag, replace its related placeholder in the template & filterFiles
for k := range userParameterNames {
replaceFlags(userParameterNames[k], userParameterValues[k])
Expand Down Expand Up @@ -137,7 +153,7 @@ var postCmd = &cobra.Command{
}()

for _, cluster := range clusters {
request, err := createPostRequest(ocmClient, cluster.ExternalID())
request, err := createPostRequest(ocmClient, cluster)
if err != nil {
failedClusters[cluster.ExternalID()] = err.Error()
continue
Expand Down Expand Up @@ -169,7 +185,6 @@ func init() {

// parseUserParameters parse all the '-p FOO=BAR' parameters and checks for syntax errors
func parseUserParameters() {
var queries []string // interpret all '-p CLUSTER_UUID' parameters as queries to be made to the ocmClient
for _, v := range templateParams {
if !strings.Contains(v, "=") {
log.Fatalf("Wrong syntax of '-p' flag. Please use it like this: '-p FOO=BAR'")
Expand All @@ -180,19 +195,8 @@ func parseUserParameters() {
log.Fatalf("Wrong syntax of '-p' flag. Please use it like this: '-p FOO=BAR'")
}

if param[0] != "CLUSTER_UUID" {
userParameterNames = append(userParameterNames, fmt.Sprintf("${%v}", param[0]))
userParameterValues = append(userParameterValues, param[1])
} else {
queries = append(queries, generateQuery(param[1]))
}
}

if len(queries) != 0 {
if len(filterParams) != 0 {
log.Warnf("At least one $CLUSTER_UUID parameter was passed with the '-q' flag. This will apply logical AND between the search query and the cluster(s) given, potentially resulting in no matches")
}
filterParams = append(filterParams, strings.Join(queries, " or "))
userParameterNames = append(userParameterNames, fmt.Sprintf("${%v}", param[0]))
userParameterValues = append(userParameterValues, param[1])
}
}

Expand Down Expand Up @@ -359,15 +363,20 @@ func printTemplate() (err error) {
return dump.Pretty(os.Stdout, exampleMessage)
}

func createPostRequest(ocmClient *sdk.Connection, clusterId string) (request *sdk.Request, err error) {
func createPostRequest(ocmClient *sdk.Connection, cluster *v1.Cluster) (request *sdk.Request, err error) {
// Create and populate the request:
request = ocmClient.Post()
err = arguments.ApplyPathArg(request, targetAPIPath)
if err != nil {
return nil, fmt.Errorf("cannot parse API path '%s': %v", targetAPIPath, err)
}

Message.ClusterUUID = clusterId
Message.ClusterUUID = cluster.ExternalID()
Message.ClusterID = cluster.ID()
if subscription := cluster.Subscription(); subscription != nil {
Message.SubscriptionID = cluster.Subscription().ID()
}

messageBytes, err := json.Marshal(Message)
if err != nil {
return nil, fmt.Errorf("cannot marshal template to json: %v", err)
Expand Down
32 changes: 25 additions & 7 deletions internal/servicelog/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@ import (

// Message is the base template structure
type Message struct {
Severity string `json:"severity"`
ServiceName string `json:"service_name"`
ClusterUUID string `json:"cluster_uuid"`
Summary string `json:"summary"`
Description string `json:"description"`
InternalOnly bool `json:"internal_only"`
EventStreamID string `json:"event_stream_id"`
Severity string `json:"severity"`
ServiceName string `json:"service_name"`
ClusterUUID string `json:"cluster_uuid,omitempty"`
ClusterID string `json:"cluster_id,omitempty"`
Summary string `json:"summary"`
Description string `json:"description"`
InternalOnly bool `json:"internal_only"`
EventStreamID string `json:"event_stream_id"`
SubscriptionID string `json:"subscription_id,omitempty"`
}

func (m *Message) GetSeverity() string {
Expand All @@ -28,6 +30,10 @@ func (m *Message) GetClusterUUID() string {
return m.ClusterUUID
}

func (m *Message) GetClusterID() string {
return m.ClusterID
}

func (m *Message) GetSummary() string {
return m.Summary
}
Expand All @@ -44,13 +50,19 @@ func (m *Message) GetEventStreamID() string {
return m.EventStreamID
}

func (m *Message) GetSubscriptionID() string {
return m.SubscriptionID
}

func (m *Message) ReplaceWithFlag(variable, value string) {
m.Severity = strings.ReplaceAll(m.Severity, variable, value)
m.ServiceName = strings.ReplaceAll(m.ServiceName, variable, value)
m.ClusterUUID = strings.ReplaceAll(m.ClusterUUID, variable, value)
m.ClusterID = strings.ReplaceAll(m.ClusterID, variable, value)
m.Summary = strings.ReplaceAll(m.Summary, variable, value)
m.Description = strings.ReplaceAll(m.Description, variable, value)
m.EventStreamID = strings.ReplaceAll(m.EventStreamID, variable, value)
m.SubscriptionID = strings.ReplaceAll(m.SubscriptionID, variable, value)
}

func (m *Message) SearchFlag(placeholder string) (found bool) {
Expand All @@ -63,6 +75,9 @@ func (m *Message) SearchFlag(placeholder string) (found bool) {
if found = strings.Contains(m.ClusterUUID, placeholder); found == true {
return found
}
if found = strings.Contains(m.ClusterID, placeholder); found == true {
return found
}
if found = strings.Contains(m.Summary, placeholder); found == true {
return found
}
Expand All @@ -72,6 +87,9 @@ func (m *Message) SearchFlag(placeholder string) (found bool) {
if found = strings.Contains(m.EventStreamID, placeholder); found == true {
return found
}
if found = strings.Contains(m.SubscriptionID, placeholder); found == true {
return found
}
return false
}

Expand Down

0 comments on commit db3f15c

Please sign in to comment.