Skip to content

Commit

Permalink
feat(dashboards): support dashboards service in SDK (#311)
Browse files Browse the repository at this point in the history
* init commit

* fix tests

* typos
  • Loading branch information
ecrupper authored May 7, 2024
1 parent 64f8eb0 commit 54c8961
Show file tree
Hide file tree
Showing 6 changed files with 234 additions and 6 deletions.
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ require (
github.com/buildkite/yaml v0.0.0-20230306222819-0e4e032d4835
github.com/coreos/go-semver v0.3.1
github.com/gin-gonic/gin v1.9.1
github.com/go-vela/server v0.23.4-0.20240424144436-b55aa2bb3684
github.com/go-vela/types v0.23.4-0.20240405205548-f24f795ac0b7
github.com/go-vela/server v0.23.4-0.20240506154118-8ad123451469
github.com/go-vela/types v0.23.4-0.20240417135026-fb4a95c30338
github.com/golang-jwt/jwt/v5 v5.2.1
github.com/google/go-cmp v0.6.0
github.com/google/go-querystring v1.1.0
Expand Down
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJn
github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY=
github.com/go-playground/validator/v10 v10.14.0 h1:vgvQWe3XCz3gIeFDm/HnTIbj6UGmg/+t63MyGU2n5js=
github.com/go-playground/validator/v10 v10.14.0/go.mod h1:9iXMNT7sEkjXb0I+enO7QXmzG6QCsPWY4zveKFVRSyU=
github.com/go-vela/server v0.23.4-0.20240424144436-b55aa2bb3684 h1:O0gfupNx7aYPCCrXwVuisrbbqbVkvrRqRBAkSg1bIww=
github.com/go-vela/server v0.23.4-0.20240424144436-b55aa2bb3684/go.mod h1:QV9JFv+LdpAgkRJhHE92dh4vVdh0kNv8OJnyOLt++84=
github.com/go-vela/types v0.23.4-0.20240405205548-f24f795ac0b7 h1:3mN7ej69dMH3Vis3G/tPLzLL0Rfp8nR5qd0gpj5ejRM=
github.com/go-vela/types v0.23.4-0.20240405205548-f24f795ac0b7/go.mod h1:mEF9dLkk00rUXf/t39n2WvXZgJbxnPEEWy+DHqIlRUo=
github.com/go-vela/server v0.23.4-0.20240506154118-8ad123451469 h1:t+g5Gqdhrv7E7XGAD3ZaoxQ1hko/lKA7N1Az0dcXrgI=
github.com/go-vela/server v0.23.4-0.20240506154118-8ad123451469/go.mod h1:2KcNYX+DfNH/aWwu0pu89Gj9+wAm3S70VMy53/LMZjs=
github.com/go-vela/types v0.23.4-0.20240417135026-fb4a95c30338 h1:I0v47dOdAvjX7lOFN4s28uONChmluD6TNgFL1hpav60=
github.com/go-vela/types v0.23.4-0.20240417135026-fb4a95c30338/go.mod h1:vISsYDdjz9RPEK6qZ+MxtrdZEjTVU4K30NomB3826u8=
github.com/goccy/go-json v0.10.2 h1:CrxCmQqYDkv1z7lO7Wbh2HN93uovUHgrECaO5ZrCXAU=
github.com/goccy/go-json v0.10.2/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I=
github.com/golang-jwt/jwt/v5 v5.2.1 h1:OuVbFODueb089Lh128TAcimifWaLhJwVflnrgM17wHk=
Expand Down
2 changes: 2 additions & 0 deletions vela/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ type (
Authentication *AuthenticationService
Authorization *AuthorizationService
Build *BuildService
Dashboard *DashboardService
Deployment *DeploymentService
Hook *HookService
Log *LogService
Expand Down Expand Up @@ -138,6 +139,7 @@ func NewClient(baseURL, id string, httpClient *http.Client) (*Client, error) {
&AdminWorkerService{client: c},
}
c.Build = &BuildService{client: c}
c.Dashboard = &DashboardService{client: c}
c.Deployment = &DeploymentService{client: c}
c.Hook = &HookService{client: c}
c.Log = &LogService{client: c}
Expand Down
1 change: 1 addition & 0 deletions vela/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ func TestVela_NewClient(t *testing.T) {
&AdminWorkerService{client: want},
}
want.Build = &BuildService{client: want}
want.Dashboard = &DashboardService{client: want}
want.Deployment = &DeploymentService{client: want}
want.Hook = &HookService{client: want}
want.Log = &LogService{client: want}
Expand Down
69 changes: 69 additions & 0 deletions vela/dashboard.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// SPDX-License-Identifier: Apache-2.0

package vela

import (
"fmt"

api "github.com/go-vela/server/api/types"
)

// DashboardService handles retrieving Dashboards from
// the server methods of the Vela API.
type DashboardService service

// Get returns the provided Dashboard.
func (svc *DashboardService) Get(dashboard string) (*api.DashCard, *Response, error) {
// set the API endpoint path we send the request to
u := fmt.Sprintf("/api/v1/dashboards/%s", dashboard)

// API Dashboard type we want to return
v := new(api.DashCard)

// send request using client
resp, err := svc.client.Call("GET", u, nil, v)

return v, resp, err
}

// GetAllUser returns a list of all dashboards for the authenticated user.
func (svc *DashboardService) GetAllUser() (*[]api.DashCard, *Response, error) {
// set the API endpoint path we send the request to
u := "/api/v1/user/dashboards"

// slice library Dashboard type we want to return
v := new([]api.DashCard)

// send request using client
resp, err := svc.client.Call("GET", u, nil, v)

return v, resp, err
}

// Add constructs a Dashboard with the provided details.
func (svc *DashboardService) Add(d *api.Dashboard) (*api.Dashboard, *Response, error) {
// set the API endpoint path we send the request to
u := "/api/v1/dashboards"

// api dashboard type we want to return
v := new(api.Dashboard)

// send request using client
resp, err := svc.client.Call("POST", u, d, v)

return v, resp, err
}

// Update modifies a dashboard with the provided details.
func (svc *DashboardService) Update(d *api.Dashboard) (*api.Dashboard, *Response, error) {
// set the API endpoint path we send the request to
u := fmt.Sprintf("/api/v1/dashboards/%s", d.GetID())

// API dashboard type we want to return
v := new(api.Dashboard)

// send request using client
resp, err := svc.client.Call("PUT", u, d, v)

return v, resp, err
}
156 changes: 156 additions & 0 deletions vela/dashboard_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
// SPDX-License-Identifier: Apache-2.0

package vela

import (
"encoding/json"
"net/http"
"net/http/httptest"
"reflect"
"testing"

"github.com/gin-gonic/gin"

api "github.com/go-vela/server/api/types"
"github.com/go-vela/server/mock/server"
)

func TestDashboard_Get_200(t *testing.T) {
// setup context
gin.SetMode(gin.TestMode)

s := httptest.NewServer(server.FakeHandler())
c, _ := NewClient(s.URL, "", nil)

data := []byte(server.DashCardResp)

var want api.DashCard
err := json.Unmarshal(data, &want)
if err != nil {
t.Errorf("unable to unmarshal data: %v", err)
}

// run test
got, resp, err := c.Dashboard.Get("c976470d-34c1-49b2-9a98-1035871c576b")

if err != nil {
t.Errorf("New returned err: %v", err)
}

if resp.StatusCode != http.StatusOK {
t.Errorf("Get returned %v, want %v", resp.StatusCode, http.StatusOK)
}

if !reflect.DeepEqual(got, &want) {
t.Errorf("Get is %v, want %v", got, want)
}
}

func TestDashboard_Get_404(t *testing.T) {
// setup context
gin.SetMode(gin.TestMode)

s := httptest.NewServer(server.FakeHandler())
c, _ := NewClient(s.URL, "", nil)

want := api.DashCard{}

// run test
got, resp, err := c.Dashboard.Get("0")

if err == nil {
t.Errorf("Get returned err: %v", err)
}

if resp.StatusCode != http.StatusNotFound {
t.Errorf("Get returned %v, want %v", resp.StatusCode, http.StatusOK)
}

if !reflect.DeepEqual(got, &want) {
t.Errorf("Get is %v, want %v", got, want)
}
}

func TestDashboard_GetAllUser_200(t *testing.T) {
// setup context
gin.SetMode(gin.TestMode)

s := httptest.NewServer(server.FakeHandler())
c, _ := NewClient(s.URL, "", nil)

data := []byte(server.DashCardsResp)

var want []api.DashCard
_ = json.Unmarshal(data, &want)

// run test
got, resp, err := c.Dashboard.GetAllUser()

if err != nil {
t.Errorf("GetAllUser returned err: %v", err)
}

if resp.StatusCode != http.StatusOK {
t.Errorf("GetAllUser returned %v, want %v", resp.StatusCode, http.StatusOK)
}

if !reflect.DeepEqual(got, &want) {
t.Errorf("GetAllUser is %v, want %v", got, want)
}
}

func TestDashboard_Add_201(t *testing.T) {
// setup context
gin.SetMode(gin.TestMode)

s := httptest.NewServer(server.FakeHandler())
c, _ := NewClient(s.URL, "", nil)

data := []byte(server.DashboardResp)

var want api.Dashboard
_ = json.Unmarshal(data, &want)

// run test
got, resp, err := c.Dashboard.Add(&want)

if err != nil {
t.Errorf("Add returned err: %v", err)
}

if resp.StatusCode != http.StatusCreated {
t.Errorf("Add returned %v, want %v", resp.StatusCode, http.StatusOK)
}

if !reflect.DeepEqual(got, &want) {
t.Errorf("Add is %v, want %v", got, want)
}
}

func TestDashboard_Update_200(t *testing.T) {
// setup context
gin.SetMode(gin.TestMode)

s := httptest.NewServer(server.FakeHandler())
c, _ := NewClient(s.URL, "", nil)

data := []byte(server.DashboardResp)

var want api.Dashboard
_ = json.Unmarshal(data, &want)

// run test
got, resp, err := c.Dashboard.Update(&want)

if err != nil {
t.Errorf("Update returned err: %v", err)
}

if resp.StatusCode != http.StatusOK {
t.Errorf("Update returned %v, want %v", resp.StatusCode, http.StatusOK)
}

if !reflect.DeepEqual(got, &want) {
t.Errorf("Update is %v, want %v", got, want)
}
}

0 comments on commit 54c8961

Please sign in to comment.