-
Notifications
You must be signed in to change notification settings - Fork 5.8k
feat(inputs.aliyuncms): Refactor common parts to plugins/common #18280
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
|
FYI: @srebhan This PR integrates the Aliyun common library. |
|
Download PR build artifacts for linux_amd64.tar.gz, darwin_arm64.tar.gz, and windows_amd64.zip. 📦 Click here to get additional PR build artifactsArtifact URLs |
srebhan
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the PR @ZPascal! I have some comments in the code. Furthermore, I would love to see inputs.aliyuncms to be migrated to this package in this PR without change of functionality.
| var ( | ||
| roleSessionExpiration = 3600 | ||
| sessionExpiration = 3600 | ||
| ) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please use
| var ( | |
| roleSessionExpiration = 3600 | |
| sessionExpiration = 3600 | |
| ) | |
| roleSessionExpiration := 3600 | |
| sessionExpiration := 3600 |
| func TestGetCredentials(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| config CredentialConfig | ||
| expectError bool | ||
| }{ | ||
| { | ||
| name: "with access key credentials", | ||
| config: CredentialConfig{ | ||
| AccessKeyID: "test-key-id", | ||
| AccessKeySecret: "test-key-secret", | ||
| }, | ||
| expectError: false, | ||
| }, | ||
| { | ||
| name: "with access key and STS token", | ||
| config: CredentialConfig{ | ||
| AccessKeyID: "test-key-id", | ||
| AccessKeySecret: "test-key-secret", | ||
| AccessKeyStsToken: "test-sts-token", | ||
| }, | ||
| expectError: false, | ||
| }, | ||
| { | ||
| name: "with role ARN", | ||
| config: CredentialConfig{ | ||
| AccessKeyID: "test-key-id", | ||
| AccessKeySecret: "test-key-secret", | ||
| RoleArn: "acs:ram::123456:role/test-role", | ||
| RoleSessionName: "test-session", | ||
| }, | ||
| expectError: false, | ||
| }, | ||
| { | ||
| name: "with RSA keypair", | ||
| config: CredentialConfig{ | ||
| PublicKeyID: "test-public-key-id", | ||
| PrivateKey: "-----BEGIN RSA PRIVATE KEY-----\ntest\n-----END RSA PRIVATE KEY-----", | ||
| }, | ||
| expectError: true, | ||
| }, | ||
| { | ||
| name: "with ECS RAM role name", | ||
| config: CredentialConfig{ | ||
| RoleName: "test-ecs-role", | ||
| }, | ||
| expectError: true, | ||
| }, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| cred, err := GetCredentials(tt.config) | ||
|
|
||
| if tt.expectError { | ||
| if err == nil { | ||
| require.NotNil(t, cred) | ||
| } | ||
| } else { | ||
| require.NoError(t, err) | ||
| require.NotNil(t, cred) | ||
| } | ||
| }) | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please split the tests into TestGetCredentialsSuccess and TestGetCredentialsFail to simplify the code and to provide context on what to expect for the reader.
| if err != nil { | ||
| require.Contains(t, err.Error(), "failed to retrieve credential") | ||
| } else { | ||
| require.NotNil(t, cred) | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No! You should know what you expect from that call! Either this should return an error, then check for it, or it doesn't!
| var ( | ||
| err error | ||
| data map[string]interface{} | ||
| lastData map[string]interface{} | ||
| ) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
None of these is required to be declared outside of the go-routine, is it? Please use implicit declarations where possible and limit the scope of variables to a minimum to avoid unintended reuse!
| case <-dt.done: | ||
| return |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wouldn't using a cancel-able context be better here?
| req = "string" | ||
| require.NotNil(t, req) | ||
|
|
||
| req = 123 | ||
| require.NotNil(t, req) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What does this test?
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| rpcReq, err := getRPCReqFromDiscoveryRequest(tt.req) | ||
| if tt.expectError { | ||
| require.Error(t, err) | ||
| require.Nil(t, rpcReq) | ||
| } else { | ||
| require.NoError(t, err) | ||
| require.NotNil(t, rpcReq) | ||
| } | ||
| }) | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please split into a test that should succeed and one that should fail to simplify code and to provide context to the reader.
Same for other cases where you have if expectedError else constructs in tests!
| var req DiscoveryRequest | ||
|
|
||
| req = &mockDiscoveryRequest{RpcRequest: &requests.RpcRequest{}} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please use implicit declarations where possible
| var req DiscoveryRequest | |
| req = &mockDiscoveryRequest{RpcRequest: &requests.RpcRequest{}} | |
| req := &mockDiscoveryRequest{RpcRequest: &requests.RpcRequest{}} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a way to get the regions from the SDK? I don't want to maintain that list in telegraf as it is likely to be changed without anyone from Aliyun notifying us...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think this test does anything useful...
Description
This PR adds the Aliyun common library to Telegraf.
Checklist
Related issues
related to #18253