github.com/yasker/longhorn-engine@v0.0.0-20160621014712-6ed6cfca0729/agent/controller/rest/model.go (about)

     1  package rest
     2  
     3  import (
     4  	"github.com/rancher/go-rancher/api"
     5  	"github.com/rancher/go-rancher/client"
     6  	rrest "github.com/rancher/longhorn/agent/replica/rest"
     7  )
     8  
     9  type snapshot struct {
    10  	client.Resource
    11  	Name string `json:"name"`
    12  }
    13  
    14  type snapshotCollection struct {
    15  	client.Collection
    16  	Data []snapshot `json:"data"`
    17  }
    18  
    19  type status struct {
    20  	client.Resource
    21  	State   string `json:"state,omitempty"`
    22  	Message string `json:"message,omitempty"`
    23  }
    24  
    25  type backupInput struct {
    26  	UUID         string             `json:"uuid,omitempty"`
    27  	BackupTarget rrest.BackupTarget `json:"backupTarget,omitempty"`
    28  }
    29  
    30  type locationInput struct {
    31  	UUID         string             `json:"uuid,omitempty"`
    32  	Location     string             `json:"location,omitempty"`
    33  	BackupTarget rrest.BackupTarget `json:"backupTarget,omitempty"`
    34  }
    35  
    36  type revertInput struct {
    37  	Name string `json:"name,omitempty"`
    38  }
    39  
    40  type volume struct {
    41  	client.Resource
    42  	Name string `json:"name,omitempty"`
    43  }
    44  
    45  func newSnapshot(context *api.ApiContext, name string) *snapshot {
    46  	snapshot := &snapshot{
    47  		Resource: client.Resource{
    48  			Id:      name,
    49  			Type:    "snapshot",
    50  			Actions: map[string]string{},
    51  		},
    52  		Name: name,
    53  	}
    54  
    55  	return snapshot
    56  }
    57  
    58  func newStatus(id string, state, message, resourceType string) *status {
    59  	return &status{
    60  		Resource: client.Resource{Id: id, Type: resourceType},
    61  		State:    state,
    62  		Message:  message,
    63  	}
    64  }
    65  
    66  func newSchema() *client.Schemas {
    67  	schemas := &client.Schemas{}
    68  
    69  	schemas.AddType("error", client.ServerApiError{})
    70  	schemas.AddType("apiVersion", client.Resource{})
    71  	schemas.AddType("schema", client.Schema{})
    72  
    73  	schemas.AddType("revertToSnapshotInput", revertInput{})
    74  	schemas.AddType("locationInput", locationInput{})
    75  	schemas.AddType("backupInput", backupInput{})
    76  
    77  	volume := schemas.AddType("volume", volume{})
    78  	volume.CollectionMethods = []string{"GET"}
    79  	volume.ResourceMethods = []string{"GET"}
    80  	volume.ResourceActions = map[string]client.Action{
    81  		"reverttosnapshot": client.Action{
    82  			Input:  "revertToSnapshotInput",
    83  			Output: "volume",
    84  		},
    85  		"restoreFromBackup": client.Action{
    86  			Input:  "locationInput",
    87  			Output: "restoreStatus",
    88  		},
    89  	}
    90  
    91  	snapshot := schemas.AddType("snapshot", snapshot{})
    92  	snapshot.CollectionMethods = []string{"GET", "POST"}
    93  	snapshot.ResourceMethods = []string{"GET", "PUT", "DELETE"}
    94  	snapshot.ResourceActions = map[string]client.Action{
    95  		"backup": client.Action{
    96  			Input:  "backupInput",
    97  			Output: "backupStatus",
    98  		},
    99  		"removeBackup": client.Action{
   100  			Input: "locationInput",
   101  		},
   102  	}
   103  
   104  	restoreStatus := schemas.AddType("restorestatus", status{})
   105  	restoreStatus.ResourceMethods = []string{"GET"}
   106  
   107  	backupStatus := schemas.AddType("backupstatus", status{})
   108  	backupStatus.ResourceMethods = []string{"GET"}
   109  
   110  	return schemas
   111  }