Skip to content

Commit

Permalink
Merge pull request #609 from alfrunes/simplify-api-model
Browse files Browse the repository at this point in the history
chore: Align internal Device and AuthSet model with the API model
  • Loading branch information
alfrunes committed Aug 30, 2022
2 parents a5c5b06 + 8aaeebe commit db98c56
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 154 deletions.
17 changes: 5 additions & 12 deletions api/http/api_devauth.go
Original file line number Diff line number Diff line change
Expand Up @@ -317,11 +317,11 @@ func (d *DevAuthApiHandlers) SearchDevicesV2Handler(w rest.ResponseWriter, r *re
return
}

len := len(devs)
numDevs := len(devs)
hasNext := false
if uint64(len) > perPage {
if uint64(numDevs) > perPage {
hasNext = true
len = int(perPage)
numDevs = int(perPage)
}

links := rest_utils.MakePageLinkHdrs(r, page, perPage, hasNext)
Expand All @@ -330,13 +330,7 @@ func (d *DevAuthApiHandlers) SearchDevicesV2Handler(w rest.ResponseWriter, r *re
w.Header().Add("Link", l)
}

outDevs, err := devicesV2FromDbModel(devs[:len])
if err != nil {
rest_utils.RestErrWithLogInternal(w, r, l, err)
return
}

_ = w.WriteJson(outDevs)
_ = w.WriteJson(devs[:numDevs])
}

func (d *DevAuthApiHandlers) GetDevicesV2Handler(w rest.ResponseWriter, r *rest.Request) {
Expand Down Expand Up @@ -391,8 +385,7 @@ func (d *DevAuthApiHandlers) GetDeviceV2Handler(w rest.ResponseWriter, r *rest.R
case err == store.ErrDevNotFound:
rest_utils.RestErrWithLog(w, r, l, err, http.StatusNotFound)
case dev != nil:
apiDev, _ := deviceV2FromDbModel(dev)
_ = w.WriteJson(apiDev)
_ = w.WriteJson(dev)
default:
rest_utils.RestErrWithLogInternal(w, r, l, err)
}
Expand Down
28 changes: 9 additions & 19 deletions api/http/api_devauth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -877,8 +877,6 @@ func TestApiV2GetDevice(t *testing.T) {
},
}

apiDev, _ := deviceV2FromDbModel(dev)

tcases := []struct {
req *http.Request

Expand All @@ -895,7 +893,7 @@ func TestApiV2GetDevice(t *testing.T) {
err: nil,

code: http.StatusOK,
body: string(asJSON(apiDev)),
body: string(asJSON(dev)),
},
{
req: test.MakeSimpleRequest("GET",
Expand Down Expand Up @@ -978,8 +976,7 @@ func TestSearchDevices(t *testing.T) {
Status: "accepted",
CreatedTs: time.Unix(1606942069, 0),
}}
devV2, _ := devicesV2FromDbModel(dev)
b, _ := json.Marshal(devV2)
b, _ := json.Marshal(dev)
return b
}(),
}, {
Expand Down Expand Up @@ -1013,8 +1010,7 @@ func TestSearchDevices(t *testing.T) {
Status: "accepted",
CreatedTs: time.Unix(1606942069, 0),
}}
devV2, _ := devicesV2FromDbModel(dev)
b, _ := json.Marshal(devV2)
b, _ := json.Marshal(dev)
return b
}(),
}, {
Expand Down Expand Up @@ -1201,9 +1197,6 @@ func TestApiV2GetDevices(t *testing.T) {
},
}

outDevs, err := devicesV2FromDbModel(devs)
assert.NoError(t, err)

