-
-
Save raffis/a201c557b17bc23c6c6f81b551495cf2 to your computer and use it in GitHub Desktop.
migrate file based acl pre balloon v2.7
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import "fmt" | |
import "sync" | |
import "net/http" | |
import "io/ioutil" | |
import "encoding/json" | |
import "encoding/base64" | |
import "bytes" | |
var username = "xxx" | |
var password = "xxx" | |
var base = "https://balloon.local" | |
var client = &http.Client{} | |
var parent string = "xxxxxxxxxxxxxxxxxxxxxxx" | |
var query = "{\"parent\":{\"$in\":[{\"$oid\":\"5d9c61ec6326d0005306573d\"}]}}" | |
//var query = "{\"parent\":{\"$in\":[{\"$oid\":\"5d25e67f5575ab66a018da1b\"},{\"$oid\":\"5bc5e5951a1a9f4b9b150cea\"},{\"$oid\":\"5b4ee790244ec200074c44dd\"},{\"$oid\":\"5d1c657e92a9a9209b618c62\"}]}}" | |
type Link struct { | |
Href string `json:"href"` | |
} | |
type Links struct { | |
Next *Link `json:"next"` | |
} | |
type List struct { | |
Links Links `json:"_links"` | |
Total int `json:"total"` | |
Count int `json:"count"` | |
Data []Node `json:"data"` | |
} | |
type Node struct { | |
Id string `json:"id"` | |
Name string `json:"name"` | |
Acl []Acl `json:"acl"` | |
} | |
type Acl struct { | |
Type string `json:"type"` | |
Id string `json:"id"` | |
Privilege string `json:"privilege"` | |
} | |
func main() { | |
fetch(base + "/api/v2/nodes?limit=5&query=" + query) | |
} | |
func fetch(link string) { | |
fmt.Println(link) | |
req, err := http.NewRequest("GET", link, nil) | |
if err != nil { | |
panic(err) | |
} | |
str := base64.StdEncoding.EncodeToString([]byte(username + ":" + password)) | |
req.Header.Add("Authorization", "Basic "+str) | |
resp, err := client.Do(req) | |
if err != nil { | |
panic(err) | |
} | |
fmt.Println(resp) | |
defer resp.Body.Close() | |
body, err := ioutil.ReadAll(resp.Body) | |
if err != nil { | |
panic(err) | |
} | |
list := &List{} | |
err = json.Unmarshal(body, list) | |
if err != nil { | |
panic(err) | |
} | |
fmt.Println(string(body)) | |
var wg sync.WaitGroup | |
if list.Links.Next != nil { | |
wg.Add(1) | |
go func() { | |
fetch(base + list.Links.Next.Href + "&query=" + query) | |
wg.Done() | |
}() | |
} | |
for _, node := range list.Data { | |
wg.Add(1) | |
go func(n Node) { | |
fmt.Println(n) | |
share := getShare(n.Acl[0].Id) | |
fmt.Println(share) | |
moveNode(&n, share) | |
wg.Done() | |
}(node) | |
} | |
wg.Wait() | |
fmt.Println(list) | |
} | |
func moveNode(node *Node, share *Node) error { | |
jsonStr := []byte("{\"destid\":\"" + share.Id + "\"}") | |
fmt.Println(string(jsonStr)) | |
req, err := http.NewRequest("POST", base+"/api/v2/nodes/"+node.Id+"/move", bytes.NewBuffer(jsonStr)) | |
str := base64.StdEncoding.EncodeToString([]byte(username + ":" + password)) | |
req.Header.Add("Authorization", "Basic "+str) | |
req.Header.Add("Content-Type", "application/json") | |
resp, err := client.Do(req) | |
fmt.Println(resp) | |
if err != nil { | |
return err | |
} | |
defer resp.Body.Close() | |
body, err := ioutil.ReadAll(resp.Body) | |
if err != nil { | |
panic(err) | |
} | |
fmt.Println(string(body)) | |
return nil | |
} | |
func getShare(userid string) *Node { | |
req, err := http.NewRequest("GET", base+"/api/v2/nodes?query={\"name\":\""+userid+"\"}", nil) | |
if err != nil { | |
panic(err) | |
} | |
str := base64.StdEncoding.EncodeToString([]byte(username + ":" + password)) | |
req.Header.Add("Authorization", "Basic "+str) | |
resp, err := client.Do(req) | |
if err != nil { | |
panic(err) | |
} | |
defer resp.Body.Close() | |
body, err := ioutil.ReadAll(resp.Body) | |
if err != nil { | |
panic(err) | |
} | |
fmt.Println(string(body)) | |
list := &List{} | |
err = json.Unmarshal(body, list) | |
if err != nil { | |
panic(err) | |
} | |
if list.Count == 0 { | |
c := createCollection(userid) | |
c.createShare(userid) | |
return c | |
} | |
return &list.Data[0] | |
} | |
func createCollection(userid string) *Node { | |
var jsonStr = []byte("{\"id\":\"" + parent + "\",\"name\":\"" + userid + "\"}") | |
fmt.Println(string(jsonStr)) | |
fmt.Println("create") | |
req, err := http.NewRequest("POST", base+"/api/v2/collections", bytes.NewBuffer(jsonStr)) | |
req.Header.Add("Content-Type", "application/json") | |
if err != nil { | |
panic(err) | |
} | |
str := base64.StdEncoding.EncodeToString([]byte(username + ":" + password)) | |
req.Header.Add("Authorization", "Basic "+str) | |
resp, err := client.Do(req) | |
fmt.Println(resp) | |
if err != nil { | |
panic(err) | |
} | |
defer resp.Body.Close() | |
body, err := ioutil.ReadAll(resp.Body) | |
if err != nil { | |
panic(err) | |
} | |
fmt.Println(string(body)) | |
node := &Node{} | |
err = json.Unmarshal(body, node) | |
if err != nil { | |
panic(err) | |
} | |
fmt.Println(node) | |
return node | |
} | |
func (c *Node) createShare(userid string) error { | |
var jsonStr = []byte("{\"name\":\"Scans\",\"acl\":[{\"type\":\"user\",\"privilege\":\"rw\",\"id\":\"" + userid + "\"}]}") | |
req, err := http.NewRequest("POST", base+"/api/v2/collections/"+c.Id+"/share", bytes.NewBuffer(jsonStr)) | |
req.Header.Add("Content-Type", "application/json") | |
if err != nil { | |
return err | |
} | |
str := base64.StdEncoding.EncodeToString([]byte(username + ":" + password)) | |
req.Header.Add("Authorization", "Basic "+str) | |
resp, err := client.Do(req) | |
fmt.Println(resp) | |
return err | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment