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

Add code generation for GraphQL enum & mutation input types #155

Closed
wants to merge 13 commits into from
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ $(BIN_PATH): FORCE

build: $(BIN_PATH)

api_gen: cmd/api_gen/generate.go
go run ./cmd/api_gen

clean:
go clean
@rm -rf bin/
Expand Down
22 changes: 1 addition & 21 deletions api/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,9 @@ package api

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

const (
ActionTypeEmail string = "EmailAction"
ActionTypeHumioRepo string = "HumioRepoAction"
ActionTypeOpsGenie string = "OpsGenieAction"
ActionTypePagerDuty string = "PagerDutyAction"
ActionTypeSlack string = "SlackAction"
ActionTypeSlackPostMessage string = "SlackPostMessageAction"
ActionTypeVictorOps string = "VictorOpsAction"
ActionTypeWebhook string = "WebhookAction"
graphql "github.com/cli/shurcooL-graphql"
)

type Actions struct {
Expand Down Expand Up @@ -44,11 +34,6 @@ type PagerDutyAction struct {
UseProxy bool `graphql:"pagerDutyUseProxy: useProxy" yaml:"useProxy,omitempty" json:"useProxy,omitempty"`
}

type SlackFieldEntryInput struct {
FieldName string `graphql:"fieldName" yaml:"fieldName" json:"fieldName"`
Value string `graphql:"value" yaml:"value" json:"value"`
}

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"`
Expand All @@ -68,11 +53,6 @@ type VictorOpsAction struct {
UseProxy bool `graphql:"victorOpsUseProxy: useProxy" yaml:"useProxy,omitempty" json:"useProxy,omitempty"`
}

type HttpHeaderEntryInput struct {
Header string `graphql:"header" yaml:"header" json:"header"`
Value string `graphql:"value" yaml:"value" json:"value"`
}

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 Down
186 changes: 40 additions & 146 deletions api/alerts.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,42 @@ package api

import (
"fmt"

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

type Alert struct {
ID string `graphql:"id" yaml:"-" json:"id"`
Name string `graphql:"name" yaml:"name" json:"name"`
QueryString string `graphql:"queryString" yaml:"queryString" json:"queryString"`
QueryStart string `graphql:"queryStart" yaml:"queryStart" json:"queryStart"`
ThrottleField string `graphql:"throttleField" yaml:"throttleField" json:"throttleField"`
TimeOfLastTrigger int `graphql:"timeOfLastTrigger" yaml:"timeOfLastTrigger" json:"timeOfLastTrigger"`
IsStarred bool `graphql:"isStarred" yaml:"isStarred" json:"isStarred"`
Description string `graphql:"description" yaml:"description,omitempty" json:"description"`
ThrottleTimeMillis int `graphql:"throttleTimeMillis" yaml:"throttleTimeMillis" json:"throttleTimeMillis"`
Enabled bool `graphql:"enabled" yaml:"enabled" json:"enabled"`
Actions []string `graphql:"actions" yaml:"actions" json:"actions"`
Labels []string `graphql:"labels" yaml:"labels,omitempty" json:"labels,omitempty"`
LastError string `graphql:"lastError" yaml:"lastError" json:"lastError"`
RunAsUserID string `graphql:"runAsUserId" yaml:"runAsUserId,omitempty" json:"runAsUserId,omitempty"`
QueryOwnershipType string `graphql:"queryOwnershipType" yaml:"queryOwnershipType,omitempty" json:"queryOwnershipType,omitempty"`
ID string `graphql:"id" yaml:"-" json:"id"`
Name string `graphql:"name" yaml:"name" json:"name"`
DisplayName string `graphql:"displayName" yaml:"displayName" json:"displayName"`
QueryString string `graphql:"queryString" yaml:"queryString" json:"queryString"`
QueryStart string `graphql:"queryStart" yaml:"queryStart" json:"queryStart"`
ThrottleField string `graphql:"throttleField" yaml:"throttleField" json:"throttleField"`
TimeOfLastTrigger int64 `graphql:"timeOfLastTrigger" yaml:"timeOfLastTrigger" json:"timeOfLastTrigger"`
IsStarred bool `graphql:"isStarred" yaml:"isStarred" json:"isStarred"`
Description string `graphql:"description" yaml:"description,omitempty" json:"description"`
ThrottleTimeMillis int64 `graphql:"throttleTimeMillis" yaml:"throttleTimeMillis" json:"throttleTimeMillis"`
Enabled bool `graphql:"enabled" yaml:"enabled" json:"enabled"`
Actions []string `graphql:"actions" yaml:"actions" json:"actions"`
Labels []string `graphql:"labels" yaml:"labels,omitempty" json:"labels,omitempty"`
LastError string `graphql:"lastError" yaml:"lastError" json:"lastError"`
RunAsUser User `graphql:"runAsUser" yaml:"runAsUser,omitempty" json:"runAsUser,omitempty"`
QueryOwnership QueryOwnership `graphql:"queryOwnership" yaml:"queryOwnership" json:"queryOwnership"`
}

const (
QueryOwnershipTypeUser string = "User"
QueryOwnershipTypeOrganization string = "Organization"
)
type QueryOwnership struct {
ID string `graphql:"id"`
UserOwnership `graphql:"... on UserOwnership"`
OrganizationOwnership `graphql:"... on OrganizationOwnership"`
}

type UserOwnership struct {
User User `graphql:"user"`
}

type OrganizationOwnership struct {
Organization Organization `graphql:"organization"`
}

type Alerts struct {
client *Client
Expand All @@ -38,7 +48,7 @@ func (c *Client) Alerts() *Alerts { return &Alerts{client: c} }
func (a *Alerts) List(viewName string) ([]Alert, error) {
var query struct {
SearchDomain struct {
Alerts []humiographql.Alert `graphql:"alerts"`
Alerts []Alert `graphql:"alerts"`
} `graphql:"searchDomain(name: $viewName)"`
}

Expand All @@ -47,112 +57,34 @@ func (a *Alerts) List(viewName string) ([]Alert, error) {
}

err := a.client.Query(&query, variables)

var alerts []Alert
for _, humioGraphqlAlert := range query.SearchDomain.Alerts {
alerts = append(alerts, mapHumioGraphqlAlertToAlert(humioGraphqlAlert))
}
return alerts, err
return query.SearchDomain.Alerts, err
}

func (a *Alerts) Update(viewName string, newAlert *Alert) (*Alert, error) {
if newAlert == nil {
return nil, fmt.Errorf("newAlert must not be nil")
}

if newAlert.ID == "" {
return nil, fmt.Errorf("newAlert must have non-empty newAlert id")
}

func (a *Alerts) Update(viewName string, newAlert UpdateAlert) (*Alert, error) {
var mutation struct {
humiographql.Alert `graphql:"updateAlert(input: $input)"`
}

actions := make([]graphql.String, len(newAlert.Actions))
for i, action := range newAlert.Actions {
actions[i] = graphql.String(action)
}

labels := make([]graphql.String, len(newAlert.Labels))
for i, label := range newAlert.Labels {
labels[i] = graphql.String(label)
}

updateAlert := humiographql.UpdateAlert{
ID: graphql.String(newAlert.ID),
ViewName: graphql.String(viewName),
Name: graphql.String(newAlert.Name),
Description: graphql.String(newAlert.Description),
QueryString: graphql.String(newAlert.QueryString),
QueryStart: graphql.String(newAlert.QueryStart),
ThrottleTimeMillis: humiographql.Long(newAlert.ThrottleTimeMillis),
Enabled: graphql.Boolean(newAlert.Enabled),
Actions: actions,
Labels: labels,
RunAsUserID: graphql.String(newAlert.RunAsUserID),
QueryOwnershipType: humiographql.QueryOwnershipType(newAlert.QueryOwnershipType),
ThrottleField: graphql.String(newAlert.ThrottleField),
Alert Alert `graphql:"updateAlert(input: $input)"`
}

variables := map[string]interface{}{
"input": updateAlert,
"input": newAlert,
}

err := a.client.Mutate(&mutation, variables)
if err != nil {
return nil, err
}

alert := mapHumioGraphqlAlertToAlert(mutation.Alert)

return &alert, nil
return &mutation.Alert, err
}

func (a *Alerts) Add(viewName string, newAlert *Alert) (*Alert, error) {
if newAlert == nil {
return nil, fmt.Errorf("newAlert must not be nil")
}
func (a *Alerts) Add(viewName string, newAlert CreateAlert) (*Alert, error) {

var mutation struct {
humiographql.Alert `graphql:"createAlert(input: $input)"`
}

actions := make([]graphql.String, len(newAlert.Actions))
for i, action := range newAlert.Actions {
actions[i] = graphql.String(action)
}

labels := make([]graphql.String, len(newAlert.Labels))
for i, label := range newAlert.Labels {
labels[i] = graphql.String(label)
}

createAlert := humiographql.CreateAlert{
ViewName: graphql.String(viewName),
Name: graphql.String(newAlert.Name),
Description: graphql.String(newAlert.Description),
QueryString: graphql.String(newAlert.QueryString),
QueryStart: graphql.String(newAlert.QueryStart),
ThrottleTimeMillis: humiographql.Long(newAlert.ThrottleTimeMillis),
Enabled: graphql.Boolean(newAlert.Enabled),
Actions: actions,
Labels: labels,
RunAsUserID: graphql.String(newAlert.RunAsUserID),
QueryOwnershipType: humiographql.QueryOwnershipType(newAlert.QueryOwnershipType),
ThrottleField: graphql.String(newAlert.ThrottleField),
Alert Alert `graphql:"createAlert(input: $input)"`
}

variables := map[string]interface{}{
"input": createAlert,
"input": newAlert,
}

err := a.client.Mutate(&mutation, variables)
if err != nil {
return nil, err
}

alert := mapHumioGraphqlAlertToAlert(mutation.Alert)
return &alert, nil
return &mutation.Alert, err
}

func (a *Alerts) Get(viewName, alertName string) (*Alert, error) {
Expand Down Expand Up @@ -196,41 +128,3 @@ func (a *Alerts) Delete(viewName, alertName string) error {

return a.client.Mutate(&mutation, variables)
}

func mapHumioGraphqlAlertToAlert(input humiographql.Alert) Alert {
var queryOwnershipType string
switch input.QueryOwnership.QueryOwnershipTypeName {
case humiographql.QueryOwnershipTypeNameOrganization:
queryOwnershipType = QueryOwnershipTypeOrganization
case humiographql.QueryOwnershipTypeNameUser:
queryOwnershipType = QueryOwnershipTypeUser
}

var actions []string
for _, action := range input.Actions {
actions = append(actions, string(action))
}

var labels []string
for _, label := range input.Labels {
labels = append(labels, string(label))
}

return Alert{
ID: string(input.ID),
Name: string(input.Name),
QueryString: string(input.QueryString),
QueryStart: string(input.QueryStart),
ThrottleField: string(input.ThrottleField),
TimeOfLastTrigger: int(input.TimeOfLastTrigger),
IsStarred: bool(input.IsStarred),
Description: string(input.Description),
ThrottleTimeMillis: int(input.ThrottleTimeMillis),
Enabled: bool(input.Enabled),
Actions: actions,
Labels: labels,
LastError: string(input.LastError),
RunAsUserID: string(input.RunAsUser.ID),
QueryOwnershipType: queryOwnershipType,
}
}
5 changes: 0 additions & 5 deletions api/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,6 @@ type StoragePartitionInput struct {
NodeIDs []graphql.Int `json:"nodeIds"`
}

type IngestPartitionInput struct {
ID graphql.Int `json:"id"`
NodeIDs []graphql.Int `json:"nodeIds"`
}

// Deprecated: returns dummy data as of LogScale 1.88
func (c *Clusters) UpdateStoragePartitionScheme(desired []StoragePartitionInput) error {
var mutation struct {
Expand Down
Loading