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

[WIP] Updating the Create Rule API to support new params. #707

Closed
wants to merge 1 commit into from

Conversation

adcoelho
Copy link
Contributor

@adcoelho adcoelho commented Aug 12, 2024

Connected with this issue.

Summary

I want to add support for a few new fields in the Create rule API. For that, I generated a new bundled.yml and tried re-generating the alerting client.

I followed the steps below:

  1. Generated bundled.yaml following [these instructions](https://github.com/elastic/kibana//tree/8.14/x-pack/plugins/alerting/docs/openapi).
  2. Moved bundled.yaml to generated/alerting/bundled.yaml
  3. make generate-alerting-client

I also see an openapi.yaml file inside generated/alerting. Is this one generated based on bundled.yaml? Do I need to do anything there? Were the steps above enough?

Issues

Calls to alerting.CreateRuleRequest in alerting.go now throw a linting error because some fields are unrecognized.

The problem is the definition of create_rule_request in bundled.yaml line 4486.

    create_rule_request:
      title: Create rule request body properties
      description: The properties vary depending on the rule type.
      oneOf:
        - $ref: '#/components/schemas/create_anomaly_detection_alert_rule_request'
        - $ref: '#/components/schemas/create_anomaly_detection_jobs_health_rule_request'
        - $ref: '#/components/schemas/create_apm_anomaly_rule_request'

The above leads to the definition of CreateRuleRequest in generated/alerting/model_create_rule_request.go.

type CreateRuleRequest struct {
	CreateAnomalyDetectionAlertRuleRequest                  *CreateAnomalyDetectionAlertRuleRequest
	CreateAnomalyDetectionJobsHealthRuleRequest       *CreateAnomalyDetectionJobsHealthRuleRequest
	CreateApmAnomalyRuleRequest                                   *CreateApmAnomalyRuleRequest
	CreateApmErrorCountRuleRequest                               *CreateApmErrorCountRuleRequest
	...
}

This struct is wrong, all these different rule-type requests are the same and have the same properties.

See for example:

type CreateAnomalyDetectionAlertRuleRequest struct {
	Actions    []ActionsInner `json:"actions,omitempty"`
	AlertDelay *AlertDelay    `json:"alert_delay,omitempty"`
	Consumer string `json:"consumer"`
	Enabled *bool `json:"enabled,omitempty"`
        Name string `json:"name"`
	NotifyWhen *NotifyWhen `json:"notify_when,omitempty"`
	Params map[string]interface{} `json:"params"`
	RuleTypeId string   `json:"rule_type_id"`
	Schedule   Schedule `json:"schedule"`
	Tags       []string `json:"tags,omitempty"`
	Throttle NullableString `json:"throttle,omitempty"`
}

and

type CreateAnomalyDetectionJobsHealthRuleRequest struct {
	Actions    []ActionsInner `json:"actions,omitempty"`
	AlertDelay *AlertDelay    `json:"alert_delay,omitempty"`
	Consumer string `json:"consumer"`
	Enabled *bool `json:"enabled,omitempty"`
	Name string `json:"name"`
	NotifyWhen *NotifyWhen `json:"notify_when,omitempty"`
	Params map[string]interface{} `json:"params"`
	RuleTypeId string   `json:"rule_type_id"`
	Schedule   Schedule `json:"schedule"`
	Tags       []string `json:"tags,omitempty"`
	Throttle NullableString `json:"throttle,omitempty"`
}

They are the same. What differs between rule-type requests are the accepted keys for the params map.

The documentation elaborates on those keys but it should make no difference for us as long as the request has params.

What should alternatively be in generated/alerting/model_create_rule_request.go is:

type CreateRuleRequest struct {
	Actions    []ActionsInner `json:"actions,omitempty"`
	AlertDelay *AlertDelay    `json:"alert_delay,omitempty"`
	Consumer string `json:"consumer"`
	Enabled *bool `json:"enabled,omitempty"`
        Name string `json:"name"`
	NotifyWhen *NotifyWhen `json:"notify_when,omitempty"`
	Params map[string]interface{} `json:"params"`
	RuleTypeId string   `json:"rule_type_id"`
	Schedule   Schedule `json:"schedule"`
	Tags       []string `json:"tags,omitempty"`
	Throttle NullableString `json:"throttle,omitempty"`
}

Any idea how I can achieve this?

One thing I can think of is editing the alerting documentation before generating the bundled.yaml but that feels very error prone and a not very sustainable way to update alerting here.

Maybe there is some param or configuration I can add to make generate-alerting-client?

PS.: There seemed to be some issues with oneOf and the openapi generator. The roadmap mentions:

OAS3.0 features support: anyOf, oneOf, callbacks, etc

But! I am not too knowledgeable about all this so I'm still investigating. Maybe you guys are familiar with it?

@adcoelho adcoelho requested a review from tobio August 12, 2024 13:54
@adcoelho
Copy link
Contributor Author

Closing this as I have found a solution for my problem. Will elaborate on a different PR.

@adcoelho adcoelho closed this Aug 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant