Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate client using GraphQL schema #179

Merged
merged 7 commits into from
Nov 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ jobs:
run: |
export PATH=$PATH:$(go env GOPATH)/bin
go install github.com/securego/gosec/v2/cmd/gosec@latest
gosec ./...
gosec -exclude-dir api -exclude-generated ./...
- name: Run Staticcheck
uses: dominikh/staticcheck-action@v1.3.1
with:
Expand Down
6 changes: 6 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ GOFILES=$(wildcard *.go)
BIN_NAME=humioctl
BIN_PATH=bin/$(BIN_NAME)
CLI_COMMAND ?= ""
SCHEMA_CLUSTER?=${HUMIO_ENDPOINT}
SCHEMA_CLUSTER_API_TOKEN?=${HUMIO_TOKEN}

$(BIN_PATH): $(GOFILES)

Expand All @@ -24,6 +26,10 @@ snapshot:
run: $(BIN_PATH)
$(BIN_PATH) $(CLI_COMMAND)

update-schema:
go run github.com/suessflorian/gqlfetch/gqlfetch@607d6757018016bba0ba7fd1cb9fed6aefa853b5 --endpoint ${SCHEMA_CLUSTER}/graphql --header "Authorization=Bearer ${SCHEMA_CLUSTER_API_TOKEN}" > internal/api/humiographql/schema/_schema.graphql
printf "# Fetched from version %s" $$(curl --silent --location '${SCHEMA_CLUSTER}/api/v1/status' | jq -r ".version") >> internal/api/humiographql/schema/_schema.graphql

e2e: $(BIN_PATH)
./e2e/run.bash

Expand Down
44 changes: 36 additions & 8 deletions api/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,77 +2,97 @@ package api

import (
"fmt"
graphql "github.com/cli/shurcooL-graphql"
"reflect"

graphql "github.com/cli/shurcooL-graphql"
)

const (
ActionTypeEmail string = "EmailAction"
ActionTypeHumioRepo string = "HumioRepoAction"
ActionTypeOpsGenie string = "OpsGenieAction"
ActionTypePagerDuty string = "PagerDutyAction"
ActionTypeSlack string = "SlackAction"
// Deprecated: Should no longer be used. https://github.com/CrowdStrike/logscale-go-api-client-example
ActionTypeEmail string = "EmailAction"
// Deprecated: Should no longer be used. https://github.com/CrowdStrike/logscale-go-api-client-example
ActionTypeHumioRepo string = "HumioRepoAction"
// Deprecated: Should no longer be used. https://github.com/CrowdStrike/logscale-go-api-client-example
ActionTypeOpsGenie string = "OpsGenieAction"
// Deprecated: Should no longer be used. https://github.com/CrowdStrike/logscale-go-api-client-example
ActionTypePagerDuty string = "PagerDutyAction"
// Deprecated: Should no longer be used. https://github.com/CrowdStrike/logscale-go-api-client-example
ActionTypeSlack string = "SlackAction"
// Deprecated: Should no longer be used. https://github.com/CrowdStrike/logscale-go-api-client-example
ActionTypeSlackPostMessage string = "SlackPostMessageAction"
ActionTypeVictorOps string = "VictorOpsAction"
ActionTypeWebhook string = "WebhookAction"
// Deprecated: Should no longer be used. https://github.com/CrowdStrike/logscale-go-api-client-example
ActionTypeVictorOps string = "VictorOpsAction"
// Deprecated: Should no longer be used. https://github.com/CrowdStrike/logscale-go-api-client-example
ActionTypeWebhook string = "WebhookAction"
)

// Deprecated: Should no longer be used. https://github.com/CrowdStrike/logscale-go-api-client-example
type Actions struct {
client *Client
}

// Deprecated: Should no longer be used. https://github.com/CrowdStrike/logscale-go-api-client-example
type EmailAction struct {
Recipients []string `graphql:"emailRecipients: recipients" yaml:"recipients,omitempty" json:"recipients,omitempty"`
SubjectTemplate string `graphql:"emailSubjectTemplate: subjectTemplate" yaml:"subjectTemplate,omitempty" json:"subjectTemplate,omitempty"`
BodyTemplate string `graphql:"emailBodyTemplate: bodyTemplate" yaml:"bodyTemplate,omitempty" json:"bodyTemplate,omitempty"`
UseProxy bool `graphql:"emailUseProxy: useProxy" yaml:"useProxy,omitempty" json:"useProxy,omitempty"`
}

