2013-08-14 14:26:51 +08:00
|
|
|
package operation
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"net/url"
|
|
|
|
"strconv"
|
2014-10-27 02:34:55 +08:00
|
|
|
|
2014-12-14 16:20:21 +08:00
|
|
|
"github.com/mcqueenorama/weed-fs/go/glog"
|
|
|
|
"github.com/mcqueenorama/weed-fs/go/util"
|
2013-08-14 14:26:51 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
type AssignResult struct {
|
2014-04-16 00:20:04 +08:00
|
|
|
Fid string `json:"fid,omitempty"`
|
|
|
|
Url string `json:"url,omitempty"`
|
|
|
|
PublicUrl string `json:"publicUrl,omitempty"`
|
|
|
|
Count int `json:"count,omitempty"`
|
|
|
|
Error string `json:"error,omitempty"`
|
2013-08-14 14:26:51 +08:00
|
|
|
}
|
|
|
|
|
2014-09-21 03:38:59 +08:00
|
|
|
func Assign(server string, count int, replication string, collection string, ttl string) (*AssignResult, error) {
|
2013-08-14 14:26:51 +08:00
|
|
|
values := make(url.Values)
|
|
|
|
values.Add("count", strconv.Itoa(count))
|
|
|
|
if replication != "" {
|
|
|
|
values.Add("replication", replication)
|
|
|
|
}
|
2014-03-10 14:54:07 +08:00
|
|
|
if collection != "" {
|
|
|
|
values.Add("collection", collection)
|
|
|
|
}
|
2014-09-21 03:38:59 +08:00
|
|
|
if ttl != "" {
|
|
|
|
values.Add("ttl", ttl)
|
|
|
|
}
|
2013-08-14 14:26:51 +08:00
|
|
|
jsonBlob, err := util.Post("http://"+server+"/dir/assign", values)
|
|
|
|
glog.V(2).Info("assign result :", string(jsonBlob))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
var ret AssignResult
|
|
|
|
err = json.Unmarshal(jsonBlob, &ret)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if ret.Count <= 0 {
|
|
|
|
return nil, errors.New(ret.Error)
|
|
|
|
}
|
|
|
|
return &ret, nil
|
|
|
|
}
|