snapshot_create_repository.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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. package elastic
  5. import (
  6. "context"
  7. "encoding/json"
  8. "fmt"
  9. "net/url"
  10. "gopkg.in/olivere/elastic.v5/uritemplates"
  11. )
  12. // SnapshotCreateRepositoryService creates a snapshot repository.
  13. // See https://www.elastic.co/guide/en/elasticsearch/reference/5.3/modules-snapshots.html
  14. // for details.
  15. type SnapshotCreateRepositoryService struct {
  16. client *Client
  17. pretty bool
  18. repository string
  19. masterTimeout string
  20. timeout string
  21. verify *bool
  22. typ string
  23. settings map[string]interface{}
  24. bodyJson interface{}
  25. bodyString string
  26. }
  27. // NewSnapshotCreateRepositoryService creates a new SnapshotCreateRepositoryService.
  28. func NewSnapshotCreateRepositoryService(client *Client) *SnapshotCreateRepositoryService {
  29. return &SnapshotCreateRepositoryService{
  30. client: client,
  31. }
  32. }
  33. // Repository is the repository name.
  34. func (s *SnapshotCreateRepositoryService) Repository(repository string) *SnapshotCreateRepositoryService {
  35. s.repository = repository
  36. return s
  37. }
  38. // MasterTimeout specifies an explicit operation timeout for connection to master node.
  39. func (s *SnapshotCreateRepositoryService) MasterTimeout(masterTimeout string) *SnapshotCreateRepositoryService {
  40. s.masterTimeout = masterTimeout
  41. return s
  42. }
  43. // Timeout is an explicit operation timeout.
  44. func (s *SnapshotCreateRepositoryService) Timeout(timeout string) *SnapshotCreateRepositoryService {
  45. s.timeout = timeout
  46. return s
  47. }
  48. // Verify indicates whether to verify the repository after creation.
  49. func (s *SnapshotCreateRepositoryService) Verify(verify bool) *SnapshotCreateRepositoryService {
  50. s.verify = &verify
  51. return s
  52. }
  53. // Pretty indicates that the JSON response be indented and human readable.
  54. func (s *SnapshotCreateRepositoryService) Pretty(pretty bool) *SnapshotCreateRepositoryService {
  55. s.pretty = pretty
  56. return s
  57. }
  58. // Type sets the snapshot repository type, e.g. "fs".
  59. func (s *SnapshotCreateRepositoryService) Type(typ string) *SnapshotCreateRepositoryService {
  60. s.typ = typ
  61. return s
  62. }
  63. // Settings sets all settings of the snapshot repository.
  64. func (s *SnapshotCreateRepositoryService) Settings(settings map[string]interface{}) *SnapshotCreateRepositoryService {
  65. s.settings = settings
  66. return s
  67. }
  68. // Setting sets a single settings of the snapshot repository.
  69. func (s *SnapshotCreateRepositoryService) Setting(name string, value interface{}) *SnapshotCreateRepositoryService {
  70. if s.settings == nil {
  71. s.settings = make(map[string]interface{})
  72. }
  73. s.settings[name] = value
  74. return s
  75. }
  76. // BodyJson is documented as: The repository definition.
  77. func (s *SnapshotCreateRepositoryService) BodyJson(body interface{}) *SnapshotCreateRepositoryService {
  78. s.bodyJson = body
  79. return s
  80. }
  81. // BodyString is documented as: The repository definition.
  82. func (s *SnapshotCreateRepositoryService) BodyString(body string) *SnapshotCreateRepositoryService {
  83. s.bodyString = body
  84. return s
  85. }
  86. // buildURL builds the URL for the operation.
  87. func (s *SnapshotCreateRepositoryService) buildURL() (string, url.Values, error) {
  88. // Build URL
  89. path, err := uritemplates.Expand("/_snapshot/{repository}", map[string]string{
  90. "repository": s.repository,
  91. })
  92. if err != nil {
  93. return "", url.Values{}, err
  94. }
  95. // Add query string parameters
  96. params := url.Values{}
  97. if s.pretty {
  98. params.Set("pretty", "1")
  99. }
  100. if s.masterTimeout != "" {
  101. params.Set("master_timeout", s.masterTimeout)
  102. }
  103. if s.timeout != "" {
  104. params.Set("timeout", s.timeout)
  105. }
  106. if s.verify != nil {
  107. params.Set("verify", fmt.Sprintf("%v", *s.verify))
  108. }
  109. return path, params, nil
  110. }
  111. // buildBody builds the body for the operation.
  112. func (s *SnapshotCreateRepositoryService) buildBody() (interface{}, error) {
  113. if s.bodyJson != nil {
  114. return s.bodyJson, nil
  115. }
  116. if s.bodyString != "" {
  117. return s.bodyString, nil
  118. }
  119. body := map[string]interface{}{
  120. "type": s.typ,
  121. }
  122. if len(s.settings) > 0 {
  123. body["settings"] = s.settings
  124. }
  125. return body, nil
  126. }
  127. // Validate checks if the operation is valid.
  128. func (s *SnapshotCreateRepositoryService) Validate() error {
  129. var invalid []string
  130. if s.repository == "" {
  131. invalid = append(invalid, "Repository")
  132. }
  133. if s.bodyString == "" && s.bodyJson == nil {
  134. invalid = append(invalid, "BodyJson")
  135. }
  136. if len(invalid) > 0 {
  137. return fmt.Errorf("missing required fields: %v", invalid)
  138. }
  139. return nil
  140. }
  141. // Do executes the operation.
  142. func (s *SnapshotCreateRepositoryService) Do(ctx context.Context) (*SnapshotCreateRepositoryResponse, error) {
  143. // Check pre-conditions
  144. if err := s.Validate(); err != nil {
  145. return nil, err
  146. }
  147. // Get URL for request
  148. path, params, err := s.buildURL()
  149. if err != nil {
  150. return nil, err
  151. }
  152. // Setup HTTP request body
  153. body, err := s.buildBody()
  154. if err != nil {
  155. return nil, err
  156. }
  157. // Get HTTP response
  158. res, err := s.client.PerformRequest(ctx, "PUT", path, params, body)
  159. if err != nil {
  160. return nil, err
  161. }
  162. // Return operation response
  163. ret := new(SnapshotCreateRepositoryResponse)
  164. if err := json.Unmarshal(res.Body, ret); err != nil {
  165. return nil, err
  166. }
  167. return ret, nil
  168. }
  169. // SnapshotCreateRepositoryResponse is the response of SnapshotCreateRepositoryService.Do.
  170. type SnapshotCreateRepositoryResponse struct {
  171. Acknowledged bool `json:"acknowledged"`
  172. }