...

Source file src/github.com/chaos-mesh/chaos-mesh/pkg/curl/render.go

Documentation: github.com/chaos-mesh/chaos-mesh/pkg/curl

     1  // Copyright 2021 Chaos Mesh Authors.
     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  
    16  package curl
    17  
    18  import (
    19  	"fmt"
    20  	"net/http"
    21  
    22  	corev1 "k8s.io/api/core/v1"
    23  
    24  	"github.com/chaos-mesh/chaos-mesh/api/v1alpha1"
    25  )
    26  
    27  const image = "curlimages/curl:7.78.0"
    28  
    29  const nameSuffix = "-rendered-http-request"
    30  
    31  func renderCommands(request CommandFlags) (Commands, error) {
    32  	// TODO: validation of request
    33  	result := []string{"curl", "-i", "-s"}
    34  
    35  	// follow the request
    36  	if request.FollowLocation {
    37  		result = append(result, "-L")
    38  	}
    39  
    40  	if request.Method != http.MethodGet {
    41  		result = append(result, "-X", request.Method)
    42  
    43  		if len(request.Body) > 0 {
    44  			result = append(result, "-d", request.Body)
    45  		}
    46  	}
    47  
    48  	renderedHeaders := http.Header{}
    49  	for k, v := range request.Header {
    50  		renderedHeaders[k] = v
    51  	}
    52  	if request.JsonContent {
    53  		if request.Header == nil {
    54  			request.Header = Header{}
    55  		}
    56  		renderedHeaders[HeaderContentType] = []string{ApplicationJson}
    57  	}
    58  
    59  	for key, values := range renderedHeaders {
    60  		for _, value := range values {
    61  			result = append(result, "-H", fmt.Sprintf("%s: %s", key, value))
    62  		}
    63  	}
    64  
    65  	result = append(result, request.URL)
    66  
    67  	return result, nil
    68  }
    69  
    70  func RenderWorkflowTaskTemplate(request RequestForm) (*v1alpha1.Template, error) {
    71  	commands, err := renderCommands(request.CommandFlags)
    72  	if err != nil {
    73  		return nil, err
    74  	}
    75  	containerName := fmt.Sprintf("%s%s", request.Name, nameSuffix)
    76  	return &v1alpha1.Template{
    77  		Name: request.Name,
    78  		Type: v1alpha1.TypeTask,
    79  		Task: &v1alpha1.Task{
    80  			Container: &corev1.Container{
    81  				Name:    containerName,
    82  				Image:   image,
    83  				Command: commands,
    84  			},
    85  		},
    86  		ConditionalBranches: nil,
    87  	}, nil
    88  }
    89