Skip to content

Commit

Permalink
Merge pull request #4 from b1zzu/improve-apply
Browse files Browse the repository at this point in the history
refactor: move apply logic outside the services
  • Loading branch information
b1zzu authored Feb 4, 2022
2 parents a85b133 + f5a8490 commit a297359
Show file tree
Hide file tree
Showing 7 changed files with 226 additions and 294 deletions.
66 changes: 7 additions & 59 deletions pkg/rpdac/dashboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"encoding/hex"
"fmt"
"io"
"log"
"sort"
"strings"

Expand All @@ -14,18 +13,6 @@ import (
"github.com/google/go-cmp/cmp/cmpopts"
)

type IDashboardService interface {
Get(project string, id int) (Object, error)
Create(project string, o Object) error
Apply(project string, o Object) error

GetDashboard(project string, id int) (*Dashboard, error)
GetDashboardByName(project, name string) (*Dashboard, error)
CreateDashboard(project string, d *Dashboard) error
ApplyDashboard(project string, d *Dashboard) error
DeleteDashboard(project, name string) error
}

type DashboardService service

type Dashboard struct {
Expand Down Expand Up @@ -66,18 +53,6 @@ type WidgetContentParameters struct {
}

func (s *DashboardService) Get(project string, id int) (Object, error) {
return s.GetDashboard(project, id)
}

func (s *DashboardService) Create(project string, o Object) error {
return s.CreateDashboard(project, o.(*Dashboard))
}

func (s *DashboardService) Apply(project string, o Object) error {
return s.ApplyDashboard(project, o.(*Dashboard))
}

func (s *DashboardService) GetDashboard(project string, id int) (*Dashboard, error) {

// retireve the dashboard defintion
d, _, err := s.client.Dashboard.GetByID(project, id)
Expand All @@ -88,7 +63,7 @@ func (s *DashboardService) GetDashboard(project string, id int) (*Dashboard, err
return s.loadDashboard(project, d)
}

func (s *DashboardService) GetDashboardByName(project, name string) (*Dashboard, error) {
func (s *DashboardService) GetByName(project, name string) (Object, error) {

d, _, err := s.client.Dashboard.GetByName(project, name)
if err != nil {
Expand Down Expand Up @@ -129,7 +104,8 @@ func (s *DashboardService) loadDashboard(project string, d *reportportal.Dashboa
return ToDashboard(d, widgets), nil
}

func (s *DashboardService) CreateDashboard(project string, d *Dashboard) error {
func (s *DashboardService) Create(project string, o Object) error {
d := o.(*Dashboard)

filtersMap, err := s.filtersMap(project, d.Widgets)
if err != nil {
Expand Down Expand Up @@ -185,36 +161,8 @@ func (s *DashboardService) createWidgets(
return nil
}

// Create or Recreate the dashboard
func (s *DashboardService) ApplyDashboard(project string, d *Dashboard) error {

currentDashboard, err := s.GetDashboardByName(project, d.Name)
if err != nil {
return fmt.Errorf("error retrieving Dashboard with name '%s': %w", d.Name, err)
}

if currentDashboard != nil {

if currentDashboard.Equals(d) {
log.Printf("Skip apply for Dashboard with name '%s' in project '%s'", d.Name, project)
return nil
}

if err = s.updateDashboard(project, currentDashboard, d); err != nil {
return err
}
log.Printf("Dashboard with name '%s' updated in project '%s'", d.Name, project)
return nil
}

if err = s.CreateDashboard(project, d); err != nil {
return err
}
log.Printf("Dashboard with name '%s' created in project '%s'", d.Name, project)
return nil
}

func (s *DashboardService) updateDashboard(project string, currentDashboard, targetDashboard *Dashboard) error {
func (s *DashboardService) Update(project string, current, target Object) error {
currentDashboard, targetDashboard := current.(*Dashboard), target.(*Dashboard)

// resolve all filters
filtersMap, err := s.filtersMap(project, targetDashboard.Widgets)
Expand Down Expand Up @@ -247,7 +195,7 @@ func (s *DashboardService) updateDashboard(project string, currentDashboard, tar
}

// Delete the Dashboard with the given name and Widgets created for it
func (s *DashboardService) DeleteDashboard(project, name string) error {
func (s *DashboardService) Delete(project, name string) error {

d, _, err := s.client.Dashboard.GetByName(project, name)
if err != nil {
Expand Down Expand Up @@ -434,7 +382,7 @@ func (d *Dashboard) GetKind() ObjectKind {
}

// Compare the two Dashboards ignoring slices order
func (left *Dashboard) Equals(right *Dashboard) bool {
func (left *Dashboard) Equals(right Object) bool {

opts := cmp.Options{
cmpopts.IgnoreUnexported(Dashboard{}, Widget{}),
Expand Down
28 changes: 14 additions & 14 deletions pkg/rpdac/dashboard_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,9 +198,9 @@ func TestGetDashboard(t *testing.T) {
Widget: mockWidget,
ProjectSettings: mockProjectSettings})

got, err := r.Dashboard.GetDashboard("test_project", 1)
got, err := r.Dashboard.Get("test_project", 1)
if err != nil {
t.Errorf("ReportPortal.GetDashboard returned error: %v", err)
t.Errorf("ReportPortal.Get returned error: %v", err)
}

want := &Dashboard{
Expand Down Expand Up @@ -458,9 +458,9 @@ func TestGetDashboardByName(t *testing.T) {
Widget: mockWidget,
ProjectSettings: mockProjectSettings})

got, err := r.Dashboard.GetDashboardByName("test_project", "MK E2E Tests Overview")
got, err := r.Dashboard.GetByName("test_project", "MK E2E Tests Overview")
if err != nil {
t.Errorf("ReportPortal.GetDashboardByName returned error: %v", err)
t.Errorf("ReportPortal.GetByName returned error: %v", err)
}

want := &Dashboard{
Expand Down Expand Up @@ -541,9 +541,9 @@ func TestGetDashboardByName_NotFound(t *testing.T) {

r := NewReportPortal(&reportportal.Client{Dashboard: mockDashboard})

got, err := r.Dashboard.GetDashboardByName("test_project", "MK E2E Tests Overview")
got, err := r.Dashboard.GetByName("test_project", "MK E2E Tests Overview")
if err != nil {
t.Errorf("ReportPortal.GetDashboardByName returned error: %v", err)
t.Errorf("ReportPortal.GetByName returned error: %v", err)
}

if got != nil {
Expand All @@ -563,9 +563,9 @@ func TestGetDashboardByName_Error(t *testing.T) {

r := NewReportPortal(&reportportal.Client{Dashboard: mockDashboard})

_, err := r.Dashboard.GetDashboardByName("test_project", "MK E2E Tests Overview")
_, err := r.Dashboard.GetByName("test_project", "MK E2E Tests Overview")
if err == nil {
t.Errorf("ReportPortal.GetDashboardByName did not return the error")
t.Errorf("ReportPortal.GetByName did not return the error")
}
}

Expand Down Expand Up @@ -807,9 +807,9 @@ func TestCreateDashboard(t *testing.T) {
},
}

err := r.Dashboard.CreateDashboard("test_project", inputDashboard)
err := r.Dashboard.Create("test_project", inputDashboard)
if err != nil {
t.Errorf("ReportPortal.CreateDashboard returned error: %v", err)
t.Errorf("ReportPortal.Create returned error: %v", err)
}

testDeepEqual(t, mockDashboard.Counter, reportportal.MockDashboardServiceCounter{Create: 1, AddWidget: 2})
Expand Down Expand Up @@ -900,7 +900,7 @@ func TestApplyDashboard_Create(t *testing.T) {
},
}

err := r.Dashboard.ApplyDashboard("test_project", inputDashboard)
err := r.ApplyObject("test_project", inputDashboard)
if err != nil {
t.Errorf("ReportPortal.ApplyDashboard returned error: %v", err)
}
Expand Down Expand Up @@ -1082,7 +1082,7 @@ func TestApplyDashboard_Update(t *testing.T) {
},
}

err := r.Dashboard.ApplyDashboard("test_project", inputDashboard)
err := r.ApplyObject("test_project", inputDashboard)
if err != nil {
t.Errorf("ReportPortal.ApplyDashboard returned error: %v", err)
}
Expand Down Expand Up @@ -1313,7 +1313,7 @@ func TestApplyDashboard_Skip(t *testing.T) {
},
}

err := r.Dashboard.ApplyDashboard("test_project", inputDashboard)
err := r.ApplyObject("test_project", inputDashboard)
if err != nil {
t.Errorf("ReportPortal.ApplyDashboard returned error: %v", err)
}
Expand Down Expand Up @@ -1363,7 +1363,7 @@ func TestDeleteDashboard(t *testing.T) {
Dashboard: mockDashboard,
})

err := r.Dashboard.DeleteDashboard("test_project", "MK E2E Tests Overview")
err := r.Dashboard.Delete("test_project", "MK E2E Tests Overview")
if err != nil {
t.Errorf("ReportPortal.DeleteDashboard returned error: %v", err)
}
Expand Down
59 changes: 11 additions & 48 deletions pkg/rpdac/filter.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package rpdac

import (
"errors"
"fmt"
"log"
"sort"
Expand All @@ -11,17 +12,6 @@ import (
"gopkg.in/yaml.v2"
)

type IFilterService interface {
Get(project string, id int) (Object, error)
Create(project string, o Object) error
Apply(project string, o Object) error

GetFilter(project string, id int) (*Filter, error)
GetFilterByName(project, name string) (*Filter, error)
CreateFilter(project string, f *Filter) error
ApplyFilter(project string, f *Filter) error
}

type FilterService service

type Filter struct {
Expand All @@ -47,18 +37,6 @@ type FilterOrder struct {
}

func (s *FilterService) Get(project string, id int) (Object, error) {
return s.GetFilter(project, id)
}

func (s *FilterService) Create(project string, o Object) error {
return s.CreateFilter(project, o.(*Filter))
}

func (s *FilterService) Apply(project string, o Object) error {
return s.ApplyFilter(project, o.(*Filter))
}

func (s *FilterService) GetFilter(project string, id int) (*Filter, error) {

// retireve the filter defintion
f, _, err := s.client.Filter.GetByID(project, id)
Expand All @@ -69,7 +47,7 @@ func (s *FilterService) GetFilter(project string, id int) (*Filter, error) {
return ToFilter(f), nil
}

func (s *FilterService) GetFilterByName(project, name string) (*Filter, error) {
func (s *FilterService) GetByName(project, name string) (Object, error) {

f, _, err := s.client.Filter.GetByName(project, name)
if err != nil {
Expand All @@ -83,7 +61,8 @@ func (s *FilterService) GetFilterByName(project, name string) (*Filter, error) {
return ToFilter(f), nil
}

func (s *FilterService) CreateFilter(project string, f *Filter) error {
func (s *FilterService) Create(project string, o Object) error {
f := o.(*Filter)

filterID, _, err := s.client.Filter.Create(project, FilterToNewFilter(f))
if err != nil {
Expand All @@ -94,28 +73,8 @@ func (s *FilterService) CreateFilter(project string, f *Filter) error {
return nil
}

func (s *FilterService) ApplyFilter(project string, f *Filter) error {

currentFilter, err := s.GetFilterByName(project, f.Name)
if err != nil {
return fmt.Errorf("error retrieving filter \"%s\" by name: %w", f.Name, err)
}

if currentFilter != nil {

if currentFilter.Equals(f) {
log.Printf("skip apply for filter \"%s\"", f.Name)
return nil
}

return s.updateFilter(project, currentFilter, f)
}

return s.CreateFilter(project, f)

}

func (s *FilterService) updateFilter(project string, currentFilter, targetFilter *Filter) error {
func (s *FilterService) Update(project string, current, target Object) error {
currentFilter, targetFilter := current.(*Filter), target.(*Filter)

_, _, err := s.client.Filter.Update(project, currentFilter.origin.ID, FilterToUpdateFilter(targetFilter))
if err != nil {
Expand All @@ -126,6 +85,10 @@ func (s *FilterService) updateFilter(project string, currentFilter, targetFilter
return nil
}

func (s *FilterService) Delete(project, name string) error {
return errors.New("unimplemented")
}

func ToFilter(f *reportportal.Filter) *Filter {

conditions := make([]FilterCondition, len(f.Conditions))
Expand Down Expand Up @@ -211,7 +174,7 @@ func (f *Filter) GetKind() ObjectKind {
return f.Kind
}

func (left *Filter) Equals(right *Filter) bool {
func (left *Filter) Equals(right Object) bool {
opts := cmp.Options{
cmpopts.IgnoreUnexported(Filter{}),

Expand Down
Loading

0 comments on commit a297359

Please sign in to comment.