/
quota.go
100 lines (89 loc) · 2.31 KB
/
quota.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
// Copyright The Athenz Authors
// Licensed under the terms of the Apache version 2.0 license. See LICENSE file for terms.
package zmscli
import (
"bytes"
"fmt"
"strconv"
"strings"
"github.com/AthenZ/athenz/clients/go/zms"
)
func (cli Zms) DeleteQuota(dn string) (*string, error) {
err := cli.Zms.DeleteQuota(zms.DomainName(dn), cli.AuditRef)
if err != nil {
return nil, err
}
s := "[Removed quota for domain " + dn + "]"
message := SuccessMessage{
Status: 200,
Message: s,
}
return cli.dumpByFormat(message, cli.buildYAMLOutput)
}
func (cli Zms) GetQuota(dn string) (*string, error) {
quota, err := cli.Zms.GetQuota(zms.DomainName(dn))
if err != nil {
return nil, err
}
oldYamlConverter := func(res interface{}) (*string, error) {
var buf bytes.Buffer
cli.dumpQuota(&buf, quota)
s := buf.String()
return &s, nil
}
return cli.dumpByFormat(quota, oldYamlConverter)
}
func (cli Zms) SetQuota(dn string, attrs []string) (*string, error) {
quota, err := cli.Zms.GetQuota(zms.DomainName(dn))
if err != nil {
return nil, err
}
for _, attr := range attrs {
idx := strings.Index(attr, "=")
if idx == -1 {
return nil, fmt.Errorf("bad quota syntax: zms-cli help set-quota")
}
key := attr[0:idx]
value, err := strconv.Atoi(attr[idx+1:])
if err != nil {
return nil, fmt.Errorf("bad quota syntax: zms-cli help set-quota")
}
switch key {
case "role":
quota.Role = int32(value)
case "role-member":
quota.RoleMember = int32(value)
case "group":
quota.Group = int32(value)
case "group-member":
quota.GroupMember = int32(value)
case "subdomain":
quota.Subdomain = int32(value)
case "policy":
quota.Policy = int32(value)
case "assertion":
quota.Assertion = int32(value)
case "service":
quota.Service = int32(value)
case "service-host":
quota.ServiceHost = int32(value)
case "public-key":
quota.PublicKey = int32(value)
case "entity":
quota.Entity = int32(value)
default:
return nil, fmt.Errorf("bad quota syntax: zms-cli help set-quota")
}
}
quota.Name = zms.DomainName(dn)
err = cli.Zms.PutQuota(zms.DomainName(dn), cli.AuditRef, quota)
if err != nil {
return nil, err
}
s := "[domain " + dn + " quota successfully set]\n"
message := SuccessMessage{
Status: 200,
Message: s,
}
return cli.dumpByFormat(message, cli.buildYAMLOutput)
}