github.com/erda-project/erda-infra@v1.0.9/providers/component-protocol/protocol/scenario.go (about)

     1  // Copyright (c) 2021 Terminus, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package protocol
    16  
    17  import (
    18  	"fmt"
    19  
    20  	"github.com/sirupsen/logrus"
    21  
    22  	"github.com/erda-project/erda-infra/providers/component-protocol/cptype"
    23  )
    24  
    25  // ScenarioRender is a group of component renders.
    26  // key: componentName
    27  // value: componentRender
    28  type ScenarioRender map[string]*CompRenderSpec
    29  
    30  // ScenarioRenders contains all scenario renders.
    31  var ScenarioRenders = make(map[string]*ScenarioRender)
    32  
    33  // getScenarioRenders .
    34  func getScenarioRenders(scenario string) (*ScenarioRender, error) {
    35  	renders := ScenarioRenders[scenario]
    36  	if renders == nil {
    37  		renders = &ScenarioRender{}
    38  	}
    39  
    40  	defaultRenders := ScenarioRenders[cptype.DefaultComponentNamespace]
    41  	if defaultRenders == nil {
    42  		defaultRenders = &ScenarioRender{}
    43  	}
    44  
    45  	// append component render from default component namespace
    46  	p, ok := defaultProtocols[scenario]
    47  	if !ok {
    48  		return nil, fmt.Errorf("failed to get scenario renders, default protocol not exist, scenario: %s", scenario)
    49  	}
    50  	for compName := range p.Components {
    51  		compName, _ = getCompNameAndInstanceName(compName)
    52  		// skip if scenario-level component render exist
    53  		_, renderExist := (*renders)[compName]
    54  		if renderExist {
    55  			continue
    56  		}
    57  		// if component render not exist, add render from default component namespace
    58  		defaultCompRender, dOK := (*defaultRenders)[compName]
    59  		if dOK {
    60  			(*renders)[compName] = defaultCompRender
    61  			logrus.Infof("use default comp render, scenario: %s, comp: %s", scenario, compName)
    62  			continue
    63  		}
    64  		// use empty component renders
    65  		logrus.Infof("use empty comp render, scenario: %s, comp: %s", scenario, compName)
    66  		(*renders)[compName] = &CompRenderSpec{RenderC: emptyRenderFunc}
    67  	}
    68  
    69  	return renders, nil
    70  }
    71  
    72  // getScenarioKey get scenario key from protocol.
    73  // return scenarioType if not empty.
    74  func getScenarioKey(req cptype.Scenario) (string, error) {
    75  	if req.ScenarioType == "" && req.ScenarioKey == "" {
    76  		return "", fmt.Errorf("scenario.is.empty")
    77  	}
    78  	if req.ScenarioType != "" {
    79  		return req.ScenarioType, nil
    80  	}
    81  	return req.ScenarioKey, nil
    82  }