1// Copyright 2012-present Oliver Eilhard. All rights reserved.
2// Use of this source code is governed by a MIT-license.
3// See http://olivere.mit-license.org/license.txt for details.
4
5package elastic
6
7import (
8	"context"
9	"encoding/json"
10	"fmt"
11	"net/url"
12	"strings"
13
14	"github.com/olivere/elastic/uritemplates"
15)
16
17// SnapshotGetRepositoryService reads a snapshot repository.
18// See https://www.elastic.co/guide/en/elasticsearch/reference/6.7/modules-snapshots.html
19// for details.
20type SnapshotGetRepositoryService struct {
21	client        *Client
22	pretty        bool
23	repository    []string
24	local         *bool
25	masterTimeout string
26}
27
28// NewSnapshotGetRepositoryService creates a new SnapshotGetRepositoryService.
29func NewSnapshotGetRepositoryService(client *Client) *SnapshotGetRepositoryService {
30	return &SnapshotGetRepositoryService{
31		client:     client,
32		repository: make([]string, 0),
33	}
34}
35
36// Repository is the list of repository names.
37func (s *SnapshotGetRepositoryService) Repository(repositories ...string) *SnapshotGetRepositoryService {
38	s.repository = append(s.repository, repositories...)
39	return s
40}
41
42// Local indicates whether to return local information, i.e. do not retrieve the state from master node (default: false).
43func (s *SnapshotGetRepositoryService) Local(local bool) *SnapshotGetRepositoryService {
44	s.local = &local
45	return s
46}
47
48// MasterTimeout specifies an explicit operation timeout for connection to master node.
49func (s *SnapshotGetRepositoryService) MasterTimeout(masterTimeout string) *SnapshotGetRepositoryService {
50	s.masterTimeout = masterTimeout
51	return s
52}
53
54// Pretty indicates that the JSON response be indented and human readable.
55func (s *SnapshotGetRepositoryService) Pretty(pretty bool) *SnapshotGetRepositoryService {
56	s.pretty = pretty
57	return s
58}
59
60// buildURL builds the URL for the operation.
61func (s *SnapshotGetRepositoryService) buildURL() (string, url.Values, error) {
62	// Build URL
63	var err error
64	var path string
65	if len(s.repository) > 0 {
66		path, err = uritemplates.Expand("/_snapshot/{repository}", map[string]string{
67			"repository": strings.Join(s.repository, ","),
68		})
69	} else {
70		path = "/_snapshot"
71	}
72	if err != nil {
73		return "", url.Values{}, err
74	}
75
76	// Add query string parameters
77	params := url.Values{}
78	if s.pretty {
79		params.Set("pretty", "true")
80	}
81	if s.local != nil {
82		params.Set("local", fmt.Sprintf("%v", *s.local))
83	}
84	if s.masterTimeout != "" {
85		params.Set("master_timeout", s.masterTimeout)
86	}
87	return path, params, nil
88}
89
90// Validate checks if the operation is valid.
91func (s *SnapshotGetRepositoryService) Validate() error {
92	return nil
93}
94
95// Do executes the operation.
96func (s *SnapshotGetRepositoryService) Do(ctx context.Context) (SnapshotGetRepositoryResponse, error) {
97	// Check pre-conditions
98	if err := s.Validate(); err != nil {
99		return nil, err
100	}
101
102	// Get URL for request
103	path, params, err := s.buildURL()
104	if err != nil {
105		return nil, err
106	}
107
108	// Get HTTP response
109	res, err := s.client.PerformRequest(ctx, PerformRequestOptions{
110		Method: "GET",
111		Path:   path,
112		Params: params,
113	})
114	if err != nil {
115		return nil, err
116	}
117
118	// Return operation response
119	var ret SnapshotGetRepositoryResponse
120	if err := json.Unmarshal(res.Body, &ret); err != nil {
121		return nil, err
122	}
123	return ret, nil
124}
125
126// SnapshotGetRepositoryResponse is the response of SnapshotGetRepositoryService.Do.
127type SnapshotGetRepositoryResponse map[string]*SnapshotRepositoryMetaData
128
129// SnapshotRepositoryMetaData contains all information about
130// a single snapshot repository.
131type SnapshotRepositoryMetaData struct {
132	Type     string                 `json:"type"`
133	Settings map[string]interface{} `json:"settings,omitempty"`
134}
135