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

Index Lifecycle Management Policy API #1105

Merged
merged 10 commits into from
Jun 6, 2019
1 change: 1 addition & 0 deletions CONTRIBUTORS
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
# Please keep this list sorted.

0x6875790d0a [@huydx](https://github.com/huydx)
Aaron Tami [@aarontami](https://github.com/aarontami)
Adam Alix [@adamalix](https://github.com/adamalix)
Adam Weiner [@adamweiner](https://github.com/adamweiner)
Adrian Lungu [@AdrianLungu](https://github.com/AdrianLungu)
Expand Down
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,19 @@ Here are a few tips on how to get used to Elastic:
- [x] Refresh
- [x] Force Merge

### Index Lifecycle Management APIs

- [x] Create Policy
- [x] Get Policy
- [x] Delete Policy
- [ ] Move to Step
- [ ] Remove Policy
- [ ] Retry Policy
- [ ] Get Ilm Status
- [ ] Explain Lifecycle
- [ ] Start Ilm
- [ ] Stop Ilm

### cat APIs

The cat APIs are not implemented as of now. We think they are better suited for operating with Elasticsearch on the command line.
Expand Down
17 changes: 17 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -1866,6 +1866,23 @@ func (c *Client) XPackInfo() *XPackInfoService {
return NewXPackInfoService(c)
}

// -- X-Pack Index Lifecycle Management --

// XPackIlmPutLifecycle adds or modifies an ilm policy.
func (c *Client) XPackIlmPutLifecycle() *XPackIlmPutLifecycleService {
return NewXPackIlmPutLifecycleService(c)
}

// XPackIlmGettLifecycle gets an ilm policy.
func (c *Client) XPackIlmGetLifecycle() *XPackIlmGetLifecycleService {
return NewXPackIlmGetLifecycleService(c)
}

// XPackIlmDeleteLifecycle deletes an ilm policy.
func (c *Client) XPackIlmDeleteLifecycle() *XPackIlmDeleteLifecycleService {
return NewXPackIlmDeleteLifecycleService(c)
}

// -- X-Pack Security --

// XPackSecurityGetRoleMapping gets a role mapping.
Expand Down
1 change: 1 addition & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ services:
environment:
- cluster.name=platinum
- bootstrap.memory_lock=true
- xpack.ilm.enabled=true
- xpack.license.self_generated.type=trial
- xpack.security.enabled=true
- xpack.watcher.enabled=true
Expand Down
142 changes: 142 additions & 0 deletions xpack_ilm_delete_lifecycle.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
// Copyright 2012-present Oliver Eilhard. All rights reserved.
// Use of this source code is governed by a MIT-license.
// See http://olivere.mit-license.org/license.txt for details.

package elastic

import (
"context"
"fmt"
"net/url"

"github.com/olivere/elastic/uritemplates"
)

// See the documentation at
// https://www.elastic.co/guide/en/elasticsearch/reference/6.7/ilm-get-lifecycle.html.
type XPackIlmDeleteLifecycleService struct {
client *Client
policy string
pretty bool
timeout string
masterTimeout string
flatSettings *bool
local *bool
}

// NewXPackIlmDeleteLifecycleService creates a new XPackIlmDeleteLifecycleService.
func NewXPackIlmDeleteLifecycleService(client *Client) *XPackIlmDeleteLifecycleService {
return &XPackIlmDeleteLifecycleService{
client: client,
}
}

// Policy is the name of the index lifecycle policy.
func (s *XPackIlmDeleteLifecycleService) Policy(policy string) *XPackIlmDeleteLifecycleService {
s.policy = policy
return s
}

// Timeout is an explicit operation timeout.
func (s *XPackIlmDeleteLifecycleService) Timeout(timeout string) *XPackIlmDeleteLifecycleService {
s.timeout = timeout
return s
}

// MasterTimeout specifies the timeout for connection to master.
func (s *XPackIlmDeleteLifecycleService) MasterTimeout(masterTimeout string) *XPackIlmDeleteLifecycleService {
s.masterTimeout = masterTimeout
return s
}

// FlatSettings is returns settings in flat format (default: false).
func (s *XPackIlmDeleteLifecycleService) FlatSettings(flatSettings bool) *XPackIlmDeleteLifecycleService {
s.flatSettings = &flatSettings
return s
}

// Pretty indicates that the JSON response be indented and human readable.
func (s *XPackIlmDeleteLifecycleService) Pretty(pretty bool) *XPackIlmDeleteLifecycleService {
s.pretty = pretty
return s
}

// buildURL builds the URL for the operation.
func (s *XPackIlmDeleteLifecycleService) buildURL() (string, url.Values, error) {
// Build URL
var err error
var path string
path, err = uritemplates.Expand("/_ilm/policy/{policy}", map[string]string{
"policy": s.policy,
})
if err != nil {
return "", url.Values{}, err
}

// Add query string parameters
params := url.Values{}
if s.pretty {
params.Set("pretty", "true")
}
if s.flatSettings != nil {
params.Set("flat_settings", fmt.Sprintf("%v", *s.flatSettings))
}
if s.timeout != "" {
params.Set("timeout", s.timeout)
}
if s.masterTimeout != "" {
params.Set("master_timeout", s.masterTimeout)
}
if s.local != nil {
params.Set("local", fmt.Sprintf("%v", *s.local))
}
return path, params, nil
}

// Validate checks if the operation is valid.
func (s *XPackIlmDeleteLifecycleService) Validate() error {
var invalid []string
if s.policy == "" {
invalid = append(invalid, "Policy")
}
if len(invalid) > 0 {
return fmt.Errorf("missing required fields: %v", invalid)
}
return nil
}

// Do executes the operation.
func (s *XPackIlmDeleteLifecycleService) Do(ctx context.Context) (*XPackIlmDeleteLifecycleResponse, error) {
// Check pre-conditions
if err := s.Validate(); err != nil {
return nil, err
}

// Delete URL for request
path, params, err := s.buildURL()
if err != nil {
return nil, err
}

// Delete HTTP response
res, err := s.client.PerformRequest(ctx, PerformRequestOptions{
Method: "DELETE",
Path: path,
Params: params,
})
if err != nil {
return nil, err
}

// Return operation response
ret := new(XPackIlmDeleteLifecycleResponse)
if err := s.client.decoder.Decode(res.Body, ret); err != nil {
return nil, err
}
return ret, nil
}

// XPackIlmDeleteLifecycleResponse is the response of XPackIlmDeleteLifecycleService.Do.
type XPackIlmDeleteLifecycleResponse struct {
Acknowledged bool `json:"acknowledged"`
}
142 changes: 142 additions & 0 deletions xpack_ilm_get_lifecycle.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
// Copyright 2012-present Oliver Eilhard. All rights reserved.
// Use of this source code is governed by a MIT-license.
// See http://olivere.mit-license.org/license.txt for details.

package elastic

import (
"context"
"fmt"
"net/url"
"strings"

"github.com/olivere/elastic/uritemplates"
)

// See the documentation at
// https://www.elastic.co/guide/en/elasticsearch/reference/6.7/ilm-get-lifecycle.html.
type XPackIlmGetLifecycleService struct {
client *Client
policy []string
pretty bool
timeout string
masterTimeout string
flatSettings *bool
local *bool
}

// NewXPackIlmGetLifecycleService creates a new XPackIlmGetLifecycleService.
func NewXPackIlmGetLifecycleService(client *Client) *XPackIlmGetLifecycleService {
return &XPackIlmGetLifecycleService{
client: client,
}
}

// Policy is the name of the index lifecycle policy.
func (s *XPackIlmGetLifecycleService) Policy(policies ...string) *XPackIlmGetLifecycleService {
s.policy = append(s.policy, policies...)
return s
}

// Timeout is an explicit operation timeout.
func (s *XPackIlmGetLifecycleService) Timeout(timeout string) *XPackIlmGetLifecycleService {
s.timeout = timeout
return s
}

// MasterTimeout specifies the timeout for connection to master.
func (s *XPackIlmGetLifecycleService) MasterTimeout(masterTimeout string) *XPackIlmGetLifecycleService {
s.masterTimeout = masterTimeout
return s
}

// FlatSettings is returns settings in flat format (default: false).
func (s *XPackIlmGetLifecycleService) FlatSettings(flatSettings bool) *XPackIlmGetLifecycleService {
s.flatSettings = &flatSettings
return s
}

// Pretty indicates that the JSON response be indented and human readable.
func (s *XPackIlmGetLifecycleService) Pretty(pretty bool) *XPackIlmGetLifecycleService {
s.pretty = pretty
return s
}

// buildURL builds the URL for the operation.
func (s *XPackIlmGetLifecycleService) buildURL() (string, url.Values, error) {
// Build URL
var err error
var path string
if len(s.policy) > 0 {
path, err = uritemplates.Expand("/_ilm/policy/{policy}", map[string]string{
"policy": strings.Join(s.policy, ","),
})
} else {
path = "/_ilm/policy"
}
if err != nil {
return "", url.Values{}, err
}

// Add query string parameters
params := url.Values{}
if s.pretty {
params.Set("pretty", "true")
}
if s.flatSettings != nil {
params.Set("flat_settings", fmt.Sprintf("%v", *s.flatSettings))
}
if s.timeout != "" {
params.Set("timeout", s.timeout)
}
if s.masterTimeout != "" {
params.Set("master_timeout", s.masterTimeout)
}
if s.local != nil {
params.Set("local", fmt.Sprintf("%v", *s.local))
}
return path, params, nil
}

// Validate checks if the operation is valid.
func (s *XPackIlmGetLifecycleService) Validate() error {
return nil
}

// Do executes the operation.
func (s *XPackIlmGetLifecycleService) Do(ctx context.Context) (map[string]*XPackIlmGetLifecycleResponse, error) {
// Check pre-conditions
if err := s.Validate(); err != nil {
return nil, err
}

// Get URL for request
path, params, err := s.buildURL()
if err != nil {
return nil, err
}

// Get HTTP response
res, err := s.client.PerformRequest(ctx, PerformRequestOptions{
Method: "GET",
Path: path,
Params: params,
})
if err != nil {
return nil, err
}

// Return operation response
var ret map[string]*XPackIlmGetLifecycleResponse
if err := s.client.decoder.Decode(res.Body, &ret); err != nil {
return nil, err
}
return ret, nil
}

// XPackIlmGetLifecycleResponse is the response of XPackIlmGetLifecycleService.Do.
type XPackIlmGetLifecycleResponse struct {
Version int `json:"version,omitempty"`
ModifiedDate int `json:"modified,omitempty"`
Policy map[string]interface{} `json:"policy,omitempty"`
}
Loading