1// Copyright 2012-2018 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 13 "github.com/olivere/elastic/uritemplates" 14) 15 16// XPackSecurityPutRoleService retrieves a role by its name. 17// See https://www.elastic.co/guide/en/elasticsearch/reference/6.7/security-api-put-role.html. 18type XPackSecurityPutRoleService struct { 19 client *Client 20 pretty bool 21 name string 22 body interface{} 23} 24 25// NewXPackSecurityPutRoleService creates a new XPackSecurityPutRoleService. 26func NewXPackSecurityPutRoleService(client *Client) *XPackSecurityPutRoleService { 27 return &XPackSecurityPutRoleService{ 28 client: client, 29 } 30} 31 32// Name is name of the role to create. 33func (s *XPackSecurityPutRoleService) Name(name string) *XPackSecurityPutRoleService { 34 s.name = name 35 return s 36} 37 38// Pretty indicates that the JSON response be indented and human readable. 39func (s *XPackSecurityPutRoleService) Pretty(pretty bool) *XPackSecurityPutRoleService { 40 s.pretty = pretty 41 return s 42} 43 44// Body specifies the role. Use a string or a type that will get serialized as JSON. 45func (s *XPackSecurityPutRoleService) Body(body interface{}) *XPackSecurityPutRoleService { 46 s.body = body 47 return s 48} 49 50// buildURL builds the URL for the operation. 51func (s *XPackSecurityPutRoleService) buildURL() (string, url.Values, error) { 52 // Build URL 53 path, err := uritemplates.Expand("/_xpack/security/role/{name}", map[string]string{ 54 "name": s.name, 55 }) 56 if err != nil { 57 return "", url.Values{}, err 58 } 59 60 // Add query string parameters 61 params := url.Values{} 62 if s.pretty { 63 params.Set("pretty", "true") 64 } 65 return path, params, nil 66} 67 68// Validate checks if the operation is valid. 69func (s *XPackSecurityPutRoleService) Validate() error { 70 var invalid []string 71 if s.name == "" { 72 invalid = append(invalid, "Name") 73 } 74 if s.body == nil { 75 invalid = append(invalid, "Body") 76 } 77 if len(invalid) > 0 { 78 return fmt.Errorf("missing required fields: %v", invalid) 79 } 80 return nil 81} 82 83// Do executes the operation. 84func (s *XPackSecurityPutRoleService) Do(ctx context.Context) (*XPackSecurityPutRoleResponse, error) { 85 // Check pre-conditions 86 if err := s.Validate(); err != nil { 87 return nil, err 88 } 89 90 // Get URL for request 91 path, params, err := s.buildURL() 92 if err != nil { 93 return nil, err 94 } 95 96 // Get HTTP response 97 res, err := s.client.PerformRequest(ctx, PerformRequestOptions{ 98 Method: "PUT", 99 Path: path, 100 Params: params, 101 Body: s.body, 102 }) 103 if err != nil { 104 return nil, err 105 } 106 107 // Return operation response 108 ret := new(XPackSecurityPutRoleResponse) 109 if err := json.Unmarshal(res.Body, ret); err != nil { 110 return nil, err 111 } 112 return ret, nil 113} 114 115// XPackSecurityPutRoleResponse is the response of XPackSecurityPutRoleService.Do. 116type XPackSecurityPutRoleResponse struct { 117 Role XPackSecurityPutRole 118} 119 120type XPackSecurityPutRole struct { 121 Created bool `json:"created"` 122} 123