github.com/gravitational/teleport/api@v0.0.0-20240507183017-3110591cbafc/types/connection_diagnostic.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  const (
    26  	// DiagnosticMessageSuccess is the message used when we the Connection was successful
    27  	DiagnosticMessageSuccess = "success"
    28  
    29  	// DiagnosticMessageFailed is the message used when we the Connection failed
    30  	DiagnosticMessageFailed = "failed"
    31  )
    32  
    33  // ConnectionDiagnostic represents a Connection Diagnostic.
    34  type ConnectionDiagnostic interface {
    35  	// ResourceWithLabels provides common resource methods.
    36  	ResourceWithLabels
    37  
    38  	// Whether the connection was successful
    39  	IsSuccess() bool
    40  	// Sets the success flag
    41  	SetSuccess(bool)
    42  
    43  	// The underlying message
    44  	GetMessage() string
    45  	// Sets the undderlying message
    46  	SetMessage(string)
    47  
    48  	// The connection test traces
    49  	GetTraces() []*ConnectionDiagnosticTrace
    50  
    51  	// AppendTrace adds a trace to the ConnectionDiagnostic Traces
    52  	AppendTrace(*ConnectionDiagnosticTrace)
    53  }
    54  
    55  type ConnectionsDiagnostic []ConnectionDiagnostic
    56  
    57  var _ ConnectionDiagnostic = &ConnectionDiagnosticV1{}
    58  
    59  // NewConnectionDiagnosticV1 creates a new ConnectionDiagnosticV1 resource.
    60  func NewConnectionDiagnosticV1(name string, labels map[string]string, spec ConnectionDiagnosticSpecV1) (*ConnectionDiagnosticV1, error) {
    61  	c := &ConnectionDiagnosticV1{
    62  		ResourceHeader: ResourceHeader{
    63  			Version: V1,
    64  			Kind:    KindConnectionDiagnostic,
    65  			Metadata: Metadata{
    66  				Name:   name,
    67  				Labels: labels,
    68  			},
    69  		},
    70  		Spec: spec,
    71  	}
    72  
    73  	if err := c.CheckAndSetDefaults(); err != nil {
    74  		return nil, trace.Wrap(err)
    75  	}
    76  
    77  	return c, nil
    78  }
    79  
    80  // CheckAndSetDefaults checks and sets default values for any missing fields.
    81  func (c *ConnectionDiagnosticV1) CheckAndSetDefaults() error {
    82  	if c.Spec.Message == "" {
    83  		return trace.BadParameter("ConnectionDiagnosticV1.Spec missing Message field")
    84  	}
    85  
    86  	return nil
    87  }
    88  
    89  // IsSuccess returns whether the connection was successful
    90  func (c *ConnectionDiagnosticV1) IsSuccess() bool {
    91  	return c.Spec.Success
    92  }
    93  
    94  // SetSuccess sets whether the Connection was a success or not
    95  func (c *ConnectionDiagnosticV1) SetSuccess(b bool) {
    96  	c.Spec.Success = b
    97  }
    98  
    99  // GetMessage returns the connection diagnostic message.
   100  func (c *ConnectionDiagnosticV1) GetMessage() string {
   101  	return c.Spec.Message
   102  }
   103  
   104  // SetMessage sets the summary message of the Connection Diagnostic
   105  func (c *ConnectionDiagnosticV1) SetMessage(s string) {
   106  	c.Spec.Message = s
   107  }
   108  
   109  // GetTraces returns the connection test traces
   110  func (c *ConnectionDiagnosticV1) GetTraces() []*ConnectionDiagnosticTrace {
   111  	return c.Spec.Traces
   112  }
   113  
   114  // AppendTrace adds a trace into the Traces list
   115  func (c *ConnectionDiagnosticV1) AppendTrace(trace *ConnectionDiagnosticTrace) {
   116  	c.Spec.Traces = append(c.Spec.Traces, trace)
   117  }
   118  
   119  // MatchSearch goes through select field values and tries to
   120  // match against the list of search values.
   121  func (c *ConnectionDiagnosticV1) MatchSearch(values []string) bool {
   122  	fieldVals := append(utils.MapToStrings(c.GetAllLabels()), c.GetName())
   123  	return MatchSearch(fieldVals, values, nil)
   124  }
   125  
   126  // SetStaticLabels sets the connection diagnostic static labels.
   127  func (c *ConnectionDiagnosticV1) SetStaticLabels(sl map[string]string) {
   128  	c.Metadata.Labels = sl
   129  }
   130  
   131  // NewTraceDiagnosticConnection creates a new Connection Diagnostic Trace.
   132  // If traceErr is not nil, it will set the Status to FAILED, SUCCESS otherwise.
   133  func NewTraceDiagnosticConnection(traceType ConnectionDiagnosticTrace_TraceType, details string, traceErr error) *ConnectionDiagnosticTrace {
   134  	ret := &ConnectionDiagnosticTrace{
   135  		Status:  ConnectionDiagnosticTrace_SUCCESS,
   136  		Type:    traceType,
   137  		Details: details,
   138  	}
   139  
   140  	if traceErr != nil {
   141  		ret.Status = ConnectionDiagnosticTrace_FAILED
   142  		ret.Error = traceErr.Error()
   143  	}
   144  
   145  	return ret
   146  }