github.com/gravitational/teleport/api@v0.0.0-20240507183017-3110591cbafc/types/statictokens.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 26 // StaticTokens define a list of static []ProvisionToken used to provision a 27 // node. StaticTokens is a configuration resource, never create more than one instance 28 // of it. 29 type StaticTokens interface { 30 // Resource provides common resource properties. 31 Resource 32 // SetStaticTokens sets the list of static tokens used to provision nodes. 33 SetStaticTokens([]ProvisionToken) 34 // GetStaticTokens gets the list of static tokens used to provision nodes. 35 GetStaticTokens() []ProvisionToken 36 } 37 38 // NewStaticTokens is a convenience wrapper to create a StaticTokens resource. 39 func NewStaticTokens(spec StaticTokensSpecV2) (StaticTokens, error) { 40 st := &StaticTokensV2{Spec: spec} 41 if err := st.CheckAndSetDefaults(); err != nil { 42 return nil, trace.Wrap(err) 43 } 44 return st, nil 45 } 46 47 // DefaultStaticTokens is used to get the default static tokens (empty list) 48 // when nothing is specified in file configuration. 49 func DefaultStaticTokens() StaticTokens { 50 token, _ := NewStaticTokens(StaticTokensSpecV2{}) 51 return token 52 } 53 54 // GetVersion returns resource version 55 func (c *StaticTokensV2) GetVersion() string { 56 return c.Version 57 } 58 59 // GetKind returns resource kind 60 func (c *StaticTokensV2) GetKind() string { 61 return c.Kind 62 } 63 64 // GetSubKind returns resource sub kind 65 func (c *StaticTokensV2) GetSubKind() string { 66 return c.SubKind 67 } 68 69 // SetSubKind sets resource subkind 70 func (c *StaticTokensV2) SetSubKind(sk string) { 71 c.SubKind = sk 72 } 73 74 // GetResourceID returns resource ID 75 func (c *StaticTokensV2) GetResourceID() int64 { 76 return c.Metadata.ID 77 } 78 79 // SetResourceID sets resource ID 80 func (c *StaticTokensV2) SetResourceID(id int64) { 81 c.Metadata.ID = id 82 } 83 84 // GetRevision returns the revision 85 func (c *StaticTokensV2) GetRevision() string { 86 return c.Metadata.GetRevision() 87 } 88 89 // SetRevision sets the revision 90 func (c *StaticTokensV2) SetRevision(rev string) { 91 c.Metadata.SetRevision(rev) 92 } 93 94 // GetName returns the name of the StaticTokens resource. 95 func (c *StaticTokensV2) GetName() string { 96 return c.Metadata.Name 97 } 98 99 // SetName sets the name of the StaticTokens resource. 100 func (c *StaticTokensV2) SetName(e string) { 101 c.Metadata.Name = e 102 } 103 104 // Expiry returns object expiry setting 105 func (c *StaticTokensV2) Expiry() time.Time { 106 return c.Metadata.Expiry() 107 } 108 109 // SetExpiry sets expiry time for the object 110 func (c *StaticTokensV2) SetExpiry(expires time.Time) { 111 c.Metadata.SetExpiry(expires) 112 } 113 114 // GetMetadata returns object metadata 115 func (c *StaticTokensV2) GetMetadata() Metadata { 116 return c.Metadata 117 } 118 119 // SetStaticTokens sets the list of static tokens used to provision nodes. 120 func (c *StaticTokensV2) SetStaticTokens(s []ProvisionToken) { 121 c.Spec.StaticTokens = ProvisionTokensToV1(s) 122 } 123 124 // GetStaticTokens gets the list of static tokens used to provision nodes. 125 func (c *StaticTokensV2) GetStaticTokens() []ProvisionToken { 126 return ProvisionTokensFromV1(c.Spec.StaticTokens) 127 } 128 129 // setStaticFields sets static resource header and metadata fields. 130 func (c *StaticTokensV2) setStaticFields() { 131 c.Kind = KindStaticTokens 132 c.Version = V2 133 c.Metadata.Name = MetaNameStaticTokens 134 } 135 136 // CheckAndSetDefaults checks validity of all parameters and sets defaults. 137 func (c *StaticTokensV2) CheckAndSetDefaults() error { 138 c.setStaticFields() 139 if err := c.Metadata.CheckAndSetDefaults(); err != nil { 140 return trace.Wrap(err) 141 } 142 return nil 143 } 144 145 // String represents a human readable version of static provisioning tokens. 146 func (c *StaticTokensV2) String() string { 147 return fmt.Sprintf("StaticTokens(%v)", c.Spec.StaticTokens) 148 }