// Deprecated: Should no longer be used. https://github.com/CrowdStrike/logscale-go-api-client-example
type HumioRepoAction struct {
IngestToken string `graphql:"humioRepoIngestToken: ingestToken" yaml:"ingestToken,omitempty" json:"ingestToken,omitempty"`
}

// Deprecated: Should no longer be used. https://github.com/CrowdStrike/logscale-go-api-client-example
type OpsGenieAction struct {
ApiUrl string `graphql:"opsGenieApiUrl: apiUrl" yaml:"apiUrl,omitempty" json:"apiUrl,omitempty"`
GenieKey string `graphql:"opsGenieGenieKey: genieKey" yaml:"genieKey,omitempty" json:"genieKey,omitempty"`
UseProxy bool `graphql:"opsGenieUseProxy: useProxy" yaml:"useProxy,omitempty" json:"useProxy,omitempty"`
}

// Deprecated: Should no longer be used. https://github.com/CrowdStrike/logscale-go-api-client-example
type PagerDutyAction struct {
Severity string `graphql:"pagerDutySeverity: severity" yaml:"severity,omitempty" json:"severity,omitempty"`
RoutingKey string `graphql:"pagerDutyRoutingKey: routingKey" yaml:"routingKey,omitempty" json:"routingKey,omitempty"`
UseProxy bool `graphql:"pagerDutyUseProxy: useProxy" yaml:"useProxy,omitempty" json:"useProxy,omitempty"`
}

// Deprecated: Should no longer be used. https://github.com/CrowdStrike/logscale-go-api-client-example
type SlackFieldEntryInput struct {
FieldName string `graphql:"fieldName" yaml:"fieldName" json:"fieldName"`
Value string `graphql:"value" yaml:"value" json:"value"`
}

// Deprecated: Should no longer be used. https://github.com/CrowdStrike/logscale-go-api-client-example
type SlackAction struct {
Url string `graphql:"slackUrl: url" yaml:"url,omitempty" json:"url,omitempty"`
Fields []SlackFieldEntryInput `graphql:"slackFields: fields" yaml:"fields,omitempty" json:"fields,omitempty"`
UseProxy bool `graphql:"slackUseProxy: useProxy" yaml:"useProxy,omitempty" json:"useProxy,omitempty"`
}

// Deprecated: Should no longer be used. https://github.com/CrowdStrike/logscale-go-api-client-example
type SlackPostMessageAction struct {
ApiToken string `graphql:"slackPostMessageApiToken: apiToken" yaml:"apiToken,omitempty" json:"apiToken,omitempty"`
Channels []string `graphql:"slackPostMessageChannels: channels" yaml:"channels,omitempty" json:"channels,omitempty"`
Fields []SlackFieldEntryInput `graphql:"slackPostMessageFields: fields" yaml:"fields,omitempty" json:"fields,omitempty"`
UseProxy bool `graphql:"slackPostMessageUseProxy: useProxy" yaml:"useProxy,omitempty" json:"useProxy,omitempty"`
}

// Deprecated: Should no longer be used. https://github.com/CrowdStrike/logscale-go-api-client-example
type VictorOpsAction struct {
MessageType string `graphql:"victorOpsMessageType: messageType" yaml:"messageType,omitempty" json:"messageType,omitempty"`
NotifyUrl string `graphql:"victorOpsNotifyUrl: notifyUrl" yaml:"notifyUrl,omitempty" json:"notifyUrl,omitempty"`
UseProxy bool `graphql:"victorOpsUseProxy: useProxy" yaml:"useProxy,omitempty" json:"useProxy,omitempty"`
}

// Deprecated: Should no longer be used. https://github.com/CrowdStrike/logscale-go-api-client-example
type HttpHeaderEntryInput struct {
Header string `graphql:"header" yaml:"header" json:"header"`
Value string `graphql:"value" yaml:"value" json:"value"`
}

