-
Notifications
You must be signed in to change notification settings - Fork 542
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement API to get apisix instances status (#958)
related #849 .
- Loading branch information
Showing
9 changed files
with
539 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package server_info | ||
|
||
import ( | ||
"reflect" | ||
"strings" | ||
|
||
"github.com/gin-gonic/gin" | ||
"github.com/shiningrush/droplet" | ||
"github.com/shiningrush/droplet/wrapper" | ||
wgin "github.com/shiningrush/droplet/wrapper/gin" | ||
|
||
"github.com/apisix/manager-api/internal/core/entity" | ||
"github.com/apisix/manager-api/internal/core/store" | ||
"github.com/apisix/manager-api/internal/handler" | ||
) | ||
|
||
type Handler struct { | ||
serverInfoStore store.Interface | ||
} | ||
|
||
func NewHandler() (handler.RouteRegister, error) { | ||
return &Handler{ | ||
serverInfoStore: store.GetStore(store.HubKeyServerInfo), | ||
}, nil | ||
} | ||
|
||
func (h *Handler) ApplyRoute(r *gin.Engine) { | ||
r.GET("/apisix/server_info/:id", wgin.Wraps(h.Get, | ||
wrapper.InputType(reflect.TypeOf(GetInput{})))) | ||
r.GET("/apisix/server_info", wgin.Wraps(h.List, | ||
wrapper.InputType(reflect.TypeOf(ListInput{})))) | ||
} | ||
|
||
type GetInput struct { | ||
ID string `auto_read:"id,path" validate:"required"` | ||
} | ||
|
||
func (h *Handler) Get(c droplet.Context) (interface{}, error) { | ||
input := c.Input().(*GetInput) | ||
|
||
r, err := h.serverInfoStore.Get(input.ID) | ||
if err != nil { | ||
return handler.SpecCodeResponse(err), err | ||
} | ||
|
||
return r, nil | ||
} | ||
|
||
type ListInput struct { | ||
store.Pagination | ||
Hostname string `auto_read:"hostname,query"` | ||
} | ||
|
||
func (h *Handler) List(c droplet.Context) (interface{}, error) { | ||
input := c.Input().(*ListInput) | ||
|
||
ret, err := h.serverInfoStore.List(store.ListInput{ | ||
Predicate: func(obj interface{}) bool { | ||
if input.Hostname != "" { | ||
return strings.Contains(obj.(*entity.ServerInfo).Hostname, input.Hostname) | ||
} | ||
return true | ||
}, | ||
PageSize: input.PageSize, | ||
PageNumber: input.PageNumber, | ||
}) | ||
|
||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return ret, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,222 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package server_info | ||
|
||
import ( | ||
"errors" | ||
"testing" | ||
|
||
"github.com/shiningrush/droplet" | ||
"github.com/shiningrush/droplet/data" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/mock" | ||
|
||
"github.com/apisix/manager-api/internal/core/entity" | ||
"github.com/apisix/manager-api/internal/core/store" | ||
) | ||
|
||
func TestHandler_Get(t *testing.T) { | ||
var ( | ||
tests = []struct { | ||
caseDesc string | ||
giveInput *GetInput | ||
giveErr error | ||
giveRet interface{} | ||
wantErr error | ||
wantGetKey string | ||
wantRet interface{} | ||
}{ | ||
{ | ||
caseDesc: "get server_info", | ||
giveInput: &GetInput{ID: "server_1"}, | ||
giveRet: &entity.ServerInfo{ | ||
BaseInfo: entity.BaseInfo{ID: "server_1"}, | ||
UpTime: 10, | ||
LastReportTime: 1608195454, | ||
BootTime: 1608195454, | ||
Hostname: "gentoo", | ||
Version: "v3", | ||
}, | ||
wantGetKey: "server_1", | ||
wantRet: &entity.ServerInfo{ | ||
BaseInfo: entity.BaseInfo{ID: "server_1"}, | ||
UpTime: 10, | ||
LastReportTime: 1608195454, | ||
BootTime: 1608195454, | ||
Hostname: "gentoo", | ||
Version: "v3", | ||
}, | ||
}, | ||
{ | ||
caseDesc: "get server_info not exist", | ||
giveInput: &GetInput{ID: "server_3"}, | ||
giveRet: &data.SpecCodeResponse{Response: data.Response{Code: 0}, StatusCode: 404}, | ||
giveErr: errors.New("not found"), | ||
wantGetKey: "server_3", | ||
wantRet: &data.SpecCodeResponse{Response: data.Response{Code: 0}, StatusCode: 404}, | ||
wantErr: errors.New("not found"), | ||
}, | ||
} | ||
) | ||
|
||
for _, tc := range tests { | ||
t.Run(tc.caseDesc, func(t *testing.T) { | ||
getCalled := true | ||
mStore := &store.MockInterface{} | ||
mStore.On("Get", mock.Anything).Run(func(args mock.Arguments) { | ||
getCalled = true | ||
assert.Equal(t, tc.wantGetKey, args.Get(0)) | ||
}).Return(tc.giveRet, tc.giveErr) | ||
|
||
h := Handler{serverInfoStore: mStore} | ||
ctx := droplet.NewContext() | ||
ctx.SetInput(tc.giveInput) | ||
ret, err := h.Get(ctx) | ||
assert.True(t, getCalled) | ||
assert.Equal(t, tc.wantErr, err) | ||
assert.Equal(t, tc.wantRet, ret) | ||
}) | ||
} | ||
} | ||
|
||
func TestHandler_List(t *testing.T) { | ||
var ( | ||
tests = []struct { | ||
caseDesc string | ||
giveInput *ListInput | ||
giveData []interface{} | ||
giveErr error | ||
wantErr error | ||
wantGetKey *ListInput | ||
wantRet interface{} | ||
}{ | ||
{ | ||
caseDesc: "list server_info", | ||
giveInput: &ListInput{Hostname: ""}, | ||
giveData: []interface{}{ | ||
&entity.ServerInfo{ | ||
BaseInfo: entity.BaseInfo{ID: "server_1"}, | ||
UpTime: 10, | ||
LastReportTime: 1608195454, | ||
BootTime: 1608195454, | ||
Hostname: "gentoo", | ||
Version: "v3", | ||
}, | ||
&entity.ServerInfo{ | ||
BaseInfo: entity.BaseInfo{ID: "server_2"}, | ||
UpTime: 10, | ||
LastReportTime: 1608195454, | ||
BootTime: 1608195454, | ||
Hostname: "ubuntu", | ||
Version: "v2", | ||
}, | ||
}, | ||
wantRet: &store.ListOutput{ | ||
Rows: []interface{}{ | ||
&entity.ServerInfo{ | ||
BaseInfo: entity.BaseInfo{ID: "server_1"}, | ||
UpTime: 10, | ||
LastReportTime: 1608195454, | ||
BootTime: 1608195454, | ||
Hostname: "gentoo", | ||
Version: "v3", | ||
}, | ||
&entity.ServerInfo{ | ||
BaseInfo: entity.BaseInfo{ID: "server_2"}, | ||
UpTime: 10, | ||
LastReportTime: 1608195454, | ||
BootTime: 1608195454, | ||
Hostname: "ubuntu", | ||
Version: "v2", | ||
}, | ||
}, | ||
TotalSize: 2, | ||
}, | ||
}, | ||
{ | ||
caseDesc: "list server_info with hostname", | ||
giveInput: &ListInput{Hostname: "ubuntu"}, | ||
giveData: []interface{}{ | ||
&entity.ServerInfo{ | ||
BaseInfo: entity.BaseInfo{ID: "server_1"}, | ||
UpTime: 10, | ||
LastReportTime: 1608195454, | ||
BootTime: 1608195454, | ||
Hostname: "gentoo", | ||
Version: "v3", | ||
}, | ||
&entity.ServerInfo{ | ||
BaseInfo: entity.BaseInfo{ID: "server_2"}, | ||
UpTime: 10, | ||
LastReportTime: 1608195454, | ||
BootTime: 1608195454, | ||
Hostname: "ubuntu", | ||
Version: "v2", | ||
}, | ||
}, | ||
wantRet: &store.ListOutput{ | ||
Rows: []interface{}{ | ||
&entity.ServerInfo{ | ||
BaseInfo: entity.BaseInfo{ID: "server_2"}, | ||
UpTime: 10, | ||
LastReportTime: 1608195454, | ||
BootTime: 1608195454, | ||
Hostname: "ubuntu", | ||
Version: "v2", | ||
}, | ||
}, | ||
TotalSize: 1, | ||
}, | ||
}, | ||
} | ||
) | ||
|
||
for _, tc := range tests { | ||
t.Run(tc.caseDesc, func(t *testing.T) { | ||
getCalled := true | ||
mStore := &store.MockInterface{} | ||
mStore.On("List", mock.Anything).Run(func(args mock.Arguments) { | ||
getCalled = true | ||
}).Return(func(input store.ListInput) *store.ListOutput { | ||
var res []interface{} | ||
for _, c := range tc.giveData { | ||
if input.Predicate(c) { | ||
if input.Format != nil { | ||
res = append(res, input.Format(c)) | ||
} else { | ||
res = append(res, c) | ||
} | ||
} | ||
} | ||
|
||
return &store.ListOutput{ | ||
Rows: res, | ||
TotalSize: len(res), | ||
} | ||
}, tc.giveErr) | ||
|
||
h := Handler{serverInfoStore: mStore} | ||
ctx := droplet.NewContext() | ||
ctx.SetInput(tc.giveInput) | ||
ret, err := h.List(ctx) | ||
assert.True(t, getCalled) | ||
assert.Equal(t, tc.wantErr, err) | ||
assert.Equal(t, tc.wantRet, ret) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.