forked from cloudfoundry/bosh-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cloud_stemcell.go
94 lines (78 loc) · 1.98 KB
/
cloud_stemcell.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
package stemcell
import (
bicloud "github.com/cloudfoundry/bosh-cli/cloud"
biconfig "github.com/cloudfoundry/bosh-cli/config"
bosherr "github.com/cloudfoundry/bosh-utils/errors"
)
type CloudStemcell interface {
CID() string
Name() string
Version() string
PromoteAsCurrent() error
Delete() error
}
type cloudStemcell struct {
cid string
name string
version string
repo biconfig.StemcellRepo
cloud bicloud.Cloud
}
func NewCloudStemcell(
stemcellRecord biconfig.StemcellRecord,
repo biconfig.StemcellRepo,
cloud bicloud.Cloud,
) CloudStemcell {
return &cloudStemcell{
cid: stemcellRecord.CID,
name: stemcellRecord.Name,
version: stemcellRecord.Version,
repo: repo,
cloud: cloud,
}
}
func (s *cloudStemcell) CID() string {
return s.cid
}
func (s *cloudStemcell) Name() string {
return s.name
}
func (s *cloudStemcell) Version() string {
return s.version
}
func (s *cloudStemcell) PromoteAsCurrent() error {
stemcellRecord, found, err := s.repo.Find(s.name, s.version)
if err != nil {
return bosherr.WrapError(err, "Finding current stemcell")
}
if !found {
return bosherr.Error("Stemcell does not exist in repo")
}
err = s.repo.UpdateCurrent(stemcellRecord.ID)
if err != nil {
return bosherr.WrapError(err, "Updating current stemcell")
}
return nil
}
func (s *cloudStemcell) Delete() error {
deleteErr := s.cloud.DeleteStemcell(s.cid)
if deleteErr != nil {
// allow StemcellNotFoundError for idempotency
cloudErr, ok := deleteErr.(bicloud.Error)
if !ok || cloudErr.Type() != bicloud.StemcellNotFoundError {
return bosherr.WrapError(deleteErr, "Deleting stemcell from cloud")
}
}
stemcellRecord, found, err := s.repo.Find(s.name, s.version)
if err != nil {
return bosherr.WrapErrorf(err, "Finding stemcell record (name=%s, version=%s)", s.name, s.version)
}
if !found {
return nil
}
err = s.repo.Delete(stemcellRecord)
if err != nil {
return bosherr.WrapError(err, "Deleting stemcell record")
}
return deleteErr
}