/
templates.go
73 lines (62 loc) · 2.74 KB
/
templates.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/*************************************************************************
* Copyright 2021 Gravwell, Inc. All rights reserved.
* Contact: <legal@gravwell.io>
*
* This software may be modified and distributed under the terms of the
* BSD 2-clause license. See the LICENSE file for details.
**************************************************************************/
package client
import (
"encoding/json"
"net/http"
"github.com/gravwell/gravwell/v4/client/types"
"github.com/google/uuid"
)
// ListTemplates returns a list of templates accessible to the current user.
func (c *Client) ListTemplates() (templates []types.WireUserTemplate, err error) {
err = c.getStaticURL(templatesUrl(), &templates)
return
}
// ListAllTemplates returns the list of all templates in the system.
// Non-administrators will receive the same list as returned by ListTemplates.
func (c *Client) ListAllTemplates() (templates []types.WireUserTemplate, err error) {
c.SetAdminMode()
if err = c.getStaticURL(templatesUrl(), &templates); err != nil {
templates = nil
}
c.ClearAdminMode()
return
}
// NewTemplate creates a new template with the given GUID, name, description, contents.
// If guid is set to uuid.Nil, a random GUID will be chosen automatically.
func (c *Client) NewTemplate(guid uuid.UUID, name, description string, contents types.RawObject) (details types.WireUserTemplate, err error) {
// Mash the content blob into an appropriate type
var ct types.TemplateContents
if err = json.Unmarshal(contents, &ct); err != nil {
return
}
template := types.UserTemplate{GUID: guid, Contents: ct, Name: name, Description: description}
err = c.methodStaticPushURL(http.MethodPost, templatesUrl(), template, &details, nil, nil)
return
}
// GetTemplate returns a types.WireUserTemplate with the requested GUID.
// Because unique GUIDs are not enforced, the following precedence
// is used when selecting a template to return:
// 1. Templates owned by the user always have highest priority
// 2. Templates shared with a group to which the user belongs are next
// 3. Global templates are the lowest priority
func (c *Client) GetTemplate(guid uuid.UUID) (template types.WireUserTemplate, err error) {
err = c.getStaticURL(templatesGuidUrl(guid), &template)
return
}
// SetTemplate allows the owner of a template (or an admin) to update
// the contents of the template.
func (c *Client) SetTemplate(guid uuid.UUID, template types.WireUserTemplate) (details types.WireUserTemplate, err error) {
err = c.methodStaticPushURL(http.MethodPut, templatesGuidUrl(guid), template, &details, nil, nil)
return
}
// DeleteTemplate deletes the template with the specified GUID
func (c *Client) DeleteTemplate(guid uuid.UUID) (err error) {
err = c.deleteStaticURL(templatesGuidUrl(guid), nil)
return
}