github.com/gravitational/teleport/api@v0.0.0-20240507183017-3110591cbafc/types/databaseservice.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  	"github.com/gravitational/trace"
    21  
    22  	"github.com/gravitational/teleport/api/utils"
    23  )
    24  
    25  // DatabaseService represents a DatabaseService (agent).
    26  type DatabaseService interface {
    27  	// ResourceWithLabels provides common resource methods.
    28  	ResourceWithLabels
    29  
    30  	// GetNamespace returns the resource namespace.
    31  	GetNamespace() string
    32  
    33  	// GetResourceMatchers returns the resource matchers of the DatabaseService.
    34  	// Database services deployed by Teleport have known configurations where
    35  	// we will only define a single resource matcher.
    36  	GetResourceMatchers() []*DatabaseResourceMatcher
    37  }
    38  
    39  // NewDatabaseServiceV1 creates a new DatabaseService instance.
    40  func NewDatabaseServiceV1(meta Metadata, spec DatabaseServiceSpecV1) (*DatabaseServiceV1, error) {
    41  	s := &DatabaseServiceV1{
    42  		ResourceHeader: ResourceHeader{
    43  			Metadata: meta,
    44  		},
    45  		Spec: spec,
    46  	}
    47  
    48  	if err := s.CheckAndSetDefaults(); err != nil {
    49  		return nil, trace.Wrap(err)
    50  	}
    51  	return s, nil
    52  }
    53  
    54  func (s *DatabaseServiceV1) setStaticFields() {
    55  	s.Kind = KindDatabaseService
    56  	s.Version = V1
    57  }
    58  
    59  // CheckAndSetDefaults checks and sets default values for any missing fields.
    60  func (s *DatabaseServiceV1) CheckAndSetDefaults() error {
    61  	s.setStaticFields()
    62  
    63  	return trace.Wrap(s.ResourceHeader.CheckAndSetDefaults())
    64  }
    65  
    66  // GetResourceMatchers returns the resource matchers of the DatabaseService.
    67  func (s *DatabaseServiceV1) GetResourceMatchers() []*DatabaseResourceMatcher {
    68  	return s.Spec.ResourceMatchers
    69  }
    70  
    71  // GetNamespace returns the resource namespace.
    72  func (s *DatabaseServiceV1) GetNamespace() string {
    73  	return s.Metadata.Namespace
    74  }
    75  
    76  // MatchSearch goes through select field values and tries to
    77  // match against the list of search values.
    78  func (s *DatabaseServiceV1) MatchSearch(values []string) bool {
    79  	fieldVals := append(utils.MapToStrings(s.GetAllLabels()), s.GetName())
    80  	return MatchSearch(fieldVals, values, nil)
    81  }