Skip to content

Commit

Permalink
Merge pull request #178 from georgettica/georgettica/servicelog-fixes
Browse files Browse the repository at this point in the history
fix(servicelog): bulk commands
  • Loading branch information
openshift-merge-robot authored Dec 22, 2021
2 parents db3f15c + ebd8d80 commit 5aa1051
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 5 deletions.
5 changes: 4 additions & 1 deletion cmd/servicelog/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func createConnection() *sdk.Connection {

// generateQuery returns an OCM search query to retrieve all clusters matching an expression (ie- "foo%")
func generateQuery(clusterIdentifier string) string {
return strings.TrimSpace(fmt.Sprintf("id like '%[1]s' or external_id like '%[1]s' or display_name like '%[1]s'", clusterIdentifier))
return strings.TrimSpace(fmt.Sprintf("(id like '%[1]s' or external_id like '%[1]s' or display_name like '%[1]s')", clusterIdentifier))
}

// getFilteredClusters retrieves clusters in OCM which match the filters given
Expand All @@ -51,6 +51,9 @@ func applyFilters(ocmClient *sdk.Connection, filters []string) ([]*v1.Cluster, e

requestSize := 50
full_filters := strings.Join(filters, " and ")

log.Infof(`running the command: 'ocm list clusters --parameter=search="%s"'`, full_filters)

request := ocmClient.ClustersMgmt().V1().Clusters().List().Search(full_filters).Size(requestSize)
response, err := request.Send()
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion cmd/servicelog/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ var listCmd = &cobra.Command{
Args: cobra.ArbitraryArgs,
SilenceErrors: true,
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) != 1 {
if len(args) == 0 {
cmd.Help()
return fmt.Errorf("cluster-identifier was not provided. please provide a cluster id, UUID, or name")
}
Expand Down
6 changes: 3 additions & 3 deletions cmd/servicelog/post.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ 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 == "" {
if len(args) == 0 && len(filterParams) == 0 && clustersFile == "" {
log.Fatalf("No cluster identifier has been found.")
}

Expand Down Expand Up @@ -108,7 +108,7 @@ var postCmd = &cobra.Command{
cluster := ClustersFile.Clusters[i]
query = append(query, generateQuery(cluster))
}
filterParams = query
filterParams = append(filterParams, strings.Join(query, " or "))
}

clusters, err := applyFilters(ocmClient, filterParams)
Expand Down Expand Up @@ -180,7 +180,7 @@ func init() {
postCmd.Flags().StringArrayVarP(&filterParams, "query", "q", filterParams, "Specify a search query (eg. -q \"name like foo\") for a bulk-post to matching clusters.")
postCmd.Flags().BoolVarP(&skipPrompts, "yes", "y", false, "Skips all prompts.")
postCmd.Flags().StringArrayVarP(&filterFiles, "query-file", "f", filterFiles, "File containing search queries to apply. All lines in the file will be concatenated into a single query. If this flag is called multiple times, every file's search query will be combined with logical AND.")
postCmd.Flags().StringVarP(&clustersFile, "clusters-file", "c", clustersFile, "Read a list of clusters to post the servicelog to")
postCmd.Flags().StringVarP(&clustersFile, "clusters-file", "c", clustersFile, `Read a list of clusters to post the servicelog to. the format of the file is: {"clusters":["$CLUSTERID"]}`)
}

// parseUserParameters parse all the '-p FOO=BAR' parameters and checks for syntax errors
Expand Down

0 comments on commit 5aa1051

Please sign in to comment.