Skip to content

Commit

Permalink
feat(http): add X-Request-ID if ID is in the context
Browse files Browse the repository at this point in the history
  • Loading branch information
EtienneM committed Aug 5, 2022
1 parent 6714c0f commit 5561b39
Show file tree
Hide file tree
Showing 54 changed files with 596 additions and 505 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## To Be Released

* feat(http): add X-Request-ID if ID is in the context [#262](https://github.com/Scalingo/go-scalingo/pull/262)
* refactor(events): moved addons related events to events_addon.go [#261](https://github.com/Scalingo/go-scalingo/pull/261)
* refactor(events): moved direct app related events to events_app.go [#260](https://github.com/Scalingo/go-scalingo/pull/260)
* feat(events): implement two factor auth related events [#259](https://github.com/Scalingo/go-scalingo/pull/259)
Expand Down
47 changes: 24 additions & 23 deletions addons.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package scalingo

import (
"context"
"encoding/json"
"io"
"strconv"
Expand All @@ -12,13 +13,13 @@ import (
)

type AddonsService interface {
AddonsList(app string) ([]*Addon, error)
AddonProvision(app string, params AddonProvisionParams) (AddonRes, error)
AddonDestroy(app, addonID string) error
AddonUpgrade(app, addonID string, params AddonUpgradeParams) (AddonRes, error)
AddonToken(app, addonID string) (string, error)
AddonLogsURL(app, addonID string) (string, error)
AddonLogsArchives(app, addonId string, page int) (*LogsArchivesResponse, error)
AddonsList(ctx context.Context, app string) ([]*Addon, error)
AddonProvision(ctx context.Context, app string, params AddonProvisionParams) (AddonRes, error)
AddonDestroy(ctx context.Context, app, addonID string) error
AddonUpgrade(ctx context.Context, app, addonID string, params AddonUpgradeParams) (AddonRes, error)
AddonToken(ctx context.Context, app, addonID string) (string, error)
AddonLogsURL(ctx context.Context, app, addonID string) (string, error)
AddonLogsArchives(ctx context.Context, app, addonId string, page int) (*LogsArchivesResponse, error)
}

var _ AddonsService = (*Client)(nil)
Expand Down Expand Up @@ -63,19 +64,19 @@ type AddonLogsURLRes struct {
URL string `json:"url"`
}

func (c *Client) AddonsList(app string) ([]*Addon, error) {
func (c *Client) AddonsList(ctx context.Context, app string) ([]*Addon, error) {
var addonsRes AddonsRes
err := c.ScalingoAPI().SubresourceList("apps", app, "addons", nil, &addonsRes)
err := c.ScalingoAPI().SubresourceList(ctx, "apps", app, "addons", nil, &addonsRes)
if err != nil {
return nil, errgo.Mask(err, errgo.Any)
}
return addonsRes.Addons, nil
}

func (c *Client) AddonShow(app, addonID string) (Addon, error) {
func (c *Client) AddonShow(ctx context.Context, app, addonID string) (Addon, error) {
var addonRes AddonRes

err := c.ScalingoAPI().SubresourceGet("apps", app, "addons", addonID, nil, &addonRes)
err := c.ScalingoAPI().SubresourceGet(ctx, "apps", app, "addons", addonID, nil, &addonRes)
if err != nil {
return Addon{}, errgo.Mask(err, errgo.Any)
}
Expand All @@ -94,17 +95,17 @@ type AddonProvisionParamsWrapper struct {
Addon AddonProvisionParams `json:"addon"`
}

func (c *Client) AddonProvision(app string, params AddonProvisionParams) (AddonRes, error) {
func (c *Client) AddonProvision(ctx context.Context, app string, params AddonProvisionParams) (AddonRes, error) {
var addonRes AddonRes
err := c.ScalingoAPI().SubresourceAdd("apps", app, "addons", AddonProvisionParamsWrapper{params}, &addonRes)
err := c.ScalingoAPI().SubresourceAdd(ctx, "apps", app, "addons", AddonProvisionParamsWrapper{params}, &addonRes)
if err != nil {
return AddonRes{}, errgo.Mask(err, errgo.Any)
}
return addonRes, nil
}

func (c *Client) AddonDestroy(app, addonID string) error {
return c.ScalingoAPI().SubresourceDelete("apps", app, "addons", addonID)
func (c *Client) AddonDestroy(ctx context.Context, app, addonID string) error {
return c.ScalingoAPI().SubresourceDelete(ctx, "apps", app, "addons", addonID)
}

type AddonUpgradeParams struct {
Expand All @@ -115,10 +116,10 @@ type AddonUpgradeParamsWrapper struct {
Addon AddonUpgradeParams `json:"addon"`
}

func (c *Client) AddonUpgrade(app, addonID string, params AddonUpgradeParams) (AddonRes, error) {
func (c *Client) AddonUpgrade(ctx context.Context, app, addonID string, params AddonUpgradeParams) (AddonRes, error) {
var addonRes AddonRes
err := c.ScalingoAPI().SubresourceUpdate(
"apps", app, "addons", addonID,
ctx, "apps", app, "addons", addonID,
AddonUpgradeParamsWrapper{Addon: params}, &addonRes,
)
if err != nil {
Expand All @@ -127,9 +128,9 @@ func (c *Client) AddonUpgrade(app, addonID string, params AddonUpgradeParams) (A
return addonRes, nil
}

func (c *Client) AddonToken(app, addonID string) (string, error) {
func (c *Client) AddonToken(ctx context.Context, app, addonID string) (string, error) {
var res AddonTokenRes
err := c.ScalingoAPI().DoRequest(&http.APIRequest{
err := c.ScalingoAPI().DoRequest(ctx, &http.APIRequest{
Method: "POST",
Endpoint: "/apps/" + app + "/addons/" + addonID + "/token",
}, &res)
Expand All @@ -140,9 +141,9 @@ func (c *Client) AddonToken(app, addonID string) (string, error) {
return res.Addon.Token, nil
}

func (c *Client) AddonLogsURL(app, addonID string) (string, error) {
func (c *Client) AddonLogsURL(ctx context.Context, app, addonID string) (string, error) {
var url AddonLogsURLRes
res, err := c.DBAPI(app, addonID).Do(&http.APIRequest{
res, err := c.DBAPI(app, addonID).Do(ctx, &http.APIRequest{
Endpoint: "/databases/" + addonID + "/logs",
})
if err != nil {
Expand All @@ -158,8 +159,8 @@ func (c *Client) AddonLogsURL(app, addonID string) (string, error) {
return url.URL, nil
}

func (c *Client) AddonLogsArchives(app, addonID string, page int) (*LogsArchivesResponse, error) {
res, err := c.DBAPI(app, addonID).Do(&http.APIRequest{
func (c *Client) AddonLogsArchives(ctx context.Context, app, addonID string, page int) (*LogsArchivesResponse, error) {
res, err := c.DBAPI(app, addonID).Do(ctx, &http.APIRequest{
Endpoint: "/databases/" + addonID + "/logs_archives",
Params: map[string]string{
"page": strconv.FormatInt(int64(page), 10),
Expand Down
14 changes: 8 additions & 6 deletions addons_providers.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
package scalingo

import (
"context"

"gopkg.in/errgo.v1"

"github.com/Scalingo/go-scalingo/v4/http"
)

type AddonProvidersService interface {
AddonProvidersList() ([]*AddonProvider, error)
AddonProviderPlansList(addon string) ([]*Plan, error)
AddonProvidersList(context.Context) ([]*AddonProvider, error)
AddonProviderPlansList(ctx context.Context, addon string) ([]*Plan, error)
}

var _ AddonProvidersService = (*Client)(nil)
Expand Down Expand Up @@ -37,13 +39,13 @@ type ListParams struct {
AddonProviders []*AddonProvider `json:"addon_providers"`
}

func (c *Client) AddonProvidersList() ([]*AddonProvider, error) {
func (c *Client) AddonProvidersList(ctx context.Context) ([]*AddonProvider, error) {
req := &http.APIRequest{
NoAuth: true,
Endpoint: "/addon_providers",
}
var params ListParams
err := c.ScalingoAPI().DoRequest(req, &params)
err := c.ScalingoAPI().DoRequest(ctx, req, &params)
if err != nil {
return nil, errgo.Mask(err)
}
Expand All @@ -60,7 +62,7 @@ var addonProviderTypo = map[string]string{
"scalingo-psql": "scalingo-postgresql",
}

func (c *Client) AddonProviderPlansList(addon string) ([]*Plan, error) {
func (c *Client) AddonProviderPlansList(ctx context.Context, addon string) ([]*Plan, error) {
correctAddon, ok := addonProviderTypo[addon]
if ok {
addon = correctAddon
Expand All @@ -71,7 +73,7 @@ func (c *Client) AddonProviderPlansList(addon string) ([]*Plan, error) {
NoAuth: true,
Endpoint: "/addon_providers/" + addon + "/plans",
}
err := c.ScalingoAPI().DoRequest(req, &params)
err := c.ScalingoAPI().DoRequest(ctx, req, &params)
if err != nil {
return nil, errgo.Notef(err, "fail to get plans")
}
Expand Down
31 changes: 16 additions & 15 deletions alerts.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
package scalingo

import (
"context"
"time"

"gopkg.in/errgo.v1"
)

type AlertsService interface {
AlertsList(app string) ([]*Alert, error)
AlertAdd(app string, params AlertAddParams) (*Alert, error)
AlertShow(app, id string) (*Alert, error)
AlertUpdate(app, id string, params AlertUpdateParams) (*Alert, error)
AlertRemove(app, id string) error
AlertsList(ctx context.Context, app string) ([]*Alert, error)
AlertAdd(ctx context.Context, app string, params AlertAddParams) (*Alert, error)
AlertShow(ctx context.Context, app, id string) (*Alert, error)
AlertUpdate(ctx context.Context, app, id string, params AlertUpdateParams) (*Alert, error)
AlertRemove(ctx context.Context, app, id string) error
}

var _ AlertsService = (*Client)(nil)
Expand All @@ -36,9 +37,9 @@ type AlertRes struct {
Alert *Alert `json:"alert"`
}

func (c *Client) AlertsList(app string) ([]*Alert, error) {
func (c *Client) AlertsList(ctx context.Context, app string) ([]*Alert, error) {
var alertsRes AlertsRes
err := c.ScalingoAPI().SubresourceList("apps", app, "alerts", nil, &alertsRes)
err := c.ScalingoAPI().SubresourceList(ctx, "apps", app, "alerts", nil, &alertsRes)
if err != nil {
return nil, errgo.Notef(err, "fail to query the API to list an alert")
}
Expand All @@ -55,7 +56,7 @@ type AlertAddParams struct {
Notifiers []string
}

func (c *Client) AlertAdd(app string, params AlertAddParams) (*Alert, error) {
func (c *Client) AlertAdd(ctx context.Context, app string, params AlertAddParams) (*Alert, error) {
var alertRes AlertRes
a := &Alert{
ContainerType: params.ContainerType,
Expand All @@ -69,7 +70,7 @@ func (c *Client) AlertAdd(app string, params AlertAddParams) (*Alert, error) {
if params.DurationBeforeTrigger != nil {
a.DurationBeforeTrigger = *params.DurationBeforeTrigger
}
err := c.ScalingoAPI().SubresourceAdd("apps", app, "alerts", AlertRes{
err := c.ScalingoAPI().SubresourceAdd(ctx, "apps", app, "alerts", AlertRes{
Alert: a,
}, &alertRes)
if err != nil {
Expand All @@ -78,9 +79,9 @@ func (c *Client) AlertAdd(app string, params AlertAddParams) (*Alert, error) {
return alertRes.Alert, nil
}

func (c *Client) AlertShow(app, id string) (*Alert, error) {
func (c *Client) AlertShow(ctx context.Context, app, id string) (*Alert, error) {
var alertRes AlertRes
err := c.ScalingoAPI().SubresourceGet("apps", app, "alerts", id, nil, &alertRes)
err := c.ScalingoAPI().SubresourceGet(ctx, "apps", app, "alerts", id, nil, &alertRes)
if err != nil {
return nil, errgo.Notef(err, "fail to query the API to show an alert")
}
Expand All @@ -98,17 +99,17 @@ type AlertUpdateParams struct {
Notifiers *[]string `json:"notifiers,omitempty"`
}

func (c *Client) AlertUpdate(app, id string, params AlertUpdateParams) (*Alert, error) {
func (c *Client) AlertUpdate(ctx context.Context, app, id string, params AlertUpdateParams) (*Alert, error) {
var alertRes AlertRes
err := c.ScalingoAPI().SubresourceUpdate("apps", app, "alerts", id, params, &alertRes)
err := c.ScalingoAPI().SubresourceUpdate(ctx, "apps", app, "alerts", id, params, &alertRes)
if err != nil {
return nil, errgo.Notef(err, "fail to query the API to update an alert")
}
return alertRes.Alert, nil
}

func (c *Client) AlertRemove(app, id string) error {
err := c.ScalingoAPI().SubresourceDelete("apps", app, "alerts", id)
func (c *Client) AlertRemove(ctx context.Context, app, id string) error {
err := c.ScalingoAPI().SubresourceDelete(ctx, "apps", app, "alerts", id)
if err != nil {
return errgo.Notef(err, "fail to query the API to remove an alert")
}
Expand Down
16 changes: 9 additions & 7 deletions alerts_test.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
package scalingo

import (
"context"
"encoding/json"
"net/http"
"net/http/httptest"
"testing"

gomock "github.com/golang/mock/gomock"
"github.com/golang/mock/gomock"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestAlertsClient(t *testing.T) {
ctx := context.Background()
appName := "my-app"
alertID := "my-id"

Expand All @@ -27,7 +29,7 @@ func TestAlertsClient(t *testing.T) {
{
action: "list",
testedClientCall: func(c AlertsService) error {
_, err := c.AlertsList(appName)
_, err := c.AlertsList(ctx, appName)
return err
},
expectedEndpoint: "/v1/apps/my-app/alerts",
Expand All @@ -37,7 +39,7 @@ func TestAlertsClient(t *testing.T) {
{
action: "add",
testedClientCall: func(c AlertsService) error {
_, err := c.AlertAdd(appName, AlertAddParams{})
_, err := c.AlertAdd(ctx, appName, AlertAddParams{})
return err
},
expectedEndpoint: "/v1/apps/my-app/alerts",
Expand All @@ -48,7 +50,7 @@ func TestAlertsClient(t *testing.T) {
{
action: "show",
testedClientCall: func(c AlertsService) error {
_, err := c.AlertShow(appName, alertID)
_, err := c.AlertShow(ctx, appName, alertID)
return err
},
expectedEndpoint: "/v1/apps/my-app/alerts/my-id",
Expand All @@ -58,7 +60,7 @@ func TestAlertsClient(t *testing.T) {
{
action: "update",
testedClientCall: func(c AlertsService) error {
_, err := c.AlertUpdate(appName, alertID, AlertUpdateParams{})
_, err := c.AlertUpdate(ctx, appName, alertID, AlertUpdateParams{})
return err
},
expectedEndpoint: "/v1/apps/my-app/alerts/my-id",
Expand All @@ -68,7 +70,7 @@ func TestAlertsClient(t *testing.T) {
{
action: "remove",
testedClientCall: func(c AlertsService) error {
return c.AlertRemove(appName, alertID)
return c.AlertRemove(ctx, appName, alertID)
},
expectedEndpoint: "/v1/apps/my-app/alerts/my-id",
expectedMethod: "DELETE",
Expand Down Expand Up @@ -109,7 +111,7 @@ func TestAlertsClient(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(handler))
defer ts.Close()

c, err := New(ClientConfig{
c, err := New(ctx, ClientConfig{
APIEndpoint: ts.URL,
APIToken: "test",
})
Expand Down
Loading

0 comments on commit 5561b39

Please sign in to comment.