forked from GoogleCloudPlatform/compute-image-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
snapshot.go
105 lines (88 loc) · 3.71 KB
/
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
// Copyright 2020 Google Inc. All Rights Reserved.
//
// 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
//
// http://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 daisy
import (
"context"
"encoding/json"
"fmt"
"net/http"
"regexp"
daisyCompute "github.com/GoogleCloudPlatform/compute-image-tools/daisy/compute"
"google.golang.org/api/compute/v1"
"google.golang.org/api/googleapi"
)
var (
snapshotURLRgx = regexp.MustCompile(fmt.Sprintf(`^(projects/(?P<project>%[1]s)/)?global/snapshots/(?P<snapshot>%[2]s)$`, projectRgxStr, rfc1035))
)
// snapshotExists should only be used during validation for existing GCE snapshots
// and should not be relied or populated for daisy created resources.
func (w *Workflow) snapshotExists(project, snapshot string) (bool, DError) {
return w.snapshotCache.resourceExists(func(project string, opts ...daisyCompute.ListCallOption) (interface{}, error) {
return w.ComputeClient.ListSnapshots(project)
}, project, snapshot)
}
// Snapshot is used to create a GCE disk in a project.
type Snapshot struct {
compute.Snapshot
Resource
}
// MarshalJSON is a hacky workaround to prevent Snapshot from using compute.Snapshot's implementation.
func (ss *Snapshot) MarshalJSON() ([]byte, error) {
return json.Marshal(*ss)
}
func (ss *Snapshot) populate(ctx context.Context, s *Step) DError {
var errs DError
ss.Name, errs = ss.Resource.populateWithGlobal(ctx, s, ss.Name)
ss.Description = strOr(ss.Description, fmt.Sprintf("Snapshot created by Daisy in workflow %q on behalf of %s.", s.w.Name, s.w.username))
// If it's a URI, try to extend it because it may missed "project" part.
// Otherwise, it can be a daisy-created resource. Leave it as-is.
// If it matches neither daisy-created disk nor existing resource URI, "validate"
// will fail.
if diskURLRgx.MatchString(ss.SourceDisk) {
ss.SourceDisk = extendPartialURL(ss.SourceDisk, ss.Project)
}
// This link can be modified later if disk project is different. Here it's a placeholder.
ss.link = fmt.Sprintf("projects/%s/global/snapshots/%s", ss.Project, ss.Name)
return errs
}
func (ss *Snapshot) validate(ctx context.Context, s *Step) DError {
pre := fmt.Sprintf("cannot create snapshot %q", ss.daisyName)
errs := ss.Resource.validate(ctx, s, pre)
// Source disk checking.
if ss.SourceDisk == "" {
errs = addErrs(errs, Errf("%s: must provide SourceDisk", pre))
} else if _, err := s.w.disks.regUse(ss.SourceDisk, s); err != nil {
errs = addErrs(errs, newErr("failed to get source disk", err))
}
// Register creation.
errs = addErrs(errs, s.w.snapshots.regCreate(ss.daisyName, &ss.Resource, s, false))
return errs
}
type snapshotRegistry struct {
baseResourceRegistry
}
func newSnapshotRegistry(w *Workflow) *snapshotRegistry {
sr := &snapshotRegistry{baseResourceRegistry: baseResourceRegistry{w: w, typeName: "snapshot", urlRgx: snapshotURLRgx}}
sr.baseResourceRegistry.deleteFn = sr.deleteFn
sr.init()
return sr
}
func (sr *snapshotRegistry) deleteFn(res *Resource) DError {
m := NamedSubexp(snapshotURLRgx, res.link)
err := sr.w.ComputeClient.DeleteSnapshot(m["project"], m["snapshot"])
if gErr, ok := err.(*googleapi.Error); ok && gErr.Code == http.StatusNotFound {
return typedErr(resourceDNEError, "failed to delete snapshot", err)
}
return newErr("failed to delete snapshot", err)
}