Skip to content

Commit

Permalink
fix: expands on get and list, and improve type struct (#49)
Browse files Browse the repository at this point in the history
  • Loading branch information
geffersonFerraz authored Feb 7, 2025
1 parent 42c1021 commit 1ca5e55
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 6 deletions.
26 changes: 21 additions & 5 deletions blockstorage/volumes.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"net/http"
"net/url"
"strconv"
"strings"
"time"

mgc_http "github.com/MagaluCloud/mgc-sdk-go/internal/http"
Expand All @@ -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"`
Expand All @@ -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"`
Expand Down Expand Up @@ -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](
Expand Down Expand Up @@ -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](
Expand All @@ -239,7 +255,7 @@ func (s *volumeService) Get(ctx context.Context, id string, expand []string) (*V
http.MethodGet,
path,
nil,
nil,
query,
)
}

Expand Down
2 changes: 1 addition & 1 deletion blockstorage/volumes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
Expand Down
17 changes: 17 additions & 0 deletions cmd/examples/blockstorage/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down

0 comments on commit 1ca5e55

Please sign in to comment.