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

     1  /*
     2   * Copyright 2022 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  	"time"
    21  
    22  	"github.com/gravitational/trace"
    23  )
    24  
    25  // Installer is an installer script resource
    26  type Installer interface {
    27  	Resource
    28  
    29  	// GetScript returns the contents of the installer script
    30  	GetScript() string
    31  	// SetScript sets the installer script
    32  	SetScript(string)
    33  
    34  	String() string
    35  }
    36  
    37  // NewInstallerV1 returns a new installer resource
    38  func NewInstallerV1(name, script string) (*InstallerV1, error) {
    39  	installer := &InstallerV1{
    40  		Metadata: Metadata{
    41  			Name: name,
    42  		},
    43  		Spec: InstallerSpecV1{
    44  			Script: script,
    45  		},
    46  	}
    47  	if err := installer.CheckAndSetDefaults(); err != nil {
    48  		return nil, trace.Wrap(err)
    49  	}
    50  	return installer, nil
    51  }
    52  
    53  // MustNewInstallerV1 creates a new installer resource from the provided script.
    54  //
    55  // Panics in case of any error when creating the resource.
    56  func MustNewInstallerV1(name, script string) *InstallerV1 {
    57  	inst, err := NewInstallerV1(name, script)
    58  	if err != nil {
    59  		panic(err)
    60  	}
    61  	return inst
    62  }
    63  
    64  // CheckAndSetDefaults implements Installer
    65  func (c *InstallerV1) CheckAndSetDefaults() error {
    66  	c.setStaticFields()
    67  	return trace.Wrap(c.Metadata.CheckAndSetDefaults())
    68  }
    69  
    70  // GetVersion returns resource version.
    71  func (c *InstallerV1) GetVersion() string {
    72  	return c.Version
    73  }
    74  
    75  // GetName returns the name of the resource.
    76  func (c *InstallerV1) GetName() string {
    77  	return c.Metadata.Name
    78  }
    79  
    80  // SetName sets the name of the resource.
    81  func (c *InstallerV1) SetName(e string) {
    82  	c.Metadata.Name = e
    83  }
    84  
    85  // SetExpiry sets expiry time for the object.
    86  func (c *InstallerV1) SetExpiry(expires time.Time) {
    87  	c.Metadata.SetExpiry(expires)
    88  }
    89  
    90  // Expiry returns object expiry setting.
    91  func (c *InstallerV1) Expiry() time.Time {
    92  	return c.Metadata.Expiry()
    93  }
    94  
    95  // GetMetadata returns object metadata.
    96  func (c *InstallerV1) GetMetadata() Metadata {
    97  	return c.Metadata
    98  }
    99  
   100  // GetResourceID returns resource ID.
   101  func (c *InstallerV1) GetResourceID() int64 {
   102  	return c.Metadata.ID
   103  }
   104  
   105  // SetResourceID sets resource ID.
   106  func (c *InstallerV1) SetResourceID(id int64) {
   107  	c.Metadata.ID = id
   108  }
   109  
   110  // GetRevision returns the revision
   111  func (c *InstallerV1) GetRevision() string {
   112  	return c.Metadata.GetRevision()
   113  }
   114  
   115  // SetRevision sets the revision
   116  func (c *InstallerV1) SetRevision(rev string) {
   117  	c.Metadata.SetRevision(rev)
   118  }
   119  
   120  // GetKind returns resource kind.
   121  func (c *InstallerV1) GetKind() string {
   122  	return c.Kind
   123  }
   124  
   125  // GetSubKind returns resource subkind.
   126  func (c *InstallerV1) GetSubKind() string {
   127  	return c.SubKind
   128  }
   129  
   130  // SetSubKind sets resource subkind.
   131  func (c *InstallerV1) SetSubKind(sk string) {
   132  	c.SubKind = sk
   133  }
   134  
   135  func (c *InstallerV1) GetScript() string {
   136  	return c.Spec.Script
   137  }
   138  
   139  func (c *InstallerV1) SetScript(s string) {
   140  	c.Spec.Script = s
   141  }
   142  
   143  // setStaticFields sets static resource header and metadata fields.
   144  func (c *InstallerV1) setStaticFields() {
   145  	c.Kind = KindInstaller
   146  	c.Version = V1
   147  }