github.com/kubeshop/testkube@v1.17.23/pkg/api/v1/client/executor.go (about)

     1  package client
     2  
     3  import (
     4  	"encoding/json"
     5  	"net/http"
     6  
     7  	"github.com/kubeshop/testkube/pkg/api/v1/testkube"
     8  )
     9  
    10  // NewExecutorClient creates new Executor client
    11  func NewExecutorClient(executorTransport Transport[testkube.ExecutorDetails]) ExecutorClient {
    12  	return ExecutorClient{
    13  		executorTransport: executorTransport,
    14  	}
    15  }
    16  
    17  // ExecutorClient is a client for executors
    18  type ExecutorClient struct {
    19  	executorTransport Transport[testkube.ExecutorDetails]
    20  }
    21  
    22  // GetExecutor gets executor by name
    23  func (c ExecutorClient) GetExecutor(name string) (executor testkube.ExecutorDetails, err error) {
    24  	uri := c.executorTransport.GetURI("/executors/%s", name)
    25  	return c.executorTransport.Execute(http.MethodGet, uri, nil, nil)
    26  }
    27  
    28  // ListExecutors list all executors
    29  func (c ExecutorClient) ListExecutors(selector string) (executors testkube.ExecutorsDetails, err error) {
    30  	uri := c.executorTransport.GetURI("/executors")
    31  	params := map[string]string{
    32  		"selector": selector,
    33  	}
    34  
    35  	return c.executorTransport.ExecuteMultiple(http.MethodGet, uri, nil, params)
    36  }
    37  
    38  // CreateExecutor creates new Executor Custom Resource
    39  func (c ExecutorClient) CreateExecutor(options UpsertExecutorOptions) (executor testkube.ExecutorDetails, err error) {
    40  	uri := c.executorTransport.GetURI("/executors")
    41  	request := testkube.ExecutorUpsertRequest(options)
    42  
    43  	body, err := json.Marshal(request)
    44  	if err != nil {
    45  		return executor, err
    46  	}
    47  
    48  	return c.executorTransport.Execute(http.MethodPost, uri, body, nil)
    49  }
    50  
    51  // UpdateExecutor updates Executor Custom Resource
    52  func (c ExecutorClient) UpdateExecutor(options UpdateExecutorOptions) (executor testkube.ExecutorDetails, err error) {
    53  	name := ""
    54  	if options.Name != nil {
    55  		name = *options.Name
    56  	}
    57  
    58  	uri := c.executorTransport.GetURI("/executors/%s", name)
    59  	request := testkube.ExecutorUpdateRequest(options)
    60  
    61  	body, err := json.Marshal(request)
    62  	if err != nil {
    63  		return executor, err
    64  	}
    65  
    66  	return c.executorTransport.Execute(http.MethodPatch, uri, body, nil)
    67  }
    68  
    69  // DeleteExecutors deletes all executors
    70  func (c ExecutorClient) DeleteExecutors(selector string) (err error) {
    71  	uri := c.executorTransport.GetURI("/executors")
    72  	return c.executorTransport.Delete(uri, selector, true)
    73  }
    74  
    75  // DeleteExecutor deletes single executor by name
    76  func (c ExecutorClient) DeleteExecutor(name string) (err error) {
    77  	uri := c.executorTransport.GetURI("/executors/%s", name)
    78  	return c.executorTransport.Delete(uri, "", true)
    79  }