• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

mendersoftware / inventory / 1401701972

05 Aug 2024 08:32PM UTC coverage: 91.217%. Remained the same
1401701972

push

gitlab-ci

web-flow
Merge pull request #460 from mendersoftware/dependabot/docker/docker-dependencies-03b04ac819

chore: bump golang from 1.22.4-alpine3.19 to 1.22.5-alpine3.19 in the docker-dependencies group

3095 of 3393 relevant lines covered (91.22%)

148.68 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

96.3
/client/devicemonitor/client.go
1
// Copyright 2023 Northern.tech AS
2
//
3
//    Licensed under the Apache License, Version 2.0 (the "License");
4
//    you may not use this file except in compliance with the License.
5
//    You may obtain a copy of the License at
6
//
7
//        http://www.apache.org/licenses/LICENSE-2.0
8
//
9
//    Unless required by applicable law or agreed to in writing, software
10
//    distributed under the License is distributed on an "AS IS" BASIS,
11
//    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
//    See the License for the specific language governing permissions and
13
//    limitations under the License.
14

15
package devicemonitor
16

17
import (
18
        "context"
19
        "encoding/json"
20
        "net/http"
21
        "strings"
22
        "time"
23

24
        "github.com/pkg/errors"
25

26
        "github.com/mendersoftware/go-lib-micro/identity"
27
)
28

29
const (
30
        AlertsURI = "/api/internal/v1/devicemonitor/tenants/#tenant_id/devices/#device_id/alerts/latest"
31
)
32

33
const (
34
        defaultTimeout = time.Duration(5) * time.Second
35
)
36

37
// Client is the devicemonitor client
38
//
39
//go:generate ../../utils/mockgen.sh
40
type Client interface {
41
        CheckAlerts(c context.Context, device string) (int, error)
42
}
43

44
type ClientOptions struct {
45
        Client *http.Client
46
}
47

48
// NewClient returns a new workflows client
49
func NewClient(url string, opts ...ClientOptions) Client {
3✔
50
        // Initialize default options
3✔
51
        var clientOpts = ClientOptions{
3✔
52
                Client: &http.Client{},
3✔
53
        }
3✔
54
        // Merge options
3✔
55
        for _, opt := range opts {
5✔
56
                if opt.Client != nil {
4✔
57
                        clientOpts.Client = opt.Client
2✔
58
                }
2✔
59
        }
60

61
        return &client{
3✔
62
                url:    strings.TrimSuffix(url, "/"),
3✔
63
                client: *clientOpts.Client,
3✔
64
        }
3✔
65
}
66

67
type client struct {
68
        url    string
69
        client http.Client
70
}
71

72
func (c *client) CheckAlerts(ctx context.Context, device string) (int, error) {
12✔
73
        if _, ok := ctx.Deadline(); !ok {
22✔
74
                var cancel context.CancelFunc
10✔
75
                ctx, cancel = context.WithTimeout(ctx, defaultTimeout)
10✔
76
                defer cancel()
10✔
77
        }
10✔
78
        id := identity.FromContext(ctx)
12✔
79
        tenant := ""
12✔
80
        if id != nil {
14✔
81
                tenant = id.Tenant
2✔
82
        }
2✔
83
        repl := strings.NewReplacer("#device_id", device,
12✔
84
                "#tenant_id", tenant)
12✔
85
        req, err := http.NewRequestWithContext(ctx,
12✔
86
                "GET",
12✔
87
                c.url+repl.Replace(AlertsURI),
12✔
88
                nil,
12✔
89
        )
12✔
90
        if err != nil {
12✔
91
                return -1, errors.Wrap(err, "devicemonitor: error preparing HTTP request")
×
92
        }
×
93

94
        req.Header.Set("Content-Type", "application/json")
12✔
95

12✔
96
        rsp, err := c.client.Do(req)
12✔
97
        if err != nil {
14✔
98
                return -1, errors.Wrap(err, "devicemonitor: failed to get alerts for the device")
2✔
99
        }
2✔
100
        defer rsp.Body.Close()
10✔
101

10✔
102
        if rsp.StatusCode != http.StatusOK {
12✔
103
                return -1, errors.Errorf(
2✔
104
                        "devicemonitor: unexpected HTTP status from devicemonitor service: %s",
2✔
105
                        rsp.Status,
2✔
106
                )
2✔
107
        }
2✔
108
        alerts := Alerts{}
8✔
109
        if err := json.NewDecoder(rsp.Body).Decode(&alerts); err != nil {
10✔
110
                return -1, errors.Wrap(err, "error parsing alerts")
2✔
111
        }
2✔
112

113
        return len(alerts), nil
6✔
114
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc