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