Skip to content

Commit

Permalink
Merge pull request #10 from ADorigi/fix-run-jobs
Browse files Browse the repository at this point in the history
Fix run jobs
  • Loading branch information
artaasadi authored Sep 16, 2024
2 parents e87fd74 + 3ee21e9 commit 718aa92
Show file tree
Hide file tree
Showing 14 changed files with 601 additions and 16 deletions.
5 changes: 5 additions & 0 deletions cmd/get/benchmarks.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,11 @@ to quickly create a Cobra application.`,
return err
}

if response.StatusCode != 200 {
fmt.Println(string(body))
return nil
}

var getBenchmarksResponse types.GetBenchmarksResponse
err = json.Unmarshal(body, &getBenchmarksResponse)
if err != nil {
Expand Down
95 changes: 95 additions & 0 deletions cmd/get/compliance_summary_for_benchmark.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package get

import (
"encoding/json"
"fmt"
"github.com/adorigi/checkctl/pkg/config"
"github.com/adorigi/checkctl/pkg/request"
"github.com/adorigi/checkctl/pkg/types"
"github.com/adorigi/checkctl/pkg/utils"
"github.com/spf13/cobra"
"io"
"net/http"
)

// complianceSummaryForBenchmarkCmd represents the benchmarks command
var complianceSummaryForBenchmarkCmd = &cobra.Command{
Use: "compliance-summary-for-benchmark",
Short: "Get compliance summary for benchmark",
Long: `Get compliance summary for benchmark`,
RunE: func(cmd *cobra.Command, args []string) error {
client := &http.Client{}
configuration, err := config.ReadConfigFile()
if err != nil {
return err
}

outputFormat := utils.ReadStringFlag(cmd, "output")
if outputFormat == "" {
outputFormat = configuration.OutputFormat
}

benchmarkIDs, err := utils.ReadStringSliceFlag(cmd, "benchmark-id")
if err != nil {
return err
}

isRoot := utils.ReadBoolFlag(cmd, "is-root")
requestPayload := types.ComplianceSummaryOfBenchmarkRequest{
Benchmarks: benchmarkIDs,
IsRoot: &isRoot,
}

payload, err := json.Marshal(requestPayload)
if err != nil {
return err
}

url := fmt.Sprintf("main/compliance/api/v3/compliance/summary/benchmark")
request, err := request.GenerateRequest(
configuration.ApiKey,
configuration.ApiEndpoint,
"POST",
url,
payload,
)
if err != nil {
return err
}

response, err := client.Do(request)
if err != nil {
return err
}
defer response.Body.Close()

body, err := io.ReadAll(response.Body)
if err != nil {
return err
}

if response.StatusCode != 200 {
fmt.Println(string(body))
return nil
}

var summary []types.ComplianceSummaryOfBenchmarkResponse
err = json.Unmarshal(body, &summary)
if err != nil {
return err
}

if outputFormat == "table" {
fmt.Println("Table view not supported, use json view: --output json")
// TODO
} else {
js, err := json.MarshalIndent(summary, "", " ")
if err != nil {
return err
}
fmt.Print(string(js))
}

return nil
},
}
103 changes: 103 additions & 0 deletions cmd/get/compliance_summary_for_integration.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
package get

import (
"encoding/json"
"fmt"
"github.com/adorigi/checkctl/pkg/config"
"github.com/adorigi/checkctl/pkg/request"
"github.com/adorigi/checkctl/pkg/types"
"github.com/adorigi/checkctl/pkg/utils"
"github.com/spf13/cobra"
"io"
"net/http"
)

// complianceSummaryForIntegrationCmd represents the benchmarks command
var complianceSummaryForIntegrationCmd = &cobra.Command{
Use: "compliance-summary-for-integration",
Short: "Get compliance summary for integration",
Long: `Get compliance summary for integration`,
RunE: func(cmd *cobra.Command, args []string) error {
client := &http.Client{}
configuration, err := config.ReadConfigFile()
if err != nil {
return err
}

outputFormat := utils.ReadStringFlag(cmd, "output")
if outputFormat == "" {
outputFormat = configuration.OutputFormat
}

benchmarkId := utils.ReadStringFlag(cmd, "benchmark-id")
if benchmarkId == "" {
fmt.Println("Error: must specify benchmark id")
return nil
}

integrationStr := utils.ReadStringFlag(cmd, "integration")
if integrationStr == "" {
fmt.Println(`Error: must specify integration
Integration info in the form 'integration=AWS,id=123,id_name=name'`)
return nil
}
integration := types.ParseIntegrationInfo(integrationStr)

requestPayload := types.ComplianceSummaryOfIntegrationRequest{
Integration: integration,
BenchmarkId: benchmarkId,
}

payload, err := json.Marshal(requestPayload)
if err != nil {
return err
}

url := fmt.Sprintf("main/compliance/api/v3/compliance/summary/integration")
request, err := request.GenerateRequest(
configuration.ApiKey,
configuration.ApiEndpoint,
"POST",
url,
payload,
)
if err != nil {
return err
}

response, err := client.Do(request)
if err != nil {
return err
}
defer response.Body.Close()

body, err := io.ReadAll(response.Body)
if err != nil {
return err
}

if response.StatusCode != 200 {
fmt.Println(string(body))
return nil
}

var summary types.ComplianceSummaryOfIntegrationResponse
err = json.Unmarshal(body, &summary)
if err != nil {
return err
}

if outputFormat == "table" {
fmt.Println("Table view not supported, use json view: --output json")
// TODO
} else {
js, err := json.MarshalIndent(summary, "", " ")
if err != nil {
return err
}
fmt.Print(string(js))
}

return nil
},
}
5 changes: 5 additions & 0 deletions cmd/get/controls.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,11 @@ to quickly create a Cobra application.`,
return err
}

