Skip to content

Commit

Permalink
Update response and query fields
Browse files Browse the repository at this point in the history
- Added new fields for the response and query params for some entities
- Updated README
- Updated Go version in GitHub Actions
- Fix golangci-lint issues
  • Loading branch information
Gogen120 committed Jan 12, 2024
1 parent db29407 commit b23d9d3
Show file tree
Hide file tree
Showing 31 changed files with 324 additions and 349 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/golangci-lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ jobs:

- uses: actions/setup-go@v3
with:
go-version: 1.17
go-version: 1.21

- name: golangci-lint
uses: golangci/golangci-lint-action@v3
with:
version: v1.39
version: v1.55.2
2 changes: 1 addition & 1 deletion .github/workflows/unit-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v2
with:
go-version: 1.16
go-version: 1.21

- name: Run test
run: make unittest
5 changes: 2 additions & 3 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ linters:
- godox
- goprintffuncname
- gosimple
- ifshort
- lll
- makezero
- nakedret
Expand Down Expand Up @@ -55,8 +54,8 @@ linters-settings:
- fieldalignment
lll:
tab-width: 4
nolintlint:
allow-leading-space: false
dupl:
threshold: 200
issues:
max-issues-per-linter: 0
max-same-issues: 0
Expand Down
17 changes: 10 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,19 @@ The Go library documentation is available at [go.dev](https://pkg.go.dev/github.

You can use this library to work with the following objects of the Selectel Managed Databases Service:

* datastore
* acl
* available extension
* configuration parameter
* database
* user
* grant
* datastore
* datastore type
* flavor
* extension
* available extension
* configuration parameter
* prometheus metrics tokens
* flavor
* grant
* logical replication slots
* prometheus metrics tokens
* topic
* user

## Getting started

Expand Down Expand Up @@ -56,6 +58,7 @@ Selectel Managed Databases Service currently has the following API endpoint:
| https://ru-9.dbaas.selcloud.ru/v1 | ru-9 |
| https://nl-1.dbaas.selcloud.ru/v1 | nl-1 |
| https://uz-1.dbaas.selcloud.ru/v1 | uz-1 |
| https://kz-1.dbaas.selcloud.ru/v1 | kz-1 |

You can also retrieve all available API endpoints from the Identity
catalog.
Expand Down
13 changes: 7 additions & 6 deletions acl.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,11 @@ type ACLQueryParams struct {
Status Status `json:"status,omitempty"`
}

const ACLsURI = "/acls"

// ACLs returns all ACLs.
func (api *API) ACLs(ctx context.Context, params *ACLQueryParams) ([]ACL, error) {
uri, err := setQueryParams("/acls", params)
uri, err := setQueryParams(ACLsURI, params)
if err != nil {
return []ACL{}, err
}
Expand All @@ -74,7 +76,7 @@ func (api *API) ACLs(ctx context.Context, params *ACLQueryParams) ([]ACL, error)

// ACL returns an ACL based on the ID.
func (api *API) ACL(ctx context.Context, aclID string) (ACL, error) {
uri := fmt.Sprintf("/acls/%s", aclID)
uri := fmt.Sprintf("%s/%s", ACLsURI, aclID)

resp, err := api.makeRequest(ctx, http.MethodGet, uri, nil)
if err != nil {
Expand All @@ -94,7 +96,6 @@ func (api *API) ACL(ctx context.Context, aclID string) (ACL, error) {

// CreateACL creates a new acl.
func (api *API) CreateACL(ctx context.Context, opts ACLCreateOpts) (ACL, error) {
uri := "/acls"
createACLOpts := struct {
ACL ACLCreateOpts `json:"acl"`
}{
Expand All @@ -105,7 +106,7 @@ func (api *API) CreateACL(ctx context.Context, opts ACLCreateOpts) (ACL, error)
return ACL{}, fmt.Errorf("Error marshalling params to JSON, %w", err)
}

resp, err := api.makeRequest(ctx, http.MethodPost, uri, requestBody)
resp, err := api.makeRequest(ctx, http.MethodPost, ACLsURI, requestBody)
if err != nil {
return ACL{}, err
}
Expand All @@ -123,7 +124,7 @@ func (api *API) CreateACL(ctx context.Context, opts ACLCreateOpts) (ACL, error)

// UpdateACL updates an existing acl.
func (api *API) UpdateACL(ctx context.Context, aclID string, opts ACLUpdateOpts) (ACL, error) {
uri := fmt.Sprintf("/acls/%s", aclID)
uri := fmt.Sprintf("%s/%s", ACLsURI, aclID)
updateACLOpts := struct {
ACL ACLUpdateOpts `json:"acl"`
}{
Expand Down Expand Up @@ -152,7 +153,7 @@ func (api *API) UpdateACL(ctx context.Context, aclID string, opts ACLUpdateOpts)

// DeleteACL deletes an existing acl.
func (api *API) DeleteACL(ctx context.Context, aclID string) error {
uri := fmt.Sprintf("/acls/%s", aclID)
uri := fmt.Sprintf("%s/%s", ACLsURI, aclID)

_, err := api.makeRequest(ctx, http.MethodDelete, uri, nil)
if err != nil {
Expand Down
41 changes: 19 additions & 22 deletions acl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

"github.com/jarcoal/httpmock"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

const aclID = "20d7bcf4-f8d6-4bf6-b8f6-46cb440a87f4"
Expand Down Expand Up @@ -119,7 +120,7 @@ func TestACLs(t *testing.T) {
testClient := SetupTestClient()
defer httpmock.DeactivateAndReset()

httpmock.RegisterResponder("GET", testClient.Endpoint+"/acls",
httpmock.RegisterResponder("GET", testClient.Endpoint+ACLsURI,
httpmock.NewStringResponder(200, testACLsResponse))

expected := []ACL{
Expand Down Expand Up @@ -153,32 +154,30 @@ func TestACLs(t *testing.T) {

actual, err := testClient.ACLs(context.Background(), nil)

if assert.NoError(t, err) {
assert.Equal(t, expected, actual)
}
require.NoError(t, err)
assert.Equal(t, expected, actual)
}

func TestACL(t *testing.T) {
httpmock.Activate()
testClient := SetupTestClient()
defer httpmock.DeactivateAndReset()

httpmock.RegisterResponder("GET", testClient.Endpoint+"/acls/"+aclID,
httpmock.RegisterResponder("GET", testClient.Endpoint+ACLsURI+"/"+aclID,
httpmock.NewStringResponder(200, testACLResponse))

actual, err := testClient.ACL(context.Background(), aclID)

if assert.NoError(t, err) {
assert.Equal(t, ACLExpected, actual)
}
require.NoError(t, err)
assert.Equal(t, ACLExpected, actual)
}

func TestACLNotFound(t *testing.T) {
httpmock.Activate()
testClient := SetupTestClient()
defer httpmock.DeactivateAndReset()

httpmock.RegisterResponder("GET", testClient.Endpoint+"/acls/123",
httpmock.RegisterResponder("GET", testClient.Endpoint+ACLsURI+"/123",

Check failure on line 180 in acl_test.go

View workflow job for this annotation

GitHub Actions / lint

string `/123` has 13 occurrences, make it a constant (goconst)
httpmock.NewStringResponder(404, testACLNotFoundResponse))

expected := &DBaaSAPIError{}
Expand All @@ -188,15 +187,15 @@ func TestACLNotFound(t *testing.T) {

_, err := testClient.ACL(context.Background(), "123")

assert.ErrorAs(t, err, &expected)
require.ErrorAs(t, err, &expected)
}

func TestCreateACL(t *testing.T) {
httpmock.Activate()
testClient := SetupTestClient()
defer httpmock.DeactivateAndReset()

httpmock.RegisterResponder("POST", testClient.Endpoint+"/acls",
httpmock.RegisterResponder("POST", testClient.Endpoint+ACLsURI,
func(req *http.Request) (*http.Response, error) {
if err := json.NewDecoder(req.Body).Decode(&ACLCreateOpts{}); err != nil {
return httpmock.NewStringResponse(400, ""), err
Expand Down Expand Up @@ -228,17 +227,16 @@ func TestCreateACL(t *testing.T) {

ACLCreateExpected := ACLExpected
ACLCreateExpected.Status = StatusPendingCreate
if assert.NoError(t, err) {
assert.Equal(t, ACLCreateExpected, actual)
}
require.NoError(t, err)
assert.Equal(t, ACLCreateExpected, actual)
}

func TestCreateACLInvalidDatastoreID(t *testing.T) {
httpmock.Activate()
testClient := SetupTestClient()
defer httpmock.DeactivateAndReset()

httpmock.RegisterResponder("POST", testClient.Endpoint+"/acls",
httpmock.RegisterResponder("POST", testClient.Endpoint+ACLsURI,
httpmock.NewStringResponder(400, testCreateACLInvalidDatastoreIDResponse))

expected := &DBaaSAPIError{}
Expand All @@ -258,15 +256,15 @@ func TestCreateACLInvalidDatastoreID(t *testing.T) {

_, err := testClient.CreateACL(context.Background(), createACLOpts)

assert.ErrorAs(t, err, &expected)
require.ErrorAs(t, err, &expected)
}

func TestUpdateACL(t *testing.T) {
httpmock.Activate()
testClient := SetupTestClient()
defer httpmock.DeactivateAndReset()

httpmock.RegisterResponder("PUT", testClient.Endpoint+"/acls/"+aclID,
httpmock.RegisterResponder("PUT", testClient.Endpoint+ACLsURI+"/"+aclID,
func(req *http.Request) (*http.Response, error) {
if err := json.NewDecoder(req.Body).Decode(&ACLUpdateOpts{}); err != nil {
return httpmock.NewStringResponse(400, ""), err
Expand Down Expand Up @@ -294,17 +292,16 @@ func TestUpdateACL(t *testing.T) {

ACLUpdateExpexted := ACLExpected
ACLUpdateExpexted.Status = StatusPendingUpdate
if assert.NoError(t, err) {
assert.Equal(t, ACLUpdateExpexted, actual)
}
require.NoError(t, err)
assert.Equal(t, ACLUpdateExpexted, actual)
}

func TestUpdateACLInvalidResponse(t *testing.T) {
httpmock.Activate()
testClient := SetupTestClient()
defer httpmock.DeactivateAndReset()

httpmock.RegisterResponder("PUT", testClient.Endpoint+"/acls/"+aclID,
httpmock.RegisterResponder("PUT", testClient.Endpoint+ACLsURI+"/"+aclID,
httpmock.NewStringResponder(400, testUpdateACLInvalidResponse))

expected := &DBaaSAPIError{}
Expand All @@ -320,5 +317,5 @@ func TestUpdateACLInvalidResponse(t *testing.T) {

_, err := testClient.UpdateACL(context.Background(), aclID, updateACLOpts)

assert.ErrorAs(t, err, &expected)
require.ErrorAs(t, err, &expected)
}
8 changes: 4 additions & 4 deletions available_extension.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ type AvailableExtension struct {
DependencyIDs []string `json:"dependency_ids"`
}

const AvailableExtensionsURI = "/available-extensions"

// AvailableExtensions returns all available extensions.
func (api *API) AvailableExtensions(ctx context.Context) ([]AvailableExtension, error) {
uri := "/available-extensions"

resp, err := api.makeRequest(ctx, http.MethodGet, uri, nil)
resp, err := api.makeRequest(ctx, http.MethodGet, AvailableExtensionsURI, nil)
if err != nil {
return []AvailableExtension{}, err
}
Expand All @@ -37,7 +37,7 @@ func (api *API) AvailableExtensions(ctx context.Context) ([]AvailableExtension,

// AvailableExtension returns an available extension based on the ID.
func (api *API) AvailableExtension(ctx context.Context, availableExtensionID string) (AvailableExtension, error) {
uri := fmt.Sprintf("/available-extensions/%s", availableExtensionID)
uri := fmt.Sprintf("%s/%s", AvailableExtensionsURI, availableExtensionID)

resp, err := api.makeRequest(ctx, http.MethodGet, uri, nil)
if err != nil {
Expand Down
19 changes: 9 additions & 10 deletions available_extension_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"github.com/jarcoal/httpmock"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

const availableExtensionID = "20d7bcf4-f8d6-4bf6-b8f6-46cb440a87f4"
Expand Down Expand Up @@ -58,7 +59,7 @@ func TestAvailableExtensions(t *testing.T) {
testClient := SetupTestClient()
defer httpmock.DeactivateAndReset()

httpmock.RegisterResponder("GET", testClient.Endpoint+"/available-extensions",
httpmock.RegisterResponder("GET", testClient.Endpoint+AvailableExtensionsURI,
httpmock.NewStringResponder(200, testAvailableExtensionsResponse))

expected := []AvailableExtension{
Expand All @@ -84,17 +85,16 @@ func TestAvailableExtensions(t *testing.T) {

actual, err := testClient.AvailableExtensions(context.Background())

if assert.NoError(t, err) {
assert.Equal(t, expected, actual)
}
require.NoError(t, err)
assert.Equal(t, expected, actual)
}

func TestAvailableExtension(t *testing.T) {
httpmock.Activate()
testClient := SetupTestClient()
defer httpmock.DeactivateAndReset()

httpmock.RegisterResponder("GET", testClient.Endpoint+"/available-extensions/"+datastoreTypeID,
httpmock.RegisterResponder("GET", testClient.Endpoint+AvailableExtensionsURI+"/"+datastoreTypeID,
httpmock.NewStringResponder(200, testAvailableExtensionResponse))

expected := AvailableExtension{
Expand All @@ -109,17 +109,16 @@ func TestAvailableExtension(t *testing.T) {

actual, err := testClient.AvailableExtension(context.Background(), availableExtensionID)

if assert.NoError(t, err) {
assert.Equal(t, expected, actual)
}
require.NoError(t, err)
assert.Equal(t, expected, actual)
}

func TestAvailableExtensionNotFound(t *testing.T) {
httpmock.Activate()
testClient := SetupTestClient()
defer httpmock.DeactivateAndReset()

httpmock.RegisterResponder("GET", testClient.Endpoint+"/available-extensions/123",
httpmock.RegisterResponder("GET", testClient.Endpoint+AvailableExtensionsURI+"/123",
httpmock.NewStringResponder(404, testAvailableExtensionNotFoundResponse))

expected := &DBaaSAPIError{}
Expand All @@ -129,5 +128,5 @@ func TestAvailableExtensionNotFound(t *testing.T) {

_, err := testClient.AvailableExtension(context.Background(), "123")

assert.ErrorAs(t, err, &expected)
require.ErrorAs(t, err, &expected)
}
8 changes: 4 additions & 4 deletions configuration_parameter.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ type ConfigurationParameter struct {
IsChangeable bool `json:"is_changeable"`
}

const ConfigurationParametersURI = "/configuration-parameters"

// ConfigurationParameters returns all configuration parameters.
func (api *API) ConfigurationParameters(ctx context.Context) ([]ConfigurationParameter, error) {
uri := "/configuration-parameters"

resp, err := api.makeRequest(ctx, http.MethodGet, uri, nil)
resp, err := api.makeRequest(ctx, http.MethodGet, ConfigurationParametersURI, nil)
if err != nil {
return []ConfigurationParameter{}, err
}
Expand All @@ -47,7 +47,7 @@ func (api *API) ConfigurationParameter(
ctx context.Context,
configurationParameterID string,
) (ConfigurationParameter, error) {
uri := fmt.Sprintf("/configuration-parameters/%s", configurationParameterID)
uri := fmt.Sprintf("%s/%s", ConfigurationParametersURI, configurationParameterID)

resp, err := api.makeRequest(ctx, http.MethodGet, uri, nil)
if err != nil {
Expand Down
Loading

0 comments on commit b23d9d3

Please sign in to comment.