Skip to content

Commit fb50dae

Browse files
committed
Added the ability to share image templates with other accounts
1 parent 1773f4b commit fb50dae

4 files changed

Lines changed: 54 additions & 31 deletions

File tree

lib/softlayer/Datacenter.rb

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,6 @@ class Datacenter < SoftLayer::ModelBase
2020
sl_attr :name
2121
sl_attr :long_name, "longName"
2222

23-
##
24-
# An array with the datacenter names of datacenters that can host
25-
# image templates
26-
IMAGE_TEMPLATE_DATACENTERS = ['ams01', 'dal01', 'dal05', 'dal07', 'hou02', 'sea01', 'sjc01', 'sng01', 'wdc01'];
27-
28-
##
29-
# Returns true if image templates can be copied to this data center
30-
def available_for_image_templates?
31-
IMAGE_TEMPLATE_DATACENTERS.include?(self.name)
32-
end
33-
3423
##
3524
# Return the datacenter with the given name ('sng01' or 'dal05')
3625
def self.datacenter_named(name, client = nil)

lib/softlayer/ImageTemplate.rb

Lines changed: 44 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -83,30 +83,59 @@ def datacenters
8383

8484
##
8585
# Accepts an array of datacenters (instances of SoftLayer::Datacenter) where this
86-
# image should be made available. The call will kick off transactions to make
87-
# the image available in the given datacenters. These transactions can take
86+
# image should be made available. The call will kick off one or more transactions
87+
# to make the image available in the given datacenters. These transactions can take
8888
# some time to complete.
8989
#
9090
# Note that the template will be REMOVED from any datacenter that does not
91-
# appear in this array! The list given must be comprehensive.
91+
# appear in this array! The list given must be comprehensive.
92+
#
93+
# The available_datacenters call returns a list of the values that are valid
94+
# whithin this array.
9295
def datacenters=(datacenters_array)
93-
datacenter_data = datacenters_array.collect do |datacenter|
94-
raise "Image templates cannot be copied to the data center #{datacenter.name}" unless datacenter.available_for_image_templates?
96+
datacenter_data = datacenters_array.collect do |datacenter|
9597
{ "id" => datacenter.id }
9698
end
9799

98100
self.service.setAvailableLocations(datacenter_data.compact)
99101
end
100102

101103
##
102-
# Works with an array of account IDs (or should use master user names i.e. "SL232279" )
103-
# that the image template is shared with
104+
# Returns an array of the datacenters that this image can be stored in.
105+
# This is the set of datacenters that you may choose from, when putting
106+
# together a list you will send to the datacenters= setter.
107+
#
108+
def available_datacenters
109+
datacenters_data = self.service.getStorageLocations()
110+
datacenters_data.collect { |datacenter_data| SoftLayer::Datacenter.datacenter_named(datacenter_data["name"]) }
111+
end
112+
113+
##
114+
# Share this image template with another account
104115
#
105-
# Should this be datacenters an "share_with_accounts, stop_sharing_with_accounts"
106-
def shared_with_accounts
116+
# The id of another account can usually be determined
117+
# by the user name of the master user which is typically
118+
# "SL<account_id>" or something similar.
119+
#
120+
# Note that this routine raises an exception if you call
121+
# it with an account that is already in the list of
122+
# shared accounts.
123+
def start_sharing(with_account_id)
124+
self.service.permitSharingAccess(with_account_id)
107125
end
108126

109-
def shared_with_accounts=
127+
##
128+
# Stop sharing this image with the account_id given
129+
def stop_sharing(with_account_id)
130+
raise "You cannot stop sharing an image template with the account that owns it" if with_account_id == self["accountId"]
131+
self.service.denySharingAccess(with_account_id)
132+
end
133+
134+
##
135+
# Return an array with the id's of accounts that this image is shared with
136+
def shared_with
137+
accounts_data = self.service.getAccountReferences
138+
accounts_data.collect { |account_data| account_data["accountId"] }
110139
end
111140

112141
##
@@ -122,16 +151,15 @@ def delete!
122151
end
123152

124153
##
125-
# Wait until transactions related to the image template are finished
154+
# Repeatedly poll the netwokr API until transactions related to this image
155+
# template are finished
126156
#
127-
# A template is not ready until all the transactions on the template
157+
# A template is not 'ready' until all the transactions on the template
128158
# itself, and all its children are complete.
129159
#
130160
# At each trial, the routine will yield to a block if one is given
131161
# The block is passed one parameter, a boolean flag indicating
132-
# whether or not the image template is 'ready'. Interim invocations
133-
# of the block should receive +false+, the final invocation should
134-
# receieve +true+
162+
# whether or not the image template is 'ready'.
135163
#
136164
def wait_until_ready(max_trials, seconds_between_tries = 2)
137165
# pessimistically assume the server is not ready
@@ -324,7 +352,7 @@ def self.template_with_id(id, options_hash = {})
324352
protected
325353

326354
def self.default_object_mask
327-
return "mask[id,name,note,globalIdentifier,datacenters,blockDevices,tagReferences,publicFlag,flexImageFlag,transactionId,children.transactionId]"
355+
return "mask[id,accountId,name,note,globalIdentifier,datacenters,blockDevices,tagReferences,publicFlag,flexImageFlag,transactionId,children.transactionId]"
328356
end
329357
end
330358
end

lib/softlayer/Server.rb

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
# For licensing information see the LICENSE.md file in the project root.
55
#++
66

7-
8-
97
module SoftLayer
108
# Server is the base class for VirtualServer and BareMetalServer.
119
# It implements some functionality common to both those classes.

lib/softlayer/VirtualServer.rb

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,13 +129,21 @@ def upgrade_max_port_speed!(network_speed_in_Mbps)
129129
#
130130
# The image_notes should be a string and will be added to the image as notes.
131131
#
132+
# The routine returns the instance of SoftLayer::ImageTemplate that is
133+
# created. That image template will probably not be available immediately, however.
134+
# You may use the wait_until_ready routine of SoftLayer::ImageTemplate to
135+
# wait on it.
136+
#
132137
def capture_image(image_name, include_attached_storage = false, image_notes = nil)
133138
disk_filter = lambda { |disk| disk['device'] == '0' }
134-
disk_filter = lambda { |disk| disk['device'] == '1' } if include_attached_storage
139+
disk_filter = lambda { |disk| disk['device'] != '1' } if include_attached_storage
135140

136141
disks = self.blockDevices.select(&disk_filter)
137142

138-
self.service.createArchiveTransaction(image_name, disks, notes) if disks && !disks.empty?
143+
self.service.createArchiveTransaction(image_name, disks, notes ? notes : "") if disks && !disks.empty?
144+
145+
image_templates = SoftLayer::ImageTemplate.find_private_templates(:name => image_name)
146+
image_templates[0] if !image_templates.empty?
139147
end
140148

141149
##

0 commit comments

Comments
 (0)