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

mendersoftware / reporting / 738346025

pending completion
738346025

Pull #87

gitlab-ci

Fabio Tranchitella
feat: add support to index the device deployment data in the indexer
Pull Request #87: MEN-5930: index device deployment objects

641 of 884 new or added lines in 15 files covered. (72.51%)

1 existing line in 1 file now uncovered.

2556 of 3139 relevant lines covered (81.43%)

16.26 hits per line

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

95.0
/api/http/management_deployments.go
1
// Copyright 2022 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 http
16

17
import (
18
        "context"
19
        "net/http"
20
        "strconv"
21

22
        "github.com/gin-gonic/gin"
23
        "github.com/mendersoftware/go-lib-micro/identity"
24
        "github.com/mendersoftware/go-lib-micro/rest.utils"
25
        "github.com/pkg/errors"
26

27
        "github.com/mendersoftware/reporting/model"
28
)
29

30
func (mc *ManagementController) AggregateDeployments(c *gin.Context) {
8✔
31
        ctx := c.Request.Context()
8✔
32

8✔
33
        params, err := parseAggregateDeploymentsParams(ctx, c)
8✔
34
        if err != nil {
12✔
35
                rest.RenderError(c,
4✔
36
                        http.StatusBadRequest,
4✔
37
                        errors.Wrap(err, "malformed request body"),
4✔
38
                )
4✔
39
                return
4✔
40
        }
4✔
41

42
        res, err := mc.reporting.AggregateDeployments(ctx, params)
4✔
43
        if err != nil {
6✔
44
                rest.RenderError(c,
2✔
45
                        http.StatusInternalServerError,
2✔
46
                        err,
2✔
47
                )
2✔
48
                return
2✔
49
        }
2✔
50

51
        c.JSON(http.StatusOK, res)
2✔
52
}
53

54
func parseAggregateDeploymentsParams(ctx context.Context, c *gin.Context) (
55
        *model.AggregateDeploymentsParams, error) {
8✔
56
        var aggregateParams model.AggregateDeploymentsParams
8✔
57

8✔
58
        err := c.ShouldBindJSON(&aggregateParams)
8✔
59
        if err != nil {
10✔
60
                return nil, err
2✔
61
        }
2✔
62

63
        if id := identity.FromContext(ctx); id != nil {
12✔
64
                aggregateParams.TenantID = id.Tenant
6✔
65
        } else {
6✔
NEW
66
                return nil, errors.New("missing tenant ID from the context")
×
NEW
67
        }
×
68

69
        if err := aggregateParams.Validate(); err != nil {
8✔
70
                return nil, err
2✔
71
        }
2✔
72

73
        return &aggregateParams, nil
4✔
74
}
75

76
func (mc *ManagementController) SearchDeployments(c *gin.Context) {
12✔
77
        ctx := c.Request.Context()
12✔
78
        params, err := parseDeploymentsSearchParams(ctx, c)
12✔
79
        if err != nil {
16✔
80
                rest.RenderError(c,
4✔
81
                        http.StatusBadRequest,
4✔
82
                        errors.Wrap(err, "malformed request body"),
4✔
83
                )
4✔
84
                return
4✔
85
        }
4✔
86

87
        res, total, err := mc.reporting.SearchDeployments(ctx, params)
8✔
88
        if err != nil {
10✔
89
                rest.RenderError(c,
2✔
90
                        http.StatusInternalServerError,
2✔
91
                        err,
2✔
92
                )
2✔
93
                return
2✔
94
        }
2✔
95

96
        pageLinkHdrs(c, params.Page, params.PerPage, total)
6✔
97

6✔
98
        c.Header(hdrTotalCount, strconv.Itoa(total))
6✔
99
        c.JSON(http.StatusOK, res)
6✔
100
}
101

102
func parseDeploymentsSearchParams(ctx context.Context, c *gin.Context) (
103
        *model.DeploymentsSearchParams, error) {
12✔
104
        var searchParams model.DeploymentsSearchParams
12✔
105

12✔
106
        err := c.ShouldBindJSON(&searchParams)
12✔
107
        if err != nil {
14✔
108
                return nil, err
2✔
109
        }
2✔
110

111
        if id := identity.FromContext(ctx); id != nil {
20✔
112
                searchParams.TenantID = id.Tenant
10✔
113
        } else {
10✔
NEW
114
                return nil, errors.New("missing tenant ID from the context")
×
NEW
115
        }
×
116

117
        if searchParams.PerPage <= 0 {
16✔
118
                searchParams.PerPage = ParamPerPageDefault
6✔
119
        }
6✔
120
        if searchParams.Page <= 0 {
16✔
121
                searchParams.Page = ParamPageDefault
6✔
122
        }
6✔
123

124
        if err := searchParams.Validate(); err != nil {
12✔
125
                return nil, err
2✔
126
        }
2✔
127

128
        return &searchParams, nil
8✔
129
}
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