forked from tmtk75/kii-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbucket.go
154 lines (140 loc) · 3.37 KB
/
bucket.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package kiicli
import (
"encoding/json"
"fmt"
"log"
"github.com/tmtk75/cli"
)
func ListBucket() {
p := Profile()
path := fmt.Sprintf("/apps/%s/buckets", p.AppId)
headers := p.HttpHeadersWithAuthorization("")
body := HttpGet(path, headers).Bytes()
var v map[string][]interface{}
json.Unmarshal(body, &v)
for _, a := range v["bucketIDs"] {
fmt.Println(a)
}
}
func ReadBucket(bucketname string) {
p := Profile()
path := fmt.Sprintf("/apps/%s/buckets/%s", p.AppId, bucketname)
headers := p.HttpHeadersWithAuthorization("")
body := HttpGet(path, headers).Bytes()
fmt.Println(string(body))
}
func DeleteBucket(name string) {
p := Profile()
path := fmt.Sprintf("/apps/%s/buckets/%v", p.AppId, name)
headers := p.HttpHeadersWithAuthorization("")
HttpDelete(path, headers).Bytes()
}
func readBucketAcl(bucketname string) []byte {
p := Profile()
path := fmt.Sprintf("/apps/%s/buckets/%s/acl", p.AppId, bucketname)
headers := p.HttpHeadersWithAuthorization("")
body := HttpGet(path, headers).Bytes()
return body
}
func ReadBucketAcl(bucketname string) {
body := readBucketAcl(bucketname)
fmt.Println(string(body))
}
func DeleteBucketAcl(bucketname, verb, userId string) {
p := Profile()
// /apps/%s/buckets/%s/acl/QUERY_OBJECTS_IN_BUCKET/UserID:ANONYMOUS_USER
path := fmt.Sprintf("/apps/%s/buckets/%s/acl/%v/UserID:%v", p.AppId, bucketname, verb, userId)
headers := p.HttpHeadersWithAuthorization("")
body := HttpDelete(path, headers).Bytes()
fmt.Println(string(body))
}
func DeleteAllBucketAcls(bucketname string) {
body := readBucketAcl(bucketname)
var j map[string][]struct {
UserId string `json:"userID"`
}
err := json.Unmarshal(body, &j)
if err != nil {
log.Fatalf("%v", err)
}
for verb, v := range j {
for _, e := range v {
//logger.Printf("%v:%v\n", verb, e.UserId)
DeleteBucketAcl(bucketname, verb, e.UserId)
}
}
}
var BucketCommands = []cli.Command{
{
Name: "list",
Usage: "List buckets",
Action: func(c *cli.Context) {
ListBucket()
},
},
{
Name: "read",
Usage: "Read a bucket",
Args: "<bucket-id>",
Action: func(c *cli.Context) {
bid, _ := c.ArgFor("bucket-id")
ReadBucket(bid)
},
},
{
Name: "delete",
Usage: "Delete a bucket",
Args: "<bucket-id>",
Action: func(c *cli.Context) {
bid, _ := c.ArgFor("bucket-id")
DeleteBucket(bid)
},
},
{
Name: "acl",
Usage: "Edit bucket ACL",
Description: `Edit bucket ACL
verb:
CREATE_OBJECTS_IN_BUCKET
QUERY_OBJECTS_IN_BUCKET
DROP_BUCKET_WITH_ALL_CONTENT
Special userID:
ANY_AUTHENTICATED_USER
ANONYMOUS_USER`,
Subcommands: cmds,
},
}
var cmds = []cli.Command{
{
Name: "read",
Usage: "Read a bucket ACL",
Args: "<bucket-id>",
Action: func(c *cli.Context) {
bid, _ := c.ArgFor("bucket-id")
ReadBucketAcl(bid)
},
},
{
Name: "delete",
Usage: "Delete a bucket ACL",
Args: `<bucket-id> <verb> <user-id>`,
Description: `\
ex) my-bucket CREATE_OBJECTS_IN_BUCKET ANONYMOUS_USER
my-bucket QUERY_OBJECTS_IN_BUCKET ANY_AUTHENTICATED_USER`,
Action: func(c *cli.Context) {
bid, _ := c.ArgFor("bucket-id")
verb, _ := c.ArgFor("verb")
uid, _ := c.ArgFor("user-id")
DeleteBucketAcl(bid, verb, uid)
},
},
{
Name: "delete-all",
Usage: "Delete all ACLs",
Args: "<bucket-id>",
Action: func(c *cli.Context) {
bid, _ := c.ArgFor("bucket-id")
DeleteAllBucketAcls(bid)
},
},
}