catalog.go 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. // Copyright 2019 Yunion
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package mcclient
  15. type IServiceCatalog interface {
  16. Len() int
  17. getServiceURL(service, region, zone, endpointType string) (string, error)
  18. getServiceURLs(service, region, zone, endpointType string) ([]string, error)
  19. GetInternalServices(region string) []string
  20. GetExternalServices(region string) []ExternalService
  21. GetServicesByInterface(region string, infType string) []ExternalService
  22. }
  23. type IServiceCatalogChangeListener interface {
  24. OnServiceCatalogChange(catalog IServiceCatalog)
  25. }
  26. type cliTask struct {
  27. cli *Client
  28. l IServiceCatalogChangeListener
  29. catalog IServiceCatalog
  30. taskType string
  31. }
  32. func (t *cliTask) Run() {
  33. switch t.taskType {
  34. case "GetServiceCatalog":
  35. t.l.OnServiceCatalogChange(t.cli.GetServiceCatalog())
  36. case "OnServiceCatalogChange":
  37. for i := range t.cli.catalogListeners {
  38. t.cli.catalogListeners[i].OnServiceCatalogChange(t.catalog)
  39. }
  40. }
  41. }
  42. func (t *cliTask) Dump() string {
  43. return ""
  44. }
  45. func (cli *Client) RegisterCatalogListener(l IServiceCatalogChangeListener) {
  46. cli.catalogListeners = append(cli.catalogListeners, l)
  47. task := &cliTask{
  48. cli: cli,
  49. l: l,
  50. taskType: "GetServiceCatalog",
  51. }
  52. if cli.GetServiceCatalog() != nil {
  53. listenerWorker.Run(task, nil, nil)
  54. }
  55. }
  56. func (cli *Client) SetServiceCatalog(catalog IServiceCatalog) {
  57. cli._serviceCatalog = catalog
  58. task := &cliTask{
  59. cli: cli,
  60. catalog: catalog,
  61. taskType: "OnServiceCatalogChange",
  62. }
  63. listenerWorker.Run(task, nil, nil)
  64. }
  65. func (this *Client) GetServiceCatalog() IServiceCatalog {
  66. return this._serviceCatalog
  67. }
  68. func CatalogGetServiceURL(catalog IServiceCatalog, service, region, zone, endpointType string) (string, error) {
  69. return catalog.getServiceURL(service, region, zone, endpointType)
  70. }
  71. func CatalogGetServiceURLs(catalog IServiceCatalog, service, region, zone, endpointType string) ([]string, error) {
  72. return catalog.getServiceURLs(service, region, zone, endpointType)
  73. }