Change admin server to core in jobservice

Signed-off-by: stonezdj <stonezdj@gmail.com>
This commit is contained in:
stonezdj 2018-10-10 17:10:56 +08:00
parent fa02521fec
commit 79bac7a64e
10 changed files with 57 additions and 8 deletions

View File

@ -1,4 +1,4 @@
CORE_SECRET=$core_secret
JOBSERVICE_SECRET=$jobservice_secret
ADMINSERVER_URL=$adminserver_url
CORE_URL=$core_url
GODEBUG=netdns=cgo

View File

@ -428,7 +428,7 @@ render(os.path.join(templates_dir, "jobservice", "env"),
job_conf_env,
core_secret=core_secret,
jobservice_secret=jobservice_secret,
adminserver_url=adminserver_url)
core_url=core_url)
render(os.path.join(templates_dir, "jobservice", "config.yml"),
jobservice_conf,

View File

@ -74,7 +74,7 @@ func (c *client) Ping() error {
// GetCfgs ...
func (c *client) GetCfgs() (map[string]interface{}, error) {
url := c.baseURL + "/api/configurations"
url := c.baseURL + "/api/configs"
cfgs := map[string]interface{}{}
if err := c.client.Get(url, &cfgs); err != nil {
return nil, err

View File

@ -23,8 +23,8 @@ import (
func newRouter() http.Handler {
r := mux.NewRouter()
r.HandleFunc("/api/configurations", api.ListCfgs).Methods("GET")
r.HandleFunc("/api/configurations", api.UpdateCfgs).Methods("PUT")
r.HandleFunc("/api/configs", api.ListCfgs).Methods("GET")
r.HandleFunc("/api/configurations/reset", api.ResetCfgs).Methods("POST")
r.HandleFunc("/api/systeminfo/capacity", api.Capacity).Methods("GET")
r.HandleFunc("/api/ping", api.Ping).Methods("GET")

View File

@ -102,7 +102,7 @@ func NewAdminserver(config map[string]interface{}) (*httptest.Server, error) {
m = append(m, &RequestHandlerMapping{
Method: "GET",
Pattern: "/api/configurations",
Pattern: "/api/configs",
Handler: Handler(resp),
})

View File

@ -74,6 +74,17 @@ func (c *ConfigAPI) Get() {
c.ServeJSON()
}
// GetInternalConfig returns internal configurations
func (c *ConfigAPI) GetInternalConfig() {
configs, err := config.GetSystemCfg()
if err != nil {
log.Errorf("failed to get configurations: %v", err)
c.CustomAbort(http.StatusInternalServerError, http.StatusText(http.StatusInternalServerError))
}
c.Data["json"] = configs
c.ServeJSON()
}
// Put updates configurations
func (c *ConfigAPI) Put() {
m := map[string]interface{}{}

View File

@ -55,6 +55,30 @@ func TestGetConfig(t *testing.T) {
t.Logf("%v", ccc)
}
func TestInternalConfig(t *testing.T) {
fmt.Println("Testing internal configurations")
assert := assert.New(t)
apiTest := newHarborAPI()
// case 1: get configurations without admin role
code, _, err := apiTest.GetInternalConfig(*testUser)
if err != nil {
t.Fatalf("failed to get configurations: %v", err)
}
assert.Equal(401, code, "the status code of getting configurations with non-admin user should be 401")
// case 2: get configurations with admin role
code, _, err = apiTest.GetInternalConfig(*admin)
if err != nil {
t.Fatalf("failed to get configurations: %v", err)
}
if !assert.Equal(200, code, "the status code of getting configurations with admin user should be 200") {
return
}
}
func TestPutConfig(t *testing.T) {
fmt.Println("Testing modifying configurations")
assert := assert.New(t)

View File

@ -161,6 +161,7 @@ func init() {
beego.Router("/api/ldap/users/import", &LdapAPI{ldapConfig: ldapTestConfig, useTestConfig: true}, "post:ImportUser")
beego.Router("/api/configurations", &ConfigAPI{})
beego.Router("/api/configurations/reset", &ConfigAPI{}, "post:Reset")
beego.Router("/api/configs", &ConfigAPI{}, "get:GetInternalConfig")
beego.Router("/api/email/ping", &EmailAPI{}, "post:Ping")
beego.Router("/api/replications", &ReplicationAPI{})
beego.Router("/api/labels", &LabelAPI{}, "post:Post;get:List")
@ -1090,6 +1091,18 @@ func (a testapi) GetConfig(authInfo usrInfo) (int, map[string]*value, error) {
return code, cfg, err
}
func (a testapi) GetInternalConfig(authInfo usrInfo) (int, map[string]interface{}, error) {
_sling := sling.New().Base(a.basePath).Get("/api/configs")
cfg := map[string]interface{}{}
code, body, err := request(_sling, jsonAcceptHeader, authInfo)
if err == nil && code == 200 {
err = json.Unmarshal(body, &cfg)
}
return code, cfg, err
}
func (a testapi) PutConfig(authInfo usrInfo, cfg map[string]interface{}) (int, error) {
_sling := sling.New().Base(a.basePath).Put("/api/configurations").BodyJSON(cfg)

View File

@ -97,6 +97,7 @@ func initRouters() {
beego.Router("/api/targets/:id([0-9]+)/policies/", &api.TargetAPI{}, "get:ListPolicies")
beego.Router("/api/targets/ping", &api.TargetAPI{}, "post:Ping")
beego.Router("/api/logs", &api.LogAPI{})
beego.Router("/api/configs", &api.ConfigAPI{}, "get:GetInternalConfig")
beego.Router("/api/configurations", &api.ConfigAPI{})
beego.Router("/api/configurations/reset", &api.ConfigAPI{}, "post:Reset")
beego.Router("/api/statistics", &api.StatisticAPI{})

View File

@ -27,7 +27,7 @@ const (
jobServiceLoggerBasePath = "JOB_SERVICE_LOGGER_BASE_PATH"
jobServiceLoggerLevel = "JOB_SERVICE_LOGGER_LEVEL"
jobServiceLoggerArchivePeriod = "JOB_SERVICE_LOGGER_ARCHIVE_PERIOD"
jobServiceAdminServerEndpoint = "ADMINSERVER_URL"
jobServiceCoreServerEndpoint = "CORE_URL"
jobServiceAuthSecret = "JOBSERVICE_SECRET"
// JobServiceProtocolHTTPS points to the 'https' protocol
@ -282,8 +282,8 @@ func (c *Configuration) loadEnvs() {
}
// admin server
if adminServer := utils.ReadEnv(jobServiceAdminServerEndpoint); !utils.IsEmptyStr(adminServer) {
c.AdminServer = adminServer
if coreServer := utils.ReadEnv(jobServiceCoreServerEndpoint); !utils.IsEmptyStr(coreServer) {
c.AdminServer = coreServer
}
}