podman

Форк
0
134 строки · 3.3 Кб
1
// Copyright 2016 go-dockerclient authors. All rights reserved.
2
// Use of this source code is governed by a BSD-style
3
// license that can be found in the LICENSE file.
4

5
package docker
6

7
import (
8
	"context"
9
	"encoding/json"
10
	"errors"
11
	"net/http"
12
	"net/url"
13
	"strconv"
14

15
	"github.com/docker/docker/api/types/swarm"
16
)
17

18
// NoSuchNode is the error returned when a given node does not exist.
19
type NoSuchNode struct {
20
	ID  string
21
	Err error
22
}
23

24
func (err *NoSuchNode) Error() string {
25
	if err.Err != nil {
26
		return err.Err.Error()
27
	}
28
	return "No such node: " + err.ID
29
}
30

31
// ListNodesOptions specify parameters to the ListNodes function.
32
//
33
// See http://goo.gl/3K4GwU for more details.
34
type ListNodesOptions struct {
35
	Filters map[string][]string
36
	Context context.Context
37
}
38

39
// ListNodes returns a slice of nodes matching the given criteria.
40
//
41
// See http://goo.gl/3K4GwU for more details.
42
func (c *Client) ListNodes(opts ListNodesOptions) ([]swarm.Node, error) {
43
	path := "/nodes?" + queryString(opts)
44
	resp, err := c.do(http.MethodGet, path, doOptions{context: opts.Context})
45
	if err != nil {
46
		return nil, err
47
	}
48
	defer resp.Body.Close()
49
	var nodes []swarm.Node
50
	if err := json.NewDecoder(resp.Body).Decode(&nodes); err != nil {
51
		return nil, err
52
	}
53
	return nodes, nil
54
}
55

56
// InspectNode returns information about a node by its ID.
57
//
58
// See http://goo.gl/WjkTOk for more details.
59
func (c *Client) InspectNode(id string) (*swarm.Node, error) {
60
	resp, err := c.do(http.MethodGet, "/nodes/"+id, doOptions{})
61
	if err != nil {
62
		var e *Error
63
		if errors.As(err, &e) && e.Status == http.StatusNotFound {
64
			return nil, &NoSuchNode{ID: id}
65
		}
66
		return nil, err
67
	}
68
	defer resp.Body.Close()
69
	var node swarm.Node
70
	if err := json.NewDecoder(resp.Body).Decode(&node); err != nil {
71
		return nil, err
72
	}
73
	return &node, nil
74
}
75

76
// UpdateNodeOptions specify parameters to the NodeUpdate function.
77
//
78
// See http://goo.gl/VPBFgA for more details.
79
type UpdateNodeOptions struct {
80
	swarm.NodeSpec
81
	Version uint64
82
	Context context.Context
83
}
84

85
// UpdateNode updates a node.
86
//
87
// See http://goo.gl/VPBFgA for more details.
88
func (c *Client) UpdateNode(id string, opts UpdateNodeOptions) error {
89
	params := make(url.Values)
90
	params.Set("version", strconv.FormatUint(opts.Version, 10))
91
	path := "/nodes/" + id + "/update?" + params.Encode()
92
	resp, err := c.do(http.MethodPost, path, doOptions{
93
		context:   opts.Context,
94
		forceJSON: true,
95
		data:      opts.NodeSpec,
96
	})
97
	if err != nil {
98
		var e *Error
99
		if errors.As(err, &e) && e.Status == http.StatusNotFound {
100
			return &NoSuchNode{ID: id}
101
		}
102
		return err
103
	}
104
	resp.Body.Close()
105
	return nil
106
}
107

108
// RemoveNodeOptions specify parameters to the RemoveNode function.
109
//
110
// See http://goo.gl/0SNvYg for more details.
111
type RemoveNodeOptions struct {
112
	ID      string
113
	Force   bool
114
	Context context.Context
115
}
116

117
// RemoveNode removes a node.
118
//
119
// See http://goo.gl/0SNvYg for more details.
120
func (c *Client) RemoveNode(opts RemoveNodeOptions) error {
121
	params := make(url.Values)
122
	params.Set("force", strconv.FormatBool(opts.Force))
123
	path := "/nodes/" + opts.ID + "?" + params.Encode()
124
	resp, err := c.do(http.MethodDelete, path, doOptions{context: opts.Context})
125
	if err != nil {
126
		var e *Error
127
		if errors.As(err, &e) && e.Status == http.StatusNotFound {
128
			return &NoSuchNode{ID: opts.ID}
129
		}
130
		return err
131
	}
132
	resp.Body.Close()
133
	return nil
134
}
135

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.