-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathclient_test.go
More file actions
125 lines (104 loc) · 2.99 KB
/
client_test.go
File metadata and controls
125 lines (104 loc) · 2.99 KB
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
115
116
117
118
119
120
121
122
123
124
125
package cycle_test
import (
"context"
"fmt"
"log"
"net/http"
"os"
"testing"
cycle "github.com/cycleplatform/api-client-go"
)
func TestClient_canCall(t *testing.T) {
apiKey := os.Getenv("CYCLE_API_KEY")
if apiKey == "" {
log.Fatal("missing env var CYCLE_API_KEY")
}
hubId := os.Getenv("CYCLE_HUB_ID")
if hubId == "" {
log.Fatal("missing env var CYCLE_HUB_ID")
}
baseUrl := os.Getenv("CYCLE_BASE_URL")
if baseUrl == "" {
baseUrl = "https://api.cycle.io"
}
c, err := cycle.NewAuthenticatedClient(cycle.ClientConfig{
BaseURL: &baseUrl,
APIKey: apiKey,
HubID: hubId,
})
if err != nil {
log.Fatal(err)
}
// Starting a container
{
var task cycle.ContainerTask
err := task.FromContainerStartAction(cycle.ContainerStartAction{Action: cycle.ContainerStartActionActionStart})
if err != nil {
log.Fatal(err)
}
res, err := c.CreateContainerJobWithResponse(context.TODO(), "678739ac492d4c2033df7a7c", task)
if err != nil {
log.Fatal(err)
}
if res.StatusCode() != http.StatusAccepted {
log.Fatalf("expected status code 202, got %v (%s)", res.StatusCode(), *res.JSONDefault.Error.Title)
}
fmt.Printf("Started container - Job ID %s\n", res.JSON202.Data.Job.Id)
}
// Listing environments with discriminated union LB service
{
resp, err := c.GetEnvironmentsWithResponse(context.TODO(), &cycle.GetEnvironmentsParams{})
if err != nil {
log.Fatal(err)
}
if resp.StatusCode() != http.StatusOK {
log.Fatalf("Expected HTTP 200 but received %d %s", resp.StatusCode(), *resp.JSONDefault.Error.Title)
}
for _, v := range resp.JSON200.Data {
fmt.Printf("Environment ID: %s - Name: %s\n", v.Id, v.Name)
if v.Services.Loadbalancer == nil {
fmt.Printf(" No Loadbalancer\n")
continue
}
if v.Services.Loadbalancer.Config == nil {
fmt.Printf(" No Loadbalancer Config Set\n")
continue
}
d, err := v.Services.Loadbalancer.Config.Discriminator()
if err != nil {
log.Printf(" Error discrimining loadbalancer: %v\n", err)
continue
}
fmt.Printf("discriminator type: %s\n", d)
value, err := v.Services.Loadbalancer.Config.ValueByDiscriminator()
if err != nil {
fmt.Printf(" Error getting loadbalancer value: %v\n", err)
continue
}
switch v := value.(type) {
case cycle.V1LbType:
fmt.Println("Using V1 Loadbalancer")
case cycle.HaProxyLbType:
fmt.Println("Using HAProxy Loadbalancer")
case cycle.DefaultLbType:
fmt.Println("Using Default Loadbalancer")
default:
log.Fatalf("Unknown load balancer type %#v", v)
}
}
}
// Updating a container name
{
name := "updated container name"
resp, err := c.UpdateContainerWithResponse(context.TODO(), "67a18129133949a588564f12", cycle.UpdateContainerJSONRequestBody{
Name: &name,
})
if err != nil {
log.Fatal(err)
}
if resp.StatusCode() != http.StatusOK {
log.Fatalf("Expected HTTP 200 but received %d %s", resp.StatusCode(), *resp.JSONDefault.Error.Title)
}
fmt.Printf("Name updated to %s\n", resp.JSON200.Data.Name)
}
}