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

mendersoftware / mender-server / 1495380963

14 Oct 2024 03:35PM UTC coverage: 70.373% (-2.5%) from 72.904%
1495380963

Pull #101

gitlab-ci

mineralsfree
feat: tenant list added

Ticket: MEN-7568
Changelog: None

Signed-off-by: Mikita Pilinka <mikita.pilinka@northern.tech>
Pull Request #101: feat: tenant list added

4406 of 6391 branches covered (68.94%)

Branch coverage included in aggregate %.

88 of 183 new or added lines in 10 files covered. (48.09%)

2623 existing lines in 65 files now uncovered.

36673 of 51982 relevant lines covered (70.55%)

31.07 hits per line

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

73.13
/backend/services/deviceconfig/api/http/devices.go
1
// Copyright 2021 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
        "net/http"
19

20
        "github.com/gin-gonic/gin"
21
        "github.com/pkg/errors"
22

23
        "github.com/mendersoftware/mender-server/pkg/identity"
24
        "github.com/mendersoftware/mender-server/pkg/rest.utils"
25

26
        "github.com/mendersoftware/mender-server/services/deviceconfig/app"
27
        "github.com/mendersoftware/mender-server/services/deviceconfig/model"
28
        "github.com/mendersoftware/mender-server/services/deviceconfig/store"
29
)
30

31
// DevicesAPI is a namespace for the APIHandler
32
type DevicesAPI APIHandler
33

34
var errInvalidIdentity = errors.New("forbidden: invalid identity data")
35

36
func NewDevicesAPI(app app.App) *DevicesAPI {
×
37
        return &DevicesAPI{
×
38
                App: app,
×
39
        }
×
40
}
×
41

42
func (api *DevicesAPI) SetConfiguration(c *gin.Context) {
1✔
43
        var configuration model.Attributes
1✔
44

1✔
45
        ctx := c.Request.Context()
1✔
46
        identity := identity.FromContext(ctx)
1✔
47
        if identity == nil || !identity.IsDevice {
2✔
48
                rest.RenderError(c, http.StatusForbidden, errInvalidIdentity)
1✔
49
                return
1✔
50
        }
1✔
51

52
        devID := identity.Subject
1✔
53
        err := c.ShouldBindJSON(&configuration)
1✔
54
        if err != nil {
2✔
55
                rest.RenderError(c,
1✔
56
                        http.StatusBadRequest,
1✔
57
                        errors.Wrap(err, "malformed request body"),
1✔
58
                )
1✔
59
                return
1✔
60
        }
1✔
61

62
        for _, a := range configuration {
2✔
63
                if err := a.Validate(); err != nil {
1✔
UNCOV
64
                        rest.RenderError(c,
×
UNCOV
65
                                http.StatusBadRequest,
×
UNCOV
66
                                errors.Wrap(err, "invalid request body"),
×
UNCOV
67
                        )
×
UNCOV
68
                        return
×
UNCOV
69
                }
×
70
        }
71

72
        err = api.App.SetReportedConfiguration(ctx, devID, configuration)
1✔
73
        if err != nil {
2✔
74
                c.Error(err) //nolint:errcheck
1✔
75
                rest.RenderError(c,
1✔
76
                        http.StatusInternalServerError,
1✔
77
                        errors.New(http.StatusText(http.StatusInternalServerError)),
1✔
78
                )
1✔
79
                return
1✔
80
        }
1✔
81
        c.Status(http.StatusNoContent)
1✔
82
}
83

84
func (api *DevicesAPI) GetConfiguration(c *gin.Context) {
1✔
85
        ctx := c.Request.Context()
1✔
86
        identity := identity.FromContext(ctx)
1✔
87
        if identity == nil || !identity.IsDevice {
2✔
88
                rest.RenderError(c, http.StatusForbidden, errInvalidIdentity)
1✔
89
                return
1✔
90
        }
1✔
91

92
        devID := identity.Subject
1✔
93
        device, err := api.App.GetDevice(ctx, devID)
1✔
94
        if err != nil {
2✔
95
                switch cause := errors.Cause(err); cause {
1✔
96
                case store.ErrDeviceNoExist:
×
97
                        c.Error(err) //nolint:errcheck
×
98
                        rest.RenderError(c,
×
99
                                http.StatusNotFound,
×
100
                                cause,
×
101
                        )
×
102
                        return
×
103
                default:
1✔
104
                        c.Error(err) //nolint:errcheck
1✔
105
                        rest.RenderError(c,
1✔
106
                                http.StatusInternalServerError,
1✔
107
                                errors.New(http.StatusText(http.StatusInternalServerError)),
1✔
108
                        )
1✔
109
                        return
1✔
110
                }
111
        }
112

113
        c.JSON(http.StatusOK, device.ConfiguredAttributes)
1✔
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