File: proxy.go

package info (click to toggle)
golang-github-ibm-sarama 1.45.1-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 2,964 kB
  • sloc: makefile: 35; sh: 19
file content (114 lines) | stat: -rw-r--r-- 2,793 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package toxiproxy

import (
	"bytes"
	"encoding/json"
	"fmt"
	"io"
	"net/http"
)

type Proxy struct {
	client     *Client
	Name       string `json:"name"`
	ListenAddr string `json:"listen"`
	TargetAddr string `json:"upstream"`
	Enabled    bool   `json:"enabled"`
}

type Attributes map[string]int

func (p *Proxy) AddToxic(
	name string,
	toxicType string,
	stream string,
	toxicity float32,
	attributes Attributes,
) (*Toxic, error) {
	toxic := &Toxic{
		Name:       name,
		Type:       toxicType,
		Stream:     stream,
		Toxicity:   toxicity,
		Attributes: attributes,
	}
	var b bytes.Buffer
	if err := json.NewEncoder(&b).Encode(&toxic); err != nil {
		return nil, fmt.Errorf("failed to json encode toxic: %w", err)
	}
	body := bytes.NewReader(b.Bytes())

	c := p.client
	req, err := http.NewRequest("POST", c.endpoint+"/proxies/"+p.Name+"/toxics", body)
	if err != nil {
		return nil, fmt.Errorf("failed to make post toxic request: %w", err)
	}
	req.Header.Set("Content-Type", "application/json")
	resp, err := c.httpClient.Do(req)
	if err != nil {
		return nil, fmt.Errorf("failed to http post toxic: %w", err)
	}
	defer resp.Body.Close()

	if resp.StatusCode != 200 {
		body, _ := io.ReadAll(resp.Body)
		return nil, fmt.Errorf("error creating toxic %s: %s %s", name, resp.Status, body)
	}

	return toxic, nil
}

func (p *Proxy) Enable() error {
	p.Enabled = true
	_, err := p.Save()
	return err
}

func (p *Proxy) Disable() error {
	p.Enabled = false
	_, err := p.Save()
	return err
}

func (p *Proxy) Save() (*Proxy, error) {
	var b bytes.Buffer
	if err := json.NewEncoder(&b).Encode(&p); err != nil {
		return nil, fmt.Errorf("failed to json encode proxy: %w", err)
	}
	body := bytes.NewReader(b.Bytes())

	c := p.client
	req, err := http.NewRequest("POST", c.endpoint+"/proxies/"+p.Name, body)
	if err != nil {
		return nil, fmt.Errorf("failed to make post proxy request: %w", err)
	}
	req.Header.Set("Content-Type", "application/json")
	resp, err := c.httpClient.Do(req)
	if err != nil {
		return nil, fmt.Errorf("failed to http post proxy: %w", err)
	}
	defer resp.Body.Close()

	if resp.StatusCode == 404 {
		if _, err := body.Seek(0, io.SeekStart); err != nil {
			return nil, fmt.Errorf("failed to rewind post body: %w", err)
		}
		req, err = http.NewRequest("POST", c.endpoint+"/proxies", body)
		if err != nil {
			return nil, fmt.Errorf("failed to make post proxy request: %w", err)
		}
		req.Header.Set("Content-Type", "application/json")
		resp, err = c.httpClient.Do(req)
		if err != nil {
			return nil, fmt.Errorf("failed to http post proxy: %w", err)
		}
		defer resp.Body.Close()
	}

	if resp.StatusCode != 200 && resp.StatusCode != 201 {
		body, _ := io.ReadAll(resp.Body)
		return nil, fmt.Errorf("error saving proxy: %s %s", resp.Status, body)
	}

	return p, nil
}