Skip to content

Commit

Permalink
TT-10430: migrate to new mongo-go driver
Browse files Browse the repository at this point in the history
  • Loading branch information
mitjaziv committed Nov 14, 2023
1 parent 70ed7e2 commit 84624c3
Show file tree
Hide file tree
Showing 23 changed files with 1,013 additions and 267 deletions.
3 changes: 1 addition & 2 deletions .github/PULL_REQUEST_TEMPLATE.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,9 @@
- [ ] My change requires a change to the documentation.
- [ ] If you've changed APIs, describe what needs to be updated in the documentation.
- [ ] I have updated the documentation accordingly.
- [ ] Modules and vendor dependencies have been updated; run `go mod tidy && go mod vendor` in applicable directories.
- [ ] Modules dependencies have been updated; run `go mod tidy` in applicable directories.
- [ ] I have added tests to cover my changes.
- [ ] All new and existing tests passed.
- [ ] Check your code additions will not fail linting checks:
- [ ] `gofmt -s -w .`
- [ ] `go vet ./...`
- [ ] `golangci-lint run`
7 changes: 7 additions & 0 deletions .golangci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@ issues:
max-same-issues: 0
new-from-rev: origin/master

# Excluding configuration per-path, per-linter, per-text and per-source
exclude-rules:
# Exclude some linters from running on test files.
- path: _test\.go
linters:
- dupl

linters-settings:
dogsled:
max-blank-identifiers: 2
Expand Down
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ swagger-client: check-swagger

clean: ## Clean up the temp and output directories, and any built binaries. This will cause everything to get rebuilt.
> rm -rf ./bin
> rm -rf ./integration/outputs
> go clean
> cd mservctl
> go clean
Expand Down
55 changes: 35 additions & 20 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package api

import (
"bufio"
"context"
"errors"
"fmt"
"io"
"io/ioutil"
Expand All @@ -21,7 +23,6 @@ import (
coprocess "github.com/TykTechnologies/mserv/coprocess/bindings/go"
"github.com/TykTechnologies/mserv/storage"
"github.com/TykTechnologies/mserv/util/logger"
"github.com/TykTechnologies/mserv/util/storage/errors"
)

const (
Expand All @@ -41,24 +42,24 @@ type API struct {
store storage.MservStore
}

func (a *API) HandleUpdateBundle(filePath string, bundleName string) (*storage.MW, error) {
mw, err := a.store.GetMWByID(bundleName)
func (a *API) HandleUpdateBundle(ctx context.Context, filePath, bundleName string) (*storage.MW, error) {
mw, err := a.store.GetMWByID(ctx, bundleName)
if err != nil {
return nil, err
return nil, fmt.Errorf("get mw by id error: %w", err)
}

err = a.store.DeleteMW(mw.UID)
err = a.store.DeleteMW(ctx, mw.UID)
if err != nil {
return nil, err
return nil, fmt.Errorf("delete mw error: %w", err)
}

return a.HandleNewBundle(filePath, mw.APIID, bundleName)
return a.HandleNewBundle(ctx, filePath, mw.APIID, bundleName)
}

func (a *API) HandleDeleteBundle(bundleName string) error {
mw, err := a.store.GetMWByID(bundleName)
func (a *API) HandleDeleteBundle(ctx context.Context, bundleName string) error {
mw, err := a.store.GetMWByID(ctx, bundleName)
if err != nil {
return err
return fmt.Errorf("get mw by id error: %w", err)
}

fStore, err := GetFileStore()
Expand Down Expand Up @@ -124,10 +125,14 @@ func (a *API) HandleDeleteBundle(bundleName string) error {
return fmt.Errorf("could not remove container '%s': %w", pluginContainerID, errRC)
}

return a.store.DeleteMW(mw.UID)
if err := a.store.DeleteMW(ctx, mw.UID); err != nil {
return fmt.Errorf("delete mw error: %w", err)
}

return nil
}

func (a *API) HandleNewBundle(filePath string, apiID, bundleName string) (*storage.MW, error) {
func (a *API) HandleNewBundle(ctx context.Context, filePath, apiID, bundleName string) (*storage.MW, error) {
// Read the zip file raw data
bData, err := ioutil.ReadFile(filePath)
if err != nil {
Expand Down Expand Up @@ -283,7 +288,7 @@ func (a *API) HandleNewBundle(filePath string, apiID, bundleName string) (*stora
// a.LoadMWIntoDispatcher(mw, bdl.Path)

// store in mongo
_, err = a.store.CreateMW(mw)
_, err = a.store.CreateMW(ctx, mw)
if err != nil {
return mw, err
}
Expand All @@ -303,7 +308,7 @@ func (a *API) HandleNewBundle(filePath string, apiID, bundleName string) (*stora
}

// Will only store the bundle file into our store so we can pull it from a gateway if necessary
func (a *API) StoreBundleOnly(filePath string, apiID, bundleName string) (*storage.MW, error) {
func (a *API) StoreBundleOnly(ctx context.Context, filePath, apiID, bundleName string) (*storage.MW, error) {
// create DB record of the bundle
mw := &storage.MW{
UID: bundleName,
Expand Down Expand Up @@ -358,9 +363,9 @@ func (a *API) StoreBundleOnly(filePath string, apiID, bundleName string) (*stora
log.Info("completed storage")

// store in mongo
_, err = a.store.CreateMW(mw)
_, err = a.store.CreateMW(ctx, mw)
if err != nil {
return mw, err
return mw, fmt.Errorf("create mw error: %w", err)
}

// clean up
Expand All @@ -371,12 +376,22 @@ func (a *API) StoreBundleOnly(filePath string, apiID, bundleName string) (*stora
return mw, nil
}

func (a *API) GetMWByID(id string) (*storage.MW, error) {
return a.store.GetMWByID(id)
func (a *API) GetMWByID(ctx context.Context, id string) (*storage.MW, error) {
mw, err := a.store.GetMWByID(ctx, id)
if err != nil {
return nil, fmt.Errorf("get mw by id error: %w", err)
}

return mw, nil
}

func (a *API) GetAllActiveMW() ([]*storage.MW, error) {
return a.store.GetAllActive()
func (a *API) GetAllActiveMW(ctx context.Context) ([]*storage.MW, error) {
list, err := a.store.GetAllActive(ctx)
if err != nil {
return nil, fmt.Errorf("get all active error: %w", err)
}

return list, nil
}

func (a *API) LoadMWIntoDispatcher(mw *storage.MW, pluginPath string) (*storage.MW, error) {
Expand Down
24 changes: 17 additions & 7 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ require (
github.com/sirupsen/logrus v1.9.3
github.com/spf13/cobra v0.0.5
github.com/spf13/viper v1.6.2
golang.org/x/net v0.0.0-20191009170851-d66e71096ffb
go.mongodb.org/mongo-driver v1.13.0
golang.org/x/net v0.18.0
google.golang.org/grpc v1.21.1
gopkg.in/mgo.v2 v2.0.0-20190816093944-a6b53ec6cb22
)

require (
Expand All @@ -40,6 +40,7 @@ require (
github.com/aws/aws-sdk-go v1.28.11 // indirect
github.com/aws/aws-sdk-go-v2 v0.7.0 // indirect
github.com/clbanning/mxj v1.8.4 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/franela/goblin v0.0.0-20200105215937-c9ffbefa60db // indirect
github.com/franela/goreq v0.0.0-20171204163338-bcd34c9993f8 // indirect
github.com/fsnotify/fsnotify v1.4.7 // indirect
Expand All @@ -48,7 +49,8 @@ require (
github.com/go-openapi/jsonreference v0.19.2 // indirect
github.com/go-openapi/loads v0.19.3 // indirect
github.com/go-openapi/spec v0.19.3 // indirect
github.com/go-stack/stack v1.8.0 // indirect
github.com/golang/snappy v0.0.1 // indirect
github.com/google/uuid v1.4.0 // indirect
github.com/hashicorp/errwrap v1.0.0 // indirect
github.com/hashicorp/go-multierror v1.0.0 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
Expand All @@ -58,6 +60,7 @@ require (
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af // indirect
github.com/kardianos/osext v0.0.0-20190222173326-2bc1f35cddc0 // indirect
github.com/klauspost/compress v1.13.6 // indirect
github.com/lonelycode/go-uuid v0.0.0-20141202165402-ed3ca8a15a93 // indirect
github.com/lonelycode/osin v0.0.0-20160423095202-da239c9dacb6 // indirect
github.com/magiconair/properties v1.8.1 // indirect
Expand All @@ -68,6 +71,7 @@ require (
github.com/mgutz/ansi v0.0.0-20170206155736-9520e82c474b // indirect
github.com/mitchellh/go-wordwrap v1.0.0 // indirect
github.com/mitchellh/mapstructure v1.1.2 // indirect
github.com/montanaflynn/stats v0.0.0-20171201202039-1bf9dbcd8cbe // indirect
github.com/onsi/ginkgo v1.12.0 // indirect
github.com/onsi/gomega v1.9.0 // indirect
github.com/pelletier/go-toml v1.6.0 // indirect
Expand All @@ -79,16 +83,22 @@ require (
github.com/spf13/pflag v1.0.5 // indirect
github.com/subosito/gotenv v1.2.0 // indirect
github.com/x-cray/logrus-prefixed-formatter v0.5.2 // indirect
github.com/xdg-go/pbkdf2 v1.0.0 // indirect
github.com/xdg-go/scram v1.1.2 // indirect
github.com/xdg-go/stringprep v1.0.4 // indirect
github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f // indirect
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 // indirect
github.com/xeipuuv/gojsonschema v1.2.0 // indirect
github.com/youmark/pkcs8 v0.0.0-20181117223130-1be2e3e5546d // indirect
github.com/zclconf/go-cty v1.2.1 // indirect
go.mongodb.org/mongo-driver v1.1.1 // indirect
golang.org/x/crypto v0.0.0-20190701094942-4def268fd1a4 // indirect
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 // indirect
golang.org/x/text v0.3.2 // indirect
golang.org/x/crypto v0.15.0 // indirect
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4 // indirect
golang.org/x/sys v0.14.0 // indirect
golang.org/x/term v0.14.0 // indirect
golang.org/x/text v0.14.0 // indirect
google.golang.org/genproto v0.0.0-20190819201941-24fa4b261c55 // indirect
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect
gopkg.in/ini.v1 v1.52.0 // indirect
gopkg.in/mgo.v2 v2.0.0-20190816093944-a6b53ec6cb22 // indirect
gopkg.in/yaml.v2 v2.2.8 // indirect
)
Loading

0 comments on commit 84624c3

Please sign in to comment.