forked from concourse/concourse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
132 lines (107 loc) · 3.58 KB
/
client.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
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
126
127
128
129
130
131
132
package gclient
import (
"code.cloudfoundry.org/garden"
"github.com/chenbh/concourse/v6/atc/worker/gclient/connection"
)
//go:generate counterfeiter . Client
type Client interface {
// Pings the garden server. Checks connectivity to the server. The server may, optionally, respond with specific
// errors indicating health issues.
//
// Errors:
// * garden.UnrecoverableError indicates that the garden server has entered an error state from which it cannot recover
Ping() error
// Capacity returns the physical capacity of the server's machine.
//
// Errors:
// * None.
Capacity() (garden.Capacity, error)
// Create creates a new container.
//
// Errors:
// * When the handle, if specified, is already taken.
// * When one of the bind_mount paths does not exist.
// * When resource allocations fail (subnet, user ID, etc).
Create(garden.ContainerSpec) (Container, error)
// Destroy destroys a container.
//
// When a container is destroyed, its resource allocations are released,
// its filesystem is removed, and all references to its handle are removed.
//
// All resources that have been acquired during the lifetime of the container are released.
// Examples of these resources are its subnet, its UID, and ports that were redirected to the container.
//
// TODO: list the resources that can be acquired during the lifetime of a container.
//
// Errors:
// * TODO.
Destroy(handle string) error
// Containers lists all containers filtered by Properties (which are ANDed together).
//
// Errors:
// * None.
Containers(garden.Properties) ([]Container, error)
// BulkInfo returns info or error for a list of containers.
BulkInfo(handles []string) (map[string]garden.ContainerInfoEntry, error)
// BulkMetrics returns metrics or error for a list of containers.
BulkMetrics(handles []string) (map[string]garden.ContainerMetricsEntry, error)
// Lookup returns the container with the specified handle.
//
// Errors:
// * Container not found.
Lookup(handle string) (Container, error)
}
type client struct {
connection connection.Connection
}
func NewClient(connection connection.Connection) Client {
return &client{
connection: connection,
}
}
func (client *client) Ping() error {
return client.connection.Ping()
}
func (client *client) Capacity() (garden.Capacity, error) {
return client.connection.Capacity()
}
func (client *client) Create(spec garden.ContainerSpec) (Container, error) {
handle, err := client.connection.Create(spec)
if err != nil {
return nil, err
}
return newContainer(handle, client.connection), nil
}
func (client *client) Containers(properties garden.Properties) ([]Container, error) {
handles, err := client.connection.List(properties)
if err != nil {
return nil, err
}
containers := []Container{}
for _, handle := range handles {
containers = append(containers, newContainer(handle, client.connection))
}
return containers, nil
}
func (client *client) Destroy(handle string) error {
err := client.connection.Destroy(handle)
return err
}
func (client *client) BulkInfo(handles []string) (map[string]garden.ContainerInfoEntry, error) {
return client.connection.BulkInfo(handles)
}
func (client *client) BulkMetrics(handles []string) (map[string]garden.ContainerMetricsEntry, error) {
return client.connection.BulkMetrics(handles)
}
func (client *client) Lookup(handle string) (Container, error) {
handles, err := client.connection.List(nil)
if err != nil {
return nil, err
}
for _, h := range handles {
if h == handle {
return newContainer(handle, client.connection), nil
}
}
return nil, garden.ContainerNotFoundError{Handle: handle}
}