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.

68 lines
1.5 KiB

  1. package operation
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "fmt"
  6. "net/url"
  7. "strconv"
  8. "github.com/chrislusf/seaweedfs/weed/glog"
  9. "github.com/chrislusf/seaweedfs/weed/util"
  10. )
  11. type VolumeAssignRequest struct {
  12. Count uint64
  13. Replication string
  14. Collection string
  15. Ttl string
  16. DataCenter string
  17. Rack string
  18. DataNode string
  19. }
  20. type AssignResult struct {
  21. Fid string `json:"fid,omitempty"`
  22. Url string `json:"url,omitempty"`
  23. PublicUrl string `json:"publicUrl,omitempty"`
  24. Count uint64 `json:"count,omitempty"`
  25. Error string `json:"error,omitempty"`
  26. }
  27. func Assign(server string, r *VolumeAssignRequest) (*AssignResult, error) {
  28. values := make(url.Values)
  29. values.Add("count", strconv.FormatUint(r.Count, 10))
  30. if r.Replication != "" {
  31. values.Add("replication", r.Replication)
  32. }
  33. if r.Collection != "" {
  34. values.Add("collection", r.Collection)
  35. }
  36. if r.Ttl != "" {
  37. values.Add("ttl", r.Ttl)
  38. }
  39. if r.DataCenter != "" {
  40. values.Add("dataCenter", r.DataCenter)
  41. }
  42. if r.Rack != "" {
  43. values.Add("rack", r.Rack)
  44. }
  45. if r.DataNode != "" {
  46. values.Add("dataNode", r.DataNode)
  47. }
  48. jsonBlob, err := util.Post("http://"+server+"/dir/assign", values)
  49. glog.V(2).Info("assign result :", string(jsonBlob))
  50. if err != nil {
  51. return nil, err
  52. }
  53. var ret AssignResult
  54. err = json.Unmarshal(jsonBlob, &ret)
  55. if err != nil {
  56. return nil, fmt.Errorf("/dir/assign result JSON unmarshal error:%v, json:%s", err, string(jsonBlob))
  57. }
  58. if ret.Count <= 0 {
  59. return nil, errors.New(ret.Error)
  60. }
  61. return &ret, nil
  62. }