You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
48 lines
1.1 KiB
48 lines
1.1 KiB
package operation
|
|
|
|
import (
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"net/url"
|
|
"strconv"
|
|
|
|
"github.com/chrislusf/weed-fs/go/glog"
|
|
"github.com/chrislusf/weed-fs/go/util"
|
|
)
|
|
|
|
type AssignResult struct {
|
|
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"`
|
|
}
|
|
|
|
func Assign(server string, count int, replication string, collection string, ttl string) (*AssignResult, error) {
|
|
values := make(url.Values)
|
|
values.Add("count", strconv.Itoa(count))
|
|
if replication != "" {
|
|
values.Add("replication", replication)
|
|
}
|
|
if collection != "" {
|
|
values.Add("collection", collection)
|
|
}
|
|
if ttl != "" {
|
|
values.Add("ttl", ttl)
|
|
}
|
|
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, fmt.Errorf("JSON unmarshal error:%v, json:%s", err, string(jsonBlob))
|
|
}
|
|
if ret.Count <= 0 {
|
|
return nil, errors.New(ret.Error)
|
|
}
|
|
return &ret, nil
|
|
}
|