common

package
v0.12.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 31, 2026 License: Apache-2.0 Imports: 34 Imported by: 1

Documentation

Overview

Package common is to include common methods for managing multi-cloud infra

Package common provides request-scoped context helpers for cross-cutting concerns. These helpers allow injecting and extracting metadata (credential holder, request ID, etc.) via context.Context, avoiding parameter drilling across function call chains.

Package common is to include common methods for managing multi-cloud infra

Package common is to include common methods for managing multi-cloud infra

Package common is to include common methods for managing multi-cloud infra

Index

Constants

View Source
const DefaultNamingRule = "^.*$" // Wildcard Pattern

Variables

View Source
var RuntimeCloudInfo = model.CloudInfo{}

RuntimeCloudInfo is global variable for model.CloudInfo

View Source
var RuntimeCloudNetworkInfo = model.CloudNetworkInfo{}

RuntimeNetworkInfo is global variable for model.NetworkInfo

View Source
var RuntimeConf = model.RuntimeConfig{}

RuntimeConf is global variable for cloud config

View Source
var RuntimeCredential = model.Credential{}
View Source
var RuntimeExtractPatternsInfo = model.ExtractPatternsInfo{}

RuntimeExtractPatternsInfo is global variable for model.ExtractPatternsInfo

View Source
var RuntimeK8sClusterInfo = model.K8sClusterAssetInfo{}

RuntimeK8sClusterInfo is global variable for model.K8sClusterAssetInfo

View Source
var RuntimeLatancyMap = [][]string{}

RuntimeLatancyMap is global variable for LatancyMap

View Source
var RuntimeLatancyMapIndex = make(map[string]int)

RuntimeLatancyMapIndex is global variable for LatancyMap (index)

Functions

func AdjustKeysToLowercase added in v0.8.7

func AdjustKeysToLowercase(cloudInfo *model.CloudInfo)

AdjustKeysToLowercase adjusts the keys of nested maps to lowercase.

func AppendIfMissing added in v0.3.11

func AppendIfMissing(slice []string, i string) []string

func ChangeIdString added in v0.5.4

func ChangeIdString(name string) string

ChangeIdString is func to change strings in id or name (special chars to -, to lower string )

func CheckBasicOSImage added in v0.11.2

func CheckBasicOSImage(combinedInfo string, providerName string) bool

CheckBasicOSImage checks if the given combined info matches any basic OS image patterns Returns true if the image is considered a basic OS image based on predefined patterns providerName is used to apply CSP-specific rules (if defined) or common rules (as fallback)

Logic: 1. Determine include patterns:

  • If CSP-specific include exists and not empty → use CSP-specific include
  • Otherwise → use common include

2. Determine exclude patterns:

  • If CSP-specific exclude exists and not empty → use CSP-specific exclude
  • Otherwise → use common exclude

3. Check exclude patterns first (return false if matched) 4. Check include patterns (return true if matched)

func CheckConfig added in v0.3.1

func CheckConfig(id string) (bool, error)

func CheckConnConfigAvailable added in v0.8.8

func CheckConnConfigAvailable(connConfigName string) (bool, error)

CheckConnConfigAvailable is func to check if connection config is available by checking allkeypair list

func CheckElement added in v0.6.4

func CheckElement(a string, list []string) bool

func CheckNs added in v0.3.1

func CheckNs(id string) (bool, error)

func CheckSpiderReady added in v0.8.8

func CheckSpiderReady() error

CheckSpiderStatus is func to check if CB-Spider is ready

func CheckString added in v0.3.7

func CheckString(name string) error

CheckString is func to check string by the given rule `[a-z]([-a-z0-9]*[a-z0-9])?`

func CompareVersions added in v0.10.0

func CompareVersions(version1, version2 string) int

func ConvertToBaseCurrency added in v0.10.7

func ConvertToBaseCurrency(cost float32, currency string) float32

ConvertToBaseCurrency converts a cost value from a specific currency to the base currency (USD) This function handles currency conversion using predefined exchange rates

func ConvertToMessage

func ConvertToMessage(inType string, inData string, obj interface{}) error

ConvertToMessage is func to change input data to gRPC message

func ConvertToOutput

func ConvertToOutput(outType string, obj interface{}) (string, error)

ConvertToOutput is func to convert gRPC message to print format

func CopySrcToDest

func CopySrcToDest(src interface{}, dest interface{}) error

CopySrcToDest is func to copy data from source to target

func CreateMciDynamicTemplate added in v0.12.3

func CreateMciDynamicTemplate(nsId string, req *model.MciDynamicTemplateReq) (model.MciDynamicTemplateInfo, error)

CreateMciDynamicTemplate creates a new MCI Dynamic Template

func CreateMciDynamicTemplateWithReq added in v0.12.3

func CreateMciDynamicTemplateWithReq(nsId string, templateName string, description string, source string, mciDynamicReq *model.MciDynamicReq) (model.MciDynamicTemplateInfo, error)

CreateMciDynamicTemplateWithReq creates a template from an MciDynamicReq (used for extraction from existing MCI)

func CreateNs

func CreateNs(u *model.NsReq) (model.NsInfo, error)

func CreateSecurityGroupTemplate added in v0.12.3

func CreateSecurityGroupTemplate(nsId string, req *model.SecurityGroupTemplateReq) (model.SecurityGroupTemplateInfo, error)

CreateSecurityGroupTemplate creates a new SecurityGroup Template

func CreateVNetTemplate added in v0.12.3

func CreateVNetTemplate(nsId string, req *model.VNetTemplateReq) (model.VNetTemplateInfo, error)

CreateVNetTemplate creates a new vNet Template

func CredentialHolderFromContext added in v0.12.4

func CredentialHolderFromContext(ctx context.Context) string

CredentialHolderFromContext extracts the credential holder from the context. Returns model.DefaultCredentialHolder if not set or empty.

func DelAllNs

func DelAllNs() error

func DelNs

func DelNs(id string) error

func DeleteAllMciDynamicTemplate added in v0.12.3

func DeleteAllMciDynamicTemplate(nsId string) error

DeleteAllMciDynamicTemplate deletes all MCI Dynamic Templates in a namespace

func DeleteAllSecurityGroupTemplate added in v0.12.3

func DeleteAllSecurityGroupTemplate(nsId string) error

DeleteAllSecurityGroupTemplate deletes all SecurityGroup Templates in a namespace

func DeleteAllVNetTemplate added in v0.12.3

func DeleteAllVNetTemplate(nsId string) error

DeleteAllVNetTemplate deletes all vNet Templates in a namespace

func DeleteMciDynamicTemplate added in v0.12.3

func DeleteMciDynamicTemplate(nsId string, templateId string) error

DeleteMciDynamicTemplate deletes an MCI Dynamic Template

func DeleteObject added in v0.3.3

func DeleteObject(key string) error

DeleteObject is func to delete the object

func DeleteObjects added in v0.3.3

func DeleteObjects(key string) error

DeleteObjects is func to delete objects

func DeleteSecurityGroupTemplate added in v0.12.3

func DeleteSecurityGroupTemplate(nsId string, templateId string) error

DeleteSecurityGroupTemplate deletes a SecurityGroup Template

func DeleteVNetTemplate added in v0.12.3

func DeleteVNetTemplate(nsId string, templateId string) error

DeleteVNetTemplate deletes a vNet Template

func ExtractOSInfo added in v0.10.7

func ExtractOSInfo(combinedInfo string) string

ExtractOSInfo extracts OS name and version from string and returns formatted string like "Ubuntu 22.04"

func FilterDigitsAndDots added in v0.10.0

func FilterDigitsAndDots(input string) string

func GenChildResourceKey added in v0.4.10

func GenChildResourceKey(nsId string, resourceType string, parentResourceId string, resourceId string) string

GenChildResourceKey is func to generate a key from resource type and id

func GenConnectionKey added in v0.8.8

func GenConnectionKey(connectionId string) string

GenConnectionKey is func to generate a key for connection info

func GenK8sClusterKey added in v0.10.0

func GenK8sClusterKey(nsId string, k8sClusterId string) string

GenK8sClusterKey is func to generate a key from K8sCluster ID

func GenMciKey added in v0.9.5

func GenMciKey(nsId string, mciId string, vmId string) string

GenMciKey is func to generate a key used in keyValue store

func GenMciPolicyKey added in v0.9.5

func GenMciPolicyKey(nsId string, mciId string, vmId string) string

GenMciPolicyKey is func to generate Mci policy key

func GenMciSubGroupKey added in v0.9.5

func GenMciSubGroupKey(nsId string, mciId string, groupId string) string

GenMciSubGroupKey is func to generate a key from subGroupId used in keyValue store

func GenRandomPassword added in v0.7.1

func GenRandomPassword(length int) string

GenRandomPassword is func to return a RandomPassword

func GenResourceKey

func GenResourceKey(nsId string, resourceType string, resourceId string) string

GenResourceKey is func to generate a key from resource type and id

func GenTemplateKey added in v0.12.3

func GenTemplateKey(nsId string, targetType string, templateId string) string

GenTemplateKey is func to generate a key for template stored in ETCD Key format: /ns/{nsId}/template/{targetType}/{templateId}

func GenUid added in v0.4.10

func GenUid() string

GenUid is func to return a uid string

func GenerateNewRandomString added in v0.6.7

func GenerateNewRandomString(n int) string

generate a random string (from CB-MCKS source code)

func GetAssetsFilePath added in v0.11.20

func GetAssetsFilePath(filename string) string

GetAssetsFilePath returns the full path to an assets file. It tries multiple paths in order: 1. TB_ROOT_PATH environment variable + /assets/filename 2. ./assets/filename (current directory) 3. ../assets/filename (parent directory - default for src/ execution)

The function logs the attempted and resolved paths for debugging.

func GetAvailableK8sNodeImage added in v0.10.0

func GetAvailableK8sNodeImage(providerName string, regionName string) (*[]model.K8sClusterNodeImageDetailAvailable, error)

GetAvailableK8sNodeImage is func to get available kubernetes cluster node images for provider and region from model.K8sClusterInfo

func GetAvailableK8sVersion added in v0.10.0

func GetAvailableK8sVersion(providerName string, regionName string) (*[]model.K8sClusterVersionDetailAvailable, error)

GetAvailableK8sVersion is func to get available kubernetes cluster versions for provider and region from model.K8sClusterInfo

func GetChildIdList

func GetChildIdList(key string) []string

GetChildIdList is func to get child id list from given key

func GetCloudInfo added in v0.8.7

func GetCloudInfo() (model.CloudInfo, error)

GetCloudInfo is func to get all cloud info from the asset

func GetConfig

func GetConfig(id string) (model.ConfigInfo, error)

func GetConnConfig

func GetConnConfig(ConnConfigName string) (model.ConnConfig, error)

GetConnConfig is func to get connection config

func GetConnConfigList

func GetConnConfigList(filterCredentialHolder string, filterVerified bool, filterRegionRepresentative bool) (model.ConnConfigList, error)

GetConnConfigList is func to list filtered connection configs

func GetConnConfigListByProviderRegionZone added in v0.12.3

func GetConnConfigListByProviderRegionZone(provider, region, zone string) ([]string, error)

GetConnConfigListByProviderRegionZone filters connection configs by provider, region, and zone - provider empty: all connections - provider specified, region empty: all connections for the provider - provider + region specified, zone empty: all connections for the provider and region - provider + region + zone specified: connections matching all three

func GetCredentialHolder added in v0.12.4

func GetCredentialHolder(holderId string) (model.CredentialHolderInfo, error)

GetCredentialHolder retrieves a specific credential holder's info derived from connection configs

func GetCredentialHolderList added in v0.12.4

func GetCredentialHolderList() (model.CredentialHolderList, error)

GetCredentialHolderList derives distinct credential holders from registered connection configs

func GetFuncName added in v0.5.4

func GetFuncName() string

GetFuncName is func to get the name of the running function

func GetK8sClusterInfo added in v0.8.14

func GetK8sClusterInfo() (model.K8sClusterAssetInfo, error)

GetK8sClusterInfo is func to get all kubernetes cluster info from the asset

func GetK8sNodeGroupNamingRule added in v0.10.2

func GetK8sNodeGroupNamingRule(providerName string) (string, error)

GetK8sNodeGroupNamingRule is func to get nodegroup's naming rule

func GetK8sNodeGroupsOnK8sCreation added in v0.10.0

func GetK8sNodeGroupsOnK8sCreation(providerName string) (bool, error)

GetK8sNodeGroupsOnK8sCreation is func to get whether nodegroups are required during the k8scluster creation

func GetK8sNodeImageDesignation added in v0.10.0

func GetK8sNodeImageDesignation(providerName string) (bool, error)

GetK8sNodeImageDesignation is func to get whether node image designation is possible to create a k8scluster

func GetK8sRequiredSubnetCount added in v0.10.0

func GetK8sRequiredSubnetCount(providerName string) (int, error)

GetK8sRequiredSubnetCount is func to get the required subnet count to create a k8scluster

func GetMciDynamicTemplate added in v0.12.3

func GetMciDynamicTemplate(nsId string, templateId string) (model.MciDynamicTemplateInfo, error)

GetMciDynamicTemplate retrieves an MCI Dynamic Template by ID

func GetModelK8sNodeGroupsOnK8sCreation added in v0.10.0

func GetModelK8sNodeGroupsOnK8sCreation(providerName string) (*model.K8sClusterNodeGroupsOnCreation, error)

GetModelK8sNodeGroupsOnK8sCreation is to convert a NodeGroupsOnK8sCreation value to model.K8sClusterNodeGroupsOnK8sCreation

func GetModelK8sNodeImageDesignation added in v0.10.0

func GetModelK8sNodeImageDesignation(providerName string) (*model.K8sClusterNodeImageDesignation, error)

GetModelK8sNodeImageDesignation is to convert a NodeImageDesignation value to model.K8sClusterNodeImageDesignation

func GetModelK8sRequiredSubnetCount added in v0.10.0

func GetModelK8sRequiredSubnetCount(providerName string) (*model.K8sClusterRequiredSubnetCount, error)

GetModelK8sRequiredSubnetCount is func to get the required subnet count to create a k8scluster

func GetNs

func GetNs(id string) (model.NsInfo, error)

func GetObjectList added in v0.3.3

func GetObjectList(key string) []string

GetObjectList is func to return IDs of each child objects that has the same key

func GetObjectValue added in v0.3.3

func GetObjectValue(key string) (string, error)

GetObjectValue is func to return the object value

func GetProviderList added in v0.8.9

func GetProviderList() (*model.IdList, error)

GetProviderList is func to list all cloud providers

func GetProviderNameFromConnConfig added in v0.11.3

func GetProviderNameFromConnConfig(ConnConfigName string) (string, error)

GetConnConfig is func to get connection config

func GetPublicKeyForCredentialEncryption added in v0.9.7

func GetPublicKeyForCredentialEncryption() (model.PublicKeyResponse, error)

GetPublicKeyForCredentialEncryption generates an RSA key pair, stores the private key in memory, and returns the public key along with its token ID.

func GetRegion

func GetRegion(ProviderName, RegionName string) (model.RegionDetail, error)

GetRegion is func to get regionInfo with the native region name

func GetRegions added in v0.9.10

func GetRegions(ProviderName string) (model.RegionList, error)

GetRegions is func to get regionInfo list

func GetSecurityGroupTemplate added in v0.12.3

func GetSecurityGroupTemplate(nsId string, templateId string) (model.SecurityGroupTemplateInfo, error)

GetSecurityGroupTemplate retrieves a SecurityGroup Template by ID

