aboutsummaryrefslogtreecommitdiffstats
path: root/src/orchestrator/api
diff options
context:
space:
mode:
authorRitu Sood <ritu.sood@intel.com>2020-01-31 23:29:51 -0800
committerSrivahni Chivukula <srivahni.chivukula@intel.com>2020-02-14 03:54:11 -0800
commita75d489bbf87712371d67dce0753577bdacce0c3 (patch)
treedee646769aa432c814abd86645af612e714640be /src/orchestrator/api
parentc06be6458e9985bd7ac0b25fab03d9c8605f6c4a (diff)
Restructure code and create module library
Restructures and moves code to make it aligned with the current design. https://wiki.onap.org/display/DW/Multi+Cluster+Application+Scheduler examples/example_module.go shows how to import and use modules from this package. Patch#2 Updated example Issue-ID: MULTICLOUD-871 Signed-off-by: Ritu Sood <ritu.sood@intel.com> Change-Id: Ia1e9802a946a07dcca8f79f0e2250933ab3efa66
Diffstat (limited to 'src/orchestrator/api')
-rw-r--r--src/orchestrator/api/api.go19
-rw-r--r--src/orchestrator/api/projecthandler.go12
-rw-r--r--src/orchestrator/api/projecthandler_test.go38
3 files changed, 34 insertions, 35 deletions
diff --git a/src/orchestrator/api/api.go b/src/orchestrator/api/api.go
index 83f17bbe..e37b158a 100644
--- a/src/orchestrator/api/api.go
+++ b/src/orchestrator/api/api.go
@@ -10,29 +10,28 @@ 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 api
import (
- "github.com/onap/multicloud-k8s/src/orchestrator/internal/project"
+ moduleLib "github.com/onap/multicloud-k8s/src/orchestrator/pkg/module"
"github.com/gorilla/mux"
)
-
+var moduleClient *moduleLib.Client
// NewRouter creates a router that registers the various urls that are supported
-func NewRouter(projectClient project.ProjectManager) *mux.Router {
+func NewRouter(projectClient moduleLib.ProjectManager) *mux.Router {
router := mux.NewRouter().PathPrefix("/v2").Subrouter()
-
+ moduleClient = moduleLib.NewClient()
if projectClient == nil {
- projectClient = project.NewProjectClient()
+ projectClient = moduleClient.Project
}
projHandler := projectHandler{
client: projectClient,
}
- router.HandleFunc("/project", projHandler.createHandler).Methods("POST")
- router.HandleFunc("/project/{project-name}", projHandler.getHandler).Methods("GET")
- router.HandleFunc("/project/{project-name}", projHandler.deleteHandler).Methods("DELETE")
+ router.HandleFunc("/projects", projHandler.createHandler).Methods("POST")
+ router.HandleFunc("/projects/{project-name}", projHandler.getHandler).Methods("GET")
+ router.HandleFunc("/projects/{project-name}", projHandler.deleteHandler).Methods("DELETE")
return router
-}
+} \ No newline at end of file
diff --git a/src/orchestrator/api/projecthandler.go b/src/orchestrator/api/projecthandler.go
index 30f21de3..1830b91d 100644
--- a/src/orchestrator/api/projecthandler.go
+++ b/src/orchestrator/api/projecthandler.go
@@ -21,7 +21,7 @@ import (
"io"
"net/http"
- "github.com/onap/multicloud-k8s/src/orchestrator/internal/project"
+ moduleLib "github.com/onap/multicloud-k8s/src/orchestrator/pkg/module"
"github.com/gorilla/mux"
)
@@ -31,12 +31,12 @@ import (
type projectHandler struct {
// Interface that implements Project operations
// We will set this variable with a mock interface for testing
- client project.ProjectManager
+ client moduleLib.ProjectManager
}
// Create handles creation of the Project entry in the database
func (h projectHandler) createHandler(w http.ResponseWriter, r *http.Request) {
- var p project.Project
+ var p moduleLib.Project
err := json.NewDecoder(r.Body).Decode(&p)
switch {
@@ -54,7 +54,7 @@ func (h projectHandler) createHandler(w http.ResponseWriter, r *http.Request) {
return
}
- ret, err := h.client.Create(p)
+ ret, err := h.client.CreateProject(p)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
@@ -75,7 +75,7 @@ func (h projectHandler) getHandler(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
name := vars["project-name"]
- ret, err := h.client.Get(name)
+ ret, err := h.client.GetProject(name)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
@@ -95,7 +95,7 @@ func (h projectHandler) deleteHandler(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
name := vars["project-name"]
- err := h.client.Delete(name)
+ err := h.client.DeleteProject(name)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
diff --git a/src/orchestrator/api/projecthandler_test.go b/src/orchestrator/api/projecthandler_test.go
index 2699f2e3..41f515d0 100644
--- a/src/orchestrator/api/projecthandler_test.go
+++ b/src/orchestrator/api/projecthandler_test.go
@@ -25,7 +25,7 @@ import (
"reflect"
"testing"
- "github.com/onap/multicloud-k8s/src/orchestrator/internal/project"
+ moduleLib "github.com/onap/multicloud-k8s/src/orchestrator/pkg/module"
pkgerrors "github.com/pkg/errors"
)
@@ -36,27 +36,27 @@ import (
type mockProjectManager struct {
// Items and err will be used to customize each test
// via a localized instantiation of mockProjectManager
- Items []project.Project
+ Items []moduleLib.Project
Err error
}
-func (m *mockProjectManager) Create(inp project.Project) (project.Project, error) {
+func (m *mockProjectManager) CreateProject(inp moduleLib.Project) (moduleLib.Project, error) {
if m.Err != nil {
- return project.Project{}, m.Err
+ return moduleLib.Project{}, m.Err
}
return m.Items[0], nil
}
-func (m *mockProjectManager) Get(name string) (project.Project, error) {
+func (m *mockProjectManager) GetProject(name string) (moduleLib.Project, error) {
if m.Err != nil {
- return project.Project{}, m.Err
+ return moduleLib.Project{}, m.Err
}
return m.Items[0], nil
}
-func (m *mockProjectManager) Delete(name string) error {
+func (m *mockProjectManager) DeleteProject(name string) error {
return m.Err
}
@@ -64,7 +64,7 @@ func TestProjectCreateHandler(t *testing.T) {
testCases := []struct {
label string
reader io.Reader
- expected project.Project
+ expected moduleLib.Project
expectedCode int
projectClient *mockProjectManager
}{
@@ -80,13 +80,13 @@ func TestProjectCreateHandler(t *testing.T) {
"project-name":"testProject",
"description":"Test Project used for unit testing"
}`)),
- expected: project.Project{
+ expected: moduleLib.Project{
ProjectName: "testProject",
Description: "Test Project used for unit testing",
},
projectClient: &mockProjectManager{
//Items that will be returned by the mocked Client
- Items: []project.Project{
+ Items: []moduleLib.Project{
{
ProjectName: "testProject",
Description: "Test Project used for unit testing",
@@ -106,7 +106,7 @@ func TestProjectCreateHandler(t *testing.T) {
for _, testCase := range testCases {
t.Run(testCase.label, func(t *testing.T) {
- request := httptest.NewRequest("POST", "/v2/project", testCase.reader)
+ request := httptest.NewRequest("POST", "/v2/projects", testCase.reader)
resp := executeRequest(request, NewRouter(testCase.projectClient))
//Check returned code
@@ -116,7 +116,7 @@ func TestProjectCreateHandler(t *testing.T) {
//Check returned body only if statusCreated
if resp.StatusCode == http.StatusCreated {
- got := project.Project{}
+ got := moduleLib.Project{}
json.NewDecoder(resp.Body).Decode(&got)
if reflect.DeepEqual(testCase.expected, got) == false {
@@ -132,7 +132,7 @@ func TestProjectGetHandler(t *testing.T) {
testCases := []struct {
label string
- expected project.Project
+ expected moduleLib.Project
name, version string
expectedCode int
projectClient *mockProjectManager
@@ -140,13 +140,13 @@ func TestProjectGetHandler(t *testing.T) {
{
label: "Get Project",
expectedCode: http.StatusOK,
- expected: project.Project{
+ expected: moduleLib.Project{
ProjectName: "testProject",
Description: "A Test project for unit testing",
},
name: "testProject",
projectClient: &mockProjectManager{
- Items: []project.Project{
+ Items: []moduleLib.Project{
{
ProjectName: "testProject",
Description: "A Test project for unit testing",
@@ -159,7 +159,7 @@ func TestProjectGetHandler(t *testing.T) {
expectedCode: http.StatusInternalServerError,
name: "nonexistingproject",
projectClient: &mockProjectManager{
- Items: []project.Project{},
+ Items: []moduleLib.Project{},
Err: pkgerrors.New("Internal Error"),
},
},
@@ -167,7 +167,7 @@ func TestProjectGetHandler(t *testing.T) {
for _, testCase := range testCases {
t.Run(testCase.label, func(t *testing.T) {
- request := httptest.NewRequest("GET", "/v2/project/"+testCase.name, nil)
+ request := httptest.NewRequest("GET", "/v2/projects/"+testCase.name, nil)
resp := executeRequest(request, NewRouter(testCase.projectClient))
//Check returned code
@@ -177,7 +177,7 @@ func TestProjectGetHandler(t *testing.T) {
//Check returned body only if statusOK
if resp.StatusCode == http.StatusOK {
- got := project.Project{}
+ got := moduleLib.Project{}
json.NewDecoder(resp.Body).Decode(&got)
if reflect.DeepEqual(testCase.expected, got) == false {
@@ -216,7 +216,7 @@ func TestProjectDeleteHandler(t *testing.T) {
for _, testCase := range testCases {
t.Run(testCase.label, func(t *testing.T) {
- request := httptest.NewRequest("DELETE", "/v2/project/"+testCase.name, nil)
+ request := httptest.NewRequest("DELETE", "/v2/projects/"+testCase.name, nil)
resp := executeRequest(request, NewRouter(testCase.projectClient))
//Check returned code