Skip to content

Commit

Permalink
Merge ad133cd into 20ad5af
Browse files Browse the repository at this point in the history
  • Loading branch information
yadutaf committed Mar 22, 2016
2 parents 20ad5af + ad133cd commit 209bd8d
Show file tree
Hide file tree
Showing 7 changed files with 274 additions and 110 deletions.
52 changes: 40 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,17 @@ application_secret=my_application_secret
consumer_key=my_consumer_key
```

Depending on the API you want to use, you may set the ``endpoint`` to:

* ``ovh-eu`` for OVH Europe API
* ``ovh-ca`` for OVH North-America API
* ``soyoustart-eu`` for So you Start Europe API
* ``soyoustart-ca`` for So you Start North America API
* ``kimsufi-eu`` for Kimsufi Europe API
* ``kimsufi-ca`` for Kimsufi North America API
* ``runabove-ca`` for RunAbove API
* Or any arbitrary URL to use in a test for example

The client will successively attempt to locate this configuration file in

1. Current working directory: ``./ovh.conf``
Expand Down Expand Up @@ -108,15 +119,28 @@ credentials at once. See below.
### Use the API on behalf of a user

Visit [https://eu.api.ovh.com/createApp](https://eu.api.ovh.com/createApp) and create your app
You'll get an application key and an application secret. To use the API you'll need a consumer key.
You'll get an application key and an application secret. To use the API you'll need a consumer key.

The consumer key has two types of restriction:

* path: eg. only the ```GET``` method on ```/me```
* time: eg. expire in 1 day


Then, get a consumer key. Here's an example on how to generate one:
Then, get a consumer key. Here's an example on how to generate one.

First, create a 'ovh.conf' file in the current directory with the application key and
application secret. You can add the consumer key once generated. For alternate
configuration method, please see the [configuration section](#configuration).

```ini
[ovh-eu]
application_key=my_app_key
application_secret=my_application_secret
; consumer_key=my_consumer_key
```

Then, you may use a program like this example to create a consumer key for the application:

```go
package main
Expand Down Expand Up @@ -162,9 +186,21 @@ typically want to do this when writing automation scripts for a single projects.

