forked from cloudfoundry-attic/garden-linux
/
client.go
75 lines (58 loc) · 1.54 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
package client
import (
"errors"
"fmt"
"github.com/cloudfoundry-incubator/garden/api"
"github.com/cloudfoundry-incubator/garden/client/connection"
)
type Client interface {
api.Client
}
var ErrContainerNotFound = errors.New("container not found")
type client struct {
connection connection.Connection
}
func New(connection connection.Connection) Client {
return &client{
connection: connection,
}
}
func (client *client) Ping() error {
return client.connection.Ping()
}
func (client *client) Capacity() (api.Capacity, error) {
return client.connection.Capacity()
}
func (client *client) Create(spec api.ContainerSpec) (api.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 api.Properties) ([]api.Container, error) {
handles, err := client.connection.List(properties)
if err != nil {
return nil, err
}
containers := []api.Container{}
for _, handle := range handles {
containers = append(containers, newContainer(handle, client.connection))
}
return containers, nil
}
func (client *client) Destroy(handle string) error {
return client.connection.Destroy(handle)
}
func (client *client) Lookup(handle string) (api.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, fmt.Errorf("container not found: %s", handle)
}