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

mendersoftware / inventory / 1358092525

01 Jul 2024 10:03AM UTC coverage: 91.217%. Remained the same
1358092525

push

gitlab-ci

web-flow
Merge pull request #459 from mendersoftware/dependabot/docker/docker-dependencies-0bdf6079d6

chore: bump golang from 1.22.3-alpine3.19 to 1.22.4-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

97.83
/client/workflows/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 workflows
16

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

25
        "github.com/pkg/errors"
26

27
        "github.com/mendersoftware/go-lib-micro/identity"
28
        "github.com/mendersoftware/go-lib-micro/requestid"
29
        "github.com/mendersoftware/go-lib-micro/rest_utils"
30

31
        "github.com/mendersoftware/inventory/model"
32
        "github.com/mendersoftware/inventory/utils"
33
)
34

35
const (
36
        ReindexURI = "/api/v1/workflow/reindex_reporting/batch"
37
        HealthURI  = "/api/v1/health"
38
)
39

40
const (
41
        defaultTimeout = time.Duration(5) * time.Second
42
)
43

44
// Client is the workflows client
45
//
46
//go:generate ../../utils/mockgen.sh
47
type Client interface {
48
        CheckHealth(ctx context.Context) error
49
        StartReindex(c context.Context, deviceIDs []model.DeviceID) error
50
}
51

52
type ClientOptions struct {
53
        Client *http.Client
54
}
55

56
// NewClient returns a new workflows client
57
func NewClient(url string, opts ...ClientOptions) Client {
10✔
58
        // Initialize default options
10✔
59
        var clientOpts = ClientOptions{
10✔
60
                Client: &http.Client{},
10✔
61
        }
10✔
62
        // Merge options
10✔
63
        for _, opt := range opts {
12✔
64
                if opt.Client != nil {
4✔
65
                        clientOpts.Client = opt.Client
2✔
66
                }
2✔
67
        }
68

69
        return &client{
10✔
70
                url:    strings.TrimSuffix(url, "/"),
10✔
71
                client: *clientOpts.Client,
10✔
72
        }
10✔
73
}
74

75
type client struct {
76
        url    string
77
        client http.Client
78
}
79

80
func (c *client) StartReindex(ctx context.Context, deviceIDs []model.DeviceID) error {
8✔
81
        if _, ok := ctx.Deadline(); !ok {
16✔
82
                var cancel context.CancelFunc
8✔
83
                ctx, cancel = context.WithTimeout(ctx, defaultTimeout)
8✔
84
                defer cancel()
8✔
85
        }
8✔
86
        tenantID := ""
8✔
87
        if id := identity.FromContext(ctx); id != nil {
16✔
88
                tenantID = id.Tenant
8✔
89
        }
8✔
90
        wflow := make([]ReindexWorkflow, len(deviceIDs))
8✔
91
        for i, deviceID := range deviceIDs {
16✔
92
                wflow[i] = ReindexWorkflow{
8✔
93
                        RequestID: requestid.FromContext(ctx),
8✔
94
                        TenantID:  tenantID,
8✔
95
                        DeviceID:  string(deviceID),
8✔
96
                        Service:   ServiceInventory,
8✔
97
                }
8✔
98
        }
8✔
99
        payload, _ := json.Marshal(wflow)
8✔
100
        req, err := http.NewRequestWithContext(ctx,
8✔
101
                "POST",
8✔
102
                c.url+ReindexURI,
8✔
103
                bytes.NewReader(payload),
8✔
104
        )
8✔
105
        if err != nil {
8✔
106
                return errors.Wrap(err, "workflows: error preparing HTTP request")
×
107
        }
×
108

109
        req.Header.Set("Content-Type", "application/json")
8✔
110

8✔
111
        rsp, err := c.client.Do(req)
8✔
112
        if err != nil {
10✔
113
                return errors.Wrap(err, "workflows: failed to submit reindex job")
2✔
114
        }
2✔
115
        defer rsp.Body.Close()
6✔
116

6✔
117
        if rsp.StatusCode < 300 {
8✔
118
                return nil
2✔
119
        } else if rsp.StatusCode == http.StatusNotFound {
8✔
120
                return errors.New(`workflows: workflow "reindex_reporting" not defined`)
2✔
121
        }
2✔
122

123
        return errors.Errorf(
2✔
124
                "workflows: unexpected HTTP status from workflows service: %s",
2✔
125
                rsp.Status,
2✔
126
        )
2✔
127
}
128

129
func (c *client) CheckHealth(ctx context.Context) error {
8✔
130
        var (
8✔
131
                apiErr rest_utils.ApiError
8✔
132
        )
8✔
133

8✔
134
        if ctx == nil {
10✔
135
                ctx = context.Background()
2✔
136
        }
2✔
137
        if _, ok := ctx.Deadline(); !ok {
12✔
138
                var cancel context.CancelFunc
4✔
139
                ctx, cancel = context.WithTimeout(ctx, defaultTimeout)
4✔
140
                defer cancel()
4✔
141
        }
4✔
142
        req, _ := http.NewRequestWithContext(
8✔
143
                ctx, "GET",
8✔
144
                utils.JoinURL(c.url, HealthURI), nil,
8✔
145
        )
8✔
146

8✔
147
        rsp, err := c.client.Do(req)
8✔
148
        if err != nil {
10✔
149
                return err
2✔
150
        }
2✔
151
        defer rsp.Body.Close()
6✔
152

6✔
153
        if rsp.StatusCode >= http.StatusOK && rsp.StatusCode < 300 {
8✔
154
                return nil
2✔
155
        }
2✔
156
        decoder := json.NewDecoder(rsp.Body)
4✔
157
        err = decoder.Decode(&apiErr)
4✔
158
        if err != nil {
6✔
159
                return errors.Errorf("health check HTTP error: %s", rsp.Status)
2✔
160
        }
2✔
161
        return &apiErr
2✔
162
}
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