If this case, you may want to directly go to https://eu.api.ovh.com/createToken/ to generate
the 3 tokens at once. Make sure to save them in one of the 'ovh.conf' configuration file.
Please see the [configuration section](#configuration).

``ovh.conf`` should look like:

```ini
[ovh-eu]
application_key=my_app_key
application_secret=my_application_secret
consumer_key=my_consumer_key
```

## Use the lib

These examples assume valid credentials are available in the [configuration](#configuration).

### GET

```go
Expand All @@ -177,11 +213,7 @@ import (
)

func main() {
ak := "your_app_key"
as := "your_app_secret"
ck := "your_consumer_key"

client, err := govh.NewClient("ovh-eu", ak, as, ck)
client, err := govh.NewEndpointClient("ovh-eu")
if err != nil {
fmt.Printf("Error: %q\n", err)
return
Expand Down Expand Up @@ -228,11 +260,7 @@ import (
)

func main() {
ak := "your_app_key"
as := "your_app_secret"
ck := "your_consumer_key"

client, err := govh.NewClient("ovh-eu", ak, as, ck)
client, err := govh.NewEndpointClient("ovh-eu")
if err != nil {
fmt.Printf("Error: %q\n", err)
return
Expand Down
22 changes: 11 additions & 11 deletions configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,33 +67,33 @@ func (c *Client) loadConfig(endpointName string) error {
endpointName = getConfigValue(cfg, "default", "endpoint", "ovh-eu")
}

if c.appKey == "" {
c.appKey = getConfigValue(cfg, endpointName, "application_key", "")
if c.AppKey == "" {
c.AppKey = getConfigValue(cfg, endpointName, "application_key", "")
}

if c.appSecret == "" {
c.appSecret = getConfigValue(cfg, endpointName, "application_secret", "")
if c.AppSecret == "" {
c.AppSecret = getConfigValue(cfg, endpointName, "application_secret", "")
}

if c.consumerKey == "" {
c.consumerKey = getConfigValue(cfg, endpointName, "consumer_key", "")
if c.ConsumerKey == "" {
c.ConsumerKey = getConfigValue(cfg, endpointName, "consumer_key", "")
}

// Load real endpoint URL by name. If endpoint contains a '/', consider it as a URL
if strings.Contains(endpointName, "/") {
c.endpoint = Endpoint(endpointName)
c.endpoint = endpointName
} else {
c.endpoint = Endpoints[endpointName]
}

// If we still have no valid endpoint, appKey or appSecret, return an error
if c.endpoint == Endpoint("") {
// If we still have no valid endpoint, AppKey or AppSecret, return an error
if c.endpoint == "" {
return fmt.Errorf("Unknown endpoint '%s'. Consider checking 'Endpoints' list of using an URL.", endpointName)
}
if c.appKey == "" {
if c.AppKey == "" {
return fmt.Errorf("Missing application key. Please check your configuration or consult the documentation to create one.")
}
if c.appSecret == "" {
if c.AppSecret == "" {
return fmt.Errorf("Missing application secret. Please check your configuration or consult the documentation to create one.")
}

Expand Down
86 changes: 43 additions & 43 deletions configuration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,14 @@ consumer_key=local
if err != nil {
t.Fatalf("loadConfig failed with: '%v'", err)
}
if client.appKey != "system" {
t.Fatalf("client.appKey should be 'system'. Got '%s'", client.appKey)
if client.AppKey != "system" {
t.Fatalf("client.AppKey should be 'system'. Got '%s'", client.AppKey)
}
if client.appSecret != "user" {
t.Fatalf("client.appSecret should be 'user'. Got '%s'", client.appSecret)
if client.AppSecret != "user" {
t.Fatalf("client.AppSecret should be 'user'. Got '%s'", client.AppSecret)
}
if client.consumerKey != "local" {
t.Fatalf("client.consumerKey should be 'local'. Got '%s'", client.consumerKey)
if client.ConsumerKey != "local" {
t.Fatalf("client.ConsumerKey should be 'local'. Got '%s'", client.ConsumerKey)
}
}

Expand Down Expand Up @@ -100,14 +100,14 @@ consumer_key=user
if err != nil {
t.Fatalf("loadConfig failed with: '%v'", err)
}
if client.appKey != "user" {
t.Fatalf("client.appKey should be 'user'. Got '%s'", client.appKey)
if client.AppKey != "user" {
t.Fatalf("client.AppKey should be 'user'. Got '%s'", client.AppKey)
}
if client.appSecret != "user" {
t.Fatalf("client.appSecret should be 'user'. Got '%s'", client.appSecret)
if client.AppSecret != "user" {
t.Fatalf("client.AppSecret should be 'user'. Got '%s'", client.AppSecret)
}
if client.consumerKey != "user" {
t.Fatalf("client.consumerKey should be 'user'. Got '%s'", client.consumerKey)
if client.ConsumerKey != "user" {
t.Fatalf("client.ConsumerKey should be 'user'. Got '%s'", client.ConsumerKey)
}
}

Expand Down Expand Up @@ -141,25 +141,25 @@ consumer_key=fail
t.Fatalf("loadConfig failed with: '%v'", err)
}
if client.endpoint != OvhEU {
t.Fatalf("client.appKey should be 'env'. Got '%s'", client.appKey)
t.Fatalf("client.AppKey should be 'env'. Got '%s'", client.AppKey)
}
if client.appKey != "env" {
t.Fatalf("client.appKey should be 'env'. Got '%s'", client.appKey)
if client.AppKey != "env" {
t.Fatalf("client.AppKey should be 'env'. Got '%s'", client.AppKey)
}
if client.appSecret != "env" {
t.Fatalf("client.appSecret should be 'env'. Got '%s'", client.appSecret)
if client.AppSecret != "env" {
t.Fatalf("client.AppSecret should be 'env'. Got '%s'", client.AppSecret)
}
if client.consumerKey != "env" {
t.Fatalf("client.consumerKey should be 'env'. Got '%s'", client.consumerKey)
if client.ConsumerKey != "env" {
t.Fatalf("client.ConsumerKey should be 'env'. Got '%s'", client.ConsumerKey)
}
}

func TestConfigFromArgs(t *testing.T) {
// Test
client := Client{
appKey: "param",
appSecret: "param",
consumerKey: "param",
AppKey: "param",
AppSecret: "param",
ConsumerKey: "param",
}
err := client.loadConfig("ovh-eu")

Expand All @@ -168,16 +168,16 @@ func TestConfigFromArgs(t *testing.T) {
t.Fatalf("loadConfig failed with: '%v'", err)
}
if client.endpoint != OvhEU {
t.Fatalf("client.appKey should be 'param'. Got '%s'", client.appKey)
t.Fatalf("client.AppKey should be 'param'. Got '%s'", client.AppKey)
}
if client.appKey != "param" {
t.Fatalf("client.appKey should be 'param'. Got '%s'", client.appKey)
if client.AppKey != "param" {
t.Fatalf("client.AppKey should be 'param'. Got '%s'", client.AppKey)
}
if client.appSecret != "param" {
t.Fatalf("client.appSecret should be 'param'. Got '%s'", client.appSecret)
if client.AppSecret != "param" {
t.Fatalf("client.AppSecret should be 'param'. Got '%s'", client.AppSecret)
}
if client.consumerKey != "param" {
t.Fatalf("client.consumerKey should be 'param'. Got '%s'", client.consumerKey)
if client.ConsumerKey != "param" {
t.Fatalf("client.ConsumerKey should be 'param'. Got '%s'", client.ConsumerKey)
}
}

Expand All @@ -204,8 +204,8 @@ consumer_key=example.com
if err != nil {
t.Fatalf("loadConfig should not fail for endpoint 'ovh-eu'. Got '%v'", err)
}
if client.appKey != "ovh" {
t.Fatalf("configured value should be 'ovh' for endpoint 'ovh-eu'. Got '%s'", client.appKey)
if client.AppKey != "ovh" {
t.Fatalf("configured value should be 'ovh' for endpoint 'ovh-eu'. Got '%s'", client.AppKey)
}

// Test: by URL
Expand All @@ -214,8 +214,8 @@ consumer_key=example.com
if err != nil {
t.Fatalf("loadConfig should not fail for endpoint 'https://api.example.com:4242'. Got '%v'", err)
}
if client.appKey != "example.com" {
t.Fatalf("configured value should be 'example.com' for endpoint 'https://api.example.com:4242'. Got '%s'", client.appKey)
if client.AppKey != "example.com" {
t.Fatalf("configured value should be 'example.com' for endpoint 'https://api.example.com:4242'. Got '%s'", client.AppKey)
}

}
Expand All @@ -224,28 +224,28 @@ func TestMissingParam(t *testing.T) {
// Setup
var err error
client := Client{
appKey: "param",
appSecret: "param",
consumerKey: "param",
AppKey: "param",
AppSecret: "param",
ConsumerKey: "param",
}

// Test
client.endpoint = Endpoint("")
client.endpoint = ""
if err = client.loadConfig(""); err == nil {
t.Fatalf("loadConfig should fail when client.endpoint is missing. Got '%s'", client.endpoint)
}

client.appKey = ""
client.AppKey = ""
if err = client.loadConfig("ovh-eu"); err == nil {
t.Fatalf("loadConfig should fail when client.appKey is missing. Got '%s'", client.appKey)
t.Fatalf("loadConfig should fail when client.AppKey is missing. Got '%s'", client.AppKey)
}
client.appKey = "param"
client.AppKey = "param"

client.appSecret = ""
client.AppSecret = ""
if err = client.loadConfig("ovh-eu"); err == nil {
t.Fatalf("loadConfig should fail when client.appSecret is missing. Got '%s'", client.appSecret)
t.Fatalf("loadConfig should fail when client.AppSecret is missing. Got '%s'", client.AppSecret)
}
client.appSecret = "param"
client.AppSecret = "param"
}

//
Expand Down
2 changes: 1 addition & 1 deletion consumer_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func (ck *CkRequest) Do() (*CkValidationState, error) {
err := ck.client.PostUnAuth("/auth/credential", ck, &state)

if err == nil {
ck.client.consumerKey = state.ConsumerKey
ck.client.ConsumerKey = state.ConsumerKey
}

return &state, err
Expand Down
10 changes: 5 additions & 5 deletions consumer_key_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ func TestNewCkReqest(t *testing.T) {
var InputRequestBody string
ts, client := initMockServer(&InputRequest, 200, `{
"validationUrl":"https://validation.url",
"consumerKey":"`+MockConsumerKey+`",
"ConsumerKey":"`+MockConsumerKey+`",
"state":"pendingValidation"
}`, &InputRequestBody)
client.consumerKey = ""
client.ConsumerKey = ""
defer ts.Close()

// Test
Expand All @@ -33,8 +33,8 @@ func TestNewCkReqest(t *testing.T) {
if err != nil {
t.Fatalf("CkRequest.Do() should not return an error. Got: %q", err)
}
if client.consumerKey != MockConsumerKey {
t.Fatalf("CkRequest.Do() should set client.consumerKey to %s. Got %s", MockConsumerKey, client.consumerKey)
if client.ConsumerKey != MockConsumerKey {
t.Fatalf("CkRequest.Do() should set client.ConsumerKey to %s. Got %s", MockConsumerKey, client.ConsumerKey)
}
if got.ConsumerKey != MockConsumerKey {
t.Fatalf("CkRequest.Do() should set CkValidationState.ConsumerKey to %s. Got %s", MockConsumerKey, got.ConsumerKey)
Expand All @@ -54,7 +54,7 @@ func TestInvalidCkReqest(t *testing.T) {
var InputRequest *http.Request
var InputRequestBody string
ts, client := initMockServer(&InputRequest, http.StatusForbidden, `{"message":"Invalid application key"}`, &InputRequestBody)
client.consumerKey = ""
client.ConsumerKey = ""
defer ts.Close()

// Test
Expand Down
Loading

0 comments on commit 209bd8d

Please sign in to comment.