Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update response and query fields #23

Merged
merged 1 commit into from
Jan 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions .github/workflows/golangci-lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3
uses: actions/checkout@v4

- 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
6 changes: 3 additions & 3 deletions .github/workflows/unit-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ jobs:

steps:
- name: Checkout
uses: actions/checkout@v2
uses: actions/checkout@v4

- name: Set up Go
uses: actions/setup-go@v2
uses: actions/setup-go@v3
with:
go-version: 1.16
go-version: 1.21

- name: Run test
run: make unittest
7 changes: 4 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,10 @@ linters-settings:
- fieldalignment
lll:
tab-width: 4
nolintlint:
allow-leading-space: false
dupl:
threshold: 500
revive:
severity: error
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
51 changes: 25 additions & 26 deletions acl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ package dbaas
import (
"context"
"encoding/json"
"fmt"
"net/http"
"testing"

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

const aclID = "20d7bcf4-f8d6-4bf6-b8f6-46cb440a87f4"
Expand All @@ -16,7 +18,7 @@ const testACLNotFoundResponse = `{
"error": {
"code": 404,
"title": "Not Found",
"message": "acl 123 not found."
"message": "acl %s not found."
}
}`

Expand Down Expand Up @@ -119,7 +121,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,50 +155,49 @@ 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.NewStringResponder(404, testACLNotFoundResponse))
notFoundResponse := fmt.Sprintf(testACLNotFoundResponse, NotFoundEntityID)
httpmock.RegisterResponder("GET", testClient.Endpoint+ACLsURI+"/"+NotFoundEntityID,
httpmock.NewStringResponder(404, notFoundResponse))

expected := &DBaaSAPIError{}
expected.APIError.Code = 404
expected.APIError.Title = ErrorNotFoundTitle
expected.APIError.Message = "acl 123 not found."
expected.APIError.Message = fmt.Sprintf("acl %s not found.", NotFoundEntityID)

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

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 +229,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 +258,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 +294,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 +319,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
Loading