aboutsummaryrefslogtreecommitdiff
path: root/weed/operation
diff options
context:
space:
mode:
Diffstat (limited to 'weed/operation')
-rw-r--r--weed/operation/allocate_volume.go32
-rw-r--r--weed/operation/delete_content.go16
-rw-r--r--weed/operation/lookup_volume_id.go38
-rw-r--r--weed/operation/upload_content.go47
4 files changed, 133 insertions, 0 deletions
diff --git a/weed/operation/allocate_volume.go b/weed/operation/allocate_volume.go
new file mode 100644
index 000000000..1e016abf7
--- /dev/null
+++ b/weed/operation/allocate_volume.go
@@ -0,0 +1,32 @@
+package operation
+
+import (
+ "encoding/json"
+ "errors"
+ "net/url"
+ "weed/storage"
+ "weed/topology"
+ "weed/util"
+)
+
+type AllocateVolumeResult struct {
+ Error string
+}
+
+func AllocateVolume(dn *topology.DataNode, vid storage.VolumeId, repType storage.ReplicationType) error {
+ values := make(url.Values)
+ values.Add("volume", vid.String())
+ values.Add("replicationType", repType.String())
+ jsonBlob, err := util.Post("http://"+dn.Url()+"/admin/assign_volume", values)
+ if err != nil {
+ return err
+ }
+ var ret AllocateVolumeResult
+ if err := json.Unmarshal(jsonBlob, &ret); err != nil {
+ return err
+ }
+ if ret.Error != "" {
+ return errors.New(ret.Error)
+ }
+ return nil
+}
diff --git a/weed/operation/delete_content.go b/weed/operation/delete_content.go
new file mode 100644
index 000000000..2bdb49651
--- /dev/null
+++ b/weed/operation/delete_content.go
@@ -0,0 +1,16 @@
+package operation
+
+import (
+ "log"
+ "net/http"
+)
+
+func Delete(url string) error {
+ req, err := http.NewRequest("DELETE", url, nil)
+ if err != nil {
+ log.Println("failing to delete", url)
+ return err
+ }
+ _, err = http.DefaultClient.Do(req)
+ return err
+}
diff --git a/weed/operation/lookup_volume_id.go b/weed/operation/lookup_volume_id.go
new file mode 100644
index 000000000..ccfca67c8
--- /dev/null
+++ b/weed/operation/lookup_volume_id.go
@@ -0,0 +1,38 @@
+package operation
+
+import (
+ "encoding/json"
+ "errors"
+ _ "fmt"
+ "net/url"
+ "weed/storage"
+ "weed/util"
+)
+
+type Location struct {
+ Url string "url"
+ PublicUrl string "publicUrl"
+}
+type LookupResult struct {
+ Locations []Location "locations"
+ Error string "error"
+}
+
+//TODO: Add a caching for vid here
+func Lookup(server string, vid storage.VolumeId) (*LookupResult, error) {
+ values := make(url.Values)
+ values.Add("volumeId", vid.String())
+ jsonBlob, err := util.Post("http://"+server+"/dir/lookup", values)
+ if err != nil {
+ return nil, err
+ }
+ var ret LookupResult
+ err = json.Unmarshal(jsonBlob, &ret)
+ if err != nil {
+ return nil, err
+ }
+ if ret.Error != "" {
+ return nil, errors.New(ret.Error)
+ }
+ return &ret, nil
+}
diff --git a/weed/operation/upload_content.go b/weed/operation/upload_content.go
new file mode 100644
index 000000000..0bdb697da
--- /dev/null
+++ b/weed/operation/upload_content.go
@@ -0,0 +1,47 @@
+package operation
+
+import (
+ "bytes"
+ "encoding/json"
+ "errors"
+ _ "fmt"
+ "io"
+ "io/ioutil"
+ "log"
+ "mime/multipart"
+ "net/http"
+)
+
+type UploadResult struct {
+ Size int
+ Error string
+}
+
+func Upload(uploadUrl string, filename string, reader io.Reader) (*UploadResult, error) {
+ body_buf := bytes.NewBufferString("")
+ body_writer := multipart.NewWriter(body_buf)
+ file_writer, err := body_writer.CreateFormFile("file", filename)
+ io.Copy(file_writer, reader)
+ content_type := body_writer.FormDataContentType()
+ body_writer.Close()
+ resp, err := http.Post(uploadUrl, content_type, body_buf)
+ if err != nil {
+ log.Println("failing to upload to", uploadUrl)
+ return nil, err
+ }
+ defer resp.Body.Close()
+ resp_body, err := ioutil.ReadAll(resp.Body)
+ if err != nil {
+ return nil, err
+ }
+ var ret UploadResult
+ err = json.Unmarshal(resp_body, &ret)
+ if err != nil {
+ log.Println("failing to read upload resonse", uploadUrl, resp_body)
+ return nil, err
+ }
+ if ret.Error != "" {
+ return nil, errors.New(ret.Error)
+ }
+ return &ret, nil
+}