Filip Tehlar | 229f5fc | 2022-08-09 14:44:47 +0000 | [diff] [blame] | 1 | package main |
| 2 | |
| 3 | import ( |
Filip Tehlar | 229f5fc | 2022-08-09 14:44:47 +0000 | [diff] [blame] | 4 | "fmt" |
| 5 | "io" |
Matus Fabian | 5409d33 | 2024-05-28 13:39:13 +0200 | [diff] [blame] | 6 | "net/http" |
Filip Tehlar | 229f5fc | 2022-08-09 14:44:47 +0000 | [diff] [blame] | 7 | "os" |
Filip Tehlar | 229f5fc | 2022-08-09 14:44:47 +0000 | [diff] [blame] | 8 | "strings" |
Matus Fabian | 5409d33 | 2024-05-28 13:39:13 +0200 | [diff] [blame] | 9 | "time" |
Filip Tehlar | 229f5fc | 2022-08-09 14:44:47 +0000 | [diff] [blame] | 10 | ) |
| 11 | |
Maros Ondrejicka | e7625d0 | 2023-02-28 16:55:01 +0100 | [diff] [blame] | 12 | const networkTopologyDir string = "topo-network/" |
| 13 | const containerTopologyDir string = "topo-containers/" |
Filip Tehlar | 229f5fc | 2022-08-09 14:44:47 +0000 | [diff] [blame] | 14 | |
| 15 | type Stanza struct { |
| 16 | content string |
| 17 | pad int |
| 18 | } |
| 19 | |
| 20 | type ActionResult struct { |
| 21 | Err error |
| 22 | Desc string |
| 23 | ErrOutput string |
| 24 | StdOutput string |
| 25 | } |
| 26 | |
| 27 | type JsonResult struct { |
| 28 | Code int |
| 29 | Desc string |
| 30 | ErrOutput string |
| 31 | StdOutput string |
| 32 | } |
| 33 | |
Filip Tehlar | 229f5fc | 2022-08-09 14:44:47 +0000 | [diff] [blame] | 34 | func assertFileSize(f1, f2 string) error { |
| 35 | fi1, err := os.Stat(f1) |
| 36 | if err != nil { |
| 37 | return err |
| 38 | } |
| 39 | |
| 40 | fi2, err1 := os.Stat(f2) |
| 41 | if err1 != nil { |
| 42 | return err1 |
| 43 | } |
| 44 | |
| 45 | if fi1.Size() != fi2.Size() { |
| 46 | return fmt.Errorf("file sizes differ (%d vs %d)", fi1.Size(), fi2.Size()) |
| 47 | } |
| 48 | return nil |
| 49 | } |
| 50 | |
Maros Ondrejicka | e7625d0 | 2023-02-28 16:55:01 +0100 | [diff] [blame] | 51 | func (c *Stanza) newStanza(name string) *Stanza { |
| 52 | c.append("\n" + name + " {") |
Filip Tehlar | 229f5fc | 2022-08-09 14:44:47 +0000 | [diff] [blame] | 53 | c.pad += 2 |
| 54 | return c |
| 55 | } |
| 56 | |
Maros Ondrejicka | e7625d0 | 2023-02-28 16:55:01 +0100 | [diff] [blame] | 57 | func (c *Stanza) append(name string) *Stanza { |
Filip Tehlar | 229f5fc | 2022-08-09 14:44:47 +0000 | [diff] [blame] | 58 | c.content += strings.Repeat(" ", c.pad) |
| 59 | c.content += name + "\n" |
| 60 | return c |
| 61 | } |
| 62 | |
Maros Ondrejicka | e7625d0 | 2023-02-28 16:55:01 +0100 | [diff] [blame] | 63 | func (c *Stanza) close() *Stanza { |
Filip Tehlar | 229f5fc | 2022-08-09 14:44:47 +0000 | [diff] [blame] | 64 | c.content += "}\n" |
| 65 | c.pad -= 2 |
| 66 | return c |
| 67 | } |
| 68 | |
Maros Ondrejicka | e7625d0 | 2023-02-28 16:55:01 +0100 | [diff] [blame] | 69 | func (s *Stanza) toString() string { |
Filip Tehlar | 229f5fc | 2022-08-09 14:44:47 +0000 | [diff] [blame] | 70 | return s.content |
| 71 | } |
| 72 | |
Maros Ondrejicka | e7625d0 | 2023-02-28 16:55:01 +0100 | [diff] [blame] | 73 | func (s *Stanza) saveToFile(fileName string) error { |
Filip Tehlar | 229f5fc | 2022-08-09 14:44:47 +0000 | [diff] [blame] | 74 | fo, err := os.Create(fileName) |
| 75 | if err != nil { |
| 76 | return err |
| 77 | } |
| 78 | defer fo.Close() |
| 79 | |
| 80 | _, err = io.Copy(fo, strings.NewReader(s.content)) |
| 81 | return err |
| 82 | } |
Matus Fabian | 5409d33 | 2024-05-28 13:39:13 +0200 | [diff] [blame] | 83 | |
| 84 | // newHttpClient creates [http.Client] with disabled proxy and redirects, it also sets timeout to 30seconds. |
| 85 | func newHttpClient() *http.Client { |
| 86 | transport := http.DefaultTransport |
| 87 | transport.(*http.Transport).Proxy = nil |
| 88 | transport.(*http.Transport).DisableKeepAlives = true |
| 89 | client := &http.Client{ |
| 90 | Transport: transport, |
| 91 | Timeout: time.Second * 30, |
| 92 | CheckRedirect: func(req *http.Request, via []*http.Request) error { |
| 93 | return http.ErrUseLastResponse |
| 94 | }} |
| 95 | return client |
| 96 | } |