Skip to content

Commit

Permalink
Add meta to the filter for the volume list method (#51)
Browse files Browse the repository at this point in the history
* Add the "Meta" field to the "Volume" struct for additional metadata.

---------

Co-authored-by: hrybun <[email protected]>
  • Loading branch information
hrybun and hrybun authored Nov 27, 2023
1 parent cb1d1d5 commit 2d8806f
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 12 deletions.
31 changes: 20 additions & 11 deletions ah/volumes.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"context"
"fmt"
"net/http"
"strings"
)

// Volume object
Expand Down Expand Up @@ -66,7 +67,7 @@ type volumeActionsRoot struct {

// VolumesAPI is an interface for volumes.
type VolumesAPI interface {
List(context.Context, *ListOptions) ([]Volume, *Meta, error)
List(context.Context, map[string]string) ([]Volume, *Meta, error)
Get(context.Context, string) (*Volume, error)
Create(context.Context, *VolumeCreateRequest) (*Volume, error)
Update(context.Context, string, *VolumeUpdateRequest) (*Volume, error)
Expand All @@ -88,12 +89,20 @@ type volumesRoot struct {
}

// List returns all available private networks
func (vs *VolumesService) List(ctx context.Context, options *ListOptions) ([]Volume, *Meta, error) {
func (vs *VolumesService) List(ctx context.Context, filters map[string]string) ([]Volume, *Meta, error) {
path := "api/v1/volumes"
if filters != nil {
var query []string
for filterName, filterVal := range filters {
query = append(query, fmt.Sprintf("%s=%s", filterName, filterVal))
}
params := strings.Join(query, "&")
path = fmt.Sprintf("%s?%s", path, params)
}

var vsRoot volumesRoot

if err := vs.client.list(ctx, path, options, &vsRoot); err != nil {
if err := vs.client.list(ctx, path, nil, &vsRoot); err != nil {
return nil, nil, err
}

Expand Down Expand Up @@ -125,17 +134,17 @@ func (vs *VolumesService) Get(ctx context.Context, volumeID string) (*Volume, er

// VolumeCreateRequest object
type VolumeCreateRequest struct {
Name string `json:"name"`
Name string `json:"name"`
Meta map[string]interface{} `json:"meta,omitempty"`
// Deprecated: Please use PlanID instead.
ProductID string `json:"product_id,omitempty"`
// Deprecated: Please use PlanSlug instead.
Meta map[string]interface{} `json:"meta,omitempty"`
ProductSlug string `json:"product_slug,omitempty"`
PlanSlug string `json:"plan_slug,omitempty"`
FileSystem string `json:"file_system,omitempty"`
InstanceID string `json:"instance_id,omitempty"`
Size int `json:"size"`
PlanID int `json:"plan_id,omitempty"`
ProductSlug string `json:"product_slug,omitempty"`
PlanSlug string `json:"plan_slug,omitempty"`
FileSystem string `json:"file_system,omitempty"`
InstanceID string `json:"instance_id,omitempty"`
Size int `json:"size"`
PlanID int `json:"plan_id,omitempty"`
}

// Create volume
Expand Down
6 changes: 5 additions & 1 deletion ah/volumes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,11 @@ func TestVolumes_List(t *testing.T) {
api, _ := NewAPIClient(fakeClientOptions)

ctx := context.Background()
volumes, meta, err := api.Volumes.List(ctx, nil)
filters := map[string]string{
"meta": "{\"kubernetes\":{\"cluster\":{\"id\":\"service-id\"}}}",
}

volumes, meta, err := api.Volumes.List(ctx, filters)
if err != nil {
t.Errorf("Unexpected error: %v", err)
}
Expand Down

0 comments on commit 2d8806f

Please sign in to comment.