diff --git a/engine/api/feature/flipping.go b/engine/api/feature/flipping.go index 2a2c559e4b..9967aa36a7 100644 --- a/engine/api/feature/flipping.go +++ b/engine/api/feature/flipping.go @@ -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" ) @@ -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 { @@ -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. @@ -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) diff --git a/go.mod b/go.mod index f13bbe9092..44d85861fd 100644 --- a/go.mod +++ b/go.mod @@ -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 @@ -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 diff --git a/go.sum b/go.sum index 87ba52f05a..b07da794c0 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/sdk/izanami/README.md b/sdk/izanami/README.md new file mode 100644 index 0000000000..7c79e36bf0 --- /dev/null +++ b/sdk/izanami/README.md @@ -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 +} +``` diff --git a/vendor/github.com/ovhlabs/izanami-go-client/client.go b/sdk/izanami/client.go similarity index 93% rename from vendor/github.com/ovhlabs/izanami-go-client/client.go rename to sdk/izanami/client.go index 9aa07c1277..aae0493ae0 100644 --- a/vendor/github.com/ovhlabs/izanami-go-client/client.go +++ b/sdk/izanami/client.go @@ -1,4 +1,4 @@ -package client +package izanami import ( "net/http" @@ -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 @@ -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, diff --git a/sdk/izanami/client_test.go b/sdk/izanami/client_test.go new file mode 100644 index 0000000000..1a1d36336f --- /dev/null +++ b/sdk/izanami/client_test.go @@ -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)) +} diff --git a/vendor/github.com/ovhlabs/izanami-go-client/feature.go b/sdk/izanami/feature.go similarity index 99% rename from vendor/github.com/ovhlabs/izanami-go-client/feature.go rename to sdk/izanami/feature.go index a7430cc241..964cdbb887 100644 --- a/vendor/github.com/ovhlabs/izanami-go-client/feature.go +++ b/sdk/izanami/feature.go @@ -1,4 +1,4 @@ -package client +package izanami import ( "encoding/json" diff --git a/vendor/github.com/ovhlabs/izanami-go-client/http.go b/sdk/izanami/http.go similarity index 89% rename from vendor/github.com/ovhlabs/izanami-go-client/http.go rename to sdk/izanami/http.go index 6fd4c944d9..917d342efd 100644 --- a/vendor/github.com/ovhlabs/izanami-go-client/http.go +++ b/sdk/izanami/http.go @@ -1,4 +1,4 @@ -package client +package izanami import ( "bytes" @@ -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 @@ -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) } @@ -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 } diff --git a/sdk/izanami/swagger.go b/sdk/izanami/swagger.go new file mode 100644 index 0000000000..a1ab12bd3d --- /dev/null +++ b/sdk/izanami/swagger.go @@ -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 +} diff --git a/vendor/github.com/ovhlabs/izanami-go-client/swagger.go b/vendor/github.com/ovhlabs/izanami-go-client/swagger.go deleted file mode 100644 index 0a4b43c1a5..0000000000 --- a/vendor/github.com/ovhlabs/izanami-go-client/swagger.go +++ /dev/null @@ -1,10 +0,0 @@ -package client - -// Get swagger.json datas -func (c *SwaggerClient) Get() (string, error) { - body, errGet := c.client.get("/swagger.json", nil) - if errGet != nil { - return "", errGet - } - return string(body), nil -} diff --git a/vendor/modules.txt b/vendor/modules.txt index 61559cad10..b09fcf21a3 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -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