feat: 设备编号从 00000001 顺序分配
This commit is contained in:
@@ -30,18 +30,18 @@ func newFakeNix() *fakeNix {
|
||||
return &fakeNix{online: map[string]bool{}, passwords: map[string]string{}}
|
||||
}
|
||||
|
||||
func (f *fakeNix) CreateEndpoint(context.Context, string) (string, string, error) {
|
||||
func (f *fakeNix) CreateEndpoint(_ context.Context, id, name string) (string, error) {
|
||||
f.mu.Lock()
|
||||
defer f.mu.Unlock()
|
||||
f.next++
|
||||
id := "e_dev" + string(rune('0'+f.next))
|
||||
if f.next >= 10 {
|
||||
id = "e_devx"
|
||||
if id == "" {
|
||||
f.next++
|
||||
id = "e_dev"
|
||||
}
|
||||
pw := "DevicePassw0rd" + string(rune('0'+f.next))
|
||||
f.online[id] = f.next == 1
|
||||
_ = name
|
||||
pw := "DevicePassw0rd"
|
||||
f.online[id] = true
|
||||
f.passwords[id] = pw
|
||||
return id, pw, nil
|
||||
return pw, nil
|
||||
}
|
||||
|
||||
func (f *fakeNix) Lookup(_ context.Context, id string) (bool, bool, error) {
|
||||
@@ -262,7 +262,7 @@ func TestGroupsDevicesAndLogs(t *testing.T) {
|
||||
deviceID := created["id"]
|
||||
endpointID, _ := created["endpoint_id"].(string)
|
||||
password, _ := created["login_password"].(string)
|
||||
if endpointID == "" || password == "" {
|
||||
if endpointID != "00000001" || password == "" {
|
||||
t.Fatalf("missing one-time secret %#v", created)
|
||||
}
|
||||
if created["presence"] != "online" {
|
||||
@@ -273,6 +273,9 @@ func TestGroupsDevicesAndLogs(t *testing.T) {
|
||||
t.Fatalf("second device %d %#v", status, body)
|
||||
}
|
||||
secondID := dataOf(t, body)["endpoint_id"].(string)
|
||||
if secondID != "00000002" {
|
||||
t.Fatalf("second endpoint %s", secondID)
|
||||
}
|
||||
|
||||
status, body = alice.call(t, http.MethodGet, "/api/devices", nil, false)
|
||||
if status != http.StatusOK {
|
||||
|
||||
@@ -110,7 +110,17 @@ func (h *Handler) createDevice(w http.ResponseWriter, r *http.Request) {
|
||||
writeError(w, http.StatusServiceUnavailable, "unavailable", "暂时无法新建设备")
|
||||
return
|
||||
}
|
||||
id, pw, err := h.nix.CreateEndpoint(r.Context(), name)
|
||||
id, err := h.store.AllocateEndpointID(r.Context())
|
||||
if errors.Is(err, store.ErrEndpointExhausted) {
|
||||
writeError(w, http.StatusConflict, "conflict", "不能再新增设备")
|
||||
return
|
||||
}
|
||||
if err != nil {
|
||||
h.log.Error("allocate endpoint id", "err", err)
|
||||
writeError(w, http.StatusInternalServerError, "internal", "出了点问题,请稍后再试")
|
||||
return
|
||||
}
|
||||
pw, err := h.nix.CreateEndpoint(r.Context(), id, name)
|
||||
if err != nil {
|
||||
h.log.Error("provision endpoint failed")
|
||||
writeError(w, http.StatusServiceUnavailable, "unavailable", "暂时无法新建设备")
|
||||
|
||||
@@ -35,7 +35,7 @@ type Handler struct {
|
||||
}
|
||||
|
||||
type EndpointService interface {
|
||||
CreateEndpoint(ctx context.Context, name string) (id, password string, err error)
|
||||
CreateEndpoint(ctx context.Context, id, name string) (password string, err error)
|
||||
Lookup(ctx context.Context, id string) (found, online bool, err error)
|
||||
DeleteEndpoint(ctx context.Context, id string) error
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user