github.com/rajatvaryani/mattermost-server@v5.11.1+incompatible/model/cluster_discovery.go (about)

     1  // Copyright (c) 2017-present Mattermost, Inc. All Rights Reserved.
     2  // See License.txt for license information.
     3  
     4  package model
     5  
     6  import (
     7  	"encoding/json"
     8  	"io"
     9  	"net/http"
    10  	"os"
    11  )
    12  
    13  const (
    14  	CDS_OFFLINE_AFTER_MILLIS = 1000 * 60 * 30 // 30 minutes
    15  	CDS_TYPE_APP             = "mattermost_app"
    16  )
    17  
    18  type ClusterDiscovery struct {
    19  	Id          string `json:"id"`
    20  	Type        string `json:"type"`
    21  	ClusterName string `json:"cluster_name"`
    22  	Hostname    string `json:"hostname"`
    23  	GossipPort  int32  `json:"gossip_port"`
    24  	Port        int32  `json:"port"`
    25  	CreateAt    int64  `json:"create_at"`
    26  	LastPingAt  int64  `json:"last_ping_at"`
    27  }
    28  
    29  func (o *ClusterDiscovery) PreSave() {
    30  	if o.Id == "" {
    31  		o.Id = NewId()
    32  	}
    33  
    34  	if o.CreateAt == 0 {
    35  		o.CreateAt = GetMillis()
    36  		o.LastPingAt = o.CreateAt
    37  	}
    38  }
    39  
    40  func (o *ClusterDiscovery) AutoFillHostname() {
    41  	// attempt to set the hostname from the OS
    42  	if len(o.Hostname) == 0 {
    43  		if hn, err := os.Hostname(); err == nil {
    44  			o.Hostname = hn
    45  		}
    46  	}
    47  }
    48  
    49  func (o *ClusterDiscovery) AutoFillIpAddress() {
    50  	// attempt to set the hostname to the first non-local IP address
    51  	if len(o.Hostname) == 0 {
    52  		o.Hostname = GetServerIpAddress()
    53  	}
    54  }
    55  
    56  func (o *ClusterDiscovery) IsEqual(in *ClusterDiscovery) bool {
    57  	if in == nil {
    58  		return false
    59  	}
    60  
    61  	if o.Type != in.Type {
    62  		return false
    63  	}
    64  
    65  	if o.ClusterName != in.ClusterName {
    66  		return false
    67  	}
    68  
    69  	if o.Hostname != in.Hostname {
    70  		return false
    71  	}
    72  
    73  	return true
    74  }
    75  
    76  func FilterClusterDiscovery(vs []*ClusterDiscovery, f func(*ClusterDiscovery) bool) []*ClusterDiscovery {
    77  	copy := make([]*ClusterDiscovery, 0)
    78  	for _, v := range vs {
    79  		if f(v) {
    80  			copy = append(copy, v)
    81  		}
    82  	}
    83  
    84  	return copy
    85  }
    86  
    87  func (o *ClusterDiscovery) IsValid() *AppError {
    88  	if len(o.Id) != 26 {
    89  		return NewAppError("ClusterDiscovery.IsValid", "model.cluster.is_valid.id.app_error", nil, "", http.StatusBadRequest)
    90  	}
    91  
    92  	if len(o.ClusterName) == 0 {
    93  		return NewAppError("ClusterDiscovery.IsValid", "model.cluster.is_valid.name.app_error", nil, "", http.StatusBadRequest)
    94  	}
    95  
    96  	if len(o.Type) == 0 {
    97  		return NewAppError("ClusterDiscovery.IsValid", "model.cluster.is_valid.type.app_error", nil, "", http.StatusBadRequest)
    98  	}
    99  
   100  	if len(o.Hostname) == 0 {
   101  		return NewAppError("ClusterDiscovery.IsValid", "model.cluster.is_valid.hostname.app_error", nil, "", http.StatusBadRequest)
   102  	}
   103  
   104  	if o.CreateAt == 0 {
   105  		return NewAppError("ClusterDiscovery.IsValid", "model.cluster.is_valid.create_at.app_error", nil, "", http.StatusBadRequest)
   106  	}
   107  
   108  	if o.LastPingAt == 0 {
   109  		return NewAppError("ClusterDiscovery.IsValid", "model.cluster.is_valid.last_ping_at.app_error", nil, "", http.StatusBadRequest)
   110  	}
   111  
   112  	return nil
   113  }
   114  
   115  func (o *ClusterDiscovery) ToJson() string {
   116  	b, err := json.Marshal(o)
   117  	if err != nil {
   118  		return ""
   119  	}
   120  
   121  	return string(b)
   122  }
   123  
   124  func ClusterDiscoveryFromJson(data io.Reader) *ClusterDiscovery {
   125  	decoder := json.NewDecoder(data)
   126  	var me ClusterDiscovery
   127  	err := decoder.Decode(&me)
   128  	if err == nil {
   129  		return &me
   130  	}
   131  
   132  	return nil
   133  }