github.com/yasker/longhorn-engine@v0.0.0-20160621014712-6ed6cfca0729/controller/rest/model.go (about) 1 package rest 2 3 import ( 4 "encoding/base64" 5 6 "github.com/rancher/go-rancher/api" 7 "github.com/rancher/go-rancher/client" 8 "github.com/rancher/longhorn/controller" 9 "github.com/rancher/longhorn/types" 10 ) 11 12 type Replica struct { 13 client.Resource 14 Address string `json:"address"` 15 Mode string `json:"mode"` 16 } 17 18 type Volume struct { 19 client.Resource 20 Name string `json:"name"` 21 ReplicaCount int `json:"replicaCount"` 22 } 23 24 type VolumeCollection struct { 25 client.Collection 26 Data []Volume `json:"data"` 27 } 28 29 type ReplicaCollection struct { 30 client.Collection 31 Data []Replica `json:"data"` 32 } 33 34 type StartInput struct { 35 client.Resource 36 Replicas []string `json:"replicas"` 37 } 38 39 type SnapshotOutput struct { 40 client.Resource 41 } 42 43 type SnapshotInput struct { 44 client.Resource 45 Name string `json:"name"` 46 } 47 48 type RevertInput struct { 49 client.Resource 50 Name string `json:"name"` 51 } 52 53 type JournalInput struct { 54 client.Resource 55 Limit int `json:"limit"` 56 } 57 58 func NewVolume(context *api.ApiContext, name string, replicas int) *Volume { 59 v := &Volume{ 60 Resource: client.Resource{ 61 Id: EncodeID(name), 62 Type: "volume", 63 Actions: map[string]string{}, 64 }, 65 Name: name, 66 ReplicaCount: replicas, 67 } 68 69 if replicas == 0 { 70 v.Actions["start"] = context.UrlBuilder.ActionLink(v.Resource, "start") 71 } else { 72 v.Actions["shutdown"] = context.UrlBuilder.ActionLink(v.Resource, "shutdown") 73 v.Actions["snapshot"] = context.UrlBuilder.ActionLink(v.Resource, "snapshot") 74 v.Actions["revert"] = context.UrlBuilder.ActionLink(v.Resource, "revert") 75 } 76 return v 77 } 78 79 func NewReplica(address string, mode types.Mode) *Replica { 80 return &Replica{ 81 Resource: client.Resource{ 82 Id: EncodeID(address), 83 Type: "replica", 84 }, 85 Address: address, 86 Mode: string(mode), 87 } 88 } 89 90 func DencodeID(id string) (string, error) { 91 b, err := base64.StdEncoding.DecodeString(id) 92 if err != nil { 93 return "", err 94 } 95 return string(b), nil 96 } 97 98 func EncodeID(id string) string { 99 return base64.StdEncoding.EncodeToString([]byte(id)) 100 } 101 102 func NewSchema() *client.Schemas { 103 schemas := &client.Schemas{} 104 105 schemas.AddType("error", client.ServerApiError{}) 106 schemas.AddType("apiVersion", client.Resource{}) 107 schemas.AddType("schema", client.Schema{}) 108 schemas.AddType("startInput", StartInput{}) 109 schemas.AddType("snapshotOutput", SnapshotOutput{}) 110 schemas.AddType("snapshotInput", SnapshotInput{}) 111 schemas.AddType("revertInput", RevertInput{}) 112 schemas.AddType("journalInput", JournalInput{}) 113 114 replica := schemas.AddType("replica", Replica{}) 115 replica.CollectionMethods = []string{"GET", "POST"} 116 replica.ResourceMethods = []string{"GET", "PUT"} 117 118 f := replica.ResourceFields["address"] 119 f.Create = true 120 replica.ResourceFields["address"] = f 121 122 f = replica.ResourceFields["mode"] 123 f.Update = true 124 replica.ResourceFields["mode"] = f 125 126 volumes := schemas.AddType("volume", Volume{}) 127 volumes.ResourceActions = map[string]client.Action{ 128 "revert": client.Action{ 129 Input: "revertInput", 130 Output: "volume", 131 }, 132 "start": client.Action{ 133 Input: "startInput", 134 Output: "volume", 135 }, 136 "shutdown": client.Action{ 137 Output: "volume", 138 }, 139 "snapshot": client.Action{ 140 Input: "snapshotInput", 141 Output: "snapshotOutput", 142 }, 143 } 144 145 return schemas 146 } 147 148 type Server struct { 149 c *controller.Controller 150 } 151 152 func NewServer(c *controller.Controller) *Server { 153 return &Server{ 154 c: c, 155 } 156 }