-
Notifications
You must be signed in to change notification settings - Fork 204
/
simulation.go
99 lines (76 loc) · 2.29 KB
/
simulation.go
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
package wrapper
import (
"encoding/json"
"io/ioutil"
"fmt"
"net/url"
"github.com/SpectoLabs/hoverfly/core/handlers/v2"
"github.com/SpectoLabs/hoverfly/hoverctl/configuration"
)
func ExportSimulation(target configuration.Target, urlPattern string) (v2.SimulationViewV5, error) {
view := v2.SimulationViewV5{}
requestUrl := v2ApiSimulation
if len(urlPattern) > 0 {
requestUrl = fmt.Sprintf("%s?urlPattern=%s", requestUrl, url.QueryEscape(urlPattern))
}
response, err := doRequest(target, "GET", requestUrl, "", nil)
if err != nil {
return view, err
}
defer response.Body.Close()
err = handleResponseError(response, "Could not retrieve simulation")
if err != nil {
return view, err
}
err = json.NewDecoder(response.Body).Decode(&view)
return view, err
}
func ImportSimulation(target configuration.Target, simulationData string) error {
response, err := doRequest(target, "PUT", v2ApiSimulation, simulationData, nil)
if err != nil {
return err
}
err = handleResponseError(response, "Could not import simulation")
if err != nil {
return err
}
responseBytes, _ := ioutil.ReadAll(response.Body)
result := &v2.SimulationImportResult{}
json.Unmarshal(responseBytes, result)
for _, warning := range result.WarningMessages {
fmt.Println(warning.Message)
fmt.Println(warning.DocsLink + "\n")
}
return nil
}
func AddSimulation(target configuration.Target, simulationData string) error {
response, err := doRequest(target, "POST", v2ApiSimulation, simulationData, nil)
if err != nil {
return err
}
err = handleResponseError(response, "Could not add simulation")
if err != nil {
return err
}
responseBytes, _ := ioutil.ReadAll(response.Body)
result := &v2.SimulationImportResult{}
json.Unmarshal(responseBytes, result)
for _, warning := range result.WarningMessages {
fmt.Println(warning.Message)
fmt.Println(warning.DocsLink + "\n")
}
return nil
}
// Wipe will call the records endpoint in Hoverfly with a DELETE request, triggering Hoverfly to wipe the database
func DeleteSimulations(target configuration.Target) error {
response, err := doRequest(target, "DELETE", v2ApiSimulation, "", nil)
if err != nil {
return err
}
defer response.Body.Close()
err = handleResponseError(response, "Could not delete simulation")
if err != nil {
return err
}
return nil
}