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

mendersoftware / iot-manager / 1457622833

13 Sep 2024 11:01AM UTC coverage: 87.172%. Remained the same
1457622833

push

gitlab-ci

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

Chore/deprecate

3255 of 3734 relevant lines covered (87.17%)

11.38 hits per line

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

97.78
/api/http/management_state.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/google/uuid"
22
        "github.com/pkg/errors"
23

24
        "github.com/mendersoftware/go-lib-micro/rest.utils"
25

26
        "github.com/mendersoftware/iot-manager/app"
27
        "github.com/mendersoftware/iot-manager/model"
28
)
29

30
const (
31
        paramDeviceID      = "id"
32
        paramIntegrationID = "integrationId"
33
)
34

35
var (
36
        ErrEmptyDeviceID        = errors.New("device ID is empty")
37
        ErrInvalidIntegrationID = errors.New("integration ID is not a valid UUID")
38
)
39

40
// GET /devices/:id/state
41
func (h *ManagementHandler) GetDeviceState(c *gin.Context) {
7✔
42
        ctx, _, err := getContextAndIdentity(c)
7✔
43
        if err != nil {
8✔
44
                return
1✔
45
        }
1✔
46

47
        deviceID := c.Param(paramDeviceID)
6✔
48
        if deviceID == "" {
7✔
49
                rest.RenderError(c, http.StatusBadRequest, ErrEmptyDeviceID)
1✔
50
                return
1✔
51
        }
1✔
52

53
        device, err := h.app.GetDevice(ctx, deviceID)
5✔
54
        if err == app.ErrDeviceNotFound {
6✔
55
                rest.RenderError(c, http.StatusNotFound, app.ErrDeviceNotFound)
1✔
56
                return
1✔
57
        } else if err != nil {
6✔
58
                rest.RenderError(c, http.StatusInternalServerError, err)
1✔
59
                return
1✔
60
        }
1✔
61

62
        states := make(model.DeviceStates)
3✔
63
        for _, integrationID := range device.IntegrationIDs {
6✔
64
                state, err := h.app.GetDeviceStateIntegration(ctx, deviceID, integrationID)
3✔
65
                if state == nil && (err == nil ||
3✔
66
                        err == app.ErrIntegrationNotFound ||
3✔
67
                        err == app.ErrUnknownIntegration) {
4✔
68
                        continue
1✔
69
                } else if err != nil {
3✔
70
                        rest.RenderError(c, http.StatusInternalServerError, err)
1✔
71
                        return
1✔
72
                }
1✔
73
                states[integrationID.String()] = *state
1✔
74
        }
75

76
        c.JSON(http.StatusOK, states)
2✔
77
}
78

79
// GET /devices/:id/state/:integrationId
80
func (h *ManagementHandler) GetDeviceStateIntegration(c *gin.Context) {
7✔
81
        ctx, _, err := getContextAndIdentity(c)
7✔
82
        if err != nil {
8✔
83
                return
1✔
84
        }
1✔
85

86
        deviceID := c.Param(paramDeviceID)
6✔
87
        if deviceID == "" {
7✔
88
                rest.RenderError(c, http.StatusBadRequest, ErrEmptyDeviceID)
1✔
89
                return
1✔
90
        }
1✔
91
        integrationID, err := uuid.Parse(c.Param(paramIntegrationID))
5✔
92
        if err != nil {
6✔
93
                rest.RenderError(c, http.StatusBadRequest, ErrInvalidIntegrationID)
1✔
94
                return
1✔
95
        }
1✔
96

97
        state, err := h.app.GetDeviceStateIntegration(ctx, deviceID, integrationID)
4✔
98
        if err == app.ErrIntegrationNotFound || err == app.ErrUnknownIntegration {
6✔
99
                rest.RenderError(c, http.StatusNotFound, err)
2✔
100
                return
2✔
101
        } else if err != nil {
5✔
102
                rest.RenderError(c, http.StatusInternalServerError, err)
1✔
103
                return
1✔
104
        }
1✔
105

106
        c.JSON(http.StatusOK, state)
1✔
107
}
108

109
// PUT /devices/:id/state/:integrationId
110
func (h *ManagementHandler) SetDeviceStateIntegration(c *gin.Context) {
7✔
111
        ctx, _, err := getContextAndIdentity(c)
7✔
112
        if err != nil {
8✔
113
                return
1✔
114
        }
1✔
115

116
        deviceID := c.Param(paramDeviceID)
6✔
117
        if deviceID == "" {
7✔
118
                rest.RenderError(c, http.StatusBadRequest, ErrEmptyDeviceID)
1✔
119
                return
1✔
120
        }
1✔
121
        integrationID, err := uuid.Parse(c.Param(paramIntegrationID))
5✔
122
        if err != nil {
6✔
123
                rest.RenderError(c, http.StatusBadRequest, ErrInvalidIntegrationID)
1✔
124
                return
1✔
125
        }
1✔
126

127
        state := &model.DeviceState{}
4✔
128
        if err := c.ShouldBindJSON(state); err != nil {
5✔
129
                rest.RenderError(c,
1✔
130
                        http.StatusBadRequest,
1✔
131
                        errors.Wrap(err, "malformed request body"),
1✔
132
                )
1✔
133
                return
1✔
134
        }
1✔
135

136
        state, err = h.app.SetDeviceStateIntegration(ctx, deviceID, integrationID, state)
3✔
137
        if err == app.ErrIntegrationNotFound || err == app.ErrUnknownIntegration {
3✔
138
                rest.RenderError(c, http.StatusNotFound, err)
×
139
                return
×
140
        } else if err == app.ErrDeviceStateConflict {
4✔
141
                rest.RenderError(c, http.StatusConflict, err)
1✔
142
                return
1✔
143
        } else if err != nil {
4✔
144
                rest.RenderError(c, http.StatusInternalServerError, err)
1✔
145
                return
1✔
146
        }
1✔
147

148
        c.JSON(http.StatusOK, state)
1✔
149
}
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