tcases := map[string]struct {
req *http.Request
code int
Expand All @@ -1221,7 +1214,7 @@ func TestApiV2GetDevices(t *testing.T) {
err: nil,
skip: 0,
limit: rest_utils.PerPageDefault + 1,
body: string(asJSON(outDevs)),
body: string(asJSON(devs)),
},
"no devices": {
req: test.MakeSimpleRequest("GET",
Expand All @@ -1243,7 +1236,7 @@ func TestApiV2GetDevices(t *testing.T) {
limit: 3,
code: http.StatusOK,
// reqquested 2 devices per page, so expect only 2
body: string(asJSON(outDevs[:2])),
body: string(asJSON(devs[:2])),
},
"internal error": {
req: test.MakeSimpleRequest("GET",
Expand Down Expand Up @@ -2174,9 +2167,6 @@ func TestApiGetTenantDevicesV2(t *testing.T) {
},
}

outDevs, err := devicesV2FromDbModel(devs)
assert.NoError(t, err)

tcases := map[string]struct {
req *http.Request
code int
Expand All @@ -2197,7 +2187,7 @@ func TestApiGetTenantDevicesV2(t *testing.T) {
err: nil,
skip: 0,
limit: rest_utils.PerPageDefault + 1,
body: string(asJSON(outDevs)),
body: string(asJSON(devs)),
tenant_id: "powerpuff123",

filterMatch: mock.AnythingOfType("model.DeviceFilter"),
Expand All @@ -2210,7 +2200,7 @@ func TestApiGetTenantDevicesV2(t *testing.T) {
err: nil,
skip: 0,
limit: rest_utils.PerPageDefault + 1,
body: string(asJSON(outDevs)),
body: string(asJSON(devs)),
tenant_id: "powerpuff123",

filterMatch: mock.AnythingOfType("model.DeviceFilter"),
Expand All @@ -2223,7 +2213,7 @@ func TestApiGetTenantDevicesV2(t *testing.T) {
err: nil,
skip: 0,
limit: rest_utils.PerPageDefault + 1,
body: string(asJSON(outDevs[:2])),
body: string(asJSON(devs[:2])),
tenant_id: "powerpuff123",

filterMatch: mock.MatchedBy(func(filter model.DeviceFilter) bool {
Expand Down Expand Up @@ -2255,7 +2245,7 @@ func TestApiGetTenantDevicesV2(t *testing.T) {
limit: 3,
code: http.StatusOK,
// reqquested 2 devices per page, so expect only 2
body: string(asJSON(outDevs[:2])),
body: string(asJSON(devs[:2])),
tenant_id: "powerpuff123",

filterMatch: mock.AnythingOfType("model.DeviceFilter"),
Expand Down
51 changes: 0 additions & 51 deletions api/http/model_auth_set_v2.go

This file was deleted.

59 changes: 0 additions & 59 deletions api/http/model_device_v2.go

This file was deleted.

13 changes: 4 additions & 9 deletions model/authset.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2021 Northern.tech AS
// Copyright 2022 Northern.tech AS
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -30,20 +30,15 @@ const (

type AuthSet struct {
Id string `json:"id" bson:"_id,omitempty"`
IdData string `json:"id_data" bson:"id_data,omitempty"`
IdDataStruct map[string]interface{} `bson:"id_data_struct,omitempty"`
IdDataSha256 []byte `bson:"id_data_sha256,omitempty"`
IdData string `json:"-" bson:"id_data,omitempty"`
IdDataStruct map[string]interface{} `json:"identity_data" bson:"id_data_struct,omitempty"`
IdDataSha256 []byte `json:"-" bson:"id_data_sha256,omitempty"`
PubKey string `json:"pubkey" bson:"pubkey,omitempty"`
DeviceId string `json:"-" bson:"device_id,omitempty"`
Timestamp *time.Time `json:"ts" bson:"ts,omitempty"`
Status string `json:"status" bson:"status,omitempty"`
}

type DevAuthSet struct {
Id string `json:"id" bson:"_id,omitempty"`
DeviceId string `json:"device_id" bson:"device_id,omitempty"`
}

type AuthSetUpdate struct {
Id string `bson:"id,omitempty"`
IdData string `bson:"id_data,omitempty"`
Expand Down
8 changes: 4 additions & 4 deletions model/device.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,10 @@ var (
// otherwise the underscore will be removed upon write to mongo
type Device struct {
Id string `json:"id" bson:"_id,omitempty"`
IdData string `json:"id_data" bson:"id_data,omitempty"`
IdDataStruct map[string]interface{} `bson:"id_data_struct,omitempty"`
IdDataSha256 []byte `bson:"id_data_sha256,omitempty"`
Status string `json:"-" bson:",omitempty"`
IdData string `json:"-" bson:"id_data,omitempty"`
IdDataStruct map[string]interface{} `json:"identity_data" bson:"id_data_struct,omitempty"`
IdDataSha256 []byte `json:"-" bson:"id_data_sha256,omitempty"`
Status string `json:"status" bson:",omitempty"`
Decommissioning bool `json:"decommissioning" bson:",omitempty"`
CreatedTs time.Time `json:"created_ts" bson:"created_ts,omitempty"`
UpdatedTs time.Time `json:"updated_ts" bson:"updated_ts,omitempty"`
Expand Down

0 comments on commit db98c56

Please sign in to comment.