Skip to content

Commit

Permalink
Fix gateway status endpoint 404 error & ListGateways spec/handler mis…
Browse files Browse the repository at this point in the history
…match (#610)

Summary:
Pull Request resolved: #610

The handler was pointing to /state where the swagger spec points to /status
Modifying ListGateways handler to reflect what is specified in the swagger spec (display all gateways instead of just listing gw ids)

{F206771016}

{F206773758}

Reviewed By: xjtian

Differential Revision: D17193878

fbshipit-source-id: 1054375d819f0bc13e716bf48e7ccdc3b0d6570e
  • Loading branch information
themarwhal authored and facebook-github-bot committed Sep 5, 2019
1 parent 94d21e9 commit 5cedeb5
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 13 deletions.
43 changes: 39 additions & 4 deletions orc8r/cloud/go/pluginimpl/handlers/gateway_handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ package handlers

import (
"net/http"
"sort"

merrors "magma/orc8r/cloud/go/errors"
"magma/orc8r/cloud/go/obsidian"
Expand All @@ -21,6 +20,7 @@ import (
"magma/orc8r/cloud/go/services/state"
"magma/orc8r/cloud/go/storage"

"github.com/go-openapi/swag"
"github.com/labstack/echo"
"github.com/pkg/errors"
)
Expand All @@ -31,12 +31,30 @@ func ListGatewaysHandler(c echo.Context) error {
return nerr
}

ids, err := configurator.ListEntityKeys(nid, orc8r.MagmadGatewayType)
ents, _, err := configurator.LoadEntities(nid, swag.String(orc8r.MagmadGatewayType), nil, nil, nil, configurator.FullEntityLoadCriteria())
if err != nil {
return obsidian.HttpError(err, http.StatusInternalServerError)
}
sort.Strings(ids)
return c.JSON(http.StatusOK, ids)
entsByTK := ents.ToEntitiesByID()

// for each magmad gateway, we have to load its corresponding device and
// its reported status
deviceIDs := make([]string, 0, len(entsByTK))
for tk, ent := range entsByTK {
if tk.Type == orc8r.MagmadGatewayType && ent.PhysicalID != "" {
deviceIDs = append(deviceIDs, ent.PhysicalID)
}
}

devicesByID, err := device.GetDevices(nid, orc8r.AccessGatewayRecordType, deviceIDs)
if err != nil {
return obsidian.HttpError(errors.Wrap(err, "failed to load devices"), http.StatusInternalServerError)
}
statusesByID, err := state.GetGatewayStatuses(nid, deviceIDs)
if err != nil {
return obsidian.HttpError(errors.Wrap(err, "failed to load statuses"), http.StatusInternalServerError)
}
return c.JSON(http.StatusOK, makeGateways(entsByTK, devicesByID, statusesByID))
}

func CreateGatewayHandler(c echo.Context) error {
Expand Down Expand Up @@ -240,3 +258,20 @@ func GetStateHandler(c echo.Context) error {

return c.JSON(http.StatusOK, state)
}

func makeGateways(
entsByTK map[storage.TypeAndKey]configurator.NetworkEntity,
devicesByID map[string]interface{},
statusesByID map[string]*models.GatewayStatus,
) map[string]*models.MagmadGateway {
gatewayEntsByKey := map[string]*models.MagmadGateway{}
for tk, ent := range entsByTK {
hwID := ent.PhysicalID
var devCasted *models.GatewayDevice
if devicesByID[hwID] != nil {
devCasted = devicesByID[hwID].(*models.GatewayDevice)
}
gatewayEntsByKey[tk.Key] = (&models.MagmadGateway{}).FromBackendModels(ent, devCasted, statusesByID[hwID])
}
return gatewayEntsByKey
}
38 changes: 31 additions & 7 deletions orc8r/cloud/go/pluginimpl/handlers/gateway_handlers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ import (
func TestListGateways(t *testing.T) {
_ = plugin.RegisterPluginForTests(t, &pluginimpl.BaseOrchestratorPlugin{})
test_init.StartTestService(t)
deviceTestInit.StartTestService(t)
stateTestInit.StartTestService(t)
err := configurator.CreateNetwork(configurator.Network{ID: "n1"})
assert.NoError(t, err)

Expand All @@ -56,22 +58,44 @@ func TestListGateways(t *testing.T) {
ParamNames: []string{"network_id"},
ParamValues: []string{"n1"},
ExpectedStatus: 200,
ExpectedResult: tests.JSONMarshaler([]string{}),
ExpectedResult: tests.JSONMarshaler(map[string]models.MagmadGateway{}),
}
tests.RunUnitTest(t, e, tc)

// happy path
_, err = configurator.CreateEntities(
"n1",
[]configurator.NetworkEntity{
{Type: orc8r.MagmadGatewayType, Key: "gz"},
{Type: orc8r.MagmadGatewayType, Key: "g2"},
{Type: orc8r.MagmadGatewayType, Key: "g1"},
{Type: orc8r.MagmadGatewayType, Key: "g3"},
{Type: orc8r.MagmadGatewayType, Key: "g1", Config: &models.MagmadGatewayConfigs{}, PhysicalID: "hw1"},
{Type: orc8r.MagmadGatewayType, Key: "g2", Config: &models.MagmadGatewayConfigs{CheckinInterval: 15}},
},
)
assert.NoError(t, err)
tc.ExpectedResult = tests.JSONMarshaler([]string{"g1", "g2", "g3", "gz"})
expectedResult := map[string]models.MagmadGateway{
"g1": {ID: "g1", Magmad: &models.MagmadGatewayConfigs{}},
"g2": {ID: "g2", Magmad: &models.MagmadGatewayConfigs{CheckinInterval: 15}},
}
tc.ExpectedResult = tests.JSONMarshaler(expectedResult)
tests.RunUnitTest(t, e, tc)

// add device and state to g1
clock.SetAndFreezeClock(t, time.Unix(1000000, 0))
defer clock.GetUnfreezeClockDeferFunc(t)()
gatewayRecord := &models.GatewayDevice{HardwareID: "hw1", Key: &models.ChallengeKey{KeyType: "ECHO"}}
err = device.RegisterDevice("n1", orc8r.AccessGatewayRecordType, "hw1", gatewayRecord)
assert.NoError(t, err)
ctx := test_utils.GetContextWithCertificate(t, "hw1")
test_utils.ReportGatewayStatus(t, ctx, models.NewDefaultGatewayStatus("hw1"))

expectedState := models.NewDefaultGatewayStatus("hw1")
expectedState.CheckinTime = uint64(time.Unix(1000000, 0).UnixNano() / (int64(time.Millisecond) / int64(time.Nanosecond)))
expectedState.CertExpirationTime = time.Unix(1000000, 0).Add(time.Hour * 4).Unix()

expectedResult = map[string]models.MagmadGateway{
"g1": {ID: "g1", Magmad: &models.MagmadGatewayConfigs{}, Device: gatewayRecord, Status: expectedState},
"g2": {ID: "g2", Magmad: &models.MagmadGatewayConfigs{CheckinInterval: 15}},
}
tc.ExpectedResult = tests.JSONMarshaler(expectedResult)
tests.RunUnitTest(t, e, tc)
}

Expand Down Expand Up @@ -650,7 +674,7 @@ func TestGetPartialReadHandlers(t *testing.T) {
obsidianHandlers := handlers.GetObsidianHandlers()
getGatewayName := tests.GetHandlerByPathAndMethod(t, obsidianHandlers, "/magma/v1/networks/:network_id/gateways/:gateway_id/name", obsidian.GET).HandlerFunc
getGatewayDescription := tests.GetHandlerByPathAndMethod(t, obsidianHandlers, "/magma/v1/networks/:network_id/gateways/:gateway_id/description", obsidian.GET).HandlerFunc
getGatewayState := tests.GetHandlerByPathAndMethod(t, obsidianHandlers, "/magma/v1/networks/:network_id/gateways/:gateway_id/state", obsidian.GET).HandlerFunc
getGatewayState := tests.GetHandlerByPathAndMethod(t, obsidianHandlers, "/magma/v1/networks/:network_id/gateways/:gateway_id/status", obsidian.GET).HandlerFunc
getGatewayDevice := tests.GetHandlerByPathAndMethod(t, obsidianHandlers, "/magma/v1/networks/:network_id/gateways/:gateway_id/device", obsidian.GET).HandlerFunc
getGatewayConfig := tests.GetHandlerByPathAndMethod(t, obsidianHandlers, "/magma/v1/networks/:network_id/gateways/:gateway_id/magmad", obsidian.GET).HandlerFunc

Expand Down
2 changes: 1 addition & 1 deletion orc8r/cloud/go/pluginimpl/handlers/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ const (
ManageGatewayDescriptionPath = ManageGatewayPath + obsidian.UrlSep + "description"
ManageGatewayConfigPath = ManageGatewayPath + obsidian.UrlSep + "magmad"
ManageGatewayDevicePath = ManageGatewayPath + obsidian.UrlSep + "device"
ManageGatewayStatePath = ManageGatewayPath + obsidian.UrlSep + "state"
ManageGatewayStatePath = ManageGatewayPath + obsidian.UrlSep + "status"
ManageGatewayTierPath = ManageGatewayPath + obsidian.UrlSep + "tier"

Channels = "channels"
Expand Down
4 changes: 3 additions & 1 deletion orc8r/cloud/go/pluginimpl/models/conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,9 @@ func (m *MagmadGateway) FromBackendModels(ent configurator.NetworkEntity, device
m.ID = models.GatewayID(ent.Key)
m.Name = models.GatewayName(ent.Name)
m.Description = models.GatewayDescription(ent.Description)
m.Magmad = ent.Config.(*MagmadGatewayConfigs)
if ent.Config != nil {
m.Magmad = ent.Config.(*MagmadGatewayConfigs)
}
m.Device = device
m.Status = status
tierTK, err := ent.GetFirstParentOfType(orc8r.UpgradeTierEntityType)
Expand Down

0 comments on commit 5cedeb5

Please sign in to comment.