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

     1  /*
     2  Copyright 2015-2018 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  	"regexp"
    21  	"time"
    22  
    23  	"github.com/gravitational/trace"
    24  
    25  	"github.com/gravitational/teleport/api/defaults"
    26  )
    27  
    28  // NewNamespace returns new namespace
    29  func NewNamespace(name string) (Namespace, error) {
    30  	n := Namespace{
    31  		Metadata: Metadata{
    32  			Name: name,
    33  		},
    34  	}
    35  	if err := n.CheckAndSetDefaults(); err != nil {
    36  		return Namespace{}, trace.Wrap(err)
    37  	}
    38  	return n, nil
    39  }
    40  
    41  // DefaultNamespace returns the default namespace.
    42  func DefaultNamespace() Namespace {
    43  	namespace, _ := NewNamespace(defaults.Namespace)
    44  	return namespace
    45  }
    46  
    47  // setStaticFields sets static resource header and metadata fields.
    48  func (n *Namespace) setStaticFields() {
    49  	n.Kind = KindNamespace
    50  	n.Version = V2
    51  }
    52  
    53  // CheckAndSetDefaults checks validity of all parameters and sets defaults
    54  func (n *Namespace) CheckAndSetDefaults() error {
    55  	n.setStaticFields()
    56  	if err := n.Metadata.CheckAndSetDefaults(); err != nil {
    57  		return trace.Wrap(err)
    58  	}
    59  
    60  	if !IsValidNamespace(n.Metadata.Name) {
    61  		return trace.BadParameter("namespace %q is invalid", n.Metadata.Name)
    62  	}
    63  
    64  	return nil
    65  }
    66  
    67  // GetVersion returns resource version
    68  func (n *Namespace) GetVersion() string {
    69  	return n.Version
    70  }
    71  
    72  // GetKind returns resource kind
    73  func (n *Namespace) GetKind() string {
    74  	return n.Kind
    75  }
    76  
    77  // GetSubKind returns resource sub kind
    78  func (n *Namespace) GetSubKind() string {
    79  	return n.SubKind
    80  }
    81  
    82  // SetSubKind sets resource subkind
    83  func (n *Namespace) SetSubKind(sk string) {
    84  	n.SubKind = sk
    85  }
    86  
    87  // GetResourceID returns resource ID
    88  func (n *Namespace) GetResourceID() int64 {
    89  	return n.Metadata.ID
    90  }
    91  
    92  // SetResourceID sets resource ID
    93  func (n *Namespace) SetResourceID(id int64) {
    94  	n.Metadata.ID = id
    95  }
    96  
    97  // GetRevision returns the revision
    98  func (n *Namespace) GetRevision() string {
    99  	return n.Metadata.GetRevision()
   100  }
   101  
   102  // SetRevision sets the revision
   103  func (n *Namespace) SetRevision(rev string) {
   104  	n.Metadata.SetRevision(rev)
   105  }
   106  
   107  // GetName returns the name of the cluster.
   108  func (n *Namespace) GetName() string {
   109  	return n.Metadata.Name
   110  }
   111  
   112  // SetName sets the name of the cluster.
   113  func (n *Namespace) SetName(e string) {
   114  	n.Metadata.Name = e
   115  }
   116  
   117  // Expiry returns object expiry setting
   118  func (n *Namespace) Expiry() time.Time {
   119  	return n.Metadata.Expiry()
   120  }
   121  
   122  // SetExpiry sets expiry time for the object
   123  func (n *Namespace) SetExpiry(expires time.Time) {
   124  	n.Metadata.SetExpiry(expires)
   125  }
   126  
   127  // GetMetadata returns object metadata
   128  func (n *Namespace) GetMetadata() Metadata {
   129  	return n.Metadata
   130  }
   131  
   132  // SortedNamespaces sorts namespaces
   133  type SortedNamespaces []Namespace
   134  
   135  // Len returns length of a role list
   136  func (s SortedNamespaces) Len() int {
   137  	return len(s)
   138  }
   139  
   140  // Less compares roles by name
   141  func (s SortedNamespaces) Less(i, j int) bool {
   142  	return s[i].Metadata.Name < s[j].Metadata.Name
   143  }
   144  
   145  // Swap swaps two roles in a list
   146  func (s SortedNamespaces) Swap(i, j int) {
   147  	s[i], s[j] = s[j], s[i]
   148  }
   149  
   150  // IsValidNamespace checks if the namespace provided is valid
   151  func IsValidNamespace(s string) bool {
   152  	return validNamespace.MatchString(s)
   153  }
   154  
   155  var validNamespace = regexp.MustCompile(`^[A-Za-z0-9]+$`)