-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
create_snapshot.go
126 lines (105 loc) · 3.19 KB
/
create_snapshot.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
// Copyright 2022 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package snippets
// [START compute_snapshot_create]
import (
"context"
"fmt"
"io"
compute "cloud.google.com/go/compute/apiv1"
computepb "cloud.google.com/go/compute/apiv1/computepb"
"google.golang.org/protobuf/proto"
)
// createSnapshot creates a snapshot of a disk.
func createSnapshot(
w io.Writer,
projectID, diskName, snapshotName, zone, region, location, diskProjectID string,
) error {
// projectID := "your_project_id"
// diskName := "your_disk_name"
// snapshotName := "your_snapshot_name"
// zone := "europe-central2-b"
// region := "eupore-central2"
// location = "eupore-central2"
// diskProjectID = "YOUR_DISK_PROJECT_ID"
ctx := context.Background()
snapshotsClient, err := compute.NewSnapshotsRESTClient(ctx)
if err != nil {
return fmt.Errorf("NewSnapshotsRESTClient: %w", err)
}
defer snapshotsClient.Close()
if zone == "" && region == "" {
return fmt.Errorf("you need to specify `zone` or `region` for this function to work")
}
if zone != "" && region != "" {
return fmt.Errorf("you can't set both `zone` and `region` parameters")
}
if diskProjectID == "" {
diskProjectID = projectID
}
disk := &computepb.Disk{}
locations := []string{}
if location != "" {
locations = append(locations, location)
}
if zone != "" {
disksClient, err := compute.NewDisksRESTClient(ctx)
if err != nil {
return fmt.Errorf("NewDisksRESTClient: %w", err)
}
defer disksClient.Close()
getDiskReq := &computepb.GetDiskRequest{
Project: projectID,
Zone: zone,
Disk: diskName,
}
disk, err = disksClient.Get(ctx, getDiskReq)
if err != nil {
return fmt.Errorf("unable to get disk: %w", err)
}
} else {
regionDisksClient, err := compute.NewRegionDisksRESTClient(ctx)
if err != nil {
return fmt.Errorf("NewRegionDisksRESTClient: %w", err)
}
defer regionDisksClient.Close()
getDiskReq := &computepb.GetRegionDiskRequest{
Project: projectID,
Region: region,
Disk: diskName,
}
disk, err = regionDisksClient.Get(ctx, getDiskReq)
if err != nil {
return fmt.Errorf("unable to get disk: %w", err)
}
}
req := &computepb.InsertSnapshotRequest{
Project: projectID,
SnapshotResource: &computepb.Snapshot{
Name: proto.String(snapshotName),
SourceDisk: proto.String(disk.GetSelfLink()),
StorageLocations: locations,
},
}
op, err := snapshotsClient.Insert(ctx, req)
if err != nil {
return fmt.Errorf("unable to create snapshot: %w", err)
}
if err = op.Wait(ctx); err != nil {
return fmt.Errorf("unable to wait for the operation: %w", err)
}
fmt.Fprintf(w, "Snapshot created\n")
return nil
}
// [END compute_snapshot_create]