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

ISM Plugin Conditions struct is invalid #555

Closed
emtammaru opened this issue May 29, 2024 · 0 comments · Fixed by #556
Closed

ISM Plugin Conditions struct is invalid #555

emtammaru opened this issue May 29, 2024 · 0 comments · Fixed by #556

Comments

@emtammaru
Copy link
Contributor

The following Conditions struct is invalid and causes a 400 error to be returned by opensearch when a value is supplied:

Conditions []struct {
MinIndexAge string `json:"min_index_age,omitempty"`
MinRolloverAge string `json:"min_rollover_age,omitempty"`
MinDocCount int `json:"min_doc_count,omitempty"`
MinSize string `json:"min_size,omitempty"`
Cron *struct {
Expression string `json:"expression"`
Timezone string `json:"timezone"`
} `json:"cron,omitempty"`

Reason being, it should be a single object, not an array of objects -- see here for example: https://opensearch.org/docs/2.14/im-plugin/ism/api/#create-policy

            "conditions": {
              "min_index_age": "5m"
            }

Here's an example request to reproduce the error:

transitions := []ism.PolicyStateTransition{
		{
			StateName: "delete",
			Conditions: []struct {
				MinIndexAge    string `json:"min_index_age,omitempty"`
				MinRolloverAge string `json:"min_rollover_age,omitempty"`
				MinDocCount    int    `json:"min_doc_count,omitempty"`
				MinSize        string `json:"min_size,omitempty"`
				Cron           *struct {
					Expression string `json:"expression"`
					Timezone   string `json:"timezone"`
				} `json:"cron,omitempty"`
			}{
				{
					MinIndexAge: "1d",
				},
			},
		},
	}

	req := ism.PoliciesPutReq{
		Policy: policyName,
		Body: ism.PoliciesPutBody{
			Policy: ism.PolicyBody{
				Description: "retention policy of 1 day",
				DefaultState: "hot",
				States: []ism.PolicyState{
					{
						Name:        "hot",
						Transitions: &transitions,
					},
					{
						Name: "delete",
						Actions: []ism.PolicyStateAction{
							{
								Delete: &ism.PolicyStateDelete{},
							},
						},
					},
				},
			},
		},
	}

Causes the error:

status: 400, type: parsing_exception, reason: Failed to parse object: expecting token of type [START_OBJECT] but found [START_ARRAY], root_cause: [{parsing_exception Failed to parse object: expecting token of type [START_OBJECT] but found [START_ARRAY] }]

Simple fix here is to change from Conditions []struct { to Conditions struct {. But, the inline struct type declaration is not user friendly. Better solution is to create a type for Conditions such as:

// PolicyStateTransitionCondition is a sub type of PolicyStateTransition containing conditions for a transition
type PolicyStateTransitionCondition struct {
	MinIndexAge    string                              `json:"min_index_age,omitempty"`
	MinRolloverAge string                              `json:"min_rollover_age,omitempty"`
	MinDocCount    int                                 `json:"min_doc_count,omitempty"`
	MinSize        string                              `json:"min_size,omitempty"`
	Cron           *PolicyStateTransitionConditionCron `json:"cron,omitempty"`
}

type PolicyStateTransitionConditionCron struct {
	Expression string `json:"expression"`
	Timezone   string `json:"timezone"`
}

// PolicyStateTransition is a sub type of PolicyState containing information about transition to other states
type PolicyStateTransition struct {
	StateName  string                          `json:"state_name"`
	Conditions *PolicyStateTransitionCondition `json:"conditions"`
}
dblock pushed a commit that referenced this issue May 29, 2024
* fixes Conditions in PolicyStateTransition of ISM plugin

Signed-off-by: Erik Tammaru <[email protected]>

* update CHANGELOG

Signed-off-by: Erik Tammaru <[email protected]>

* add comment for exported type

Signed-off-by: Erik Tammaru <[email protected]>

---------

Signed-off-by: Erik Tammaru <[email protected]>
Co-authored-by: Erik Tammaru <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant