Skip to content

Commit

Permalink
refactor(sdk): izanami client
Browse files Browse the repository at this point in the history
Signed-off-by: Yvonnick Esnault <yvonnick.esnault@corp.ovh.com>
  • Loading branch information
yesnault committed Jul 1, 2019
1 parent 1fed1b6 commit 573d2e8
Show file tree
Hide file tree
Showing 11 changed files with 123 additions and 36 deletions.
15 changes: 7 additions & 8 deletions engine/api/feature/flipping.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@ package feature
import (
"strings"

client "github.com/ovhlabs/izanami-go-client"

"github.com/ovh/cds/engine/api/cache"
"github.com/ovh/cds/sdk/izanami"
"github.com/ovh/cds/sdk/log"
)

Expand All @@ -19,7 +18,7 @@ const (
cacheFeatureKey = "feature:"
)

var izanami *client.Client
var izanamiClient *izanami.Client

// CheckContext represents the context send to Izanami to check if the feature is enabled
type CheckContext struct {
Expand All @@ -39,14 +38,14 @@ func List() []string {

// Init initialize Izanami client
func Init(apiURL, clientID, clientSecret string) error {
izc, err := client.New(apiURL, clientID, clientSecret)
izc, err := izanami.New(apiURL, clientID, clientSecret)
SetClient(izc)
return err
}

// SetClient set a client driver for Izanami
func SetClient(c *client.Client) {
izanami = c
func SetClient(c *izanami.Client) {
izanamiClient = c
}

// GetFeatures tree for the given project from cache, if not found in cache init from Izanami.
Expand Down Expand Up @@ -94,12 +93,12 @@ func IsEnabled(store cache.Store, featureID string, projectKey string) bool {

func getStatusFromIzanami(featureID string, projectKey string) bool {
// no feature flipping always return active.
if izanami == nil || izanami.Feature() == nil {
if izanamiClient == nil || izanamiClient.Feature() == nil {
return true
}

// get from Izanami
resp, errCheck := izanami.Feature().CheckWithContext(featureID, CheckContext{projectKey})
resp, errCheck := izanamiClient.Feature().CheckWithContext(featureID, CheckContext{projectKey})
if errCheck != nil {
if !strings.Contains(errCheck.Error(), "404") {
log.Warning("Feature.IsEnabled > Cannot check feature %s: %s", featureID, errCheck)
Expand Down
3 changes: 1 addition & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ require (
github.com/eapache/queue v1.1.0 // indirect
github.com/facebookgo/ensure v0.0.0-20160127193407-b4ab57deab51 // indirect
github.com/facebookgo/freeport v0.0.0-20150612182905-d4adf43b75b9 // indirect
github.com/facebookgo/httpcontrol v0.0.0-20150708234001-ccde4420e1fe // indirect
github.com/facebookgo/httpcontrol v0.0.0-20150708234001-ccde4420e1fe
github.com/facebookgo/stack v0.0.0-20160209184415-751773369052 // indirect
github.com/facebookgo/subset v0.0.0-20150612182917-8dac2c3c4870 // indirect
github.com/fatih/color v0.0.0-20161018201348-a360acfe359f
Expand Down Expand Up @@ -186,7 +186,6 @@ require (
github.com/ovh/cds/sdk/interpolate v0.0.0-20190319104452-71125b036b25
github.com/ovh/go-ovh v0.0.0-20171219162654-02f7e9439689 // indirect
github.com/ovh/venom v0.25.0
github.com/ovhlabs/izanami-go-client v0.0.0-20180321094556-73dedd898473
github.com/pascaldekloe/goe v0.0.0-20180627143212-57f6aae5913c // indirect
github.com/patrickmn/go-cache v2.1.0+incompatible
github.com/pborman/uuid v0.0.0-20170612153648-e790cca94e6c
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -407,8 +407,6 @@ github.com/ovh/go-ovh v0.0.0-20171219162654-02f7e9439689 h1:x9UmLJfoTojbsVpUh0sO
github.com/ovh/go-ovh v0.0.0-20171219162654-02f7e9439689/go.mod h1:joRatxRJaZBsY3JAOEMcoOp05CnZzsx4scTxi95DHyQ=
github.com/ovh/venom v0.25.0 h1:q0NC8KHuG1UoWNYXqhZUM/VbOnS8ciQJr6Afn230OIw=
github.com/ovh/venom v0.25.0/go.mod h1:vf+2wztk/hOPS9AU32/B9XhwVPxKZ97wBJLu+AGXhYQ=
github.com/ovhlabs/izanami-go-client v0.0.0-20180321094556-73dedd898473 h1:WX3Cy2kyJeQp/HGFao07xm75Ec5BlF41hpi0U/37oHg=
github.com/ovhlabs/izanami-go-client v0.0.0-20180321094556-73dedd898473/go.mod h1:5Qy53wJuE3WEGY1sMAlDyhu/Rx9ICM+uUU6X0tBR3Xo=
github.com/pascaldekloe/goe v0.0.0-20180627143212-57f6aae5913c h1:Lgl0gzECD8GnQ5QCWA8o6BtfL6mDH5rQgM4/fX3avOs=
github.com/pascaldekloe/goe v0.0.0-20180627143212-57f6aae5913c/go.mod h1:lzWF7FIEvWOWxwDKqyGYQf6ZUaNfKdP144TG7ZOy1lc=
github.com/patrickmn/go-cache v2.1.0+incompatible h1:HRMgzkcYKYpi3C8ajMPV8OFXaaRUnok+kx1WdO15EQc=
Expand Down
50 changes: 50 additions & 0 deletions sdk/izanami/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# izanami-go-client
Go client for [izanami](https://github.com/maif/izanami)


### Usage

```go
c, errNew := New("host", "clientID", "clientSecret")
if errNew != nil {
return errNew
}

// List all features
features, errF := c.Feature().ListAll()
if errF != nil {
return errF
}

// Create a feature
f := FeatureModel{
ID: "my-feature",
Enabled: true,
Strategy: NoStrategy,
}
if err := c.Feature().Create(f); err != nil {
return err
}

// Get a feature
myFeature, errF := c.Feature().Get(f.ID)
if errF != nil {
return errF
}

// Update a feature
if err := c.Feature().Update(myFeature); err != nil {
return err
}

// Check a feature
check, err := c.Feature().CheckWithoutContext(feat.ID)
if err != nil {
return err
}

// Delete a feature
if err := c.Feature().Delete(myFeature.ID); err != nil {
return err
}
```
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package client
package izanami

import (
"net/http"
Expand All @@ -12,7 +12,7 @@ type Client struct {
apiURL string
clientID string
clientSecret string
HttpClient *http.Client
HTTPClient *http.Client
}

// FeatureClient represents a client for feature management
Expand All @@ -31,7 +31,7 @@ func New(apiURL, clientID, secret string) (*Client, error) {
apiURL: apiURL,
clientID: clientID,
clientSecret: secret,
HttpClient: &http.Client{
HTTPClient: &http.Client{
Transport: &httpcontrol.Transport{
RequestTimeout: time.Second * 30,
MaxTries: 5,
Expand Down
43 changes: 43 additions & 0 deletions sdk/izanami/client_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package izanami

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestFeature(t *testing.T) {
c, err := New("http://localhost:8080/api", "yourclientid", "yoursecret")
if err != nil {
t.Fatal(err)
}

t.Log("Creating a feature")
feat := FeatureModel{
ID: "test:feature",
Enabled: false,
Strategy: NoStrategy,
}
assert.NoError(t, c.Feature().Create(feat))

t.Log("Updating a feature")
feat.Enabled = true
assert.NoError(t, c.Feature().Update(feat))

check, err := c.Feature().CheckWithoutContext(feat.ID)
assert.NoError(t, err)
assert.Equal(t, check.Active, true)

t.Log("Getting a feature")
f, errF := c.Feature().Get(feat.ID)
assert.NoError(t, errF)

assert.Equal(t, true, f.Enabled)

assert.NoError(t, c.Feature().Delete(f.ID))

features, errS := c.Feature().ListAll()
assert.NoError(t, errS)

assert.Equal(t, 0, len(features))
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package client
package izanami

import (
"encoding/json"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package client
package izanami

import (
"bytes"
Expand Down Expand Up @@ -27,9 +27,9 @@ type Metadata struct {

func (c *Client) buildURL(path string, method string, httpParams map[string]string, body io.Reader) (*http.Request, error) {
url := fmt.Sprintf("%s%s", c.apiURL, path)
req, errRequest := http.NewRequest(method, url, body)
if errRequest != nil {
return nil, errRequest
req, err := http.NewRequest(method, url, body)
if err != nil {
return nil, err
}
if httpParams != nil {
// Add query params
Expand All @@ -48,9 +48,9 @@ func (c *Client) buildURL(path string, method string, httpParams map[string]stri
}

func (c *Client) get(path string, httpParams map[string]string) ([]byte, error) {
req, errReq := c.buildURL(path, http.MethodGet, httpParams, nil)
if errReq != nil {
return nil, errReq
req, err := c.buildURL(path, http.MethodGet, httpParams, nil)
if err != nil {
return nil, err
}
return c.do(req)
}
Expand Down Expand Up @@ -89,7 +89,7 @@ func (c *Client) delete(path string) error {
}

func (c *Client) do(req *http.Request) ([]byte, error) {
res, errDo := c.HttpClient.Do(req)
res, errDo := c.HTTPClient.Do(req)
if errDo != nil {
return nil, errDo
}
Expand Down
10 changes: 10 additions & 0 deletions sdk/izanami/swagger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package izanami

// Get swagger.json datas
func (c *SwaggerClient) Get() (string, error) {
body, err := c.client.get("/swagger.json", nil)
if err != nil {
return "", err
}
return string(body), nil
}
10 changes: 0 additions & 10 deletions vendor/github.com/ovhlabs/izanami-go-client/swagger.go

This file was deleted.

2 changes: 0 additions & 2 deletions vendor/modules.txt
Original file line number Diff line number Diff line change
Expand Up @@ -446,8 +446,6 @@ github.com/ovh/venom/executors/smtp
github.com/ovh/venom/executors/ssh
github.com/ovh/venom/executors/web
github.com/ovh/venom/executors
# github.com/ovhlabs/izanami-go-client v0.0.0-20180321094556-73dedd898473
github.com/ovhlabs/izanami-go-client
# github.com/patrickmn/go-cache v2.1.0+incompatible
github.com/patrickmn/go-cache
# github.com/pborman/uuid v0.0.0-20170612153648-e790cca94e6c
Expand Down

0 comments on commit 573d2e8

Please sign in to comment.