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

mendersoftware / mender-server / 1622978334

13 Jan 2025 03:51PM UTC coverage: 72.802% (-3.8%) from 76.608%
1622978334

Pull #300

gitlab-ci

alfrunes
fix: Deployment device count should not exceed max devices

Added a condition to skip deployments when the device count reaches max
devices.

Changelog: Title
Ticket: MEN-7847
Signed-off-by: Alf-Rune Siqveland <alf.rune@northern.tech>
Pull Request #300: fix: Deployment device count should not exceed max devices

4251 of 6164 branches covered (68.96%)

Branch coverage included in aggregate %.

0 of 18 new or added lines in 1 file covered. (0.0%)

2544 existing lines in 83 files now uncovered.

42741 of 58384 relevant lines covered (73.21%)

21.49 hits per line

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

0.0
/backend/pkg/rest_utils/paging.go
1
// Copyright 2024 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 rest_utils
16

17
import (
18
        "errors"
19
        "fmt"
20
        "math"
21
        "net/url"
22
        "strconv"
23
        "strings"
24

25
        "github.com/ant0ine/go-json-rest/rest"
26

27
        micro_strings "github.com/mendersoftware/mender-server/pkg/strings"
28
)
29

30
// pagination constants
31
const (
32
        PageName       = "page"
33
        PerPageName    = "per_page"
34
        PageMin        = 1
35
        PageDefault    = 1
36
        PerPageMin     = 1
37
        PerPageMax     = 500
38
        PerPageDefault = 20
39
        LinkHdr        = "Link"
40
        LinkTmpl       = "<%s>; rel=\"%s\""
41
        LinkPrev       = "prev"
42
        LinkNext       = "next"
43
        LinkFirst      = "first"
44
        DefaultScheme  = "http"
45
)
46

47
// error msgs
UNCOV
48
func MsgQueryParmInvalid(name string) string {
×
UNCOV
49
        return fmt.Sprintf("Can't parse param %s", name)
×
UNCOV
50
}
×
51

52
func MsgQueryParmMissing(name string) string {
×
53
        return fmt.Sprintf("Missing required param %s", name)
×
54
}
×
55

56
func MsgQueryParmLimit(name string) string {
×
57
        return fmt.Sprintf("Param %s is out of bounds", name)
×
58
}
×
59

60
func MsgQueryParmOneOf(name string, allowed []string) string {
×
61
        return fmt.Sprintf("Param %s must be one of %v", name, allowed)
×
62
}
×
63

64
// query param parsing/validation
65
func ParseQueryParmUInt(
66
        r *rest.Request,
67
        name string,
68
        required bool,
69
        min, max, def uint64,
UNCOV
70
) (uint64, error) {
×
UNCOV
71
        strVal := r.URL.Query().Get(name)
×
UNCOV
72

×
UNCOV
73
        if strVal == "" {
×
UNCOV
74
                if required {
×
75
                        return 0, errors.New(MsgQueryParmMissing(name))
×
UNCOV
76
                } else {
×
UNCOV
77
                        return def, nil
×
UNCOV
78
                }
×
79
        }
80

UNCOV
81
        uintVal, err := strconv.ParseUint(strVal, 10, 32)
×
UNCOV
82
        if err != nil {
×
UNCOV
83
                return 0, errors.New(MsgQueryParmInvalid(name))
×
UNCOV
84
        }
×
85

UNCOV
86
        if uintVal < min || uintVal > max {
×
87
                return 0, errors.New(MsgQueryParmLimit(name))
×
88
        }
×
89

UNCOV
90
        return uintVal, nil
×
91
}
92

93
func ParseQueryParmBool(r *rest.Request, name string, required bool, def *bool) (*bool, error) {
×
94
        strVal := r.URL.Query().Get(name)
×
95

×
96
        if strVal == "" {
×
97
                if required {
×
98
                        return nil, errors.New(MsgQueryParmMissing(name))
×
99
                } else {
×
100
                        return def, nil
×
101
                }
×
102
        }
103

104
        boolVal, err := strconv.ParseBool(strVal)
×
105
        if err != nil {
×
106
                return nil, errors.New(MsgQueryParmInvalid(name))
×
107
        }
×
108

109
        return &boolVal, nil
×
110
}
111

