diff --git a/blockstorage/volumes.go b/blockstorage/volumes.go index 6b3df43..2620403 100644 --- a/blockstorage/volumes.go +++ b/blockstorage/volumes.go @@ -6,7 +6,6 @@ import ( "net/http" "net/url" "strconv" - "strings" "time" mgc_http "github.com/MagaluCloud/mgc-sdk-go/internal/http" @@ -21,6 +20,18 @@ type ( ListVolumesResponse struct { Volumes []Volume `json:"volumes"` } + Iops struct { + Read int `json:"read"` + Write int `json:"write"` + Total int `json:"total"` + } + Type struct { + Iops *Iops `json:"iops,omitempty"` + ID string `json:"id"` + Name *string `json:"name,omitempty"` + DiskType *string `json:"disk_type,omitempty"` + Status *string `json:"status,omitempty"` + } Volume struct { ID string `json:"id"` @@ -30,7 +41,7 @@ type ( State string `json:"state"` CreatedAt time.Time `json:"created_at"` UpdatedAt time.Time `json:"updated_at"` - Type IDOrName `json:"type"` + Type Type `json:"type"` Error *VolumeError `json:"error,omitempty"` Attachment *VolumeAttachment `json:"attachment,omitempty"` AvailabilityZone string `json:"availability_zone"` @@ -190,7 +201,9 @@ func (s *volumeService) List(ctx context.Context, opts ListOptions) ([]Volume, e query.Set("_sort", *opts.Sort) } if len(opts.Expand) > 0 { - query.Set("expand", strings.Join(opts.Expand, ",")) + for _, expand := range opts.Expand { + query.Add("expand", expand) + } } result, err := mgc_http.ExecuteSimpleRequestWithRespBody[ListVolumesResponse]( @@ -228,8 +241,11 @@ func (s *volumeService) Create(ctx context.Context, req CreateVolumeRequest) (st // Get retrieves a specific volume func (s *volumeService) Get(ctx context.Context, id string, expand []string) (*Volume, error) { path := fmt.Sprintf("/v1/volumes/%s", id) + query := make(url.Values) if len(expand) > 0 { - path = fmt.Sprintf("%s?expand=%s", path, strings.Join(expand, ",")) + for _, expand := range expand { + query.Add("expand", expand) + } } return mgc_http.ExecuteSimpleRequestWithRespBody[Volume]( @@ -239,7 +255,7 @@ func (s *volumeService) Get(ctx context.Context, id string, expand []string) (*V http.MethodGet, path, nil, - nil, + query, ) } diff --git a/blockstorage/volumes_test.go b/blockstorage/volumes_test.go index b504e18..3895923 100644 --- a/blockstorage/volumes_test.go +++ b/blockstorage/volumes_test.go @@ -232,7 +232,7 @@ func TestVolumeService_Get(t *testing.T) { want: &Volume{ ID: "vol1", Status: "completed", - Type: IDOrName{ID: helpers.StrPtr("type1")}, + Type: Type{ID: "type1"}, }, wantErr: false, }, diff --git a/cmd/examples/blockstorage/main.go b/cmd/examples/blockstorage/main.go index c2d9467..cdbd0cf 100644 --- a/cmd/examples/blockstorage/main.go +++ b/cmd/examples/blockstorage/main.go @@ -24,11 +24,28 @@ func main() { ExampleListVolumeTypes() ExampleListVolumes() id := ExampleCreateVolume() + ExampleGetVolume(id) ExampleManageVolume(id) ExampleVolumeAttachments(id) ExampleDeleteVolume(id) } +func ExampleGetVolume(id string) { + apiToken := os.Getenv("MGC_API_TOKEN") + if apiToken == "" { + log.Fatal("MGC_API_TOKEN environment variable is not set") + } + c := client.NewMgcClient(apiToken) + blockClient := blockstorage.New(c) + + volume, err := blockClient.Volumes().Get(context.Background(), id, []string{blockstorage.VolumeTypeExpand, blockstorage.VolumeAttachExpand}) + if err != nil { + log.Fatal(err) + } + + fmt.Printf("Volume: %s (ID: %s)\n", volume.Name, volume.ID) +} + func ExampleListVolumes() { // Create a new client apiToken := os.Getenv("MGC_API_TOKEN")