Skip to content

Commit

Permalink
feat(admin): add clean endpoint to sdk (#241)
Browse files Browse the repository at this point in the history
* feat(admin): add clean endpoint to sdk

* tidy
  • Loading branch information
ecrupper authored Jun 8, 2023
1 parent 5f95f6d commit 2b87f24
Show file tree
Hide file tree
Showing 6 changed files with 101 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.0
github.com/go-vela/server v0.19.3-0.20230522192918-46fcc273b3aa
github.com/go-vela/types v0.19.3-0.20230519215217-0da8c8b5e90f
github.com/go-vela/server v0.19.3-0.20230608164526-470791d51178
github.com/go-vela/types v0.19.3-0.20230523200921-35a0d5fc088c
github.com/golang-jwt/jwt/v5 v5.0.0
github.com/google/go-cmp v0.5.9
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 @@ -26,10 +26,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.11.2 h1:q3SHpufmypg+erIExEKUmsgmhDTyhcJ38oeKGACXohU=
github.com/go-playground/validator/v10 v10.11.2/go.mod h1:NieE624vt4SCTJtD87arVLvdmjPAeV8BQlHtMnw9D7s=
github.com/go-vela/server v0.19.3-0.20230522192918-46fcc273b3aa h1:hx23gVdmDlMG/yU1rbyGF5+NFgLElw2XWhRhiGkaEd0=
github.com/go-vela/server v0.19.3-0.20230522192918-46fcc273b3aa/go.mod h1:sjfqYOyuXG7uqgeQF03URz7GGNjVE+JNIhkGK00eT9o=
github.com/go-vela/types v0.19.3-0.20230519215217-0da8c8b5e90f h1:13H381Djx9iFC3BSj2f/ac57HlaI3mQL0el9vM7a3+k=
github.com/go-vela/types v0.19.3-0.20230519215217-0da8c8b5e90f/go.mod h1:0lsuPfGyVyTWJSi2h3NS6uaEW6DgnFvIzaZu1sXYKrs=
github.com/go-vela/server v0.19.3-0.20230608164526-470791d51178 h1:B8ElmNWhG7T6+eBOQ/JUqFj+TsJ89e6DXWxU/unTMtI=
github.com/go-vela/server v0.19.3-0.20230608164526-470791d51178/go.mod h1:b520o4N7ss4kHATH291Ui1LHwuC0qEgJgg/Jab6yPIQ=
github.com/go-vela/types v0.19.3-0.20230523200921-35a0d5fc088c h1:eAApIK5e5MxFF8RzZAFsvTSdwq/AzdUrdhJHOGQ0ILc=
github.com/go-vela/types v0.19.3-0.20230523200921-35a0d5fc088c/go.mod h1:0lsuPfGyVyTWJSi2h3NS6uaEW6DgnFvIzaZu1sXYKrs=
github.com/goccy/go-json v0.10.0 h1:mXKd9Qw4NuzShiRlOXKews24ufknHO7gx30lsDyokKA=
github.com/goccy/go-json v0.10.0/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I=
github.com/golang-jwt/jwt/v5 v5.0.0 h1:1n1XNM9hk7O9mnQoNBGolZvzebBQ7p93ULHRc28XJUE=
Expand Down
30 changes: 30 additions & 0 deletions vela/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"fmt"
"strings"

"github.com/go-vela/types"
"github.com/go-vela/types/library"
)

Expand All @@ -17,6 +18,7 @@ type (
// the server methods of the Vela API.
AdminService struct {
Build *AdminBuildService
Clean *AdminCleanService
Deployment *AdminDeploymentService
Hook *AdminHookService
Repo *AdminRepoService
Expand All @@ -31,6 +33,10 @@ type (
// the server methods of the Vela API.
AdminBuildService service

// AdminCleanService handles cleaning resources using
// the server methods of the Vela API.
AdminCleanService service

// AdminDeploymentService handles retrieving admin deployments from
// the server methods of the Vela API.
AdminDeploymentService service
Expand Down Expand Up @@ -75,6 +81,12 @@ type GetQueueOptions struct {
ListOptions
}

// CleanOptions specifies the optional parameters to the
// Clean.Clean method.
type CleanOptions struct {
Before int64 `url:"before,omitempty"`
}

// Update modifies a build with the provided details.
func (svc *AdminBuildService) Update(b *library.Build) (*library.Build, *Response, error) {
// set the API endpoint path we send the request to
Expand All @@ -89,6 +101,24 @@ func (svc *AdminBuildService) Update(b *library.Build) (*library.Build, *Respons
return v, resp, err
}

// Clean sets build resources older than a specified time to a proper canceled / finished state with the provided message.
func (svc *AdminCleanService) Clean(e *types.Error, opt *CleanOptions) (*string, *Response, error) {
// set the API endpoint path we send the request to
u := "/api/v1/admin/clean"

// add optional arguments if supplied
u, err := addOptions(u, opt)
if err != nil {
return nil, nil, err
}

v := new(string)

resp, err := svc.client.Call("PUT", u, e, v)

return v, resp, err
}

// GetQueue returns the list of builds in pending and running status.
func (svc *AdminBuildService) GetQueue(opt *GetQueueOptions) (*[]library.BuildQueue, *Response, error) {
// set the API endpoint path we send the request to
Expand Down
63 changes: 63 additions & 0 deletions vela/admin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (

"github.com/gin-gonic/gin"
"github.com/go-vela/server/mock/server"
"github.com/go-vela/types"
"github.com/go-vela/types/library"
)

Expand Down Expand Up @@ -53,6 +54,68 @@ func TestAdmin_Build_Update_200(t *testing.T) {
}
}

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

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

want := server.CleanResourcesResp

req := types.Error{
Message: String("msg"),
}

// run test
got, resp, err := c.Admin.Clean.Clean(&req, nil)

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

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

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

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

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

req := types.Error{
Message: String("msg"),
}

opt500 := CleanOptions{
Before: 1,
}

opt401 := CleanOptions{
Before: 2,
}

// run tests
_, resp, _ := c.Admin.Clean.Clean(&req, &opt500)

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

_, resp, _ = c.Admin.Clean.Clean(&req, &opt401)

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

func TestAdmin_Deployment_Update_200(t *testing.T) {
// setup context
gin.SetMode(gin.TestMode)
Expand Down
1 change: 1 addition & 0 deletions vela/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ func NewClient(baseURL, id string, httpClient *http.Client) (*Client, error) {
c.Authorization = &AuthorizationService{client: c}
c.Admin = &AdminService{
&AdminBuildService{client: c},
&AdminCleanService{client: c},
&AdminDeploymentService{client: c},
&AdminHookService{client: c},
&AdminRepoService{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 @@ -38,6 +38,7 @@ func TestVela_NewClient(t *testing.T) {
want.Authorization = &AuthorizationService{client: want}
want.Admin = &AdminService{
&AdminBuildService{client: want},
&AdminCleanService{client: want},
&AdminDeploymentService{client: want},
&AdminHookService{client: want},
&AdminRepoService{client: want},
Expand Down

0 comments on commit 2b87f24

Please sign in to comment.