Search code examples
c#google-cloud-platformsnapshotgoogle-cloud-compute-engine

Creating regional snapshots with GCP .Net client library


I'm working on GCP through their .Net client library Google.Cloud.Compute.V1.

I'm creating new Snapshots with SnapshotsClient.InsertAsync command using the example provided in the official docs:

https://cloud.google.com/dotnet/docs/reference/Google.Cloud.Compute.V1/latest/Google.Cloud.Compute.V1.SnapshotsClient#Google_Cloud_Compute_V1_SnapshotsClient_InsertAsync_System_String_Google_Cloud_Compute_V1_Snapshot_Google_Api_Gax_Grpc_CallSettings_

SnapshotsClient snapshotsClient = await SnapshotsClient.CreateAsync();
Snapshot snapshotResource = new Snapshot()
{
   Name = "#SNAPSHOT_NAME#",
   SourceDisk = "#SOURCE_DISK#",
};
var response = await snapshotsClient.InsertAsync("#PROJET_ID#", snapshotResource);
var completedResponse = await response.PollUntilCompletedAsync();
Operation result = completedResponse.Result;

and it works well. My problem is that it creates multi-regional snapshots and puts them in US, whereas what I want is to create regional snapshots (for example under us-central1).

The only relevant property that I see in the Snapshot model is StorageLocations, and the docs don't state that it is [Output Only] like they do for other properties, however when I try to assign a new value for it I get:

Property or indexer 'Snapshot.StorageLocations' cannot be assigned to -- it is read only

Can you please help me understand how to create a regional snapshot with this library?


Solution

  • It can take an array of strings, You can use create it using this example:

    Snapshot snapshotResource = new()
    {
        Name = GetNameForNewSnapshot(sourceDisk, zone),
        SourceDisk = sourceDisk,
        StorageLocations = { "us-west1", "us-central1" }
    };