-
Notifications
You must be signed in to change notification settings - Fork 20
PMM-4879 Adding support for defaults-file. #356
base: main
Are you sure you want to change the base?
Changes from 5 commits
c7dd697
3b2f119
4609d70
fce92b6
e7e3f27
be3fc93
e81f979
cb785cb
d0bdf98
48cef33
0fc621f
8bc76a5
3d6e28a
a8bb846
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,110 @@ | ||
| // pmm-agent | ||
| // Copyright 2019 Percona LLC | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| // Package defaultsfile provides managing of defaults file. | ||
| package defaultsfile | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "os/user" | ||
| "path/filepath" | ||
| "strings" | ||
|
|
||
| "github.com/percona/pmm/api/agentpb" | ||
| "github.com/percona/pmm/api/inventorypb" | ||
| "github.com/pkg/errors" | ||
| "gopkg.in/ini.v1" | ||
| ) | ||
|
|
||
| // DefaultsFile is a struct with database specs fetched from file. | ||
| type DefaultsFile struct { | ||
| username string | ||
| password string | ||
| host string | ||
| port uint32 | ||
| } | ||
|
|
||
| // New creates new DefaultsFile. | ||
| func New() *DefaultsFile { | ||
| return &DefaultsFile{} | ||
| } | ||
|
|
||
| // ParseDefaultsFile parses given defaultsFile. It returns the database specs. | ||
| func (d *DefaultsFile) ParseDefaultsFile(req *agentpb.ParseDefaultsFileRequest) *agentpb.ParseDefaultsFileResponse { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. let's create one more struct like DefaultsFileParser with no fields and make this method part of it.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. |
||
| var res agentpb.ParseDefaultsFileResponse | ||
| defaultsFile, err := parseDefaultsFile(req.ConfigPath, req.ServiceType) | ||
| if err != nil { | ||
| res.Error = err.Error() | ||
| return &res | ||
| } | ||
|
|
||
| res.Username = defaultsFile.username | ||
| res.Password = defaultsFile.password | ||
| res.Host = defaultsFile.host | ||
| res.Port = defaultsFile.port | ||
|
|
||
| return &res | ||
| } | ||
|
|
||
| func parseDefaultsFile(configPath string, serviceType inventorypb.ServiceType) (*DefaultsFile, error) { | ||
| if len(configPath) == 0 { | ||
| return nil, errors.New("configPath for DefaultsFile is empty") | ||
| } | ||
|
|
||
| switch serviceType { | ||
| case inventorypb.ServiceType_MYSQL_SERVICE: | ||
| return parseMySQLDefaultsFile(configPath) | ||
| case inventorypb.ServiceType_EXTERNAL_SERVICE: | ||
| case inventorypb.ServiceType_HAPROXY_SERVICE: | ||
| case inventorypb.ServiceType_MONGODB_SERVICE: | ||
| case inventorypb.ServiceType_POSTGRESQL_SERVICE: | ||
| case inventorypb.ServiceType_PROXYSQL_SERVICE: | ||
| case inventorypb.ServiceType_SERVICE_TYPE_INVALID: | ||
| return nil, errors.Errorf("unimplemented service type %s", serviceType) | ||
| } | ||
|
|
||
| return nil, errors.Errorf("unimplemented service type %s", serviceType) | ||
| } | ||
|
|
||
| func parseMySQLDefaultsFile(configPath string) (*DefaultsFile, error) { | ||
| configPath, err := expandPath(configPath) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("fail to normalize path: %w", err) | ||
| } | ||
|
|
||
| cfg, err := ini.Load(configPath) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("fail to read config file: %w", err) | ||
| } | ||
|
|
||
| port, _ := cfg.Section("client").Key("port").Uint() | ||
| return &DefaultsFile{ | ||
| username: cfg.Section("client").Key("user").String(), | ||
| password: cfg.Section("client").Key("password").String(), | ||
| host: cfg.Section("client").Key("host").String(), | ||
| port: uint32(port), | ||
|
BupycHuk marked this conversation as resolved.
|
||
| }, nil | ||
| } | ||
|
|
||
| func expandPath(path string) (string, error) { | ||
| if strings.HasPrefix(path, "~/") { | ||
| usr, err := user.Current() | ||
| if err != nil { | ||
| return "", fmt.Errorf("failed to expand path: %w", err) | ||
| } | ||
| return filepath.Join(usr.HomeDir, path[2:]), nil | ||
| } | ||
| return path, nil | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| // pmm-agent | ||
| // Copyright 2019 Percona LLC | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| package defaultsfile | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/percona/pmm/api/agentpb" | ||
| "github.com/percona/pmm/api/inventorypb" | ||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func TestDefaultsFileParser(t *testing.T) { | ||
| t.Parallel() | ||
| testCases := []struct { | ||
| name string | ||
| req *agentpb.ParseDefaultsFileRequest | ||
| expectedErr string | ||
| }{ | ||
| { | ||
| name: "Valid MySQL file", | ||
| req: &agentpb.ParseDefaultsFileRequest{ | ||
| ServiceType: inventorypb.ServiceType_MYSQL_SERVICE, | ||
| ConfigPath: "../testdata/defaultsfile/.my.cnf", | ||
| }, | ||
| }, | ||
| { | ||
| name: "File not found", | ||
| req: &agentpb.ParseDefaultsFileRequest{ | ||
| ServiceType: inventorypb.ServiceType_MYSQL_SERVICE, | ||
| ConfigPath: "path/to/invalid/file.cnf", | ||
| }, | ||
| expectedErr: `no such file or directory`, | ||
| }, | ||
| { | ||
| name: "Service type not supported", | ||
| req: &agentpb.ParseDefaultsFileRequest{ | ||
| ServiceType: inventorypb.ServiceType_HAPROXY_SERVICE, | ||
| ConfigPath: "../testdata/defaultsfile/.my.cnf", | ||
| }, | ||
| expectedErr: `unimplemented service type HAPROXY_SERVICE`, | ||
| }, | ||
| } | ||
|
|
||
| for _, testCase := range testCases { | ||
| testCase := testCase | ||
| t.Run(testCase.name, func(t *testing.T) { | ||
| t.Parallel() | ||
| c := New() | ||
| resp := c.ParseDefaultsFile(testCase.req) | ||
| require.NotNil(t, resp) | ||
| if testCase.expectedErr == "" { | ||
| assert.Empty(t, resp.Error) | ||
| } else { | ||
| require.NotEmpty(t, resp.Error) | ||
| assert.Regexp(t, `.*`+testCase.expectedErr+`.*`, resp.Error) | ||
| } | ||
| }) | ||
| } | ||
| } |
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.
in case of new struct
DefaultFileParser,DefaultsFilecan be private.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.
Great remark. Thank you, changed.