github.com/kyma-project/kyma/components/asset-store-controller-manager@v0.0.0-20191203152857-3792b5df17c5/internal/assethook/validation_engine.go (about)

     1  package assethook
     2  
     3  import (
     4  	"bytes"
     5  	"context"
     6  	"github.com/pkg/errors"
     7  	"io"
     8  	"time"
     9  
    10  	"github.com/kyma-project/kyma/components/asset-store-controller-manager/pkg/apis/assetstore/v1alpha2"
    11  )
    12  
    13  type validationEngine struct {
    14  	processor httpProcessor
    15  }
    16  
    17  //go:generate mockery -name=Validator -output=automock -outpkg=automock -case=underscore
    18  type Validator interface {
    19  	Validate(ctx context.Context, basePath string, files []string, services []v1alpha2.AssetWebhookService) (Result, error)
    20  }
    21  
    22  func NewValidator(httpClient HttpClient, timeout time.Duration, workers int) *validationEngine {
    23  	return &validationEngine{
    24  		processor: &processor{
    25  			timeout:        timeout,
    26  			workers:        workers,
    27  			onFail:         validationFailureHandler,
    28  			continueOnFail: true,
    29  			httpClient:     httpClient,
    30  		},
    31  	}
    32  }
    33  
    34  func validationFailureHandler(_ context.Context, _, filePath string, responseBody io.Reader, messagesChan chan Message, _ chan error) {
    35  	buffer := new(bytes.Buffer)
    36  	buffer.ReadFrom(responseBody)
    37  
    38  	message := Message{Filename: filePath, Message: buffer.String()}
    39  	messagesChan <- message
    40  }
    41  
    42  func (e *validationEngine) Validate(ctx context.Context, basePath string, files []string, services []v1alpha2.AssetWebhookService) (Result, error) {
    43  	results, err := e.processor.Do(ctx, basePath, files, services)
    44  	if err != nil {
    45  		return Result{}, errors.Wrap(err, "while validating")
    46  	}
    47  
    48  	return Result{
    49  		Success:  len(results) == 0,
    50  		Messages: results,
    51  	}, nil
    52  }