github.com/gravitational/teleport/api@v0.0.0-20240507183017-3110591cbafc/types/remotecluster.go (about)

     1  /*
     2  Copyright 2020 Gravitational, Inc.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package types
    18  
    19  import (
    20  	"fmt"
    21  	"time"
    22  
    23  	"github.com/gravitational/trace"
    24  
    25  	"github.com/gravitational/teleport/api/utils"
    26  )
    27  
    28  // RemoteCluster represents a remote cluster that has connected via reverse tunnel
    29  // to this cluster
    30  type RemoteCluster interface {
    31  	// Resource provides common resource properties
    32  	Resource
    33  	// GetConnectionStatus returns connection status
    34  	GetConnectionStatus() string
    35  	// SetConnectionStatus sets connection  status
    36  	SetConnectionStatus(string)
    37  
    38  	// GetLastHeartbeat returns last heartbeat of the cluster
    39  	GetLastHeartbeat() time.Time
    40  	// SetLastHeartbeat sets last heartbeat of the cluster
    41  	SetLastHeartbeat(t time.Time)
    42  
    43  	// SetMetadata sets remote cluster metatada
    44  	SetMetadata(Metadata)
    45  
    46  	// Clone performs a deep copy.
    47  	Clone() RemoteCluster
    48  
    49  	// GetLabel retrieves the label with the provided key. If not found value
    50  	// will be empty and ok will be false.
    51  	GetLabel(key string) (value string, ok bool)
    52  
    53  	// GetAllLabels returns all labels for the remote cluster
    54  	GetAllLabels() map[string]string
    55  }
    56  
    57  // NewRemoteCluster is a convenience way to create a RemoteCluster resource.
    58  func NewRemoteCluster(name string) (RemoteCluster, error) {
    59  	c := &RemoteClusterV3{
    60  		Metadata: Metadata{
    61  			Name: name,
    62  		},
    63  	}
    64  	if err := c.CheckAndSetDefaults(); err != nil {
    65  		return nil, trace.Wrap(err)
    66  	}
    67  	return c, nil
    68  }
    69  
    70  // GetVersion returns resource version
    71  func (c *RemoteClusterV3) GetVersion() string {
    72  	return c.Version
    73  }
    74  
    75  // GetKind returns resource kind
    76  func (c *RemoteClusterV3) GetKind() string {
    77  	return c.Kind
    78  }
    79  
    80  // GetSubKind returns resource sub kind
    81  func (c *RemoteClusterV3) GetSubKind() string {
    82  	return c.SubKind
    83  }
    84  
    85  // SetSubKind sets resource subkind
    86  func (c *RemoteClusterV3) SetSubKind(s string) {
    87  	c.SubKind = s
    88  }
    89  
    90  // GetResourceID returns resource ID
    91  func (c *RemoteClusterV3) GetResourceID() int64 {
    92  	return c.Metadata.ID
    93  }
    94  
    95  // SetResourceID sets resource ID
    96  func (c *RemoteClusterV3) SetResourceID(id int64) {
    97  	c.Metadata.ID = id
    98  }
    99  
   100  // GetRevision returns the revision
   101  func (c *RemoteClusterV3) GetRevision() string {
   102  	return c.Metadata.GetRevision()
   103  }
   104  
   105  // SetRevision sets the revision
   106  func (c *RemoteClusterV3) SetRevision(rev string) {
   107  	c.Metadata.SetRevision(rev)
   108  }
   109  
   110  // setStaticFields sets static resource header and metadata fields.
   111  func (c *RemoteClusterV3) setStaticFields() {
   112  	c.Kind = KindRemoteCluster
   113  	c.Version = V3
   114  }
   115  
   116  // CheckAndSetDefaults checks and sets default values
   117  func (c *RemoteClusterV3) CheckAndSetDefaults() error {
   118  	c.setStaticFields()
   119  	if err := c.Metadata.CheckAndSetDefaults(); err != nil {
   120  		return trace.Wrap(err)
   121  	}
   122  	return nil
   123  }
   124  
   125  // GetLastHeartbeat returns last heartbeat of the cluster
   126  func (c *RemoteClusterV3) GetLastHeartbeat() time.Time {
   127  	return c.Status.LastHeartbeat
   128  }
   129  
   130  // SetLastHeartbeat sets last heartbeat of the cluster
   131  func (c *RemoteClusterV3) SetLastHeartbeat(t time.Time) {
   132  	c.Status.LastHeartbeat = t
   133  }
   134  
   135  // Clone performs a deep copy.
   136  func (c *RemoteClusterV3) Clone() RemoteCluster {
   137  	return utils.CloneProtoMsg(c)
   138  }
   139  
   140  // GetConnectionStatus returns connection status
   141  func (c *RemoteClusterV3) GetConnectionStatus() string {
   142  	return c.Status.Connection
   143  }
   144  
   145  // SetConnectionStatus sets connection  status
   146  func (c *RemoteClusterV3) SetConnectionStatus(status string) {
   147  	c.Status.Connection = status
   148  }
   149  
   150  // GetMetadata returns object metadata
   151  func (c *RemoteClusterV3) GetMetadata() Metadata {
   152  	return c.Metadata
   153  }
   154  
   155  // SetMetadata sets remote cluster metatada
   156  func (c *RemoteClusterV3) SetMetadata(meta Metadata) {
   157  	c.Metadata = meta
   158  }
   159  
   160  // SetExpiry sets expiry time for the object
   161  func (c *RemoteClusterV3) SetExpiry(expires time.Time) {
   162  	c.Metadata.SetExpiry(expires)
   163  }
   164  
   165  // Expiry returns object expiry setting
   166  func (c *RemoteClusterV3) Expiry() time.Time {
   167  	return c.Metadata.Expiry()
   168  }
   169  
   170  // GetName returns the name of the RemoteCluster.
   171  func (c *RemoteClusterV3) GetName() string {
   172  	return c.Metadata.Name
   173  }
   174  
   175  // SetName sets the name of the RemoteCluster.
   176  func (c *RemoteClusterV3) SetName(e string) {
   177  	c.Metadata.Name = e
   178  }
   179  
   180  // String represents a human readable version of remote cluster settings.
   181  func (c *RemoteClusterV3) String() string {
   182  	return fmt.Sprintf("RemoteCluster(%v, %v)", c.Metadata.Name, c.Status.Connection)
   183  }
   184  
   185  // GetLabel retrieves the label with the provided key. If not found value
   186  // will be empty and ok will be false.
   187  func (c *RemoteClusterV3) GetLabel(key string) (value string, ok bool) {
   188  	value, ok = c.Metadata.Labels[key]
   189  	return value, ok
   190  }
   191  
   192  // GetAllLabels returns all labels for the remote cluster. Remote clusters only
   193  // have static labels.
   194  func (c *RemoteClusterV3) GetAllLabels() map[string]string {
   195  	return c.Metadata.Labels
   196  }