112
func ParseQueryParmStr(
113
        r *rest.Request,
114
        name string,
115
        required bool,
116
        allowed []string,
117
) (string, error) {
×
118
        val := r.URL.Query().Get(name)
×
119

×
120
        if val == "" {
×
121
                if required {
×
122
                        return "", errors.New(MsgQueryParmMissing(name))
×
123
                }
×
124
        } else {
×
125
                if allowed != nil && !micro_strings.ContainsString(val, allowed) {
×
126
                        return "", errors.New(MsgQueryParmOneOf(name, allowed))
×
127
                }
×
128
        }
129

130
        return val, nil
×
131
}
132

133
// pagination helpers
UNCOV
134
func ParsePagination(r *rest.Request) (uint64, uint64, error) {
×
UNCOV
135
        page, err := ParseQueryParmUInt(r, PageName, false, PageMin, math.MaxUint64, PageDefault)
×
UNCOV
136
        if err != nil {
×
UNCOV
137
                return 0, 0, err
×
UNCOV
138
        }
×
139

UNCOV
140
        per_page, err := ParseQueryParmUInt(
×
UNCOV
141
                r,
×
UNCOV
142
                PerPageName,
×
UNCOV
143
                false,
×
UNCOV
144
                PerPageMin,
×
UNCOV
145
                PerPageMax,
×
UNCOV
146
                PerPageDefault,
×
UNCOV
147
        )
×
UNCOV
148
        if err != nil {
×
UNCOV
149
                return 0, 0, err
×
UNCOV
150
        }
×
151

UNCOV
152
        return page, per_page, nil
×
153
}
154

UNCOV
155
func MakePageLinkHdrs(r *rest.Request, page, per_page uint64, has_next bool) []string {
×
UNCOV
156
        var links []string
×
UNCOV
157
        if page > 1 {
×
UNCOV
158
                links = append(links, MakeLink(LinkPrev, r, page-1, per_page))
×
UNCOV
159
        }
×
160

UNCOV
161
        if has_next {
×
UNCOV
162
                links = append(links, MakeLink(LinkNext, r, page+1, per_page))
×
UNCOV
163
        }
×
164

UNCOV
165
        links = append(links, MakeLink(LinkFirst, r, 1, per_page))
×
UNCOV
166
        return links
×
167
}
168

169
// MakeLink creates a relative URL for insertion in the link header URL field.
UNCOV
170
func MakeLink(link_type string, r *rest.Request, page, per_page uint64) string {
×
UNCOV
171
        q := r.URL.Query()
×
UNCOV
172
        q.Set(PageName, strconv.Itoa(int(page)))
×
UNCOV
173
        q.Set(PerPageName, strconv.Itoa(int(per_page)))
×
UNCOV
174
        url := url.URL{
×
UNCOV
175
                Path:     r.URL.Path,
×
UNCOV
176
                RawPath:  r.URL.RawPath,
×
UNCOV
177
                RawQuery: q.Encode(),
×
UNCOV
178
                Fragment: r.URL.Fragment,
×
UNCOV
179
        }
×
UNCOV
180

×
UNCOV
181
        return fmt.Sprintf(LinkTmpl, url.String(), link_type)
×
UNCOV
182
}
×
183

184
// build URL using request 'r' and template, replace path params with
185
// elements from 'params' using lexical match as in strings.Replace()
186
func BuildURL(r *rest.Request, template string, params map[string]string) *url.URL {
×
187
        url := r.BaseUrl()
×
188

×
189
        path := template
×
190
        for k, v := range params {
×
191
                path = strings.Replace(path, k, v, -1)
×
192
        }
×
193
        url.Path = path
×
194

×
195
        return url
×
196
}
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