github.com/rawahars/moby@v24.0.4+incompatible/libnetwork/diagnostic/types.go (about)

     1  package diagnostic
     2  
     3  import "fmt"
     4  
     5  // StringInterface interface that has to be implemented by messages
     6  type StringInterface interface {
     7  	String() string
     8  }
     9  
    10  // CommandSucceed creates a success message
    11  func CommandSucceed(result StringInterface) *HTTPResult {
    12  	return &HTTPResult{
    13  		Message: "OK",
    14  		Details: result,
    15  	}
    16  }
    17  
    18  // FailCommand creates a failure message with error
    19  func FailCommand(err error) *HTTPResult {
    20  	return &HTTPResult{
    21  		Message: "FAIL",
    22  		Details: &ErrorCmd{Error: err.Error()},
    23  	}
    24  }
    25  
    26  // WrongCommand creates a wrong command response
    27  func WrongCommand(message, usage string) *HTTPResult {
    28  	return &HTTPResult{
    29  		Message: message,
    30  		Details: &UsageCmd{Usage: usage},
    31  	}
    32  }
    33  
    34  // HTTPResult Diagnostic Server HTTP result operation
    35  type HTTPResult struct {
    36  	Message string          `json:"message"`
    37  	Details StringInterface `json:"details"`
    38  }
    39  
    40  func (h *HTTPResult) String() string {
    41  	rsp := h.Message
    42  	if h.Details != nil {
    43  		rsp += "\n" + h.Details.String()
    44  	}
    45  	return rsp
    46  }
    47  
    48  // UsageCmd command with usage field
    49  type UsageCmd struct {
    50  	Usage string `json:"usage"`
    51  }
    52  
    53  func (u *UsageCmd) String() string {
    54  	return "Usage: " + u.Usage
    55  }
    56  
    57  // StringCmd command with info string
    58  type StringCmd struct {
    59  	Info string `json:"info"`
    60  }
    61  
    62  func (s *StringCmd) String() string {
    63  	return s.Info
    64  }
    65  
    66  // ErrorCmd command with error
    67  type ErrorCmd struct {
    68  	Error string `json:"error"`
    69  }
    70  
    71  func (e *ErrorCmd) String() string {
    72  	return "Error: " + e.Error
    73  }
    74  
    75  // TableObj network db table object
    76  type TableObj struct {
    77  	Length   int               `json:"size"`
    78  	Elements []StringInterface `json:"entries"`
    79  }
    80  
    81  func (t *TableObj) String() string {
    82  	output := fmt.Sprintf("total entries: %d\n", t.Length)
    83  	for _, e := range t.Elements {
    84  		output += e.String()
    85  	}
    86  	return output
    87  }
    88  
    89  // PeerEntryObj entry in the networkdb peer table
    90  type PeerEntryObj struct {
    91  	Index int    `json:"-"`
    92  	Name  string `json:"-=name"`
    93  	IP    string `json:"ip"`
    94  }
    95  
    96  func (p *PeerEntryObj) String() string {
    97  	return fmt.Sprintf("%d) %s -> %s\n", p.Index, p.Name, p.IP)
    98  }
    99  
   100  // TableEntryObj network db table entry object
   101  type TableEntryObj struct {
   102  	Index int    `json:"-"`
   103  	Key   string `json:"key"`
   104  	Value string `json:"value"`
   105  	Owner string `json:"owner"`
   106  }
   107  
   108  func (t *TableEntryObj) String() string {
   109  	return fmt.Sprintf("%d) k:`%s` -> v:`%s` owner:`%s`\n", t.Index, t.Key, t.Value, t.Owner)
   110  }
   111  
   112  // TableEndpointsResult fully typed message for proper unmarshaling on the client side
   113  type TableEndpointsResult struct {
   114  	TableObj
   115  	Elements []TableEntryObj `json:"entries"`
   116  }
   117  
   118  // TablePeersResult fully typed message for proper unmarshaling on the client side
   119  type TablePeersResult struct {
   120  	TableObj
   121  	Elements []PeerEntryObj `json:"entries"`
   122  }
   123  
   124  // NetworkStatsResult network db stats related to entries and queue len for a network
   125  type NetworkStatsResult struct {
   126  	Entries  int `json:"entries"`
   127  	QueueLen int `jsoin:"qlen"`
   128  }
   129  
   130  func (n *NetworkStatsResult) String() string {
   131  	return fmt.Sprintf("entries: %d, qlen: %d\n", n.Entries, n.QueueLen)
   132  }