Skip to content

Commit

Permalink
Merge branch 'feat/endpoint-config' into 'master'
Browse files Browse the repository at this point in the history
Feat/endpoint config

See merge request iaasng/volcengine-go-sdk!397
  • Loading branch information
修杰 committed Dec 2, 2024
2 parents 28b7238 + 5651b9b commit 3cf6be2
Show file tree
Hide file tree
Showing 12 changed files with 198 additions and 14 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
/vendor/src/
doc
.idea
!/main.go
6 changes: 5 additions & 1 deletion README.MD
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,11 @@
WithCredentials(credentials.NewStaticCredentials(ak, sk, "")).
WithRegion(region)

sess, _ = session.NewSession(config)
sess, err = session.NewSession(config)
if err != nil {
panic(err)
}

client = vpc.New(sess)

resp, err = client.DescribeVpcs(&vpc.DescribeVpcsInput{
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ require (
github.com/google/uuid v1.3.0
github.com/jmespath/go-jmespath v0.4.0
github.com/volcengine/volc-sdk-golang v1.0.23
gopkg.in/yaml.v2 v2.2.8
)
11 changes: 11 additions & 0 deletions internal/shareddefaults/shared_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,17 @@ func SharedConfigFilename() string {
return filepath.Join(UserHomeDir(), ".volcengine", "config")
}

// SharedEndpointConfigFilename returns the SDK's default file path for
// the shared endpoint config file.
//
// Builds the shared config file path based on the OS's platform.
//
// - Linux/Unix: $HOME/.volcengine/endpoint
// - Windows: %USERPROFILE%\.volcengine\endpoint
func SharedEndpointConfigFilename() string {
return filepath.Join(UserHomeDir(), ".volcengine", "endpoint")
}

// UserHomeDir returns the home directory for the user the process is
// running under.
func UserHomeDir() string {
Expand Down
2 changes: 1 addition & 1 deletion meta.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"lasted": "1.0.167",
"lasted": "1.0.168",
"meta_commit": "0e88bf7fed13f035b45fdb59b915aee4654f8fe6"
}
23 changes: 23 additions & 0 deletions volcengine/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,10 @@ type Config struct {
SimpleError *bool

ForceJsonNumberDecode custom.ForceJsonNumberDecode

EndpointConfigState *bool

EndpointConfigPath *string
}

// NewConfig returns a new Config pointer that can be chained with builder
Expand Down Expand Up @@ -485,6 +489,17 @@ func (c *Config) WithSleepDelay(fn func(time.Duration)) *Config {
// c.DisableEndpointHostPrefix = &t
// return c
//}
// WithEndpointConfigState will set whether or not to use FileEndpointResolver
func (c *Config) WithEndpointConfigState(t bool) *Config {
c.EndpointConfigState = &t
return c
}

// WithEndpointConfigPath will set fileEndpointResolver config path . This takes effect when EndpointConfigState is true.
func (c *Config) WithEndpointConfigPath(path string) *Config {
c.EndpointConfigPath = &path
return c
}

// MergeIn merges the passed in configs into the existing config object.
func (c *Config) MergeIn(cfgs ...*Config) {
Expand Down Expand Up @@ -646,6 +661,14 @@ func mergeInConfig(dst *Config, other *Config) {
dst.CustomerUnmarshalData = other.CustomerUnmarshalData
}

if other.EndpointConfigState != nil {
dst.EndpointConfigState = other.EndpointConfigState
}

if other.EndpointConfigPath != nil {
dst.EndpointConfigPath = other.EndpointConfigPath
}

dst.Interceptors = other.Interceptors
}

Expand Down
19 changes: 15 additions & 4 deletions volcengine/defaults/shared_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import (
//
// Builds the shared config file path based on the OS's platform.
//
// - Linux/Unix: $HOME/.aws/credentials
// - Windows: %USERPROFILE%\.aws\credentials
// - Linux/Unix: $HOME/.volcengine/credentials
// - Windows: %USERPROFILE%\.volcengine\credentials
func SharedCredentialsFilename() string {
return shareddefaults.SharedCredentialsFilename()
}
Expand All @@ -23,8 +23,19 @@ func SharedCredentialsFilename() string {
//
// Builds the shared config file path based on the OS's platform.
//
// - Linux/Unix: $HOME/.aws/config
// - Windows: %USERPROFILE%\.aws\config
// - Linux/Unix: $HOME/.volcengine/config
// - Windows: %USERPROFILE%\.volcengine\config
func SharedConfigFilename() string {
return shareddefaults.SharedConfigFilename()
}

// SharedEndpointConfigFilename returns the SDK's default file path for
// the endpoint config file.
//
// Builds the shared config file path based on the OS's platform.
//
// - Linux/Unix: $HOME/.volcengine/endpoint
// - Windows: %USERPROFILE%\.volcengine\endpoint
func SharedEndpointConfigFilename() string {
return shareddefaults.SharedEndpointConfigFilename()
}
68 changes: 68 additions & 0 deletions volcengine/endpoints/endpoint_config_resolver.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package endpoints

import (
"io/ioutil"

"gopkg.in/yaml.v2"
)

const (
separator = "."
openPrefix = "open"
endpointSuffix = separator + "volcengineapi.com"
)

var defaultEndpoint = openPrefix + endpointSuffix

type RegionEndpointMap map[string]string

type ServiceEndpointInfo struct {
Service string `yaml:"Service"`
IsGlobal bool `yaml:"IsGlobal"`
GlobalEndpoint string `yaml:"GlobalEndpoint"`
RegionEndpointMap `yaml:"RegionEndpointMap"`
}

type FileEndpointConfigResolver struct {
Path string
EndpointConfig map[string]*ServiceEndpointInfo
}

func (endpointResolver *FileEndpointConfigResolver) Load() error {
file, err := ioutil.ReadFile(endpointResolver.Path)
if err != nil {
return err
}
config := make(map[string]*ServiceEndpointInfo)

err = yaml.Unmarshal(file, config)
if err != nil {
return err
}
endpointResolver.EndpointConfig = config
return nil
}

func (endpointResolver *FileEndpointConfigResolver) EndpointFor(service, region string, opts ...func(*Options)) (ResolvedEndpoint, error) {
result := ResolvedEndpoint{}
result.URL = defaultEndpoint
defaultEndpointInfo, sExist := endpointResolver.EndpointConfig[service]
if !sExist {
return result, nil
}

isGlobal := defaultEndpointInfo.IsGlobal
if isGlobal {
result.URL = defaultEndpointInfo.GlobalEndpoint
return result, nil
}

regionEndpointMp := defaultEndpointInfo.RegionEndpointMap
regionEndpointStr, rExist := regionEndpointMp[region]
if !rExist {
return result, nil
}

result.URL = regionEndpointStr
return result, nil
}
22 changes: 21 additions & 1 deletion volcengine/session/env_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,14 @@ type envConfig struct {
//
// VOLCSTACK_ROLE_SESSION_NAME=session_name
RoleSessionName string
// Specifies the endpoint config file path
//
// VOLCSTACK_ENABLE_ENDPOINT_CONFIG_STATE=session_name
EndpointConfigState *bool
// Specifies the endpoint config file path
//
// VOLCSTACK_ENDPOINT_CONFIG_FILE=session_name
EndpointConfigPath string
}

var (
Expand Down Expand Up @@ -158,6 +166,9 @@ var (
enableEndpointDiscoveryEnvKey = []string{
"VOLCSTACK_ENABLE_ENDPOINT_DISCOVERY",
}
enableEndpointConfigStateEnvKey = []string{
"VOLCSTACK_ENABLE_ENDPOINT_CONFIG_STATE",
}

regionEnvKeys = []string{
"VOLCSTACK_REGION",
Expand All @@ -173,6 +184,10 @@ var (
sharedConfigFileEnvKey = []string{
"VOLCSTACK_CONFIG_FILE",
}

EndpointConfigPathEnvKey = []string{
"VOLCSTACK_ENDPOINT_CONFIG_PATH",
}
webIdentityTokenFilePathEnvKey = []string{
"VOLCSTACK_WEB_IDENTITY_TOKEN_FILE",
}
Expand Down Expand Up @@ -254,9 +269,14 @@ func envConfigLoad(enableSharedConfig bool) envConfig {
if len(cfg.enableEndpointDiscovery) > 0 {
cfg.EnableEndpointDiscovery = volcengine.Bool(cfg.enableEndpointDiscovery != "false")
}

var endpointConfigState string
setFromEnvVal(&endpointConfigState, enableEndpointConfigStateEnvKey)
if len(cfg.enableEndpointDiscovery) > 0 {
cfg.EndpointConfigState = volcengine.Bool(endpointConfigState != "false")
}
setFromEnvVal(&cfg.SharedCredentialsFile, sharedCredsFileEnvKey)
setFromEnvVal(&cfg.SharedConfigFile, sharedConfigFileEnvKey)
setFromEnvVal(&cfg.EndpointConfigPath, EndpointConfigPathEnvKey)

if len(cfg.SharedCredentialsFile) == 0 {
cfg.SharedCredentialsFile = defaults.SharedCredentialsFilename()
Expand Down
39 changes: 38 additions & 1 deletion volcengine/session/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,16 @@ func newSession(opts Options, envCfg envConfig, cfgs ...*volcengine.Config) (*Se
return nil, err
}

if volcengine.BoolValue(cfg.EndpointConfigState) && cfg.EndpointResolver == nil {
resolver := &endpoints.FileEndpointConfigResolver{
Path: volcengine.StringValue(cfg.EndpointConfigPath),
}
if err := resolver.Load(); err != nil {
return nil, err
}
cfg.EndpointResolver = resolver
}

s := &Session{
Config: cfg,
Handlers: handlers,
Expand Down Expand Up @@ -504,6 +514,24 @@ func mergeConfigSrcs(cfg, userCfg *volcengine.Config,
cfg.Credentials = creds
}

if cfg.EndpointConfigState == nil {
if envCfg.EndpointConfigState != nil {
cfg.WithEndpointConfigState(*envCfg.EndpointConfigState)
} else if envCfg.EnableSharedConfig && sharedCfg.EndpointConfigState != nil {
cfg.WithEndpointConfigState(*sharedCfg.EndpointConfigState)
}
}

if cfg.EndpointConfigPath == nil {
if len(envCfg.EndpointConfigPath) > 0 {
cfg.WithEndpointConfigPath(envCfg.EndpointConfigPath)
} else if envCfg.EnableSharedConfig && len(sharedCfg.EndpointConfigPath) > 0 {
cfg.WithEndpointConfigPath(sharedCfg.EndpointConfigPath)
} else {
cfg.WithEndpointConfigPath(defaults.SharedEndpointConfigFilename())
}
}

return nil
}

Expand Down Expand Up @@ -553,7 +581,16 @@ func (s *Session) clientConfigWithErr(serviceName string, cfgs ...*volcengine.Co
region := volcengine.StringValue(s.Config.Region)

if s.Config.Endpoint == nil {
endpoint := volcengineutil.GetDefaultEndpointByServiceInfo(serviceName, region)
var endpoint *string
if volcengine.BoolValue(s.Config.EndpointConfigState) && s.Config.EndpointResolver != nil {
endpointFor, err := s.Config.EndpointResolver.EndpointFor(serviceName, region)
if err != nil {
return client.Config{}, err
}
endpoint = &endpointFor.URL
} else {
endpoint = volcengineutil.GetDefaultEndpointByServiceInfo(serviceName, region)
}
s.Config.Endpoint = endpoint
}

Expand Down
18 changes: 13 additions & 5 deletions volcengine/session/shared_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ const (
// loading configuration from the config files if another profile name
// is not provided.
DefaultSharedConfigProfile = `default`

// endpointConfigState if true ,Enable endpoint config
endpointConfigState = "endpoint_config_state"
// endpointConfigPath Enable endpoint config file path
endpointConfigPath = "endpoint_config_path"
)

// sharedConfig represents the configuration fields of the SDK config files.
Expand Down Expand Up @@ -81,10 +86,12 @@ type sharedConfig struct {
EnableEndpointDiscovery *bool

// CSM Options
CSMEnabled *bool
CSMHost string
CSMPort string
CSMClientID string
CSMEnabled *bool
CSMHost string
CSMPort string
CSMClientID string
EndpointConfigState *bool
EndpointConfigPath string
}

type sharedConfigFile struct {
Expand Down Expand Up @@ -261,7 +268,8 @@ func (cfg *sharedConfig) setFromIniFile(profile string, file sharedConfigFile, e

// Endpoint discovery
updateBoolPtr(&cfg.EnableEndpointDiscovery, section, enableEndpointDiscoveryKey)

updateBoolPtr(&cfg.EndpointConfigState, section, endpointConfigState)
updateString(&cfg.EndpointConfigPath, section, endpointConfigPath)
return nil
}

Expand Down
2 changes: 1 addition & 1 deletion volcengine/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ package volcengine
const SDKName = "volcengine-go-sdk"

// SDKVersion is the version of this SDK
const SDKVersion = "1.0.167"
const SDKVersion = "1.0.168"

0 comments on commit 3cf6be2

Please sign in to comment.