-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathroles.go
More file actions
54 lines (45 loc) · 1.67 KB
/
Copy pathroles.go
File metadata and controls
54 lines (45 loc) · 1.67 KB
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
package mackerel
import (
"context"
"fmt"
)
// Role represents Mackerel "role".
type Role struct {
Name string `json:"name"`
Memo string `json:"memo"`
}
// CreateRoleParam parameters for CreateRole
type CreateRoleParam Role
// FindRoles finds roles.
func (c *Client) FindRoles(serviceName string) ([]*Role, error) {
return c.FindRolesContext(context.Background(), serviceName)
}
// FindRolesContext finds roles.
func (c *Client) FindRolesContext(ctx context.Context, serviceName string) ([]*Role, error) {
data, err := requestGetContext[struct {
Roles []*Role `json:"roles"`
}](ctx, c, fmt.Sprintf("/api/v0/services/%s/roles", serviceName))
if err != nil {
return nil, err
}
return data.Roles, nil
}
// CreateRole creates a role.
func (c *Client) CreateRole(serviceName string, param *CreateRoleParam) (*Role, error) {
return c.CreateRoleContext(context.Background(), serviceName, param)
}
// CreateRoleContext creates a role.
func (c *Client) CreateRoleContext(ctx context.Context, serviceName string, param *CreateRoleParam) (*Role, error) {
path := fmt.Sprintf("/api/v0/services/%s/roles", serviceName)
return requestPostContext[Role](ctx, c, path, param)
}
// DeleteRole deletes a role.
func (c *Client) DeleteRole(serviceName, roleName string) (*Role, error) {
path := fmt.Sprintf("/api/v0/services/%s/roles/%s", serviceName, roleName)
return requestDeleteContext[Role](context.Background(), c, path)
}
// DeleteRoleContext is like [DeleteRole].
func (c *Client) DeleteRoleContext(ctx context.Context, serviceName, roleName string) (*Role, error) {
path := fmt.Sprintf("/api/v0/services/%s/roles/%s", serviceName, roleName)
return requestDeleteContext[Role](ctx, c, path)
}