From 108613bd3225b08749be93e311b812199cbb46ee Mon Sep 17 00:00:00 2001 From: Qiu Jian Date: Mon, 11 May 2020 17:25:43 +0800 Subject: [PATCH] detect bmagent and hostagent service --- pkg/apigateway/handler/auth.go | 127 ++++++++++++++++++++++----------- pkg/mcclient/catalog.go | 2 + pkg/mcclient/token.go | 2 - pkg/mcclient/token2.go | 16 +++-- pkg/mcclient/token3.go | 16 ++--- 5 files changed, 106 insertions(+), 57 deletions(-) diff --git a/pkg/apigateway/handler/auth.go b/pkg/apigateway/handler/auth.go index fb89f19c52..e4c32623ab 100644 --- a/pkg/apigateway/handler/auth.go +++ b/pkg/apigateway/handler/auth.go @@ -677,26 +677,57 @@ func getUserAuthCookie(ctx context.Context, s *mcclient.ClientSession, token mcc return info.String(), nil } -func getLBAgentInfo(s *mcclient.ClientSession, token mcclient.TokenCredential) (*jsonutils.JSONDict, error) { +func isLBAgentExists(s *mcclient.ClientSession) (bool, error) { params := jsonutils.NewDict() params.Add(jsonutils.NewString("hb_last_seen.isnotempty()"), "filter.0") params.Add(jsonutils.NewInt(1), "limit") params.Add(jsonutils.JSONFalse, "details") - lbagents, err := modules.LoadbalancerAgents.List(s, params) + agents, err := modules.LoadbalancerAgents.List(s, params) if err != nil { - return nil, errors.Wrapf(err, "user %s get lbagent", token.GetUserName()) + return false, errors.Wrap(err, "modules.LoadbalancerAgents.List") } - item := jsonutils.NewDict() - item.Add(jsonutils.NewString(""), "name") - item.Add(jsonutils.NewString("lbagent"), "type") - item.Add(jsonutils.JSONTrue, "as_menu") - if len(lbagents.Data) > 0 { - item.Add(jsonutils.JSONTrue, "status") + if len(agents.Data) > 0 { + return true, nil } else { - item.Add(jsonutils.JSONFalse, "status") + return false, nil + } +} + +func isBaremetalAgentExists(s *mcclient.ClientSession) (bool, error) { + params := jsonutils.NewDict() + params.Add(jsonutils.NewString("agent_type.equals(baremetal)"), "filter.0") + params.Add(jsonutils.NewInt(1), "limit") + params.Add(jsonutils.JSONFalse, "details") + agents, err := modules.Baremetalagents.List(s, params) + if err != nil { + return false, errors.Wrap(err, "modules.LoadbalancerAgents.List") + } + + if len(agents.Data) > 0 { + return true, nil + } else { + return false, nil + } +} + +func isHostAgentExists(s *mcclient.ClientSession) (bool, error) { + params := jsonutils.NewDict() + params.Add(jsonutils.JSONFalse, "show_emulated") + params.Add(jsonutils.JSONFalse, "baremetal") + params.Add(jsonutils.NewString("system"), "scope") + params.Add(jsonutils.NewInt(1), "limit") + params.Add(jsonutils.JSONFalse, "details") + agents, err := modules.Baremetalagents.List(s, params) + if err != nil { + return false, errors.Wrap(err, "modules.LoadbalancerAgents.List") + } + + if len(agents.Data) > 0 { + return true, nil + } else { + return false, nil } - return item, nil } func getUserInfo(ctx context.Context, s *mcclient.ClientSession, token mcclient.TokenCredential, req *http.Request) (*jsonutils.JSONDict, error) { @@ -787,43 +818,53 @@ func getUserInfo(ctx context.Context, s *mcclient.ClientSession, token mcclient. menus := jsonutils.NewArray() k8s := jsonutils.NewArray() - adminToken := auth.AdminCredential() curReg := FetchRegion(req) - allsrv := adminToken.GetInternalServices(curReg) - alleps := auth.Client().GetServiceCatalog().GetServicesByInterface(curReg, "console") - - if allsrv != nil && len(allsrv) > 0 { - for _, srv := range allsrv { - item := jsonutils.NewDict() - item.Add(jsonutils.NewString(""), "name") - item.Add(jsonutils.NewString(srv), "type") - item.Add(jsonutils.JSONTrue, "status") - if srv == "notify" { - item.Add(jsonutils.JSONFalse, "as_menu") - } else { - item.Add(jsonutils.JSONTrue, "as_menu") - } - services.Add(item) - } - } else { - log.Errorf("fail to find services????: %#v %s", adminToken, curReg) + srvCat := auth.Client().GetServiceCatalog() + var allsrv []string + var alleps []mcclient.ExternalService + if srvCat != nil { + allsrv = srvCat.GetInternalServices(curReg) + alleps = srvCat.GetServicesByInterface(curReg, "console") } - log.Infof("getUserInfo getLBAgentInfo") - lb, err := getLBAgentInfo(s, adminToken) - if err != nil { - log.Errorf("getLBAgentInfo fail %s", err) - } else { - services.Add(lb) + log.Infof("getUserInfo checkAgent exists") + for _, cf := range []struct { + existFunc func(*mcclient.ClientSession) (bool, error) + srvName string + }{ + { + existFunc: isLBAgentExists, + srvName: "lbagent", + }, + { + existFunc: isBaremetalAgentExists, + srvName: "bmagent", + }, + { + existFunc: isHostAgentExists, + srvName: "hostagent", + }, + } { + exist, err := cf.existFunc(s) + if err != nil { + log.Errorf("isLBAgentExists fail %s", err) + } else if exist { + allsrv = append(allsrv, cf.srvName) + } } - if alleps != nil { - for _, ep := range alleps { - item := jsonutils.NewDict() - item.Add(jsonutils.NewString(ep.Url), "url") - item.Add(jsonutils.NewString(ep.Name), "name") - menus.Add(item) - } + for _, srv := range allsrv { + item := jsonutils.NewDict() + item.Add(jsonutils.NewString(srv), "type") + item.Add(jsonutils.JSONTrue, "status") + services.Add(item) + } + + for _, ep := range alleps { + item := jsonutils.NewDict() + item.Add(jsonutils.NewString(ep.Url), "url") + item.Add(jsonutils.NewString(ep.Name), "name") + menus.Add(item) } log.Infof("getUserInfo modules.Hosts.Get") diff --git a/pkg/mcclient/catalog.go b/pkg/mcclient/catalog.go index 4e38e1003a..7ddd263920 100644 --- a/pkg/mcclient/catalog.go +++ b/pkg/mcclient/catalog.go @@ -18,5 +18,7 @@ type IServiceCatalog interface { Len() int GetServiceURL(service, region, zone, endpointType string) (string, error) GetServiceURLs(service, region, zone, endpointType string) ([]string, error) + GetInternalServices(region string) []string + GetExternalServices(region string) []ExternalService GetServicesByInterface(region string, infType string) []ExternalService } diff --git a/pkg/mcclient/token.go b/pkg/mcclient/token.go index dd25edaafc..8557c0203f 100644 --- a/pkg/mcclient/token.go +++ b/pkg/mcclient/token.go @@ -87,8 +87,6 @@ type TokenCredential interface { GetServiceCatalog() IServiceCatalog GetCatalogData(serviceTypes []string, region string) jsonutils.JSONObject - GetInternalServices(region string) []string - GetExternalServices(region string) []ExternalService GetEndpoints(region string, endpointType string) []Endpoint ToJson() jsonutils.JSONObject diff --git a/pkg/mcclient/token2.go b/pkg/mcclient/token2.go index 3ff68a4a6f..044fc76c49 100644 --- a/pkg/mcclient/token2.go +++ b/pkg/mcclient/token2.go @@ -217,10 +217,6 @@ func (this *TokenCredentialV2) GetServiceURLs(service, region, zone, endpointTyp return this.ServiceCatalog.GetServiceURLs(service, region, zone, endpointType) } -func (this *TokenCredentialV2) GetServicesByInterface(region string, infType string) []ExternalService { - return nil -} - func (this *TokenCredentialV2) GetInternalServices(region string) []string { return nil } @@ -229,6 +225,10 @@ func (this *TokenCredentialV2) GetExternalServices(region string) []ExternalServ return nil } +func (this *TokenCredentialV2) GetServicesByInterface(region string, infType string) []ExternalService { + return nil +} + func (this *TokenCredentialV2) GetEndpoints(region string, endpointType string) []Endpoint { return nil } @@ -333,6 +333,14 @@ func (catalog KeystoneServiceCatalogV2) GetServiceURLs(service, region, zone, en return []string{url}, nil } +func (catalog KeystoneServiceCatalogV2) GetInternalServices(region string) []string { + return nil +} + +func (catalog KeystoneServiceCatalogV2) GetExternalServices(region string) []ExternalService { + return nil +} + func (catalog KeystoneServiceCatalogV2) GetServicesByInterface(region string, infType string) []ExternalService { return nil } diff --git a/pkg/mcclient/token3.go b/pkg/mcclient/token3.go index 6d0967c7f3..016f77bc9e 100644 --- a/pkg/mcclient/token3.go +++ b/pkg/mcclient/token3.go @@ -249,16 +249,16 @@ func (this *TokenCredentialV3) GetServiceURLs(service, region, zone, endpointTyp return this.Token.Catalog.GetServiceURLs(service, region, zone, endpointType) } -func (this *TokenCredentialV3) GetServicesByInterface(region string, infType string) []ExternalService { - return this.Token.Catalog.GetServicesByInterface(region, infType) -} - func (this *TokenCredentialV3) GetInternalServices(region string) []string { - return this.Token.Catalog.getInternalServices(region) + return this.Token.Catalog.GetInternalServices(region) } func (this *TokenCredentialV3) GetExternalServices(region string) []ExternalService { - return this.Token.Catalog.getExternalServices(region) + return this.Token.Catalog.GetExternalServices(region) +} + +func (this *TokenCredentialV3) GetServicesByInterface(region string, infType string) []ExternalService { + return this.Token.Catalog.GetServicesByInterface(region, infType) } func (this *TokenCredentialV3) GetEndpoints(region string, endpointType string) []Endpoint { @@ -277,7 +277,7 @@ func (this *TokenCredentialV3) GetLoginIp() string { return this.Token.Context.Ip } -func (catalog KeystoneServiceCatalogV3) getInternalServices(region string) []string { +func (catalog KeystoneServiceCatalogV3) GetInternalServices(region string) []string { services := make([]string, 0) for i := 0; i < len(catalog); i++ { exit := false @@ -295,7 +295,7 @@ func (catalog KeystoneServiceCatalogV3) getInternalServices(region string) []str return services } -func (catalog KeystoneServiceCatalogV3) getExternalServices(region string) []ExternalService { +func (catalog KeystoneServiceCatalogV3) GetExternalServices(region string) []ExternalService { return catalog.GetServicesByInterface(region, "console") }