github.com/erda-project/erda-infra@v1.0.9/providers/component-protocol/protocol.service.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 componentprotocol
    16  
    17  import (
    18  	"context"
    19  
    20  	"github.com/erda-project/erda-infra/providers/component-protocol/cptype"
    21  	"github.com/erda-project/erda-infra/providers/component-protocol/protobuf/proto-go/cp/pb"
    22  	"github.com/erda-project/erda-infra/providers/component-protocol/protocol"
    23  	"github.com/erda-project/erda-infra/providers/component-protocol/utils/cputil"
    24  )
    25  
    26  type protocolService struct {
    27  	p *provider
    28  }
    29  
    30  // Render .
    31  func (s *protocolService) Render(ctx context.Context, req *pb.RenderRequest) (*pb.RenderResponse, error) {
    32  	s.p.Log.Debugf("request header: %v", cputil.GetAllHeaders(ctx))
    33  
    34  	// transfer pb to ComponentProtocolRequest for easy use
    35  	renderReq := &cptype.ComponentProtocolRequest{}
    36  	if err := cputil.ObjJSONTransfer(req, renderReq); err != nil {
    37  		return nil, err
    38  	}
    39  
    40  	// make sdk
    41  	sdk := cptype.SDK{
    42  		Scenario: req.Scenario.ScenarioKey,
    43  		Tran:     s.p.tran.Translator(req.Scenario.ScenarioKey),
    44  		Identity: cputil.GetIdentity(ctx),
    45  		InParams: renderReq.InParams,
    46  		Lang:     cputil.Language(ctx),
    47  	}
    48  
    49  	// make ctx with sdk
    50  	ctx = context.WithValue(ctx, cptype.GlobalInnerKeyCtxSDK, &sdk)
    51  	for k, v := range s.p.customContextKVs {
    52  		ctx = context.WithValue(ctx, k, v)
    53  	}
    54  
    55  	// temp state
    56  	ctx = context.WithValue(ctx, cptype.GlobalInnerKeyStateTemp, make(map[string]interface{}))
    57  
    58  	sdk.Ctx = ctx
    59  
    60  	// render concrete scenario
    61  	if err := protocol.RunScenarioRender(ctx, renderReq); err != nil {
    62  		return nil, err
    63  	}
    64  
    65  	// make response
    66  	resp, err := s.makeResponse(renderReq)
    67  
    68  	return resp, err
    69  }
    70  
    71  func (s *protocolService) makeResponse(renderReq *cptype.ComponentProtocolRequest) (*pb.RenderResponse, error) {
    72  	// check if business error exist, not platform error
    73  	businessErr := protocol.GetGlobalStateKV(renderReq.Protocol, cptype.GlobalInnerKeyError.String())
    74  	if businessErr != nil {
    75  		if err, ok := businessErr.(error); ok {
    76  			return nil, err
    77  		}
    78  		s.p.Log.Warnf("business error type is not error, %#v", businessErr)
    79  	}
    80  
    81  	// render response
    82  	polishedPbReq := &pb.RenderRequest{}
    83  	if err := cputil.ObjJSONTransfer(renderReq, polishedPbReq); err != nil {
    84  		return nil, err
    85  	}
    86  	pbResp := pb.RenderResponse{
    87  		Scenario: polishedPbReq.Scenario,
    88  		Protocol: polishedPbReq.Protocol,
    89  	}
    90  
    91  	return &pbResp, nil
    92  }