podman
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
5package docker6
7import (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.
19type NoSuchNode struct {20ID string21Err error22}
23
24func (err *NoSuchNode) Error() string {25if err.Err != nil {26return err.Err.Error()27}28return "No such node: " + err.ID29}
30
31// ListNodesOptions specify parameters to the ListNodes function.
32//
33// See http://goo.gl/3K4GwU for more details.
34type ListNodesOptions struct {35Filters map[string][]string36Context context.Context37}
38
39// ListNodes returns a slice of nodes matching the given criteria.
40//
41// See http://goo.gl/3K4GwU for more details.
42func (c *Client) ListNodes(opts ListNodesOptions) ([]swarm.Node, error) {43path := "/nodes?" + queryString(opts)44resp, err := c.do(http.MethodGet, path, doOptions{context: opts.Context})45if err != nil {46return nil, err47}48defer resp.Body.Close()49var nodes []swarm.Node50if err := json.NewDecoder(resp.Body).Decode(&nodes); err != nil {51return nil, err52}53return nodes, nil54}
55
56// InspectNode returns information about a node by its ID.
57//
58// See http://goo.gl/WjkTOk for more details.
59func (c *Client) InspectNode(id string) (*swarm.Node, error) {60resp, err := c.do(http.MethodGet, "/nodes/"+id, doOptions{})61if err != nil {62var e *Error63if errors.As(err, &e) && e.Status == http.StatusNotFound {64return nil, &NoSuchNode{ID: id}65}66return nil, err67}68defer resp.Body.Close()69var node swarm.Node70if err := json.NewDecoder(resp.Body).Decode(&node); err != nil {71return nil, err72}73return &node, nil74}
75
76// UpdateNodeOptions specify parameters to the NodeUpdate function.
77//
78// See http://goo.gl/VPBFgA for more details.
79type UpdateNodeOptions struct {80swarm.NodeSpec81Version uint6482Context context.Context83}
84
85// UpdateNode updates a node.
86//
87// See http://goo.gl/VPBFgA for more details.
88func (c *Client) UpdateNode(id string, opts UpdateNodeOptions) error {89params := make(url.Values)90params.Set("version", strconv.FormatUint(opts.Version, 10))91path := "/nodes/" + id + "/update?" + params.Encode()92resp, err := c.do(http.MethodPost, path, doOptions{93context: opts.Context,94forceJSON: true,95data: opts.NodeSpec,96})97if err != nil {98var e *Error99if errors.As(err, &e) && e.Status == http.StatusNotFound {100return &NoSuchNode{ID: id}101}102return err103}104resp.Body.Close()105return nil106}
107
108// RemoveNodeOptions specify parameters to the RemoveNode function.
109//
110// See http://goo.gl/0SNvYg for more details.
111type RemoveNodeOptions struct {112ID string113Force bool114Context context.Context115}
116
117// RemoveNode removes a node.
118//
119// See http://goo.gl/0SNvYg for more details.
120func (c *Client) RemoveNode(opts RemoveNodeOptions) error {121params := make(url.Values)122params.Set("force", strconv.FormatBool(opts.Force))123path := "/nodes/" + opts.ID + "?" + params.Encode()124resp, err := c.do(http.MethodDelete, path, doOptions{context: opts.Context})125if err != nil {126var e *Error127if errors.As(err, &e) && e.Status == http.StatusNotFound {128return &NoSuchNode{ID: opts.ID}129}130return err131}132resp.Body.Close()133return nil134}
135