-
Notifications
You must be signed in to change notification settings - Fork 157
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
**What this PR does / why we need it**: This reverts commit 6c2b0bd. **Which issue(s) this PR fixes**: Rel #1792 **Does this PR introduce a user-facing change?**: <!-- If no, just write "NONE" in the release-note block below. --> ```release-note NONE ``` This PR was merged by Kapetanios.
- Loading branch information
1 parent
5a1e38a
commit d3396f3
Showing
11 changed files
with
837 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
load("@io_bazel_rules_go//go:def.bzl", "go_library") | ||
|
||
go_library( | ||
name = "go_default_library", | ||
srcs = [ | ||
"iterator.go", | ||
"model_wrapper.go", | ||
"mongodb.go", | ||
"operator.go", | ||
], | ||
importpath = "github.com/pipe-cd/pipe/pkg/datastore/mongodb", | ||
visibility = ["//visibility:public"], | ||
deps = [ | ||
"//pkg/datastore:go_default_library", | ||
"//pkg/model:go_default_library", | ||
"@org_mongodb_go_mongo_driver//bson:go_default_library", | ||
"@org_mongodb_go_mongo_driver//mongo:go_default_library", | ||
"@org_mongodb_go_mongo_driver//mongo/options:go_default_library", | ||
"@org_uber_go_zap//:go_default_library", | ||
], | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
// Copyright 2020 The PipeCD Authors. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package mongodb | ||
|
||
import ( | ||
"context" | ||
|
||
"go.mongodb.org/mongo-driver/mongo" | ||
|
||
"github.com/pipe-cd/pipe/pkg/datastore" | ||
) | ||
|
||
type Iterator struct { | ||
ctx context.Context | ||
cur *mongo.Cursor | ||
} | ||
|
||
func (it *Iterator) Next(dst interface{}) error { | ||
if !it.cur.Next(it.ctx) { | ||
return datastore.ErrIteratorDone | ||
} | ||
wrapper, err := wrapModel(dst) | ||
if err != nil { | ||
return err | ||
} | ||
if err := it.cur.Decode(wrapper); err != nil { | ||
return err | ||
} | ||
return extractModel(wrapper, dst) | ||
} | ||
|
||
func (it *Iterator) Cursor() (string, error) { | ||
// Note: Perhaps, not required. | ||
return "", datastore.ErrUnimplemented | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,193 @@ | ||
// Copyright 2020 The PipeCD Authors. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package mongodb | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/pipe-cd/pipe/pkg/model" | ||
) | ||
|
||
// wrapModel returns a wrapper corresponding to the given entity. | ||
// A wrapper wraps a model representing BSON a document so that the model comes with "_id". | ||
func wrapModel(entity interface{}) (interface{}, error) { | ||
switch e := entity.(type) { | ||
case *model.Application: | ||
if e == nil { | ||
return nil, fmt.Errorf("nil entity given") | ||
} | ||
return &application{ | ||
ID: e.GetId(), | ||
Application: *e, | ||
}, nil | ||
case *model.Command: | ||
if e == nil { | ||
return nil, fmt.Errorf("nil entity given") | ||
} | ||
return &command{ | ||
ID: e.GetId(), | ||
Command: *e, | ||
}, nil | ||
case *model.Deployment: | ||
if e == nil { | ||
return nil, fmt.Errorf("nil entity given") | ||
} | ||
return &deployment{ | ||
ID: e.GetId(), | ||
Deployment: *e, | ||
}, nil | ||
case *model.Environment: | ||
if e == nil { | ||
return nil, fmt.Errorf("nil entity given") | ||
} | ||
return &environment{ | ||
ID: e.GetId(), | ||
Environment: *e, | ||
}, nil | ||
case *model.Piped: | ||
if e == nil { | ||
return nil, fmt.Errorf("nil entity given") | ||
} | ||
return &piped{ | ||
ID: e.GetId(), | ||
Piped: *e, | ||
}, nil | ||
case *model.Project: | ||
if e == nil { | ||
return nil, fmt.Errorf("nil entity given") | ||
} | ||
return &project{ | ||
ID: e.GetId(), | ||
Project: *e, | ||
}, nil | ||
case *model.APIKey: | ||
if e == nil { | ||
return nil, fmt.Errorf("nil entity given") | ||
} | ||
return &apiKey{ | ||
ID: e.GetId(), | ||
APIKey: *e, | ||
}, nil | ||
case *model.Event: | ||
if e == nil { | ||
return nil, fmt.Errorf("nil entity given") | ||
} | ||
return &event{ | ||
ID: e.GetId(), | ||
Event: *e, | ||
}, nil | ||
default: | ||
return nil, fmt.Errorf("%T is not supported", e) | ||
} | ||
} | ||
|
||
// extractModel stores the unwrapped model in the value pointed to by e. | ||
func extractModel(wrapper interface{}, e interface{}) error { | ||
msg := "entity type doesn't correspond to the wrapper type (%T)" | ||
|
||
switch w := wrapper.(type) { | ||
case *application: | ||
e, ok := e.(*model.Application) | ||
if !ok { | ||
return fmt.Errorf(msg, w) | ||
} | ||
*e = w.Application | ||
case *command: | ||
e, ok := e.(*model.Command) | ||
if !ok { | ||
return fmt.Errorf(msg, w) | ||
} | ||
*e = w.Command | ||
case *deployment: | ||
e, ok := e.(*model.Deployment) | ||
if !ok { | ||
return fmt.Errorf(msg, w) | ||
} | ||
*e = w.Deployment | ||
case *environment: | ||
e, ok := e.(*model.Environment) | ||
if !ok { | ||
return fmt.Errorf(msg, w) | ||
} | ||
*e = w.Environment | ||
case *piped: | ||
e, ok := e.(*model.Piped) | ||
if !ok { | ||
return fmt.Errorf(msg, w) | ||
} | ||
*e = w.Piped | ||
case *project: | ||
e, ok := e.(*model.Project) | ||
if !ok { | ||
return fmt.Errorf(msg, w) | ||
} | ||
*e = w.Project | ||
case *apiKey: | ||
e, ok := e.(*model.APIKey) | ||
if !ok { | ||
return fmt.Errorf(msg, w) | ||
} | ||
*e = w.APIKey | ||
case *event: | ||
e, ok := e.(*model.Event) | ||
if !ok { | ||
return fmt.Errorf(msg, w) | ||
} | ||
*e = w.Event | ||
default: | ||
return fmt.Errorf("%T is not supported", w) | ||
} | ||
return nil | ||
} | ||
|
||
type application struct { | ||
model.Application `bson:",inline"` | ||
ID string `bson:"_id"` | ||
} | ||
|
||
type command struct { | ||
model.Command `bson:",inline"` | ||
ID string `bson:"_id"` | ||
} | ||
|
||
type deployment struct { | ||
model.Deployment `bson:",inline"` | ||
ID string `bson:"_id"` | ||
} | ||
|
||
type environment struct { | ||
model.Environment `bson:",inline"` | ||
ID string `bson:"_id"` | ||
} | ||
|
||
type piped struct { | ||
model.Piped `bson:",inline"` | ||
ID string `bson:"_id"` | ||
} | ||
|
||
type project struct { | ||
model.Project `bson:",inline"` | ||
ID string `bson:"_id"` | ||
} | ||
|
||
type apiKey struct { | ||
model.APIKey `bson:",inline"` | ||
ID string `bson:"_id"` | ||
} | ||
|
||
type event struct { | ||
model.Event `bson:",inline"` | ||
ID string `bson:"_id"` | ||
} |
Oops, something went wrong.