func GetVNetTemplate added in v0.12.3

func GetVNetTemplate(nsId string, templateId string) (model.VNetTemplateInfo, error)

GetVNetTemplate retrieves a vNet Template by ID

func InitAllConfig added in v0.3.9

func InitAllConfig() error

func InitConfig added in v0.3.9

func InitConfig(id string) error

func IsDeprecatedImage added in v0.10.7

func IsDeprecatedImage(combinedInfo string) bool

IsDeprecatedImage checks if an image is deprecated

func IsGPUImage added in v0.10.7

func IsGPUImage(combinedInfo string) bool

IsGPUImage checks if an image has GPU support

func IsK8sImage added in v0.10.7

func IsK8sImage(combinedInfo string) bool

IsK8sImage checks if an image is for Kubernetes

func ListConfig

func ListConfig() ([]model.ConfigInfo, error)

func ListConfigId

func ListConfigId() []string

func ListMciDynamicTemplate added in v0.12.3

func ListMciDynamicTemplate(nsId string, filterKeyword string) ([]model.MciDynamicTemplateInfo, error)

ListMciDynamicTemplate lists all MCI Dynamic Templates in a namespace filterKeyword is optional; if non-empty, only templates whose Name or Description contains the keyword (case-insensitive) are returned.

func ListNs

func ListNs() ([]model.NsInfo, error)

func ListNsId

func ListNsId() ([]string, error)

func ListSecurityGroupTemplate added in v0.12.3

func ListSecurityGroupTemplate(nsId string, filterKeyword string) ([]model.SecurityGroupTemplateInfo, error)

ListSecurityGroupTemplate lists all SecurityGroup Templates in a namespace filterKeyword is optional; if non-empty, only templates whose Name or Description contains the keyword (case-insensitive) are returned.

func ListVNetTemplate added in v0.12.3

func ListVNetTemplate(nsId string, filterKeyword string) ([]model.VNetTemplateInfo, error)

ListVNetTemplate lists all vNet Templates in a namespace filterKeyword is optional; if non-empty, only templates whose Name or Description contains the keyword (case-insensitive) are returned.

func LookupKeyValueList

func LookupKeyValueList(kvl []model.KeyValue, key string) string

LookupKeyValueList is func to lookup model.KeyValue list

func NVL

func NVL(str string, def string) string

NVL is func for null value logic

func NewDefaultContext added in v0.12.4

func NewDefaultContext() context.Context

NewDefaultContext creates a context with default credential holder. Use this for internal/system calls that don't originate from HTTP requests.

func NsValidation

func NsValidation() echo.MiddlewareFunc

func PrintCloudInfoTable added in v0.8.5

func PrintCloudInfoTable(cloudInfo model.CloudInfo)

PrintCloudInfoTable prints model.CloudInfo in table format

func PrintCredentialInfo added in v0.8.8

func PrintCredentialInfo(credential model.Credential)

PrintCredentialInfo prints model.Credential information in table format

func PrintJsonPretty

func PrintJsonPretty(v interface{})

PrintJsonPretty is func to print JSON pretty with indent

func RandomSleep added in v0.4.10

func RandomSleep(from, to int)

RandomSleep is func to make a caller waits for during random Milliseconds (from ms, to ms)

func RegisterAllCloudInfo added in v0.8.8

func RegisterAllCloudInfo() error

RegisterAllCloudInfo is func to register all cloud info from asset to CB-Spider

func RegisterCloudInfo added in v0.8.8

func RegisterCloudInfo(providerName string) error

RegisterCloudInfo is func to register cloud info from asset to CB-Spider

func RegisterConnectionConfig added in v0.8.8

func RegisterConnectionConfig(connConfig model.ConnConfig) (model.ConnConfig, error)

RegisterConnectionConfig is func to register connection config to CB-Spider

func RegisterCredential added in v0.8.8

func RegisterCredential(req model.CredentialReq) (model.CredentialInfo, error)

RegisterCredential is func to register credential and all related connection configs

