or run

tessl search
Log in

Version

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
golangpkg:golang/cloud.google.com/go/compute@v1.53.0

docs

clients

disks.mdinstances.mdload-balancing.mdnetworks.mdoperations.mdother-clients.mdregional.mdsecurity.mdstorage.md
index.mdmetadata.mdtypes.md
tile.json

tessl/golang-cloud-google-com--go--compute

tessl install tessl/golang-cloud-google-com--go--compute@1.53.0

Go client library for Google Cloud Compute Engine API providing programmatic access to manage virtual machines, disks, networks, and other compute resources

disks.mddocs/clients/

Disks Client

The DisksClient manages persistent disks in Google Compute Engine. It provides operations for creating, resizing, snapshotting, and managing disk lifecycle, including advanced features like async replication and resource policies.

Client Creation

func NewDisksRESTClient(ctx context.Context, opts ...option.ClientOption) (*DisksClient, error)

Creates a new DisksClient using REST transport.

Example:

import (
    "context"
    compute "cloud.google.com/go/compute/apiv1"
)

ctx := context.Background()
client, err := compute.NewDisksRESTClient(ctx)
if err != nil {
    // handle error
}
defer client.Close()

Client Methods

Common Methods

// Close closes the client connection
func (c *DisksClient) Close() error

// Connection returns the gRPC connection (deprecated)
func (c *DisksClient) Connection() *grpc.ClientConn

Read Operations

// Get retrieves a specific disk
func (c *DisksClient) Get(ctx context.Context, req *computepb.GetDiskRequest, opts ...gax.CallOption) (*computepb.Disk, error)

// List returns an iterator over disks in a zone
func (c *DisksClient) List(ctx context.Context, req *computepb.ListDisksRequest, opts ...gax.CallOption) *DiskIterator

// AggregatedList returns an iterator over disks across all zones
func (c *DisksClient) AggregatedList(ctx context.Context, req *computepb.AggregatedListDisksRequest, opts ...gax.CallOption) *DisksScopedListPairIterator

Example - Get Disk:

import "cloud.google.com/go/compute/apiv1/computepb"

req := &computepb.GetDiskRequest{
    Project: "my-project",
    Zone:    "us-central1-a",
    Disk:    "my-disk",
}
disk, err := client.Get(ctx, req)
if err != nil {
    // handle error
}
fmt.Printf("Disk: %s, Size: %d GB, Status: %s\n",
    *disk.Name, *disk.SizeGb, *disk.Status)

Example - List Disks:

import "google.golang.org/api/iterator"

listReq := &computepb.ListDisksRequest{
    Project: "my-project",
    Zone:    "us-central1-a",
}
it := client.List(ctx, listReq)
for {
    disk, err := it.Next()
    if err == iterator.Done {
        break
    }
    if err != nil {
        // handle error
    }
    fmt.Printf("Disk: %s, Size: %d GB\n", *disk.Name, *disk.SizeGb)
}

Disk Lifecycle

// Insert creates a new persistent disk
func (c *DisksClient) Insert(ctx context.Context, req *computepb.InsertDiskRequest, opts ...gax.CallOption) (*Operation, error)

// Delete deletes a persistent disk
func (c *DisksClient) Delete(ctx context.Context, req *computepb.DeleteDiskRequest, opts ...gax.CallOption) (*Operation, error)

// Update updates a disk (full replacement)
func (c *DisksClient) Update(ctx context.Context, req *computepb.UpdateDiskRequest, opts ...gax.CallOption) (*Operation, error)

// Resize changes the size of a disk
func (c *DisksClient) Resize(ctx context.Context, req *computepb.ResizeDiskRequest, opts ...gax.CallOption) (*Operation, error)

Example - Create Disk:

import "google.golang.org/protobuf/proto"

insertReq := &computepb.InsertDiskRequest{
    Project: "my-project",
    Zone:    "us-central1-a",
    DiskResource: &computepb.Disk{
        Name:   proto.String("new-disk"),
        SizeGb: proto.Int64(100),
        Type:   proto.String("zones/us-central1-a/diskTypes/pd-standard"),
    },
}
op, err := client.Insert(ctx, insertReq)
if err != nil {
    // handle error
}
err = op.Wait(ctx)

Example - Create Disk from Snapshot:

