• 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

91.35
/backend/services/deviceconfig/api/http/management.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/mendersoftware/mender-server/services/deviceconfig/model"
21
        "github.com/mendersoftware/mender-server/services/deviceconfig/store"
22

23
        "github.com/gin-gonic/gin"
24
        "github.com/pkg/errors"
25

26
        "github.com/mendersoftware/mender-server/pkg/identity"
27
        "github.com/mendersoftware/mender-server/pkg/plan"
28
        "github.com/mendersoftware/mender-server/pkg/rest.utils"
29
)
30

31
// API errors
32
var (
33
        errUpdateContrloMapForbidden = errors.New(
34
                "forbidden: update control map is available only for Enterprise customers")
35
)
36

37
// ManagementAPI is a namespace for the APIHandlers
38
type ManagementAPI APIHandler
39

40
func (api *ManagementAPI) SetConfiguration(c *gin.Context) {
1✔
41
        var configuration model.Attributes
1✔
42

1✔
43
        ctx := c.Request.Context()
1✔
44
        devID := c.Param("device_id")
1✔
45

1✔
46
        err := c.ShouldBindJSON(&configuration)
1✔
47
        if err != nil {
2✔
48
                rest.RenderError(c,
1✔
49
                        http.StatusBadRequest,
1✔
50
                        errors.Wrap(err, "malformed request body"),
1✔
51
                )
1✔
52
                return
1✔
53
        }
1✔
54

55
        for _, a := range configuration {
2✔
56
                if err := a.Validate(); err != nil {
1✔
UNCOV
57
                        rest.RenderError(c,
×
UNCOV
58
                                http.StatusBadRequest,
×
UNCOV
59
                                errors.Wrap(err, "invalid request body"),
×
UNCOV
60
                        )
×
UNCOV
61
                        return
×
UNCOV
62
                }
×
63
        }
64

65
        err = api.App.SetConfiguration(ctx, devID, configuration)
1✔
66
        if err != nil {
2✔
67
                c.Error(err) //nolint:errcheck
1✔
68
                rest.RenderError(c,
1✔
69
                        http.StatusInternalServerError,
1✔
70
                        errors.New(http.StatusText(http.StatusInternalServerError)),
1✔
71
                )
1✔
72
                return
1✔
73
        }
1✔
74
        c.Status(http.StatusNoContent)
1✔
75
}
76

77
func (api *ManagementAPI) GetConfiguration(c *gin.Context) {
1✔
78
        ctx := c.Request.Context()
1✔
79

1✔
80
        devID := c.Param("device_id")
1✔
81

1✔
82
        device, err := api.App.GetDevice(ctx, devID)
1✔
83
        if err != nil {
2✔
84
                switch cause := errors.Cause(err); cause {
1✔
85
                case store.ErrDeviceNoExist:
1✔
86
                        c.Error(err) //nolint:errcheck
1✔
87
                        rest.RenderError(c,
1✔
88
                                http.StatusNotFound,
1✔
89
                                cause,
1✔
90
                        )
1✔
91
                        return
1✔
92
                default:
1✔
93
                        c.Error(err) //nolint:errcheck
1✔
94
                        rest.RenderError(c,
1✔
95
                                http.StatusInternalServerError,
1✔
96
                                errors.New(http.StatusText(http.StatusInternalServerError)),
1✔
97
                        )
1✔
98
                        return
1✔
99
                }
100
        }
101

102
        c.JSON(http.StatusOK, device)
1✔
103
}
104

105
func (api *ManagementAPI) DeployConfiguration(c *gin.Context) {
1✔
106
        ctx := c.Request.Context()
1✔
107
        devID := c.Param("device_id")
1✔
108

1✔
109
        device, err := api.App.GetDevice(ctx, devID)
1✔
110
        if err != nil {
2✔
111
                switch cause := errors.Cause(err); cause {
1✔
112
                case store.ErrDeviceNoExist:
1✔
113
                        c.Error(err) //nolint:errcheck
1✔
114
                        rest.RenderError(c,
1✔
115
                                http.StatusNotFound,
1✔
116
                                cause,
1✔
117
                        )
1✔
118
                        return
1✔
119
                default:
1✔
120
                        c.Error(err) //nolint:errcheck
1✔
121
                        rest.RenderError(c,
1✔
122
                                http.StatusInternalServerError,
1✔
123
                                errors.New(http.StatusText(http.StatusInternalServerError)),
1✔
124
                        )
1✔
125
                        return
1✔
126
                }
127
        }
128

129
        request := model.DeployConfigurationRequest{}
1✔
130
        err = c.ShouldBindJSON(&request)
1✔
131
        if err != nil {
2✔
132
                rest.RenderError(c,
1✔
133
                        http.StatusBadRequest,
1✔
134
                        errors.Wrap(err, "malformed request body"),
1✔
135
                )
1✔
136
                return
1✔
137
        }
1✔
138

139
        identity := identity.FromContext(ctx)
1✔
140
        if identity == nil {
1✔
141
                rest.RenderError(c, http.StatusForbidden, errInvalidIdentity)
×
142
                return
×
143
        }
×
144
        // udpate control map is available only for Enterprise customers
145
        if len(request.UpdateControlMap) > 0 &&
1✔
146
                !plan.IsHigherOrEqual(identity.Plan, plan.PlanEnterprise) {
2✔
147
                rest.RenderError(c, http.StatusForbidden, errUpdateContrloMapForbidden)
1✔
148
                return
1✔
149
        }
1✔
150

151
        response, err := api.App.DeployConfiguration(ctx, device, request)
1✔
152
        if err != nil {
2✔
153
                rest.RenderError(c,
1✔
154
                        http.StatusInternalServerError,
1✔
155
                        errors.Wrap(err, "configuration deployment failed"),
1✔
156
                )
1✔
157
                return
1✔
158
        }
1✔
159

160
        c.JSON(http.StatusOK, response)
1✔
161
}
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