// Deprecated: Should no longer be used. https://github.com/CrowdStrike/logscale-go-api-client-example
type WebhookAction struct {
Method string `graphql:"webhookMethod: method" yaml:"method,omitempty" json:"method,omitempty"`
Url string `graphql:"webhookUrl: url" yaml:"url,omitempty" json:"url,omitempty"`
Expand All @@ -82,6 +102,7 @@ type WebhookAction struct {
UseProxy bool `graphql:"webhookUseProxy: useProxy" yaml:"useProxy,omitempty" json:"useProxy,omitempty"`
}

// Deprecated: Should no longer be used. https://github.com/CrowdStrike/logscale-go-api-client-example
type Action struct {
Type string `graphql:"__typename" yaml:"type" json:"type"`
ID string `graphql:"id" yaml:"-" json:"id"`
Expand All @@ -97,8 +118,10 @@ type Action struct {
WebhookAction WebhookAction `graphql:"... on WebhookAction" yaml:"webhookAction,omitempty" json:"webhookAction,omitempty"`
}

// Deprecated: Should no longer be used. https://github.com/CrowdStrike/logscale-go-api-client-example
func (c *Client) Actions() *Actions { return &Actions{client: c} }

// Deprecated: Should no longer be used. https://github.com/CrowdStrike/logscale-go-api-client-example
func (n *Actions) List(viewName string) ([]Action, error) {
var query struct {
SearchDomain struct {
Expand All @@ -114,6 +137,7 @@ func (n *Actions) List(viewName string) ([]Action, error) {
return query.SearchDomain.Actions, err
}

// Deprecated: Should no longer be used. https://github.com/CrowdStrike/logscale-go-api-client-example
func (n *Actions) Update(viewName string, newAction *Action) (*Action, error) {
if newAction == nil {
return nil, fmt.Errorf("action must not be nil")
Expand Down Expand Up @@ -454,6 +478,7 @@ func (n *Actions) Update(viewName string, newAction *Action) (*Action, error) {
return nil, fmt.Errorf("no action details specified or unsupported action type used")
}

// Deprecated: Should no longer be used. https://github.com/CrowdStrike/logscale-go-api-client-example
func (n *Actions) Add(viewName string, newAction *Action) (*Action, error) {
if newAction == nil {
return nil, fmt.Errorf("action must not be nil")
Expand Down Expand Up @@ -781,6 +806,7 @@ func (n *Actions) Add(viewName string, newAction *Action) (*Action, error) {
return nil, fmt.Errorf("no action details specified or unsupported action type used")
}

// Deprecated: Should no longer be used. https://github.com/CrowdStrike/logscale-go-api-client-example
func (n *Actions) Get(viewName, actionName string) (*Action, error) {
actions, err := n.List(viewName)
if err != nil {
Expand All @@ -795,6 +821,7 @@ func (n *Actions) Get(viewName, actionName string) (*Action, error) {
return nil, ActionNotFound(actionName)
}

// Deprecated: Should no longer be used. https://github.com/CrowdStrike/logscale-go-api-client-example
func (n *Actions) GetByID(viewName, actionID string) (*Action, error) {
var query struct {
SearchDomain struct {
Expand All @@ -819,6 +846,7 @@ func (n *Actions) GetByID(viewName, actionID string) (*Action, error) {
return query.SearchDomain.Action, err
}

// Deprecated: Should no longer be used. https://github.com/CrowdStrike/logscale-go-api-client-example
func (n *Actions) Delete(viewName, actionName string) error {
actions, err := n.List(viewName)
if err != nil {
Expand Down
9 changes: 9 additions & 0 deletions api/aggregate-alerts.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ package api

import (
"fmt"

graphql "github.com/cli/shurcooL-graphql"
"github.com/humio/cli/api/internal/humiographql"
)

// Deprecated: Should no longer be used. https://github.com/CrowdStrike/logscale-go-api-client-example
type AggregateAlert struct {
ID string `graphql:"id" yaml:"-" json:"id"`
Name string `graphql:"name" yaml:"name" json:"name"`
Expand All @@ -23,12 +25,15 @@ type AggregateAlert struct {
RunAsUserID string `graphql:"runAsUserId" yaml:"runAsUserId,omitempty" json:"runAsUserId,omitempty"`
}

// Deprecated: Should no longer be used. https://github.com/CrowdStrike/logscale-go-api-client-example
type AggregateAlerts struct {
client *Client
}

// Deprecated: Should no longer be used. https://github.com/CrowdStrike/logscale-go-api-client-example
func (c *Client) AggregateAlerts() *AggregateAlerts { return &AggregateAlerts{client: c} }

// Deprecated: Should no longer be used. https://github.com/CrowdStrike/logscale-go-api-client-example
func (a *AggregateAlerts) List(viewName string) ([]*AggregateAlert, error) {
if viewName == "" {
return nil, fmt.Errorf("viewName must not be empty")
Expand Down Expand Up @@ -58,6 +63,7 @@ func (a *AggregateAlerts) List(viewName string) ([]*AggregateAlert, error) {
return aggregateAlerts, nil
}

// Deprecated: Should no longer be used. https://github.com/CrowdStrike/logscale-go-api-client-example
func (a *AggregateAlerts) Update(viewName string, updatedAggregateAlert *AggregateAlert) (*AggregateAlert, error) {
if viewName == "" {
return nil, fmt.Errorf("viewName must not be empty")
Expand Down Expand Up @@ -117,6 +123,7 @@ func (a *AggregateAlerts) Update(viewName string, updatedAggregateAlert *Aggrega
return &aggregateAlert, nil
}

// Deprecated: Should no longer be used. https://github.com/CrowdStrike/logscale-go-api-client-example
func (a *AggregateAlerts) Create(viewName string, newAggregateAlert *AggregateAlert) (*AggregateAlert, error) {
if viewName == "" {
return nil, fmt.Errorf("viewName must not be empty")
Expand Down Expand Up @@ -171,6 +178,7 @@ func (a *AggregateAlerts) Create(viewName string, newAggregateAlert *AggregateAl
return &aggregateAlert, nil
}

// Deprecated: Should no longer be used. https://github.com/CrowdStrike/logscale-go-api-client-example
func (a *AggregateAlerts) Delete(viewName, aggregateAlertID string) error {
if viewName == "" {
return fmt.Errorf("viewName must not be empty")
Expand Down Expand Up @@ -198,6 +206,7 @@ func (a *AggregateAlerts) Delete(viewName, aggregateAlertID string) error {
return err
}

// Deprecated: Should no longer be used. https://github.com/CrowdStrike/logscale-go-api-client-example
func (a *AggregateAlerts) Get(viewName string, aggregateAlertID string) (*AggregateAlert, error) {
var query struct {
SearchDomain struct {
Expand Down
13 changes: 12 additions & 1 deletion api/alerts.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ package api

import (
"fmt"

graphql "github.com/cli/shurcooL-graphql"
"github.com/humio/cli/api/internal/humiographql"
)

// Deprecated: Should no longer be used. https://github.com/CrowdStrike/logscale-go-api-client-example
type Alert struct {
ID string `graphql:"id" yaml:"-" json:"id"`
Name string `graphql:"name" yaml:"name" json:"name"`
Expand All @@ -25,16 +27,21 @@ type Alert struct {
}

const (
QueryOwnershipTypeUser string = "User"
// Deprecated: Should no longer be used. https://github.com/CrowdStrike/logscale-go-api-client-example
QueryOwnershipTypeUser string = "User"
// Deprecated: Should no longer be used. https://github.com/CrowdStrike/logscale-go-api-client-example
QueryOwnershipTypeOrganization string = "Organization"
)

// Deprecated: Should no longer be used. https://github.com/CrowdStrike/logscale-go-api-client-example
type Alerts struct {
client *Client
}

// Deprecated: Should no longer be used. https://github.com/CrowdStrike/logscale-go-api-client-example
func (c *Client) Alerts() *Alerts { return &Alerts{client: c} }

// Deprecated: Should no longer be used. https://github.com/CrowdStrike/logscale-go-api-client-example
func (a *Alerts) List(viewName string) ([]Alert, error) {
var query struct {
SearchDomain struct {
Expand All @@ -55,6 +62,7 @@ func (a *Alerts) List(viewName string) ([]Alert, error) {
return alerts, err
}

// Deprecated: Should no longer be used. https://github.com/CrowdStrike/logscale-go-api-client-example
func (a *Alerts) Update(viewName string, newAlert *Alert) (*Alert, error) {
if newAlert == nil {
return nil, fmt.Errorf("newAlert must not be nil")
Expand Down Expand Up @@ -108,6 +116,7 @@ func (a *Alerts) Update(viewName string, newAlert *Alert) (*Alert, error) {
return &alert, nil
}

// Deprecated: Should no longer be used. https://github.com/CrowdStrike/logscale-go-api-client-example
func (a *Alerts) Add(viewName string, newAlert *Alert) (*Alert, error) {
if newAlert == nil {
return nil, fmt.Errorf("newAlert must not be nil")
Expand Down Expand Up @@ -155,6 +164,7 @@ func (a *Alerts) Add(viewName string, newAlert *Alert) (*Alert, error) {
return &alert, nil
}

// Deprecated: Should no longer be used. https://github.com/CrowdStrike/logscale-go-api-client-example
func (a *Alerts) Get(viewName, alertName string) (*Alert, error) {
alerts, err := a.List(viewName)
if err != nil {
Expand All @@ -169,6 +179,7 @@ func (a *Alerts) Get(viewName, alertName string) (*Alert, error) {
return nil, AlertNotFound(alertName)
}

// Deprecated: Should no longer be used. https://github.com/CrowdStrike/logscale-go-api-client-example
func (a *Alerts) Delete(viewName, alertName string) error {
actions, err := a.List(viewName)
if err != nil {
Expand Down
Loading
Loading