insertReq := &computepb.InsertDiskRequest{
    Project: "my-project",
    Zone:    "us-central1-a",
    DiskResource: &computepb.Disk{
        Name:           proto.String("restored-disk"),
        SourceSnapshot: proto.String("projects/my-project/global/snapshots/my-snapshot"),
    },
}
op, err := client.Insert(ctx, insertReq)

Example - Resize Disk:

resizeReq := &computepb.ResizeDiskRequest{
    Project: "my-project",
    Zone:    "us-central1-a",
    Disk:    "my-disk",
    DisksResizeRequestResource: &computepb.DisksResizeRequest{
        SizeGb: proto.Int64(200),
    },
}
op, err := client.Resize(ctx, resizeReq)
if err != nil {
    // handle error
}
err = op.Wait(ctx)

Snapshot Operations

// CreateSnapshot creates a snapshot of a disk
func (c *DisksClient) CreateSnapshot(ctx context.Context, req *computepb.CreateSnapshotDiskRequest, opts ...gax.CallOption) (*Operation, error)

Example - Create Snapshot:

snapshotReq := &computepb.CreateSnapshotDiskRequest{
    Project: "my-project",
    Zone:    "us-central1-a",
    Disk:    "my-disk",
    SnapshotResource: &computepb.Snapshot{
        Name:        proto.String("my-snapshot"),
        Description: proto.String("Backup before update"),
    },
}
op, err := client.CreateSnapshot(ctx, snapshotReq)
if err != nil {
    // handle error
}
err = op.Wait(ctx)

Resource Policy Management

// AddResourcePolicies adds resource policies (e.g., snapshot schedules) to a disk
func (c *DisksClient) AddResourcePolicies(ctx context.Context, req *computepb.AddResourcePoliciesDiskRequest, opts ...gax.CallOption) (*Operation, error)

// RemoveResourcePolicies removes resource policies from a disk
func (c *DisksClient) RemoveResourcePolicies(ctx context.Context, req *computepb.RemoveResourcePoliciesDiskRequest, opts ...gax.CallOption) (*Operation, error)

Example - Add Snapshot Schedule:

addPolicyReq := &computepb.AddResourcePoliciesDiskRequest{
    Project: "my-project",
    Zone:    "us-central1-a",
    Disk:    "my-disk",
    DisksAddResourcePoliciesRequestResource: &computepb.DisksAddResourcePoliciesRequest{
        ResourcePolicies: []string{
            "projects/my-project/regions/us-central1/resourcePolicies/daily-snapshot",
        },
    },
}
op, err := client.AddResourcePolicies(ctx, addPolicyReq)

Async Replication

// StartAsyncReplication starts asynchronous replication for a disk
func (c *DisksClient) StartAsyncReplication(ctx context.Context, req *computepb.StartAsyncReplicationDiskRequest, opts ...gax.CallOption) (*Operation, error)

// StopAsyncReplication stops asynchronous replication for a disk
func (c *DisksClient) StopAsyncReplication(ctx context.Context, req *computepb.StopAsyncReplicationDiskRequest, opts ...gax.CallOption) (*Operation, error)

// StopGroupAsyncReplication stops replication for a group of disks
func (c *DisksClient) StopGroupAsyncReplication(ctx context.Context, req *computepb.StopGroupAsyncReplicationDiskRequest, opts ...gax.CallOption) (*Operation, error)

Label Management

// SetLabels sets or updates resource labels on a disk
func (c *DisksClient) SetLabels(ctx context.Context, req *computepb.SetLabelsDiskRequest, opts ...gax.CallOption) (*Operation, error)

// BulkSetLabels sets labels on multiple disks at once
func (c *DisksClient) BulkSetLabels(ctx context.Context, req *computepb.BulkSetLabelsDiskRequest, opts ...gax.CallOption) (*Operation, error)

Example - Set Labels:

setLabelsReq := &computepb.SetLabelsDiskRequest{
    Project:  "my-project",
    Zone:     "us-central1-a",
    Resource: "my-disk",
    ZoneSetLabelsRequestResource: &computepb.ZoneSetLabelsRequest{
        Labels: map[string]string{
            "environment": "production",
            "backup":      "enabled",
        },
        LabelFingerprint: disk.LabelFingerprint,
    },
}
op, err := client.SetLabels(ctx, setLabelsReq)

Bulk Operations

// BulkInsert creates multiple disks in a single operation
func (c *DisksClient) BulkInsert(ctx context.Context, req *computepb.BulkInsertDiskRequest, opts ...gax.CallOption) (*Operation, error)

