aboutsummaryrefslogtreecommitdiff
path: root/weed/operation/assign_file_id.go
blob: a893e836b4d96843784821caa4a1931695725e61 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package operation

import (
	"encoding/json"
	"errors"
	"fmt"
	"net/url"
	"strconv"

	"github.com/chrislusf/seaweedfs/weed/glog"
	"github.com/chrislusf/seaweedfs/weed/util"
)

type AssignResult struct {
	Fid       string `json:"fid,omitempty"`
	Url       string `json:"url,omitempty"`
	PublicUrl string `json:"publicUrl,omitempty"`
	Count     uint64 `json:"count,omitempty"`
	Error     string `json:"error,omitempty"`
}

/*
options params meaning:
options[0] main data Center
options[1] main rack
options[2] main data node
*/
func Assign(server string, count uint64, replication string, collection string, ttl string, options ...string) (*AssignResult, error) {
	values := make(url.Values)
	values.Add("count", strconv.FormatUint(count, 10))
	if replication != "" {
		values.Add("replication", replication)
	}
	if collection != "" {
		values.Add("collection", collection)
	}
	if ttl != "" {
		values.Add("ttl", ttl)
	}

	var dataCenter, rack, dataNode string
	switch len(options) {
	case 1:
		dataCenter = options[0]
	case 2:
		dataCenter = options[0]
		rack = options[1]
	case 3:
		dataCenter = options[0]
		rack = options[1]
		dataNode = options[2]
	default:
	}

	if dataCenter != "" {
		values.Add("dataCenter", dataCenter)
	}
	if rack != "" {
		values.Add("rack", rack)
	}
	if dataNode != "" {
		values.Add("dataNode", dataNode)
	}

	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("/dir/assign result JSON unmarshal error:%v, json:%s", err, string(jsonBlob))
	}
	if ret.Count <= 0 {
		return nil, errors.New(ret.Error)
	}
	return &ret, nil
}