aboutsummaryrefslogtreecommitdiffstats
path: root/library/go/yandex/deploy/podagent/client.go
blob: 8f87d0e6824f8671f171dfd0b33da9957e44f083 (plain) (blame)
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
package podagent

import (
	"context"
	"time"

	"github.com/go-resty/resty/v2"
	"github.com/ydb-platform/ydb/library/go/core/xerrors"
	"github.com/ydb-platform/ydb/library/go/httputil/headers"
)

const (
	EndpointURL = "http://127.0.0.1:1/"
	HTTPTimeout = 500 * time.Millisecond
)

type Client struct {
	httpc *resty.Client
}

func NewClient(opts ...Option) *Client {
	c := &Client{
		httpc: resty.New().
			SetBaseURL(EndpointURL).
			SetTimeout(HTTPTimeout),
	}

	for _, opt := range opts {
		opt(c)
	}
	return c
}

// PodAttributes returns current pod attributes.
//
// Documentation: https://deploy.yandex-team.ru/docs/reference/api/pod-agent-public-api#localhost:1pod_attributes
func (c *Client) PodAttributes(ctx context.Context) (rsp PodAttributesResponse, err error) {
	err = c.call(ctx, "/pod_attributes", &rsp)
	return
}

// PodStatus returns current pod status.
//
// Documentation: https://deploy.yandex-team.ru/docs/reference/api/pod-agent-public-api#localhost:1pod_status
func (c *Client) PodStatus(ctx context.Context) (rsp PodStatusResponse, err error) {
	err = c.call(ctx, "/pod_status", &rsp)
	return
}

func (c *Client) call(ctx context.Context, handler string, result interface{}) error {
	rsp, err := c.httpc.R().
		SetContext(ctx).
		ExpectContentType(headers.TypeApplicationJSON.String()).
		SetResult(&result).
		Get(handler)

	if err != nil {
		return xerrors.Errorf("failed to request pod agent API: %w", err)
	}

	if !rsp.IsSuccess() {
		return xerrors.Errorf("unexpected status code: %d", rsp.StatusCode())
	}

	return nil
}