IAM Operations

// GetIamPolicy retrieves the IAM policy for a disk
func (c *DisksClient) GetIamPolicy(ctx context.Context, req *computepb.GetIamPolicyDiskRequest, opts ...gax.CallOption) (*computepb.Policy, error)

// SetIamPolicy sets the IAM policy for a disk
func (c *DisksClient) SetIamPolicy(ctx context.Context, req *computepb.SetIamPolicyDiskRequest, opts ...gax.CallOption) (*computepb.Policy, error)

// TestIamPermissions tests IAM permissions for a disk
func (c *DisksClient) TestIamPermissions(ctx context.Context, req *computepb.TestIamPermissionsDiskRequest, opts ...gax.CallOption) (*computepb.TestPermissionsResponse, error)

Call Options

// DisksCallOptions contains options for configuring retry and timeout behavior
type DisksCallOptions struct {
    AddResourcePolicies        []gax.CallOption
    AggregatedList             []gax.CallOption
    BulkInsert                 []gax.CallOption
    CreateSnapshot             []gax.CallOption
    Delete                     []gax.CallOption
    Get                        []gax.CallOption
    GetIamPolicy               []gax.CallOption
    Insert                     []gax.CallOption
    List                       []gax.CallOption
    RemoveResourcePolicies     []gax.CallOption
    Resize                     []gax.CallOption
    SetIamPolicy               []gax.CallOption
    SetLabels                  []gax.CallOption
    StartAsyncReplication      []gax.CallOption
    StopAsyncReplication       []gax.CallOption
    StopGroupAsyncReplication  []gax.CallOption
    TestIamPermissions         []gax.CallOption
    Update                     []gax.CallOption
}

Iterator Types

// DiskIterator manages iteration over Disk results
type DiskIterator struct {
    // Next returns the next result or iterator.Done
    Next() (*computepb.Disk, error)

    // PageInfo supports pagination
    PageInfo() *iterator.PageInfo
}

// DisksScopedListPairIterator iterates over disks grouped by scope
type DisksScopedListPairIterator struct {
    // Next returns the next result or iterator.Done
    Next() (DisksScopedListPair, error)

    // PageInfo supports pagination
    PageInfo() *iterator.PageInfo
}

// DisksScopedListPair represents a scope (zone) and its disks
type DisksScopedListPair struct {
    Key   string
    Value *computepb.DisksScopedList
}

Key Request Types

// GetDiskRequest retrieves a single disk
type GetDiskRequest struct {
    Disk    string  // Disk name
    Project string  // Project ID
    Zone    string  // Zone name
}

// ListDisksRequest lists disks in a zone
type ListDisksRequest struct {
    Filter     *string  // Optional filter expression
    MaxResults *uint32  // Maximum results per page
    OrderBy    *string  // Sort order
    PageToken  *string  // Pagination token
    Project    string   // Project ID
    Zone       string   // Zone name
}

// InsertDiskRequest creates a new disk
type InsertDiskRequest struct {
    DiskResource      *Disk    // Disk specification
    Project           string   // Project ID
    RequestId         *string  // Optional idempotency token
    SourceImage       *string  // Optional source image
    Zone              string   // Zone name
}

// ResizeDiskRequest resizes a disk
type ResizeDiskRequest struct {
    Disk                       string               // Disk name
    DisksResizeRequestResource *DisksResizeRequest  // Resize parameters
    Project                    string               // Project ID
    RequestId                  *string              // Optional idempotency token
    Zone                       string               // Zone name
}

// DisksResizeRequest specifies new disk size
type DisksResizeRequest struct {
    SizeGb *int64  // New size in GB
}

// CreateSnapshotDiskRequest creates a disk snapshot
type CreateSnapshotDiskRequest struct {
    Disk             string     // Disk name
    GuestFlush       *bool      // Flush filesystem before snapshot
    Project          string     // Project ID
    RequestId        *string    // Optional idempotency token
    SnapshotResource *Snapshot  // Snapshot specification
    Zone             string     // Zone name
}

Related Clients

For regional persistent disks, use the RegionDisksClient:

regionalClient, err := compute.NewRegionDisksRESTClient(ctx)

For disk type information (pd-standard, pd-ssd, etc.), use DiskTypesClient:

typesClient, err := compute.NewDiskTypesRESTClient(ctx)

See Regional Clients Documentation for more information.