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

mendersoftware / reporting / 1571619767

13 Sep 2024 10:52AM UTC coverage: 85.292% (+0.06%) from 85.235%
1571619767

push

gitlab-ci

web-flow
Merge pull request #202 from mzedel/chore/deprecate

Chore/deprecate

2998 of 3515 relevant lines covered (85.29%)

14.21 hits per line

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

95.35
/api/http/management_deployments.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 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/rbac"
25
        "github.com/mendersoftware/go-lib-micro/rest.utils"
26
        "github.com/pkg/errors"
27

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

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

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

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

52
        c.JSON(http.StatusOK, res)
4✔
53
}
54

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

7✔
59
        err := c.ShouldBindJSON(&aggregateParams)
7✔
60
        if err != nil {
8✔
61
                return nil, err
1✔
62
        }
1✔
63

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

70
        if scope := rbac.ExtractScopeFromHeader(c.Request); scope != nil {
7✔
71
                aggregateParams.DeploymentGroups = scope.DeviceGroups
1✔
72
        }
1✔
73

74
        if err := aggregateParams.Validate(); err != nil {
7✔
75
                return nil, err
1✔
76
        }
1✔
77

78
        return &aggregateParams, nil
5✔
79
}
80

81
func (mc *ManagementController) SearchDeployments(c *gin.Context) {
12✔
82
        ctx := c.Request.Context()
12✔
83
        params, err := parseDeploymentsSearchParams(ctx, c)
12✔
84
        if err != nil {
14✔
85
                rest.RenderError(c,
2✔
86
                        http.StatusBadRequest,
2✔
87
                        errors.Wrap(err, "malformed request body"),
2✔
88
                )
2✔
89
                return
2✔
90
        }
2✔
91

92
        res, total, err := mc.reporting.SearchDeployments(ctx, params)
10✔
93
        if err != nil {
11✔
94
                rest.RenderError(c,
1✔
95
                        http.StatusInternalServerError,
1✔
96
                        err,
1✔
97
                )
1✔
98
                return
1✔
99
        }
1✔
100

101
        pageLinkHdrs(c, params.Page, params.PerPage, total)
9✔
102

9✔
103
        c.Header(hdrTotalCount, strconv.Itoa(total))
9✔
104
        c.JSON(http.StatusOK, res)
9✔
105
}
106

107
func parseDeploymentsSearchParams(ctx context.Context, c *gin.Context) (
108
        *model.DeploymentsSearchParams, error) {
12✔
109
        var searchParams model.DeploymentsSearchParams
12✔
110

12✔
111
        err := c.ShouldBindJSON(&searchParams)
12✔
112
        if err != nil {
13✔
113
                return nil, err
1✔
114
        }
1✔
115

116
        if id := identity.FromContext(ctx); id != nil {
22✔
117
                searchParams.TenantID = id.Tenant
11✔
118
        } else {
11✔
119
                return nil, errors.New("missing tenant ID from the context")
×
120
        }
×
121

122
        if searchParams.PerPage <= 0 {
20✔
123
                searchParams.PerPage = ParamPerPageDefault
9✔
124
        }
9✔
125
        if searchParams.Page <= 0 {
20✔
126
                searchParams.Page = ParamPageDefault
9✔
127
        }
9✔
128

129
        if scope := rbac.ExtractScopeFromHeader(c.Request); scope != nil {
12✔
130
                searchParams.DeploymentGroups = scope.DeviceGroups
1✔
131
        }
1✔
132

133
        if err := searchParams.Validate(); err != nil {
12✔
134
                return nil, err
1✔
135
        }
1✔
136

137
        return &searchParams, nil
10✔
138
}
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