func RegisterRegionZone added in v0.8.8

func RegisterRegionZone(providerName string, regionName string) error

RegisterRegionZone is func to register all regions to CB-Spider

func RequestIDFromContext added in v0.12.4

func RequestIDFromContext(ctx context.Context) string

RequestIDFromContext extracts the request ID from the context. Returns empty string if not set.

func ResolveConnectionName added in v0.12.4

func ResolveConnectionName(defaultConnectionName string, credentialHolder string) string

ResolveConnectionName converts a default credential holder's connection name to the appropriate connection name for the given credential holder. Default holder connections use the pattern: "{provider}-{region}" (e.g., "aws-ap-northeast-2") Non-default holder connections use: "{holder}-{provider}-{region}" (e.g., "team-a-aws-ap-northeast-2") If credentialHolder is empty or matches the default, the original name is returned as-is.

func RetrieveRegionListFromCsp added in v0.9.10

func RetrieveRegionListFromCsp() (model.RetrievedRegionList, error)

RetrieveRegionListFromCsp is func to retrieve region list

func SetupViperPaths added in v0.11.20

func SetupViperPaths(v *viper.Viper)

SetupViperPaths configures standard asset paths for a Viper instance. This centralizes the path configuration logic used across multiple config files. It adds paths in priority order: 1. TB_ROOT_PATH/assets (if TB_ROOT_PATH is set) 2. TB_ROOT_PATH (if TB_ROOT_PATH is set) 3. . (current directory) 4. ./assets/ 5. ../assets/ (default for src/ execution)

func ToLower added in v0.3.1

func ToLower(name string) string

ToLower is func to change strings (_ to -, " " to -, to lower string ) (deprecated soon)

func UpdateConfig

func UpdateConfig(u *model.ConfigReq) (model.ConfigInfo, error)

func UpdateGlobalVariable added in v0.3.9

func UpdateGlobalVariable(id string) error

func UpdateMciDynamicTemplate added in v0.12.3

func UpdateMciDynamicTemplate(nsId string, templateId string, req *model.MciDynamicTemplateReq) (model.MciDynamicTemplateInfo, error)

UpdateMciDynamicTemplate updates an existing MCI Dynamic Template

func UpdateNs added in v0.5.5

func UpdateNs(id string, u *model.NsReq) (model.NsInfo, error)

UpdateNs is func to update namespace info

func UpdateSecurityGroupTemplate added in v0.12.3

func UpdateSecurityGroupTemplate(nsId string, templateId string, req *model.SecurityGroupTemplateReq) (model.SecurityGroupTemplateInfo, error)

UpdateSecurityGroupTemplate updates an existing SecurityGroup Template

func UpdateVNetTemplate added in v0.12.3

func UpdateVNetTemplate(nsId string, templateId string, req *model.VNetTemplateReq) (model.VNetTemplateInfo, error)

UpdateVNetTemplate updates an existing vNet Template

func ValidateCredentialHolderName added in v0.12.4

func ValidateCredentialHolderName(holder string) error

ValidateCredentialHolderName validates a credential holder name. Rules:

  • Must not be empty
  • Must contain only lowercase alphanumeric characters and underscores [a-z0-9_]
  • Hyphens (-) are forbidden (used as connection name delimiter)
  • Must be 1-50 characters long

func WithCredentialHolder added in v0.12.4

func WithCredentialHolder(ctx context.Context, holder string) context.Context

WithCredentialHolder returns a new context with the given credential holder value.

func WithRequestID added in v0.12.4

func WithRequestID(ctx context.Context, reqID string) context.Context

WithRequestID returns a new context with the given request ID value.

Types

This section is empty.

Directories

Path Synopsis
Package client is to manage internal HTTP requests and caching
Package client is to manage internal HTTP requests and caching
Package label is to handle label selector for resources
Package label is to handle label selector for resources
Package logfilter provides centralized log filtering configuration with minimal dependencies.
Package logfilter provides centralized log filtering configuration with minimal dependencies.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL