forked from galaxydi/go-loghub
-
Notifications
You must be signed in to change notification settings - Fork 113
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #257 from crimson-gao/region-helper
auto parse region and use signature v4 in acdr-ut
- Loading branch information
Showing
7 changed files
with
120 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package main | ||
|
||
import ( | ||
sls "github.com/aliyun/aliyun-log-go-sdk" | ||
"github.com/aliyun/aliyun-log-go-sdk/util" | ||
) | ||
|
||
func main() { | ||
accessKeyId, accessKeySecret := "", "" // replace with your access key and secret | ||
endpoint := "cn-hangzhou-intranet.log.aliyuncs.com" // replace with your endpoint | ||
|
||
client := sls.CreateNormalInterfaceV2(endpoint, | ||
sls.NewStaticCredentialsProvider(accessKeyId, accessKeySecret, "")) | ||
region, err := util.ParseRegion(endpoint) // parse region from endpoint | ||
if err != nil { | ||
panic(err) | ||
} | ||
client.SetRegion(region) // region must be set if using signature v4 | ||
client.SetAuthVersion(sls.AuthV4) // set signature v4 | ||
|
||
client.GetProject("example-project") // call client API | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package util | ||
|
||
import ( | ||
"fmt" | ||
"regexp" | ||
"strings" | ||
) | ||
|
||
const ENDPOINT_REGEX_PATTERN = `^(?:http[s]?:\/\/)?([a-z-0-9]+)\.(?:sls|log)\.aliyuncs\.com$` | ||
|
||
var regionSuffixs = []string{"-intranet", "-share", "-vpc"} | ||
|
||
func ParseRegion(endpoint string) (string, error) { | ||
var re = regexp.MustCompile(ENDPOINT_REGEX_PATTERN) | ||
groups := re.FindStringSubmatch(endpoint) | ||
if groups == nil { | ||
return "", fmt.Errorf("invalid endpoint format: %s", endpoint) | ||
} | ||
region := groups[1] | ||
for _, suffix := range regionSuffixs { | ||
if strings.HasSuffix(region, suffix) { | ||
return region[:len(region)-len(suffix)], nil | ||
} | ||
} | ||
return region, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package util | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestParseRegion(t *testing.T) { | ||
region, err := ParseRegion("xx-test-acdr-ut-1-intranet.log.aliyuncs.com") | ||
assert.NoError(t, err) | ||
assert.Equal(t, "xx-test-acdr-ut-1", region) | ||
|
||
region, err = ParseRegion("http://cn-hangzhou-intranet.log.aliyuncs.com") | ||
assert.NoError(t, err) | ||
assert.Equal(t, "cn-hangzhou", region) | ||
|
||
region, err = ParseRegion("https://cn-hangzhou.log.aliyuncs.com") | ||
assert.NoError(t, err) | ||
assert.Equal(t, "cn-hangzhou", region) | ||
|
||
region, err = ParseRegion("ap-southease-1-intranet.log.aliyuncs.com") | ||
assert.NoError(t, err) | ||
assert.Equal(t, "ap-southease-1", region) | ||
|
||
region, err = ParseRegion("cn-shanghai-corp.sls.aliyuncs.com") | ||
assert.NoError(t, err) | ||
assert.Equal(t, "cn-shanghai-corp", region) | ||
|
||
_, err = ParseRegion("sls.aliyuncs.com") | ||
assert.Error(t, err) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters