github.com/polarismesh/polaris@v1.17.8/plugin/cmdb.go (about)

     1  /**
     2   * Tencent is pleased to support the open source community by making Polaris available.
     3   *
     4   * Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
     5   *
     6   * Licensed under the BSD 3-Clause License (the "License");
     7   * you may not use this file except in compliance with the License.
     8   * You may obtain a copy of the License at
     9   *
    10   * https://opensource.org/licenses/BSD-3-Clause
    11   *
    12   * Unless required by applicable law or agreed to in writing, software distributed
    13   * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
    14   * CONDITIONS OF ANY KIND, either express or implied. See the License for the
    15   * specific language governing permissions and limitations under the License.
    16   */
    17  
    18  package plugin
    19  
    20  import (
    21  	"os"
    22  
    23  	commonLog "github.com/polarismesh/polaris/common/log"
    24  	"github.com/polarismesh/polaris/common/model"
    25  )
    26  
    27  // CMDB CMDB插件接口
    28  type CMDB interface {
    29  	Plugin
    30  
    31  	// GetLocation 在CMDB中没有找到Host,返回error为nil,location为nil
    32  	// 插件内部出现错误,返回error不为nil,忽略location
    33  	GetLocation(host string) (*model.Location, error)
    34  
    35  	// Range 提供一个Range接口,遍历所有的数据
    36  	// 遍历失败,通过Range返回值error可以额捕获
    37  	// 参数为一个回调函数
    38  	// 返回值:bool,是否继续遍历
    39  	// 返回值:error,回调函数处理结果,error不为nil,则停止遍历过程,并且通过Range返回error
    40  	Range(handler func(host string, location *model.Location) (bool, error)) error
    41  
    42  	// Size 获取当前CMDB存储的entry个数
    43  	Size() int32
    44  }
    45  
    46  // GetCMDB 获取CMDB插件
    47  func GetCMDB() CMDB {
    48  	c := &config.CMDB
    49  
    50  	plugin, exist := pluginSet[c.Name]
    51  	if !exist {
    52  		return nil
    53  	}
    54  
    55  	once.Do(func() {
    56  		if err := plugin.Initialize(c); err != nil {
    57  			commonLog.GetScopeOrDefaultByName(c.Name).Errorf("plugin init err: %s", err.Error())
    58  			os.Exit(-1)
    59  		}
    60  	})
    61  
    62  	return plugin.(CMDB)
    63  }