forked from cloudfoundry/bosh-softlayer-cpi-release
-
Notifications
You must be signed in to change notification settings - Fork 0
/
softlayer_finder.go
executable file
·123 lines (99 loc) · 3.68 KB
/
softlayer_finder.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
package stemcell
import (
bosherr "github.com/cloudfoundry/bosh-agent/errors"
boshlog "github.com/cloudfoundry/bosh-agent/logger"
sl "github.com/maximilien/softlayer-go/softlayer"
)
type SoftLayerFinder struct {
client sl.Client
logger boshlog.Logger
}
func NewSoftLayerFinder(client sl.Client, logger boshlog.Logger) SoftLayerFinder {
return SoftLayerFinder{client: client, logger: logger}
}
func (f SoftLayerFinder) FindById(id int) (Stemcell, bool, error) {
accountService, err := f.client.GetSoftLayer_Account_Service()
if err != nil {
return nil, false, bosherr.WrapError(err, "Getting SoftLayer AccountService")
}
stemcell, found, err := f.findByIdInVirtualDiskImages(id, accountService)
if err != nil {
return stemcell, found, err
}
if found {
return stemcell, found, nil
} else {
stemcell, found, err = f.findByIdInVirtualGuestDeviceTemplateGroups(id, accountService)
if err != nil {
return stemcell, found, err
}
}
return stemcell, found, nil
}
func (f SoftLayerFinder) Find(uuid string) (Stemcell, bool, error) {
accountService, err := f.client.GetSoftLayer_Account_Service()
if err != nil {
return nil, false, bosherr.WrapError(err, "Getting SoftLayer AccountService")
}
stemcell, found, err := f.findInVirtualDiskImages(uuid, accountService)
if err != nil {
return stemcell, found, err
}
if found {
return stemcell, found, nil
} else {
stemcell, found, err = f.findInVirtualGuestDeviceTemplateGroups(uuid, accountService)
if err != nil {
return stemcell, found, err
}
}
return stemcell, found, nil
}
func (f SoftLayerFinder) findInVirtualDiskImages(uuid string, accountService sl.SoftLayer_Account_Service) (Stemcell, bool, error) {
virtualDiskImages, err := accountService.GetVirtualDiskImages()
if err != nil {
return nil, false, bosherr.WrapError(err, "Getting virtual disk images")
}
for _, vdImage := range virtualDiskImages {
if vdImage.Uuid == uuid {
return NewSoftLayerStemcell(vdImage.Id, vdImage.Uuid, VirtualDiskImageKind, f.client, f.logger), true, nil
}
}
return nil, false, nil
}
func (f SoftLayerFinder) findByIdInVirtualDiskImages(id int, accountService sl.SoftLayer_Account_Service) (Stemcell, bool, error) {
virtualDiskImages, err := accountService.GetVirtualDiskImages()
if err != nil {
return nil, false, bosherr.WrapError(err, "Getting virtual disk images")
}
for _, vdImage := range virtualDiskImages {
if vdImage.Id == id {
return NewSoftLayerStemcell(vdImage.Id, vdImage.Uuid, VirtualDiskImageKind, f.client, f.logger), true, nil
}
}
return nil, false, nil
}
func (f SoftLayerFinder) findInVirtualGuestDeviceTemplateGroups(uuid string, accountService sl.SoftLayer_Account_Service) (Stemcell, bool, error) {
vgdtgGroups, err := accountService.GetBlockDeviceTemplateGroups()
if err != nil {
return nil, false, bosherr.WrapError(err, "Getting virtual guest device template groups")
}
for _, vgdtgGroup := range vgdtgGroups {
if vgdtgGroup.GlobalIdentifier == uuid {
return NewSoftLayerStemcell(vgdtgGroup.Id, vgdtgGroup.GlobalIdentifier, VirtualGuestDeviceTemplateGroupKind, f.client, f.logger), true, nil
}
}
return nil, false, nil
}
func (f SoftLayerFinder) findByIdInVirtualGuestDeviceTemplateGroups(id int, accountService sl.SoftLayer_Account_Service) (Stemcell, bool, error) {
vgdtgGroups, err := accountService.GetBlockDeviceTemplateGroups()
if err != nil {
return nil, false, bosherr.WrapError(err, "Getting virtual guest device template groups")
}
for _, vgdtgGroup := range vgdtgGroups {
if vgdtgGroup.Id == id {
return NewSoftLayerStemcell(vgdtgGroup.Id, vgdtgGroup.GlobalIdentifier, VirtualGuestDeviceTemplateGroupKind, f.client, f.logger), true, nil
}
}
return nil, false, nil
}