Skip to content

Commit

Permalink
api/v1: Adds component deletion endpoint and client implementation (#166
Browse files Browse the repository at this point in the history
)
  • Loading branch information
joelrebel authored Dec 2, 2022
1 parent f338dc7 commit f120866
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 0 deletions.
1 change: 1 addition & 0 deletions pkg/api/v1/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ func (r *Router) Routes(rg *gin.RouterGroup) {
srvComponents.POST("", amw.RequiredScopes(createScopes("server", "server:component")), r.serverComponentsCreate)
srvComponents.GET("", amw.RequiredScopes(readScopes("server", "server:component")), r.serverComponentGet)
srvComponents.PUT("", amw.RequiredScopes(updateScopes("server", "server:component")), r.serverComponentUpdate)
srvComponents.DELETE("", amw.RequiredScopes(deleteScopes("server", "server:component")), r.serverComponentDelete)
}

// /servers/:uuid/credentials/:slug
Expand Down
17 changes: 17 additions & 0 deletions pkg/api/v1/router_server_components.go
Original file line number Diff line number Diff line change
Expand Up @@ -333,3 +333,20 @@ func (r *Router) serverComponentUpdate(c *gin.Context) {

updatedResponse(c, "")
}

// serverComponentDelete deletes a server component.
func (r *Router) serverComponentDelete(c *gin.Context) {
// load server based on the UUID parameter
server, err := r.loadServerFromParams(c)
if err != nil {
dbErrorResponse(c, err)
return
}

if _, err := server.ServerComponents().DeleteAll(c.Request.Context(), r.DB); err != nil {
dbErrorResponse(c, err)
return
}

deletedResponse(c)
}
64 changes: 64 additions & 0 deletions pkg/api/v1/router_server_components_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -737,3 +737,67 @@ func TestIntegrationServerUpdateComponents(t *testing.T) {
})
}
}

func TestIntegrationServerComponentDelete(t *testing.T) {
s := serverTest(t)

var serverID uuid.UUID

realClientTests(t, func(ctx context.Context, authToken string, respCode int, expectError bool) error {
s.Client.SetToken(authToken)

var err error

_, err = s.Client.DeleteServerComponents(ctx, serverID)
if !expectError {
return nil
}

return err
})

serverID, err := uuid.Parse(dbtools.FixtureNemo.ID)
if err != nil {
t.Fatal(err)
}

var testCases = []struct {
testName string
serverID uuid.UUID
expectedError bool
errorMsg string
expectedResponse string
}{
{
"unknown server UUID returns not found",
uuid.New(),
true,
"",
"resource not found",
},
{
"server components removed",
serverID,
false,
"",
"resource deleted",
},
}

for _, tt := range testCases {
t.Run(tt.testName, func(t *testing.T) {
resp, err := s.Client.DeleteServerComponents(context.TODO(), tt.serverID)

if tt.expectedError {
assert.Error(t, err)
assert.Contains(t, err.Error(), tt.errorMsg)
assert.Contains(t, err.Error(), tt.expectedResponse)
return
}

assert.Nil(t, err)
assert.NotNil(t, resp)
assert.Contains(t, tt.expectedResponse, resp.Message)
})
}
}
6 changes: 6 additions & 0 deletions pkg/api/v1/server_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ type ClientInterface interface {
ListComponents(context.Context, *ServerComponentListParams) ([]ServerComponent, *ServerResponse, error)
CreateComponents(context.Context, uuid.UUID, ServerComponentSlice) (*ServerResponse, error)
UpdateComponents(context.Context, uuid.UUID, ServerComponentSlice) (*ServerResponse, error)
DeleteServerComponents(context.Context, uuid.UUID) (*ServerResponse, error)
CreateVersionedAttributes(context.Context, uuid.UUID, VersionedAttributes) (*ServerResponse, error)
GetVersionedAttributes(context.Context, uuid.UUID, string) ([]VersionedAttributes, *ServerResponse, error)
ListVersionedAttributes(context.Context, uuid.UUID) ([]VersionedAttributes, *ServerResponse, error)
Expand Down Expand Up @@ -188,6 +189,11 @@ func (c *Client) UpdateComponents(ctx context.Context, srvUUID uuid.UUID, compon
return c.put(ctx, path, components)
}

// DeleteServerComponents will delete components for the given server identifier.
func (c *Client) DeleteServerComponents(ctx context.Context, srvUUID uuid.UUID) (*ServerResponse, error) {
return c.delete(ctx, fmt.Sprintf("%s/%s/%s", serversEndpoint, srvUUID, serverComponentsEndpoint))
}

// CreateVersionedAttributes will create a new versioned attribute for a given server
func (c *Client) CreateVersionedAttributes(ctx context.Context, srvUUID uuid.UUID, va VersionedAttributes) (*ServerResponse, error) {
path := fmt.Sprintf("%s/%s/%s", serversEndpoint, srvUUID, serverVersionedAttributesEndpoint)
Expand Down

0 comments on commit f120866

Please sign in to comment.