diff --git a/management/client/rest/client.go b/management/client/rest/client.go index c3995a3a8..886a59f2c 100644 --- a/management/client/rest/client.go +++ b/management/client/rest/client.go @@ -14,7 +14,7 @@ import ( type Client struct { managementURL string authHeader string - httpClient *http.Client + httpClient HttpClient // Accounts NetBird account APIs // see more: https://docs.netbird.io/api/resources/accounts @@ -70,27 +70,24 @@ type Client struct { } // New initialize new Client instance using PAT token -func New(managementURL, token string, opts ...option) *Client { - client := &Client{ - managementURL: managementURL, - authHeader: "Token " + token, - httpClient: http.DefaultClient, - } - - for _, option := range opts { - option(client) - } - - client.initialize() - return client +func New(managementURL, token string) *Client { + return NewWithOptions( + WithManagementURL(managementURL), + WithPAT(token), + ) } // NewWithBearerToken initialize new Client instance using Bearer token type -func NewWithBearerToken(managementURL, token string, opts ...option) *Client { +func NewWithBearerToken(managementURL, token string) *Client { + return NewWithOptions( + WithManagementURL(managementURL), + WithBearerToken(token), + ) +} + +func NewWithOptions(opts ...option) *Client { client := &Client{ - managementURL: managementURL, - authHeader: "Bearer " + token, - httpClient: http.DefaultClient, + httpClient: http.DefaultClient, } for _, option := range opts { @@ -129,7 +126,7 @@ func (c *Client) NewRequest(ctx context.Context, method, path string, body io.Re req.Header.Add("Content-Type", "application/json") } - resp, err := http.DefaultClient.Do(req) + resp, err := c.httpClient.Do(req) if err != nil { return nil, err } diff --git a/management/client/rest/options.go b/management/client/rest/options.go index 35cf11b17..5aad7dd7e 100644 --- a/management/client/rest/options.go +++ b/management/client/rest/options.go @@ -4,8 +4,32 @@ import "net/http" type option func(*Client) -func WithHttpClient(client *http.Client) option { +type HttpClient interface { + Do(req *http.Request) (*http.Response, error) +} + +func WithHttpClient(client HttpClient) option { return func(c *Client) { c.httpClient = client } } + +func WithBearerToken(token string) option { + return WithAuthHeader("Bearer " + token) +} + +func WithPAT(token string) option { + return WithAuthHeader("Token " + token) +} + +func WithManagementURL(url string) option { + return func(c *Client) { + c.managementURL = url + } +} + +func WithAuthHeader(value string) option { + return func(c *Client) { + c.authHeader = value + } +} diff --git a/management/client/rest/policies.go b/management/client/rest/policies.go index 006b0eeb7..2f2df4a78 100644 --- a/management/client/rest/policies.go +++ b/management/client/rest/policies.go @@ -15,11 +15,9 @@ type PoliciesAPI struct { // List list all policies // See more: https://docs.netbird.io/api/resources/policies#list-all-policies -func (a *PoliciesAPI) List(ctx context.Context, accountID string) ([]api.Policy, error) { +func (a *PoliciesAPI) List(ctx context.Context) ([]api.Policy, error) { path := "/api/policies" - if accountID != "" { - path += "?account=" + accountID - } + resp, err := a.c.NewRequest(ctx, "GET", path, nil) if err != nil { return nil, err @@ -65,11 +63,9 @@ func (a *PoliciesAPI) Create(ctx context.Context, request api.PostApiPoliciesJSO // Update update policy info // See more: https://docs.netbird.io/api/resources/policies#update-a-policy -func (a *PoliciesAPI) Update(ctx context.Context, policyID string, request api.PutApiPoliciesPolicyIdJSONRequestBody, accountID string) (*api.Policy, error) { +func (a *PoliciesAPI) Update(ctx context.Context, policyID string, request api.PutApiPoliciesPolicyIdJSONRequestBody) (*api.Policy, error) { path := "/api/policies/" + policyID - if accountID != "" { - path += "?account=" + accountID - } + requestBytes, err := json.Marshal(request) if err != nil { return nil, err diff --git a/management/client/rest/setupkeys.go b/management/client/rest/setupkeys.go index ceb3996d6..5625b6acc 100644 --- a/management/client/rest/setupkeys.go +++ b/management/client/rest/setupkeys.go @@ -43,11 +43,8 @@ func (a *SetupKeysAPI) Get(ctx context.Context, setupKeyID string) (*api.SetupKe // Create generate new Setup Key // See more: https://docs.netbird.io/api/resources/setup-keys#create-a-setup-key -func (a *SetupKeysAPI) Create(ctx context.Context, request api.PostApiSetupKeysJSONRequestBody, accountID string) (*api.SetupKeyClear, error) { +func (a *SetupKeysAPI) Create(ctx context.Context, request api.PostApiSetupKeysJSONRequestBody) (*api.SetupKeyClear, error) { path := "/api/setup-keys" - if accountID != "" { - path += "?account=" + accountID - } requestBytes, err := json.Marshal(request) if err != nil { diff --git a/management/client/rest/setupkeys_test.go b/management/client/rest/setupkeys_test.go index 22459cff3..8edce8428 100644 --- a/management/client/rest/setupkeys_test.go +++ b/management/client/rest/setupkeys_test.go @@ -108,7 +108,7 @@ func TestSetupKeys_Create_200(t *testing.T) { }) ret, err := c.SetupKeys.Create(context.Background(), api.PostApiSetupKeysJSONRequestBody{ ExpiresIn: 5, - }, "") + }) require.NoError(t, err) assert.Equal(t, testSteupKeyGenerated, *ret) }) @@ -124,7 +124,7 @@ func TestSetupKeys_Create_Err(t *testing.T) { }) ret, err := c.SetupKeys.Create(context.Background(), api.PostApiSetupKeysJSONRequestBody{ ExpiresIn: 5, - }, "") + }) assert.Error(t, err) assert.Equal(t, "No", err.Error()) assert.Nil(t, ret) @@ -207,7 +207,7 @@ func TestSetupKeys_Integration(t *testing.T) { Ephemeral: ptr(false), Name: "test", Type: "reusable", - }, "") + }) require.NoError(t, err) assert.Equal(t, true, skClear.Valid)