github.com/rancher/longhorn-engine@v0.6.2/replica/rest/model.go (about)

     1  package rest
     2  
     3  import (
     4  	"strconv"
     5  
     6  	"github.com/rancher/go-rancher/api"
     7  	"github.com/rancher/go-rancher/client"
     8  
     9  	"github.com/longhorn/longhorn-engine/replica"
    10  )
    11  
    12  type Replica struct {
    13  	client.Resource
    14  	Dirty           bool                        `json:"dirty"`
    15  	Rebuilding      bool                        `json:"rebuilding"`
    16  	Head            string                      `json:"head"`
    17  	Parent          string                      `json:"parent"`
    18  	Size            string                      `json:"size"`
    19  	SectorSize      int64                       `json:"sectorSize,string"`
    20  	BackingFile     string                      `json:"backingFile"`
    21  	State           string                      `json:"state"`
    22  	Chain           []string                    `json:"chain"`
    23  	Disks           map[string]replica.DiskInfo `json:"disks"`
    24  	RemainSnapshots int                         `json:"remainsnapshots"`
    25  	RevisionCounter int64                       `json:"revisioncounter,string"`
    26  }
    27  
    28  func NewReplica(context *api.ApiContext, state replica.State, info replica.Info, rep *replica.Replica) *Replica {
    29  	r := &Replica{
    30  		Resource: client.Resource{
    31  			Type:    "replica",
    32  			Id:      "1",
    33  			Actions: map[string]string{},
    34  		},
    35  	}
    36  
    37  	r.State = string(state)
    38  
    39  	actions := map[string]bool{}
    40  
    41  	switch state {
    42  	case replica.Initial:
    43  	case replica.Open:
    44  	case replica.Closed:
    45  	case replica.Dirty:
    46  	case replica.Rebuilding:
    47  	case replica.Error:
    48  	}
    49  
    50  	for action := range actions {
    51  		r.Actions[action] = context.UrlBuilder.ActionLink(r.Resource, action)
    52  	}
    53  
    54  	r.Dirty = info.Dirty
    55  	r.Rebuilding = info.Rebuilding
    56  	r.Head = info.Head
    57  	r.Parent = info.Parent
    58  	r.SectorSize = info.SectorSize
    59  	r.Size = strconv.FormatInt(info.Size, 10)
    60  	r.BackingFile = info.BackingFileName
    61  
    62  	if rep != nil {
    63  		r.Chain, _ = rep.DisplayChain()
    64  		r.Disks = rep.ListDisks()
    65  		r.RemainSnapshots = rep.GetRemainSnapshotCounts()
    66  		r.RevisionCounter = rep.GetRevisionCounter()
    67  	}
    68  
    69  	return r
    70  }
    71  
    72  func NewSchema() *client.Schemas {
    73  	schemas := &client.Schemas{}
    74  
    75  	schemas.AddType("error", client.ServerApiError{})
    76  	schemas.AddType("apiVersion", client.Resource{})
    77  	schemas.AddType("schema", client.Schema{})
    78  	replica := schemas.AddType("replica", Replica{})
    79  
    80  	replica.ResourceMethods = []string{"GET", "DELETE"}
    81  	replica.ResourceActions = map[string]client.Action{}
    82  
    83  	return schemas
    84  }
    85  
    86  type Server struct {
    87  	s *replica.Server
    88  }
    89  
    90  func NewServer(s *replica.Server) *Server {
    91  	return &Server{
    92  		s: s,
    93  	}
    94  }