Skip to content

Commit 05694a5

Browse files
committed
Changed the shared accounts to be array based instead of forcing individual calls
1 parent fb50dae commit 05694a5

1 file changed

Lines changed: 37 additions & 2 deletions

File tree

lib/softlayer/ImageTemplate.rb

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,43 @@ def available_datacenters
110110
datacenters_data.collect { |datacenter_data| SoftLayer::Datacenter.datacenter_named(datacenter_data["name"]) }
111111
end
112112

113+
114+
##
115+
# Returns a list of the accounts (identified by account ID numbers)
116+
# that this image is shared with
117+
def shared_with_accounts
118+
accounts_data = self.service.getAccountReferences
119+
accounts_data.collect { |account_data| account_data["accountId"] }
120+
end
121+
122+
##
123+
# Change the set of accounts that this image is shared with.
124+
# The parameter is an array of account ID's.
125+
#
126+
# Note that this routine will "unshare" with any accounts
127+
# not included in the list passed in so the list should
128+
# be comprehensive
129+
#
130+
def shared_with_accounts= (account_id_list)
131+
already_sharing_with = self.shared_with_accounts
132+
133+
accounts_to_add = account_id_list.select { |account_id| !already_sharing_with.include?(account_id) }
134+
135+
# Note, using the network API, it is possible to "unshare" an image template
136+
# with the account that owns it, however, this leads to a rather odd state
137+
# where the image has allocated resources (that the account may be charged for)
138+
# but no way to delete those resources. For that reason this model
139+
# always includes the account ID that owns the image in the list of
140+
# accounts the image will be shared with.
141+
my_account_id = self['accountId']
142+
accounts_to_add.push(my_account_id) if !already_sharing_with.include?(my_account_id) && !accounts_to_add.include?(my_account_id)
143+
144+
accounts_to_remove = already_sharing_with.select { |account_id| (account_id != my_account_id) && !account_id_list.include?(account_id) }
145+
146+
accounts_to_add.each {|account_id| self.service.permitSharingAccess account_id }
147+
accounts_to_remove.each {|account_id| self.service.denySharingAccess account_id }
148+
end
149+
113150
##
114151
# Share this image template with another account
115152
#
@@ -134,8 +171,6 @@ def stop_sharing(with_account_id)
134171
##
135172
# Return an array with the id's of accounts that this image is shared with
136173
def shared_with
137-
accounts_data = self.service.getAccountReferences
138-
accounts_data.collect { |account_data| account_data["accountId"] }
139174
end
140175

141176
##

0 commit comments

Comments
 (0)