Skip to content
This repository has been archived by the owner on Apr 12, 2023. It is now read-only.

Commit

Permalink
add update directory openapi spec (#86)
Browse files Browse the repository at this point in the history
Signed-off-by: Mike Mason <mimason@equinix.com>
  • Loading branch information
mikemrm committed Feb 24, 2023
1 parent ae0c631 commit 29e61b7
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 33 deletions.
39 changes: 20 additions & 19 deletions api/v1/openapi.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 10 additions & 7 deletions api/v1/types.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions internal/httpsrv/treemanager/treemanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,8 +250,8 @@ func updateDirectory(s *common.Server) gin.HandlerFunc {
return
}

if req.Name != "" {
d.Name = req.Name
if req.Name != nil && *req.Name != "" {
d.Name = *req.Name
}

if req.Metadata != nil {
Expand Down
24 changes: 19 additions & 5 deletions internal/httpsrv/treemanager/treemanager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ func TestDirectoryOperations(t *testing.T) {
assert.Equal(t, rd.Directory.Id, listrd.Directories[0], "directory is not the same")

d2, err := cli.UpdateDirectory(context.Background(), d.Directory.Id, &apiv1.UpdateDirectoryRequest{
Name: "test2",
Name: ptr("test2"),
Metadata: &apiv1.DirectoryMetadata{
"item1": "value1",
},
Expand All @@ -230,7 +230,7 @@ func TestDirectoryOperations(t *testing.T) {
assert.Contains(t, map[string]string(*d2.Directory.Metadata), "item1", "expected metadata to be updated")

d2, err = cli.UpdateDirectory(context.Background(), d.Directory.Id, &apiv1.UpdateDirectoryRequest{
Name: "",
Name: nil,
Metadata: &apiv1.DirectoryMetadata{
"item2": "value2",
},
Expand All @@ -240,11 +240,21 @@ func TestDirectoryOperations(t *testing.T) {
assert.Contains(t, map[string]string(*d2.Directory.Metadata), "item2", "expected metadata to be updated")

d2, err = cli.UpdateDirectory(context.Background(), d.Directory.Id, &apiv1.UpdateDirectoryRequest{
Name: "test3",
Name: ptr(""),
Metadata: &apiv1.DirectoryMetadata{
"item3": "value3",
},
})
assert.NoError(t, err, "expected no error updating directory")
assert.Equal(t, "test2", d2.Directory.Name, "expected name to be not change")
assert.Contains(t, map[string]string(*d2.Directory.Metadata), "item3", "expected metadata to be updated")

d2, err = cli.UpdateDirectory(context.Background(), d.Directory.Id, &apiv1.UpdateDirectoryRequest{
Name: ptr("test3"),
})
assert.NoError(t, err, "expected no error updating directory")
assert.Equal(t, "test3", d2.Directory.Name, "expected name to be updated")
assert.Contains(t, map[string]string(*d2.Directory.Metadata), "item2", "expected metadata to not be updated")
assert.Contains(t, map[string]string(*d2.Directory.Metadata), "item3", "expected metadata to not be updated")

// Delete directory
affected, err := cli.DeleteDirectory(context.Background(), d.Directory.Id)
Expand Down Expand Up @@ -327,6 +337,10 @@ func (m *mockDriver) UpdateDirectory(ctx context.Context, d *apiv1.Directory) er
return m.DirectoryAdmin.UpdateDirectory(ctx, d)
}

func ptr[T any](v T) *T {
return &v
}

func TestUpdateDirectoryError(t *testing.T) {
t.Parallel()

Expand Down Expand Up @@ -375,7 +389,7 @@ func TestUpdateDirectoryError(t *testing.T) {
assert.NotNil(t, d, "directory is nil")

_, err = cli.UpdateDirectory(context.Background(), d.Directory.Id, &apiv1.UpdateDirectoryRequest{
Name: "test2",
Name: ptr("test2"),
Metadata: &apiv1.DirectoryMetadata{
"item1": "value1",
},
Expand Down
42 changes: 42 additions & 0 deletions treeman-openapi-v1.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,37 @@ paths:
application/json:
schema:
$ref: '#/components/schemas/Error'
patch:
description: Updates the given directory for the provided ID
operationId: updateDirectory
parameters:
- name: id
in: path
description: ID of the directory to update
required: true
schema:
type: string
x-go-type: DirectoryID
requestBody:
description: Fields to update for the directory
required: true
content:
application/json:
schema:
$ref: '#/components/schemas/UpdateDirectoryRequest'
responses:
'201':
description: directory response
content:
application/json:
schema:
$ref: '#/components/schemas/DirectoryFetch'
default:
description: unexpected error
content:
application/json:
schema:
$ref: '#/components/schemas/Error'

# TODO: Implement this
# put:
Expand Down Expand Up @@ -287,6 +318,17 @@ components:
- $ref: '#/components/schemas/DirectoryRequestMeta'
- $ref: '#/components/schemas/NewDirectory'

UpdateDirectoryRequest:
allOf:
- $ref: '#/components/schemas/DirectoryRequestMeta'
- type: object
properties:
name:
type: string
metadata:
type: object
x-go-type: DirectoryMetadata

# Response for fetching directories
DirectoryFetch:
allOf:
Expand Down

0 comments on commit 29e61b7

Please sign in to comment.