if response.StatusCode != 200 {
fmt.Println(string(body))
return nil
}

var getControlsResponse types.GetControlsResponse
err = json.Unmarshal(body, &getControlsResponse)
if err != nil {
Expand Down
22 changes: 20 additions & 2 deletions cmd/get/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,27 @@ to quickly create a Cobra application.`,
func init() {

GetCmd.AddCommand(controlsCmd)
controlsCmd.PersistentFlags().Int("page-size", 25, "Defines page size of response")
controlsCmd.PersistentFlags().Int("page-number", 1, "Defines page number of response")

GetCmd.AddCommand(benchmarksCmd)
benchmarksCmd.PersistentFlags().Int("page-size", 25, "Defines page size of response")
benchmarksCmd.PersistentFlags().Int("page-number", 1, "Defines page number of response")

GetCmd.AddCommand(complianceSummaryForIntegrationCmd)
complianceSummaryForIntegrationCmd.PersistentFlags().String("integration", "", "Integration info in the form 'integration=AWS,id=123,id_name=name'"+
"values are optional and support regex")
complianceSummaryForIntegrationCmd.PersistentFlags().String("benchmark-id", "", "Benchmark ID")

GetCmd.AddCommand(complianceSummaryForBenchmarkCmd)
complianceSummaryForBenchmarkCmd.PersistentFlags().StringSlice("benchmark-id", []string{}, "List of Benchmark IDs to get the summary for (optional)")
complianceSummaryForBenchmarkCmd.PersistentFlags().Bool("is-root", true, "Whether to return only root benchmarks or not. (matters if benchmark-id list not provided)")

GetCmd.PersistentFlags().Int("page-size", 25, "Defines page size of response")
GetCmd.PersistentFlags().Int("page-number", 1, "Defines page number of response")
GetCmd.AddCommand(jobDetailsCmd)
jobDetailsCmd.PersistentFlags().String("job-id", "", "Job ID")
jobDetailsCmd.PersistentFlags().String("job-type", "", "Job Type. Options: compliance, analytics, discovery")

GetCmd.AddCommand(jobsCmd)
jobsCmd.PersistentFlags().String("job-type", "", "Job Type. Options: compliance, analytics, discovery")
jobsCmd.PersistentFlags().String("interval", "90m", "Specify time interval like: 90m, 1h, 50 minutes, 2 hours")
}
93 changes: 93 additions & 0 deletions cmd/get/job_details.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package get

import (
"encoding/json"
"fmt"
"io"
"net/http"

"github.com/adorigi/checkctl/pkg/config"
"github.com/adorigi/checkctl/pkg/request"
"github.com/adorigi/checkctl/pkg/types"
"github.com/adorigi/checkctl/pkg/utils"
"github.com/spf13/cobra"
)

// jobDetailsCmd represents the controls command
var jobDetailsCmd = &cobra.Command{
Use: "job-details",
Short: "Get Job Details",
Long: `Get Job Details`,
RunE: func(cmd *cobra.Command, args []string) error {
client := &http.Client{}
configuration, err := config.ReadConfigFile()
if err != nil {
return err
}

outputFormat := utils.ReadStringFlag(cmd, "output")
if outputFormat == "" {
outputFormat = configuration.OutputFormat
}

jobId := utils.ReadStringFlag(cmd, "job-id")
if jobId == "" {
return fmt.Errorf("job-id flag is required")
}

var url string
jobType := utils.ReadStringFlag(cmd, "job-type")
switch jobType {
case "compliance", "analytics", "discovery":
url = fmt.Sprintf("main/schedule/api/v3/job/%s/%s", jobType, jobId)
default:
return fmt.Errorf("please provide a valid job-type: compliance, analytics, discovery")
}

request, err := request.GenerateRequest(
configuration.ApiKey,
configuration.ApiEndpoint,
"GET",
url,
[]byte{},
)
if err != nil {
return err
}

response, err := client.Do(request)
if err != nil {
return err
}
defer response.Body.Close()

body, err := io.ReadAll(response.Body)
if err != nil {
return err
}

if response.StatusCode != 200 {
fmt.Println(string(body))
return nil
}

var job types.GetComplianceJobStatusResponse
err = json.Unmarshal(body, &job)
if err != nil {
return err
}

if outputFormat == "table" {
fmt.Println("Table view not supported, use json view: --output json")
//TODO
} else {
js, err := json.MarshalIndent(job, "", " ")
if err != nil {
return err
}
fmt.Print(string(js))
}

return nil
},
}
Loading

0 comments on commit 718aa92

Please sign in to comment.