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

     1  /*
     2   * Copyright 2023 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  	"github.com/gravitational/teleport/api/constants"
    25  )
    26  
    27  // UIConfig defines configuration for the web UI served
    28  // by the proxy service. This is a configuration resource,
    29  // never create more than one instance of it.
    30  type UIConfig interface {
    31  	Resource
    32  	// GetShowResources will returns which resources should be shown in the unified resources UI
    33  	GetShowResources() constants.ShowResources
    34  	// GetScrollbackLines returns the amount of scrollback lines the terminal remembers
    35  	GetScrollbackLines() int32
    36  	// SetScrollbackLines sets the amount of scrollback lines the terminal remembers
    37  	SetScrollbackLines(int32)
    38  
    39  	String() string
    40  }
    41  
    42  func NewUIConfigV1() (*UIConfigV1, error) {
    43  	uiconfig := &UIConfigV1{
    44  		ResourceHeader: ResourceHeader{},
    45  	}
    46  	if err := uiconfig.CheckAndSetDefaults(); err != nil {
    47  		return nil, trace.Wrap(err)
    48  	}
    49  	return uiconfig, nil
    50  }
    51  
    52  // CheckAndSetDefaults verifies the constraints for UIConfig.
    53  func (c *UIConfigV1) CheckAndSetDefaults() error {
    54  	c.setStaticFields()
    55  	if err := c.Metadata.CheckAndSetDefaults(); err != nil {
    56  		return trace.Wrap(err)
    57  	}
    58  	if c.Spec.ScrollbackLines < 0 {
    59  		return trace.BadParameter("invalid scrollback lines value. Must be greater than or equal to 0.")
    60  	}
    61  	if c.Spec.ShowResources == "" {
    62  		c.Spec.ShowResources = constants.ShowResourcesRequestable
    63  	}
    64  	switch c.Spec.ShowResources {
    65  	case constants.ShowResourcesaccessibleOnly,
    66  		constants.ShowResourcesRequestable:
    67  	default:
    68  		return trace.BadParameter("show resources %q not supported", c.Spec.ShowResources)
    69  	}
    70  	return nil
    71  }
    72  
    73  // GetVersion returns resource version.
    74  func (c *UIConfigV1) GetVersion() string {
    75  	return c.Version
    76  }
    77  
    78  // GetName returns the name of the resource.
    79  func (c *UIConfigV1) GetName() string {
    80  	return c.Metadata.Name
    81  }
    82  
    83  // SetName sets the name of the resource.
    84  func (c *UIConfigV1) SetName(e string) {
    85  	c.Metadata.Name = e
    86  }
    87  
    88  // SetExpiry sets expiry time for the object.
    89  func (c *UIConfigV1) SetExpiry(expires time.Time) {
    90  	c.Metadata.SetExpiry(expires)
    91  }
    92  
    93  // Expiry returns object expiry setting.
    94  func (c *UIConfigV1) Expiry() time.Time {
    95  	return c.Metadata.Expiry()
    96  }
    97  
    98  // GetMetadata returns object metadata.
    99  func (c *UIConfigV1) GetMetadata() Metadata {
   100  	return c.Metadata
   101  }
   102  
   103  // GetResourceID returns resource ID.
   104  func (c *UIConfigV1) GetResourceID() int64 {
   105  	return c.Metadata.ID
   106  }
   107  
   108  // SetResourceID sets resource ID.
   109  func (c *UIConfigV1) SetResourceID(id int64) {
   110  	c.Metadata.ID = id
   111  }
   112  
   113  // GetKind returns resource kind.
   114  func (c *UIConfigV1) GetKind() string {
   115  	return c.Kind
   116  }
   117  
   118  // GetSubKind returns resource subkind.
   119  func (c *UIConfigV1) GetSubKind() string {
   120  	return c.SubKind
   121  }
   122  
   123  // SetSubKind sets resource subkind.
   124  func (c *UIConfigV1) SetSubKind(sk string) {
   125  	c.SubKind = sk
   126  }
   127  
   128  // GetShowResources will returns which resources should be shown in the unified resources UI
   129  func (c *UIConfigV1) GetShowResources() constants.ShowResources {
   130  	return c.Spec.ShowResources
   131  }
   132  
   133  func (c *UIConfigV1) GetScrollbackLines() int32 {
   134  	return c.Spec.ScrollbackLines
   135  }
   136  
   137  func (c *UIConfigV1) SetScrollbackLines(lines int32) {
   138  	c.Spec.ScrollbackLines = lines
   139  }
   140  
   141  // setStaticFields sets static resource header and metadata fields.
   142  func (c *UIConfigV1) setStaticFields() {
   143  	c.Kind = KindUIConfig
   144  	c.Version = V1
   145  }