github.com/argoproj/argo-events@v1.9.1/eventsources/common/webhook/validate.go (about)

     1  /*
     2  Copyright 2018 BlackRock, 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 webhook
    18  
    19  import (
    20  	"fmt"
    21  	"strconv"
    22  
    23  	"github.com/argoproj/argo-events/pkg/apis/eventsource/v1alpha1"
    24  )
    25  
    26  // ValidateWebhookContext validates a webhook context
    27  func ValidateWebhookContext(context *v1alpha1.WebhookContext) error {
    28  	if context == nil {
    29  		return fmt.Errorf("")
    30  	}
    31  	if context.Endpoint == "" {
    32  		return fmt.Errorf("endpoint can't be empty")
    33  	}
    34  	if context.Port == "" {
    35  		return fmt.Errorf("port can't be empty")
    36  	}
    37  	if context.Port != "" {
    38  		_, err := strconv.Atoi(context.Port)
    39  		if err != nil {
    40  			return fmt.Errorf("failed to parse server port %s. err: %+v", context.Port, err)
    41  		}
    42  	}
    43  	return nil
    44  }
    45  
    46  // validateRoute validates a route
    47  func validateRoute(r *Route) error {
    48  	if r == nil {
    49  		return fmt.Errorf("route can't be nil")
    50  	}
    51  	if r.Context == nil {
    52  		return fmt.Errorf("webhook can't be nil")
    53  	}
    54  	if r.StartCh == nil {
    55  		return fmt.Errorf("start channel can't be nil")
    56  	}
    57  	if r.EventSourceName == "" {
    58  		return fmt.Errorf("event source name can't be empty")
    59  	}
    60  	if r.EventName == "" {
    61  		return fmt.Errorf("event name can't be empty")
    62  	}
    63  	if r.Logger == nil {
    64  		return fmt.Errorf("logger can't be nil")
    65  	}
    66  	return nil
    67  }