github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/pkg/cli/preflight/analyzer/analyze_result.go (about)

     1  /*
     2  Copyright (C) 2022-2023 ApeCloud Co., Ltd
     3  
     4  This file is part of KubeBlocks project
     5  
     6  This program is free software: you can redistribute it and/or modify
     7  it under the terms of the GNU Affero General Public License as published by
     8  the Free Software Foundation, either version 3 of the License, or
     9  (at your option) any later version.
    10  
    11  This program is distributed in the hope that it will be useful
    12  but WITHOUT ANY WARRANTY; without even the implied warranty of
    13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    14  GNU Affero General Public License for more details.
    15  
    16  You should have received a copy of the GNU Affero General Public License
    17  along with this program.  If not, see <http://www.gnu.org/licenses/>.
    18  */
    19  
    20  package analyzer
    21  
    22  import (
    23  	analyze "github.com/replicatedhq/troubleshoot/pkg/analyze"
    24  	troubleshoot "github.com/replicatedhq/troubleshoot/pkg/apis/troubleshoot/v1beta2"
    25  )
    26  
    27  const (
    28  	MissingOutcomeMessage = "there is a missing outcome message"
    29  	IncorrectOutcomeType  = "there is an incorrect outcome type"
    30  	PassType              = "Pass"
    31  	WarnType              = "Warn"
    32  	FailType              = "Fail"
    33  )
    34  
    35  func newAnalyzeResult(title string, resultType string, outcomes []*troubleshoot.Outcome) *analyze.AnalyzeResult {
    36  	for _, outcome := range outcomes {
    37  		if outcome == nil {
    38  			continue
    39  		}
    40  		switch resultType {
    41  		case PassType:
    42  			if outcome.Pass != nil {
    43  				return newPassAnalyzeResult(title, outcome)
    44  			}
    45  		case WarnType:
    46  			if outcome.Warn != nil {
    47  				return newWarnAnalyzeResult(title, outcome)
    48  			}
    49  		case FailType:
    50  			if outcome.Fail != nil {
    51  				return newFailAnalyzeResult(title, outcome)
    52  			}
    53  		default:
    54  			return newFailedResultWithMessage(title, IncorrectOutcomeType)
    55  		}
    56  	}
    57  	return newFailedResultWithMessage(title, MissingOutcomeMessage)
    58  }
    59  
    60  func newFailAnalyzeResult(title string, outcome *troubleshoot.Outcome) *analyze.AnalyzeResult {
    61  	return &analyze.AnalyzeResult{
    62  		Title:   title,
    63  		IsFail:  true,
    64  		Message: outcome.Fail.Message,
    65  		URI:     outcome.Fail.URI,
    66  	}
    67  }
    68  
    69  func newWarnAnalyzeResult(title string, outcome *troubleshoot.Outcome) *analyze.AnalyzeResult {
    70  	return &analyze.AnalyzeResult{
    71  		Title:   title,
    72  		IsWarn:  true,
    73  		Message: outcome.Warn.Message,
    74  		URI:     outcome.Warn.URI,
    75  	}
    76  }
    77  
    78  func newPassAnalyzeResult(title string, outcome *troubleshoot.Outcome) *analyze.AnalyzeResult {
    79  	return &analyze.AnalyzeResult{
    80  		Title:   title,
    81  		IsPass:  true,
    82  		Message: outcome.Pass.Message,
    83  		URI:     outcome.Pass.URI,
    84  	}
    85  }
    86  
    87  func newFailedResultWithMessage(title, message string) *analyze.AnalyzeResult {
    88  	return newFailAnalyzeResult(title, &troubleshoot.Outcome{Fail: &troubleshoot.SingleOutcome{Message: message}})
    89  }
    90  
    91  func newWarnResultWithMessage(title, message string) *analyze.AnalyzeResult {
    92  	return newWarnAnalyzeResult(title, &troubleshoot.Outcome{Warn: &troubleshoot.SingleOutcome{Message: message}})
    93  }