Skip to content
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

feat: expose Client.LoadConfig method #84

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions ovh/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func expandConfigPaths() []interface{} {
}

// loadINI builds a ini.File from the configuration paths provided in configPaths.
// It's a helper for loadConfig.
// It's a helper for LoadConfig.
func loadINI() (*ini.File, error) {
paths := expandConfigPaths()
if len(paths) == 0 {
Expand All @@ -74,21 +74,21 @@ func loadINI() (*ini.File, error) {
return ini.LooseLoad(paths[0], paths[1:]...)
}

// loadConfig loads client configuration from params, environments or configuration
// LoadConfig loads client configuration from params, environments or configuration
// files (by order of decreasing precedence).
//
// loadConfig will check OVH_CONSUMER_KEY, OVH_APPLICATION_KEY, OVH_APPLICATION_SECRET
// LoadConfig will check OVH_CONSUMER_KEY, OVH_APPLICATION_KEY, OVH_APPLICATION_SECRET
// and OVH_ENDPOINT environment variables. If any is present, it will take precedence
// over any configuration from file.
//
// Configuration files are ini files. They share the same format as python-ovh,
// node-ovh, php-ovh and all other wrappers. If any wrapper is configured, all
// can re-use the same configuration. loadConfig will check for configuration in:
// can re-use the same configuration. LoadConfig will check for configuration in:
//
// - ./ovh.conf
// - $HOME/.ovh.conf
// - /etc/ovh.conf
func (c *Client) loadConfig(endpointName string) error {
func (c *Client) LoadConfig(endpointName string) error {
if strings.HasSuffix(endpointName, "/") {
return fmt.Errorf("endpoint name cannot have a tailing slash")
}
Expand Down
34 changes: 17 additions & 17 deletions ovh/configuration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,15 @@ func setConfigPaths(t testing.TB, paths ...string) {

func TestConfigForbidsTrailingSlash(t *testing.T) {
client := Client{}
err := client.loadConfig("https://example.org/")
err := client.LoadConfig("https://example.org/")
td.Require(t).String(err, "endpoint name cannot have a tailing slash")
}

func TestConfigFromFiles(t *testing.T) {
setConfigPaths(t, systemConf, userPartialConf, localPartialConf)

client := Client{}
err := client.loadConfig("ovh-eu")
err := client.LoadConfig("ovh-eu")
td.Require(t).CmpNoError(err)
td.Cmp(t, client, td.Struct(Client{
AppKey: "system",
Expand All @@ -50,7 +50,7 @@ func TestConfigFromOnlyOneFile(t *testing.T) {
setConfigPaths(t, userConf)

client := Client{}
err := client.loadConfig("ovh-eu")
err := client.LoadConfig("ovh-eu")
td.Require(t).CmpNoError(err)
td.Cmp(t, client, td.Struct(Client{
AppKey: "user",
Expand All @@ -63,23 +63,23 @@ func TestConfigFromNonExistingFile(t *testing.T) {
setConfigPaths(t, doesNotExistConf)

client := Client{}
err := client.loadConfig("ovh-eu")
err := client.LoadConfig("ovh-eu")
td.CmpString(t, err, `missing authentication information, you need to provide one of the following: application_key/application_secret, client_id/client_secret, or access_token`)
}

func TestConfigFromInvalidINIFile(t *testing.T) {
setConfigPaths(t, invalidINIConf)

client := Client{}
err := client.loadConfig("ovh-eu")
err := client.LoadConfig("ovh-eu")
td.CmpString(t, err, "cannot load configuration: unclosed section: [ovh\n")
}

func TestConfigFromInvalidFile(t *testing.T) {
setConfigPaths(t, errorConf)

client := Client{}
err := client.loadConfig("ovh-eu")
err := client.LoadConfig("ovh-eu")
td.CmpString(t, err, "cannot load configuration: BOM: read testdata: is a directory")
}

Expand All @@ -92,7 +92,7 @@ func TestConfigFromEnv(t *testing.T) {
t.Setenv("OVH_CONSUMER_KEY", "env")

client := Client{}
err := client.loadConfig("")
err := client.LoadConfig("")
td.Require(t).CmpNoError(err)
td.Cmp(t, client, td.Struct(Client{
AppKey: "env",
Expand All @@ -106,7 +106,7 @@ func TestConfigFromArgs(t *testing.T) {
setConfigPaths(t, userConf)

client := Client{AppKey: "param", AppSecret: "param", ConsumerKey: "param"}
err := client.loadConfig("ovh-eu")
err := client.LoadConfig("ovh-eu")
td.Require(t).CmpNoError(err)
td.Cmp(t, client, td.Struct(Client{
AppKey: "param",
Expand All @@ -123,15 +123,15 @@ func TestEndpoint(t *testing.T) {

// Test: by name
client := Client{}
err := client.loadConfig("ovh-eu")
err := client.LoadConfig("ovh-eu")
require.CmpNoError(err)
assert.Cmp(client, td.Struct(Client{
AppKey: "ovh",
}))

// Test: by URL
client = Client{}
err = client.loadConfig("https://api.example.com:4242")
err = client.LoadConfig("https://api.example.com:4242")
require.CmpNoError(err)
assert.Cmp(client, td.Struct(Client{
AppKey: "example.com",
Expand All @@ -142,16 +142,16 @@ func TestMissingParam(t *testing.T) {
client := Client{AppKey: "param", AppSecret: "param", ConsumerKey: "param"}

client.endpoint = ""
err := client.loadConfig("")
err := client.LoadConfig("")
td.CmpString(t, err, `unknown endpoint '', consider checking 'Endpoints' list or using an URL`)

client.AppKey = ""
err = client.loadConfig("ovh-eu")
err = client.LoadConfig("ovh-eu")
td.CmpString(t, err, `invalid authentication config, both application_key and application_secret must be given`)
client.AppKey = "param"

client.AppSecret = ""
err = client.loadConfig("ovh-eu")
err = client.LoadConfig("ovh-eu")
td.CmpString(t, err, `invalid authentication config, both application_key and application_secret must be given`)
}

Expand All @@ -172,7 +172,7 @@ func TestConfigOAuth2(t *testing.T) {
setConfigPaths(t, userOAuth2Conf)

client := Client{}
err := client.loadConfig("ovh-eu")
err := client.LoadConfig("ovh-eu")
td.Require(t).CmpNoError(err)
td.Cmp(t, client, td.Struct(Client{
ClientID: "foo",
Expand All @@ -184,22 +184,22 @@ func TestConfigInvalidBoth(t *testing.T) {
setConfigPaths(t, userBothConf)

client := Client{}
err := client.loadConfig("ovh-eu")
err := client.LoadConfig("ovh-eu")
td.CmpString(t, err, "can't use multiple authentication methods: application_key/application_secret, client_id/client_secret")
}

func TestConfigOAuth2Invalid(t *testing.T) {
setConfigPaths(t, userOAuth2InvalidConf)

client := Client{}
err := client.loadConfig("ovh-eu")
err := client.LoadConfig("ovh-eu")
td.CmpString(t, err, "invalid oauth2 config, both client_id and client_secret must be given")
}

func TestConfigOAuth2Incompatible(t *testing.T) {
setConfigPaths(t, userOAuth2IncompatibleConfig)

client := Client{}
err := client.loadConfig("kimsufi-eu")
err := client.LoadConfig("kimsufi-eu")
td.CmpString(t, err, `oauth2 authentication is not compatible with endpoint "https://eu.api.kimsufi.com/1.0"`)
}
6 changes: 3 additions & 3 deletions ovh/ovh.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ func NewClient(endpoint, appKey, appSecret, consumerKey string) (*Client, error)
}

// Get and check the configuration
if err := client.loadConfig(endpoint); err != nil {
if err := client.LoadConfig(endpoint); err != nil {
return nil, err
}
return &client, nil
Expand Down Expand Up @@ -138,7 +138,7 @@ func NewOAuth2Client(endpoint, clientID, clientSecret string) (*Client, error) {
}

// Get and check the configuration
if err := client.loadConfig(endpoint); err != nil {
if err := client.LoadConfig(endpoint); err != nil {
return nil, err
}
return &client, nil
Expand All @@ -152,7 +152,7 @@ func NewAccessTokenClient(endpoint, accessToken string) (*Client, error) {
}

// Get and check the configuration
if err := client.loadConfig(endpoint); err != nil {
if err := client.LoadConfig(endpoint); err != nil {
return nil, err
}
return &client, nil
Expand Down
Loading