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

  1. package operation
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "fmt"
  6. "net/url"
  7. "strconv"
  8. "github.com/chrislusf/weed-fs/go/glog"
  9. "github.com/chrislusf/weed-fs/go/util"
  10. )
  11. type AssignResult struct {
  12. Fid string `json:"fid,omitempty"`
  13. Url string `json:"url,omitempty"`
  14. PublicUrl string `json:"publicUrl,omitempty"`
  15. Count int `json:"count,omitempty"`
  16. Error string `json:"error,omitempty"`
  17. }
  18. func Assign(server string, count int, replication string, collection string, ttl string) (*AssignResult, error) {
  19. values := make(url.Values)
  20. values.Add("count", strconv.Itoa(count))
  21. if replication != "" {
  22. values.Add("replication", replication)
  23. }
  24. if collection != "" {
  25. values.Add("collection", collection)
  26. }
  27. if ttl != "" {
  28. values.Add("ttl", ttl)
  29. }
  30. jsonBlob, err := util.Post("http://"+server+"/dir/assign", values)
  31. glog.V(2).Info("assign result :", string(jsonBlob))
  32. if err != nil {
  33. return nil, err
  34. }
  35. var ret AssignResult
  36. err = json.Unmarshal(jsonBlob, &ret)
  37. if err != nil {
  38. return nil, fmt.Errorf("JSON unmarshal error:%v, json:%s", err, string(jsonBlob))
  39. }
  40. if ret.Count <= 0 {
  41. return nil, errors.New(ret.Error)
  42. }
  43. return &ret, nil
  44. }