github.com/kubeshop/testkube@v1.17.23/pkg/api/v1/client/webhook.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 // NewWebhookClient creates new Webhook client 11 func NewWebhookClient(webhookTransport Transport[testkube.Webhook]) WebhookClient { 12 return WebhookClient{ 13 webhookTransport: webhookTransport, 14 } 15 } 16 17 // WebhookClient is a client for webhooks 18 type WebhookClient struct { 19 webhookTransport Transport[testkube.Webhook] 20 } 21 22 // GetWebhook gets webhook by name 23 func (c WebhookClient) GetWebhook(name string) (webhook testkube.Webhook, err error) { 24 uri := c.webhookTransport.GetURI("/webhooks/%s", name) 25 return c.webhookTransport.Execute(http.MethodGet, uri, nil, nil) 26 } 27 28 // ListWebhooks list all webhooks 29 func (c WebhookClient) ListWebhooks(selector string) (webhooks testkube.Webhooks, err error) { 30 uri := c.webhookTransport.GetURI("/webhooks") 31 params := map[string]string{ 32 "selector": selector, 33 } 34 35 return c.webhookTransport.ExecuteMultiple(http.MethodGet, uri, nil, params) 36 } 37 38 // CreateWebhook creates new Webhook Custom Resource 39 func (c WebhookClient) CreateWebhook(options CreateWebhookOptions) (webhook testkube.Webhook, err error) { 40 uri := c.webhookTransport.GetURI("/webhooks") 41 request := testkube.WebhookCreateRequest(options) 42 43 body, err := json.Marshal(request) 44 if err != nil { 45 return webhook, err 46 } 47 48 return c.webhookTransport.Execute(http.MethodPost, uri, body, nil) 49 } 50 51 // UpdateWebhook updates Webhook Custom Resource 52 func (c WebhookClient) UpdateWebhook(options UpdateWebhookOptions) (webhook testkube.Webhook, err error) { 53 name := "" 54 if options.Name != nil { 55 name = *options.Name 56 } 57 58 uri := c.webhookTransport.GetURI("/webhooks/%s", name) 59 request := testkube.WebhookUpdateRequest(options) 60 61 body, err := json.Marshal(request) 62 if err != nil { 63 return webhook, err 64 } 65 66 return c.webhookTransport.Execute(http.MethodPatch, uri, body, nil) 67 } 68 69 // DeleteWebhooks deletes all webhooks 70 func (c WebhookClient) DeleteWebhooks(selector string) (err error) { 71 uri := c.webhookTransport.GetURI("/webhooks") 72 return c.webhookTransport.Delete(uri, selector, true) 73 } 74 75 // DeleteWebhook deletes single webhook by name 76 func (c WebhookClient) DeleteWebhook(name string) (err error) { 77 uri := c.webhookTransport.GetURI("/webhooks/%s", name) 78 return c.webhookTransport.Delete(uri, "", true) 79 }