2013-08-14 06:26:51 +00:00
|
|
|
package operation
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
2015-03-09 08:10:01 +00:00
|
|
|
"fmt"
|
2013-08-14 06:26:51 +00:00
|
|
|
"net/url"
|
|
|
|
"strconv"
|
2014-10-26 18:34:55 +00:00
|
|
|
|
2015-04-16 19:18:06 +00:00
|
|
|
"github.com/chrislusf/seaweedfs/go/glog"
|
|
|
|
"github.com/chrislusf/seaweedfs/go/util"
|
2013-08-14 06:26:51 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type AssignResult struct {
|
2014-04-15 16:20:04 +00:00
|
|
|
Fid string `json:"fid,omitempty"`
|
|
|
|
Url string `json:"url,omitempty"`
|
|
|
|
PublicUrl string `json:"publicUrl,omitempty"`
|
2015-04-06 21:17:36 +00:00
|
|
|
Count uint64 `json:"count,omitempty"`
|
2014-04-15 16:20:04 +00:00
|
|
|
Error string `json:"error,omitempty"`
|
2013-08-14 06:26:51 +00:00
|
|
|
}
|
|
|
|
|
2015-04-06 21:17:36 +00:00
|
|
|
func Assign(server string, count uint64, replication string, collection string, ttl string) (*AssignResult, error) {
|
2013-08-14 06:26:51 +00:00
|
|
|
values := make(url.Values)
|
2015-04-06 21:17:36 +00:00
|
|
|
values.Add("count", strconv.FormatUint(count, 10))
|
2013-08-14 06:26:51 +00:00
|
|
|
if replication != "" {
|
|
|
|
values.Add("replication", replication)
|
|
|
|
}
|
2014-03-10 06:54:07 +00:00
|
|
|
if collection != "" {
|
|
|
|
values.Add("collection", collection)
|
|
|
|
}
|
2014-09-20 19:38:59 +00:00
|
|
|
if ttl != "" {
|
|
|
|
values.Add("ttl", ttl)
|
|
|
|
}
|
2013-08-14 06:26:51 +00: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 {
|
2015-03-19 17:48:28 +00:00
|
|
|
return nil, fmt.Errorf("/dir/assign result JSON unmarshal error:%v, json:%s", err, string(jsonBlob))
|
2013-08-14 06:26:51 +00:00
|
|
|
}
|
|
|
|
if ret.Count <= 0 {
|
|
|
|
return nil, errors.New(ret.Error)
|
|
|
|
}
|
|
|
|
return &ret, nil
|
|
|
|
}
|