Skip to content

Commit

Permalink
Merge pull request #17 from alfrunes/device-ids
Browse files Browse the repository at this point in the history
Change device ID data type from uuid.UUID to string
  • Loading branch information
tranchitella committed Mar 9, 2021
2 parents 0a6b516 + 607b5c5 commit b2f86fc
Show file tree
Hide file tree
Showing 20 changed files with 116 additions and 261 deletions.
23 changes: 3 additions & 20 deletions api/http/devices.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
"net/http"

"github.com/gin-gonic/gin"
"github.com/google/uuid"
"github.com/mendersoftware/go-lib-micro/identity"
"github.com/mendersoftware/go-lib-micro/rest.utils"
"github.com/pkg/errors"
Expand Down Expand Up @@ -50,16 +49,8 @@ func (api *DevicesAPI) SetConfiguration(c *gin.Context) {
return
}

devID, err := uuid.Parse(identity.Subject)
if err != nil {
rest.RenderError(c,
http.StatusBadRequest,
errors.Wrap(err, "malformed device ID"),
)
return
}

err = c.ShouldBindJSON(&configuration)
devID := identity.Subject
err := c.ShouldBindJSON(&configuration)
if err != nil {
rest.RenderError(c,
http.StatusBadRequest,
Expand Down Expand Up @@ -98,15 +89,7 @@ func (api *DevicesAPI) GetConfiguration(c *gin.Context) {
return
}

devID, err := uuid.Parse(identity.Subject)
if err != nil {
rest.RenderError(c,
http.StatusBadRequest,
errors.Wrap(err, "malformed device ID"),
)
return
}

devID := identity.Subject
device, err := api.App.GetDevice(ctx, devID)
if err != nil {
switch cause := errors.Cause(err); cause {
Expand Down
10 changes: 5 additions & 5 deletions api/http/devices_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func TestDevicesSetConfiguration(t *testing.T) {
app := new(mapp.App)
app.On("SetReportedConfiguration",
contextMatcher,
mock.AnythingOfType("uuid.UUID"),
mock.AnythingOfType("string"),
model.Attributes{
{
Key: "key0",
Expand Down Expand Up @@ -99,7 +99,7 @@ func TestDevicesSetConfiguration(t *testing.T) {
app := new(mapp.App)
app.On("SetReportedConfiguration",
contextMatcher,
mock.AnythingOfType("uuid.UUID"),
mock.AnythingOfType("string"),
model.Attributes{
{
Key: "key0",
Expand Down Expand Up @@ -231,7 +231,7 @@ func TestDevicesGetConfiguration(t *testing.T) {
t.Parallel()

device := model.Device{
ID: uuid.New(),
ID: uuid.New().String(),
ConfiguredAttributes: []model.Attribute{
{
Key: "key1",
Expand Down Expand Up @@ -283,7 +283,7 @@ func TestDevicesGetConfiguration(t *testing.T) {
app := new(mapp.App)
app.On("GetDevice",
contextMatcher,
mock.AnythingOfType("uuid.UUID"),
mock.AnythingOfType("string"),
).Return(device, nil)
return app
}(),
Expand Down Expand Up @@ -365,7 +365,7 @@ func TestDevicesGetConfiguration(t *testing.T) {
app := new(mapp.App)
app.On("GetDevice",
contextMatcher,
mock.AnythingOfType("uuid.UUID"),
mock.AnythingOfType("string"),
).Return(device, errors.New("some other error"))
return app
}(),
Expand Down
11 changes: 1 addition & 10 deletions api/http/internal.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
"net/http"

"github.com/gin-gonic/gin"
"github.com/google/uuid"
"github.com/pkg/errors"

"github.com/mendersoftware/go-lib-micro/identity"
Expand Down Expand Up @@ -136,15 +135,7 @@ func (api *InternalAPI) DecommissionDevice(c *gin.Context) {
)
c.Request = c.Request.WithContext(ctx)

deviceUUID, err := uuid.Parse(deviceID)
if err != nil {
rest.RenderError(c, http.StatusBadRequest,
errors.New("device_id is not a valid UUID."),
)
return
}

err = api.App.DecommissionDevice(ctx, deviceUUID)
err := api.App.DecommissionDevice(ctx, deviceID)
if err != nil {
switch errors.Cause(err) {
case store.ErrDeviceNoExist:
Expand Down
37 changes: 7 additions & 30 deletions api/http/internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ func TestProvisionTenant(t *testing.T) {

func TestProvisionDevice(t *testing.T) {
t.Parallel()
newDeviceMatcher := func(expected uuid.UUID) interface{} {
newDeviceMatcher := func(expected string) interface{} {
return mock.MatchedBy(func(dev model.NewDevice) bool {
return assert.Equal(t, expected, dev.ID)
})
Expand Down Expand Up @@ -281,7 +281,7 @@ func TestProvisionDevice(t *testing.T) {
contextMatcher,
newDeviceMatcher(uuid.NewSHA1(
uuid.NameSpaceDNS, []byte("mender.io"),
)),
).String()),
).Return(nil)
return app
}(),
Expand Down Expand Up @@ -353,7 +353,7 @@ func TestProvisionDevice(t *testing.T) {
contextMatcher,
newDeviceMatcher(uuid.NewSHA1(
uuid.NameSpaceDNS, []byte("mender.io"),
)),
).String()),
).Return(errors.New("something went wrong!"))
return app
}(),
Expand Down Expand Up @@ -386,7 +386,7 @@ func TestProvisionDevice(t *testing.T) {
contextMatcher,
newDeviceMatcher(uuid.NewSHA1(
uuid.NameSpaceDNS, []byte("mender.io"),
)),
).String()),
).Return(store.ErrDeviceAlreadyExists)
return app
}(),
Expand Down Expand Up @@ -449,7 +449,7 @@ func TestDecommissionDevice(t *testing.T) {
contextMatcher,
uuid.NewSHA1(
uuid.NameSpaceDNS, []byte("mender.io"),
),
).String(),
).Return(nil)
return app
}(),
Expand Down Expand Up @@ -479,7 +479,7 @@ func TestDecommissionDevice(t *testing.T) {
contextMatcher,
uuid.NewSHA1(
uuid.NameSpaceDNS, []byte("mender.io"),
),
).String(),
).Return(errors.Wrap(store.ErrDeviceNoExist, "mongo"))
return app
}(),
Expand All @@ -488,29 +488,6 @@ func TestDecommissionDevice(t *testing.T) {
RequestID: "test",
},
Status: http.StatusNotFound,
}, {
Name: "error invalid request parameters",

Request: func() *http.Request {
repl := strings.NewReplacer(
":tenant_id", "123456789012345678901234",
":device_id", "notUUID",
)
req, _ := http.NewRequest("DELETE",
"http://localhost"+URIInternal+
repl.Replace(URITenantDevice),
nil,
)
req.Header.Set("X-Men-Requestid", "test")
return req
}(),

App: new(mapp.App),
Error: &rest.Error{
Err: "device_id is not a valid UUID.",
RequestID: "test",
},
Status: http.StatusBadRequest,
}, {
Name: "error, internal server error",

Expand All @@ -536,7 +513,7 @@ func TestDecommissionDevice(t *testing.T) {
contextMatcher,
uuid.NewSHA1(
uuid.NameSpaceDNS, []byte("mender.io"),
),
).String(),
).Return(errors.New("Oh noez!"))
return app
}(),
Expand Down
36 changes: 7 additions & 29 deletions api/http/management.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
"github.com/mendersoftware/deviceconfig/store"

"github.com/gin-gonic/gin"
"github.com/google/uuid"
"github.com/pkg/errors"

"github.com/mendersoftware/go-lib-micro/rest.utils"
Expand All @@ -50,16 +49,9 @@ func (api *ManagementAPI) SetConfiguration(c *gin.Context) {
var configuration model.Attributes

ctx := c.Request.Context()
devID, err := uuid.Parse(c.Param("device_id"))
if err != nil {
rest.RenderError(c,
http.StatusBadRequest,
errors.Wrap(err, "correctly formatted device id is needed"),
)
return
}
devID := c.Param("device_id")

err = c.ShouldBindJSON(&configuration)
err := c.ShouldBindJSON(&configuration)
if err != nil {
rest.RenderError(c,
http.StatusBadRequest,
Expand All @@ -71,7 +63,7 @@ func (api *ManagementAPI) SetConfiguration(c *gin.Context) {
// RBAC
if len(c.Request.Header.Get(model.RBACHeaderDeploymentsGroups)) > 1 {
allowed, err := api.isAllowed(
ctx, c.Request, devID.String(), model.RBACHeaderDeploymentsGroups)
ctx, c.Request, devID, model.RBACHeaderDeploymentsGroups)
if err != nil {
c.Error(err) //nolint:errcheck
rest.RenderError(c,
Expand Down Expand Up @@ -112,19 +104,12 @@ func (api *ManagementAPI) SetConfiguration(c *gin.Context) {
func (api *ManagementAPI) GetConfiguration(c *gin.Context) {
ctx := c.Request.Context()

devID, err := uuid.Parse(c.Param("device_id"))
if err != nil {
rest.RenderError(c,
http.StatusBadRequest,
errors.Wrap(err, "correctly formatted device id is needed"),
)
return
}
devID := c.Param("device_id")

// RBAC
if len(c.Request.Header.Get(model.RBACHeaderInvetoryGroups)) > 1 {
allowed, err := api.isAllowed(
ctx, c.Request, devID.String(), model.RBACHeaderInvetoryGroups)
ctx, c.Request, devID, model.RBACHeaderInvetoryGroups)
if err != nil {
c.Error(err) //nolint:errcheck
rest.RenderError(c,
Expand Down Expand Up @@ -165,19 +150,12 @@ func (api *ManagementAPI) GetConfiguration(c *gin.Context) {

func (api *ManagementAPI) DeployConfiguration(c *gin.Context) {
ctx := c.Request.Context()
devID, err := uuid.Parse(c.Param("device_id"))
if err != nil {
rest.RenderError(c,
http.StatusBadRequest,
errors.Wrap(err, "correctly formatted device id is needed"),
)
return
}
devID := c.Param("device_id")

// RBAC
if len(c.Request.Header.Get(model.RBACHeaderDeploymentsGroups)) > 1 {
allowed, err := api.isAllowed(
ctx, c.Request, devID.String(), model.RBACHeaderDeploymentsGroups)
ctx, c.Request, devID, model.RBACHeaderDeploymentsGroups)
if err != nil {
c.Error(err) //nolint:errcheck
rest.RenderError(c,
Expand Down

0 comments on commit b2f86fc

Please sign in to comment.