Volumes are block storage devices that can be attached to instances as boot or data disks, with support for resizing and type changes.
Methods
New
Create a new volume from scratch, an image, or a snapshot.
func (r *VolumeService) New(ctx context.Context, params VolumeNewParams, opts ...option.RequestOption) (*TaskIDList, error)
func (r *VolumeService) NewAndPoll(ctx context.Context, params VolumeNewParams, opts ...option.RequestOption) (*Volume, error)
Volume source: new-volume, image, or snapshot
Image ID (required if source is image)
Snapshot ID (required if source is snapshot)
Volume type: standard, ssd_hiiops, ssd_local, ssd_lowlatency, cold, ultra. Defaults to standard
Instance ID to attach volume during creation
Block device attachment tag (use with instance_id_to_attach_to)
Snapshot schedule policy IDs
Volume status: available, in-use, creating, deleting, error, etc.
Whether the volume is bootable
List of instance attachments
Update
Rename a volume or update tags.
func (r *VolumeService) Update(ctx context.Context, volumeID string, params VolumeUpdateParams, opts ...option.RequestOption) (*Volume, error)
Tags to add, update, or remove
List
List volumes with optional filtering.
func (r *VolumeService) List(ctx context.Context, params VolumeListParams, opts ...option.RequestOption) (*pagination.OffsetPage[Volume], error)
func (r *VolumeService) ListAutoPaging(ctx context.Context, params VolumeListParams, opts ...option.RequestOption) *pagination.OffsetPageAutoPager[Volume]
Filter by bootable status
Filter by attachment presence
Filter by attached instance ID
Get
Retrieve detailed information about a volume.
func (r *VolumeService) Get(ctx context.Context, volumeID string, query VolumeGetParams, opts ...option.RequestOption) (*Volume, error)
Delete
Delete a volume and optionally its snapshots.
func (r *VolumeService) Delete(ctx context.Context, volumeID string, params VolumeDeleteParams, opts ...option.RequestOption) (*TaskIDList, error)
func (r *VolumeService) DeleteAndPoll(ctx context.Context, volumeID string, params VolumeDeleteParams, opts ...option.RequestOption) error
Comma-separated list of snapshot IDs to delete with the volume
Volume must be in available state to be deleted.
AttachToInstance
Attach a volume to an instance.
func (r *VolumeService) AttachToInstance(ctx context.Context, volumeID string, params VolumeAttachToInstanceParams, opts ...option.RequestOption) (*TaskIDList, error)
func (r *VolumeService) AttachToInstanceAndPoll(ctx context.Context, volumeID string, params VolumeAttachToInstanceParams, opts ...option.RequestOption) error
Block device attachment tag
DetachFromInstance
Detach a volume from an instance.
func (r *VolumeService) DetachFromInstance(ctx context.Context, volumeID string, params VolumeDetachFromInstanceParams, opts ...option.RequestOption) (*TaskIDList, error)
func (r *VolumeService) DetachFromInstanceAndPoll(ctx context.Context, volumeID string, params VolumeDetachFromInstanceParams, opts ...option.RequestOption) error
Resize
Increase the size of a volume.
func (r *VolumeService) Resize(ctx context.Context, volumeID string, params VolumeResizeParams, opts ...option.RequestOption) (*TaskIDList, error)
func (r *VolumeService) ResizeAndPoll(ctx context.Context, volumeID string, params VolumeResizeParams, opts ...option.RequestOption) (*Volume, error)
New size in GiB (must be greater than current size)
ChangeType
Change the volume type.
func (r *VolumeService) ChangeType(ctx context.Context, volumeID string, params VolumeChangeTypeParams, opts ...option.RequestOption) (*Volume, error)
New volume type: standard or ssd_hiiops
Volume must not have snapshots to change type.
RevertToLastSnapshot
Revert a volume to its last snapshot.
func (r *VolumeService) RevertToLastSnapshot(ctx context.Context, volumeID string, body VolumeRevertToLastSnapshotParams, opts ...option.RequestOption) error
Volume must be in available state to be reverted.
Examples
Create an Empty Volume
import (
"context"
"github.com/G-Core/gcore-go"
"github.com/G-Core/gcore-go/cloud"
"github.com/G-Core/gcore-go/shared/constant"
"github.com/G-Core/gcore-go/option"
)
client := gcore.NewClient(
option.WithCloudProjectID(projectID),
option.WithCloudRegionID(regionID),
)
volume, err := client.Cloud.Volumes.NewAndPoll(context.Background(),
cloud.VolumeNewParams{
OfNewVolume: &cloud.VolumeNewParamsBodyNewVolume{
Source: constant.ValueOf[constant.NewVolume](),
Name: "data-volume",
Size: 100, // 100 GB
TypeName: "ssd_hiiops",
Tags: map[string]string{"purpose": "database"},
},
},
)
if err != nil {
log.Fatal(err)
}
fmt.Printf("Created volume: %s\n", volume.ID)
Create Volume from Image
volume, err := client.Cloud.Volumes.NewAndPoll(context.Background(),
cloud.VolumeNewParams{
OfImage: &cloud.VolumeNewParamsBodyImage{
Source: constant.ValueOf[constant.Image](),
Name: "boot-volume",
ImageID: "image-uuid",
Size: 20,
TypeName: "standard",
},
},
)
Create and Attach Volume
volume, err := client.Cloud.Volumes.NewAndPoll(context.Background(),
cloud.VolumeNewParams{
OfNewVolume: &cloud.VolumeNewParamsBodyNewVolume{
Source: constant.ValueOf[constant.NewVolume](),
Name: "attached-volume",
Size: 50,
InstanceIDToAttachTo: gcore.String(instanceID),
},
},
)
List All Volumes
volumes, err := client.Cloud.Volumes.List(context.Background(),
cloud.VolumeListParams{},
)
if err != nil {
log.Fatal(err)
}
for _, vol := range volumes.Results {
fmt.Printf("Volume: %s, Size: %d GB, Status: %s\n",
vol.Name, vol.Size, vol.Status)
}
List Volumes Attached to Instance
volumes, err := client.Cloud.Volumes.List(context.Background(),
cloud.VolumeListParams{
InstanceID: gcore.String(instanceID),
},
)
Attach Volume to Instance
err := client.Cloud.Volumes.AttachToInstanceAndPoll(context.Background(),
volumeID,
cloud.VolumeAttachToInstanceParams{
InstanceID: instanceID,
},
)
if err != nil {
log.Fatal(err)
}
Detach Volume from Instance
err := client.Cloud.Volumes.DetachFromInstanceAndPoll(context.Background(),
volumeID,
cloud.VolumeDetachFromInstanceParams{
InstanceID: instanceID,
},
)
Resize a Volume
volume, err := client.Cloud.Volumes.ResizeAndPoll(context.Background(),
volumeID,
cloud.VolumeResizeParams{
Size: 200, // Expand to 200 GB
},
)
Change Volume Type
volume, err := client.Cloud.Volumes.ChangeType(context.Background(),
volumeID,
cloud.VolumeChangeTypeParams{
VolumeType: cloud.VolumeChangeTypeParamsVolumeTypeSsdHiiops,
},
)
Delete Volume
err := client.Cloud.Volumes.DeleteAndPoll(context.Background(),
volumeID,
cloud.VolumeDeleteParams{},
)
Common Use Cases
Create Database Volume with Snapshots
volume, err := client.Cloud.Volumes.NewAndPoll(context.Background(),
cloud.VolumeNewParams{
OfNewVolume: &cloud.VolumeNewParamsBodyNewVolume{
Source: constant.ValueOf[constant.NewVolume](),
Name: "postgres-data",
Size: 500,
TypeName: "ssd_hiiops",
LifecyclePolicyIDs: []int64{policyID}, // Daily snapshots
Tags: map[string]string{
"database": "postgres",
"environment": "production",
},
},
},
)
Clone Volume from Snapshot
volume, err := client.Cloud.Volumes.NewAndPoll(context.Background(),
cloud.VolumeNewParams{
OfSnapshot: &cloud.VolumeNewParamsBodySnapshot{
Source: constant.ValueOf[constant.Snapshot](),
Name: "cloned-volume",
SnapshotID: snapshotID,
},
},
)