github.com/argoproj/argo-events@v1.9.1/sensors/policy/status.go (about)

     1  /*
     2  Copyright 2020 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 policy
    18  
    19  import (
    20  	"context"
    21  	"fmt"
    22  )
    23  
    24  // StatusPolicy implements the policy for a HTTP trigger
    25  type StatusPolicy struct {
    26  	// Status represents the status of a generic operation
    27  	Status int
    28  	// Statuses refers to list of response status allowed
    29  	Statuses []int
    30  }
    31  
    32  // NewStatusPolicy returns a new HTTP trigger policy
    33  func NewStatusPolicy(status int, statuses []int) *StatusPolicy {
    34  	return &StatusPolicy{
    35  		Status:   status,
    36  		Statuses: statuses,
    37  	}
    38  }
    39  
    40  func (hp *StatusPolicy) ApplyPolicy(ctx context.Context) error {
    41  	for _, status := range hp.Statuses {
    42  		if hp.Status == status {
    43  			return nil
    44  		}
    45  	}
    46  	return fmt.Errorf("policy application resulted in failure. http response status %d is not allowed", hp.Status)
    47  }