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

     1  /*
     2  Copyright 2017 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  // ClusterName defines the name of the cluster. This is a configuration
    29  // resource, never create more than one instance of it.
    30  type ClusterName interface {
    31  	// Resource provides common resource properties.
    32  	Resource
    33  
    34  	// SetClusterName sets the name of the cluster.
    35  	SetClusterName(string)
    36  	// GetClusterName gets the name of the cluster.
    37  	GetClusterName() string
    38  
    39  	// SetClusterID sets the ID of the cluster.
    40  	SetClusterID(string)
    41  	// GetClusterID gets the ID of the cluster.
    42  	GetClusterID() string
    43  
    44  	// Clone performs a deep copy.
    45  	Clone() ClusterName
    46  }
    47  
    48  // NewClusterName is a convenience wrapper to create a ClusterName resource.
    49  func NewClusterName(spec ClusterNameSpecV2) (ClusterName, error) {
    50  	cn := &ClusterNameV2{Spec: spec}
    51  	if err := cn.CheckAndSetDefaults(); err != nil {
    52  		return nil, trace.Wrap(err)
    53  	}
    54  	return cn, nil
    55  }
    56  
    57  // GetVersion returns resource version
    58  func (c *ClusterNameV2) GetVersion() string {
    59  	return c.Version
    60  }
    61  
    62  // GetKind returns resource kind
    63  func (c *ClusterNameV2) GetKind() string {
    64  	return c.Kind
    65  }
    66  
    67  // GetSubKind returns resource sub kind
    68  func (c *ClusterNameV2) GetSubKind() string {
    69  	return c.SubKind
    70  }
    71  
    72  // SetSubKind sets resource subkind
    73  func (c *ClusterNameV2) SetSubKind(sk string) {
    74  	c.SubKind = sk
    75  }
    76  
    77  // GetResourceID returns resource ID
    78  func (c *ClusterNameV2) GetResourceID() int64 {
    79  	return c.Metadata.ID
    80  }
    81  
    82  // SetResourceID sets resource ID
    83  func (c *ClusterNameV2) SetResourceID(id int64) {
    84  	c.Metadata.ID = id
    85  }
    86  
    87  // GetRevision returns the revision
    88  func (c *ClusterNameV2) GetRevision() string {
    89  	return c.Metadata.GetRevision()
    90  }
    91  
    92  // SetRevision sets the revision
    93  func (c *ClusterNameV2) SetRevision(rev string) {
    94  	c.Metadata.SetRevision(rev)
    95  }
    96  
    97  // GetName returns the name of the cluster.
    98  func (c *ClusterNameV2) GetName() string {
    99  	return c.Metadata.Name
   100  }
   101  
   102  // SetName sets the name of the cluster.
   103  func (c *ClusterNameV2) SetName(e string) {
   104  	c.Metadata.Name = e
   105  }
   106  
   107  // Expiry returns object expiry setting
   108  func (c *ClusterNameV2) Expiry() time.Time {
   109  	return c.Metadata.Expiry()
   110  }
   111  
   112  // SetExpiry sets expiry time for the object
   113  func (c *ClusterNameV2) SetExpiry(expires time.Time) {
   114  	c.Metadata.SetExpiry(expires)
   115  }
   116  
   117  // GetMetadata returns object metadata
   118  func (c *ClusterNameV2) GetMetadata() Metadata {
   119  	return c.Metadata
   120  }
   121  
   122  // SetClusterName sets the name of the cluster.
   123  func (c *ClusterNameV2) SetClusterName(n string) {
   124  	c.Spec.ClusterName = n
   125  }
   126  
   127  // GetClusterName gets the name of the cluster.
   128  func (c *ClusterNameV2) GetClusterName() string {
   129  	return c.Spec.ClusterName
   130  }
   131  
   132  // SetClusterID sets the ID of the cluster.
   133  func (c *ClusterNameV2) SetClusterID(id string) {
   134  	c.Spec.ClusterID = id
   135  }
   136  
   137  // GetClusterID gets the ID of the cluster.
   138  func (c *ClusterNameV2) GetClusterID() string {
   139  	return c.Spec.ClusterID
   140  }
   141  
   142  // Clone performs a deep copy.
   143  func (c *ClusterNameV2) Clone() ClusterName {
   144  	return utils.CloneProtoMsg(c)
   145  }
   146  
   147  // setStaticFields sets static resource header and metadata fields.
   148  func (c *ClusterNameV2) setStaticFields() {
   149  	c.Kind = KindClusterName
   150  	c.Version = V2
   151  	c.Metadata.Name = MetaNameClusterName
   152  }
   153  
   154  // CheckAndSetDefaults checks validity of all parameters and sets defaults.
   155  func (c *ClusterNameV2) CheckAndSetDefaults() error {
   156  	c.setStaticFields()
   157  	if err := c.Metadata.CheckAndSetDefaults(); err != nil {
   158  		return trace.Wrap(err)
   159  	}
   160  
   161  	if c.Spec.ClusterName == "" {
   162  		return trace.BadParameter("cluster name is required")
   163  	}
   164  	if c.Spec.ClusterID == "" {
   165  		return trace.BadParameter("cluster ID is required")
   166  	}
   167  	return nil
   168  }
   169  
   170  // String represents a human readable version of the cluster name.
   171  func (c *ClusterNameV2) String() string {
   172  	return fmt.Sprintf("ClusterName(%v, ID=%v)", c.Spec.ClusterName, c.Spec.ClusterID)
   173  }