github.com/yasker/longhorn-engine@v0.0.0-20160621014712-6ed6cfca0729/frontend/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  )
     9  
    10  type Volume struct {
    11  	client.Resource
    12  	Name string `json:"name"`
    13  }
    14  
    15  type ReadInput struct {
    16  	client.Resource
    17  	Offset int64 `json:"offset"`
    18  	Length int64 `json:"length"`
    19  }
    20  
    21  type ReadOutput struct {
    22  	client.Resource
    23  	Data string `json:"data"`
    24  }
    25  
    26  type WriteInput struct {
    27  	client.Resource
    28  	Offset int64  `json:"offset"`
    29  	Length int    `json:"length"`
    30  	Data   string `json:"data"`
    31  }
    32  
    33  type WriteOutput struct {
    34  	client.Resource
    35  }
    36  
    37  func NewVolume(context *api.ApiContext, name string) *Volume {
    38  	v := &Volume{
    39  		Resource: client.Resource{
    40  			Id:      EncodeID(name),
    41  			Type:    "volume",
    42  			Actions: map[string]string{},
    43  		},
    44  		Name: name,
    45  	}
    46  
    47  	v.Actions["read"] = context.UrlBuilder.ActionLink(v.Resource, "read")
    48  	v.Actions["write"] = context.UrlBuilder.ActionLink(v.Resource, "write")
    49  	return v
    50  }
    51  
    52  func DecodeID(id string) (string, error) {
    53  	b, err := DecodeData(id)
    54  	if err != nil {
    55  		return "", err
    56  	}
    57  	return string(b), nil
    58  }
    59  
    60  func EncodeID(id string) string {
    61  	return EncodeData([]byte(id))
    62  }
    63  func DecodeData(data string) ([]byte, error) {
    64  	b, err := base64.StdEncoding.DecodeString(data)
    65  	if err != nil {
    66  		return nil, err
    67  	}
    68  	return b, nil
    69  }
    70  
    71  func EncodeData(data []byte) string {
    72  	return base64.StdEncoding.EncodeToString(data)
    73  }
    74  
    75  func NewSchema() *client.Schemas {
    76  	schemas := &client.Schemas{}
    77  
    78  	schemas.AddType("error", client.ServerApiError{})
    79  	schemas.AddType("apiVersion", client.Resource{})
    80  	schemas.AddType("schema", client.Schema{})
    81  	schemas.AddType("readInput", ReadInput{})
    82  	schemas.AddType("readOutput", ReadOutput{})
    83  	schemas.AddType("writeInput", WriteInput{})
    84  	schemas.AddType("writeOutput", WriteOutput{})
    85  
    86  	volumes := schemas.AddType("volume", Volume{})
    87  	volumes.ResourceActions = map[string]client.Action{
    88  		"read": client.Action{
    89  			Input:  "readInput",
    90  			Output: "readOutput",
    91  		},
    92  		"write": client.Action{
    93  			Input:  "writeInput",
    94  			Output: "writeOutput",
    95  		},
    96  	}
    97  
    98  	return schemas
    99  }
   100  
   101  type Server struct {
   102  	d *Device
   103  }
   104  
   105  func NewServer(d *Device) *Server {
   106  	return &Server{
   107  		d: d,
   108  	}
   109  }