Class: Miasma::Models::Orchestration::Azure
- Inherits:
-
Miasma::Models::Orchestration
- Object
- Miasma::Models::Orchestration
- Miasma::Models::Orchestration::Azure
- Includes:
- Contrib::AzureApiCore::ApiCommon
- Defined in:
- lib/miasma/contrib/azure/orchestration.rb
Constant Summary collapse
- STATUS_MAP =
Resource status to state mapping
Smash.new( 'Failed' => :create_failed, 'Canceled' => :create_failed, 'Succeeded' => :create_complete, 'Deleting' => :delete_in_progress, 'Deleted' => :delete_complete )
Instance Method Summary collapse
-
#api_version ⇒ String
Supported API version.
-
#delete_template!(stack) ⇒ TrueClass, NilClass
Delete the stack template persisted in the object store.
-
#event_all(stack, evt_id = nil) ⇒ Array<Models::Orchestration::Stack::Event>
Return all events for stack.
-
#event_all_new(events) ⇒ Array<Models::Orchestration::Stack::Event>
Return all new events for event collection.
-
#event_reload(event) ⇒ Models::Orchestration::Event
Reload the stack event data from the API.
-
#fetch_all_stacks ⇒ Array<Models::Orchestration::Stack>
Fetch all available stacks.
-
#fetch_single_stack(stack) ⇒ Models::Orchestration::Stack
Populate stack model data.
-
#generate_path(stack = nil) ⇒ String
Generate the URL path required for given stack.
-
#load_stack_data(stack = nil) ⇒ Array<Models::Orchestration::Stack>
Fetch stacks or update provided stack data.
-
#resource_all(stack) ⇒ Array<Models::Orchestration::Stack::Resource>
Return all resources for stack.
-
#resource_reload(resource) ⇒ Models::Orchestration::Resource
Reload the stack resource data from the API.
-
#stack_all ⇒ Array<Models::Orchestration::Stack>
Return all stacks.
-
#stack_destroy(stack) ⇒ TrueClass, FalseClass
Delete the stack.
-
#stack_get(ident) ⇒ Stack
Return single stack.
-
#stack_reload(stack) ⇒ Models::Orchestration::Stack
Reload the stack data from the API.
-
#stack_save(stack) ⇒ Models::Orchestration::Stack
Save the stack.
-
#stack_template_load(stack) ⇒ Smash
Fetch stack template.
-
#stack_template_validate(stack) ⇒ NilClass, String
Validate stack template.
-
#status_to_state(val, modifier = nil) ⇒ Symbol
Convert given status value to correct state value.
-
#store_template!(stack) ⇒ Models::Orchestration::Stack
Store the stack template in the object store for future reference.
Methods included from Contrib::AzureApiCore::ApiCommon
#access_token_expired?, #client_access_token, #connect, #connection, #endpoint, included, #make_request, #oauth_token_buffer_seconds, #oauth_token_information, #perform_request_retry, #request_client_token, #retryable_allowed?, #uri_escape
Instance Method Details
#api_version ⇒ String
Returns supported API version.
21 22 23 |
# File 'lib/miasma/contrib/azure/orchestration.rb', line 21 def api_version '2015-01-01' end |
#delete_template!(stack) ⇒ TrueClass, NilClass
Delete the stack template persisted in the object store
243 244 245 246 247 248 249 250 251 252 253 |
# File 'lib/miasma/contrib/azure/orchestration.rb', line 243 def delete_template!(stack) storage = api_for(:storage) bucket = storage.buckets.get(azure_root_orchestration_container) if(bucket) t_file = bucket.files.get("#{stack.name}-#{attributes.checksum}.json") if(t_file) t_file.destroy true end end end |
#event_all(stack, evt_id = nil) ⇒ Array<Models::Orchestration::Stack::Event>
Return all events for stack
418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 |
# File 'lib/miasma/contrib/azure/orchestration.rb', line 418 def event_all(stack, evt_id=nil) # TODO and NOTE # Operations can't be viewed when deletion is progress. For now # just return nothing. This should be replaced with customized # events: poll resource group resources and generate event items # as resources are deleted if(stack.state.to_s.start_with?('delete')) [] else result = request( :path => [generate_path(stack), 'operations'].join('/') ) events = result.get(:body, :value).map do |event| Stack::Event.new( stack, :id => event[:operationId], :resource_id => event.get(:properties, :targetResource, :id), :resource_name => event.get(:properties, :targetResource, :resourceName), :resource_logical_id => event.get(:properties, :targetResource, :resourceName), :resource_state => status_to_state(event.get(:properties, :provisioningState)), :resource_status => event.get(:properties, :provisioningState), :resource_status_reason => event.get(:properties, :statusCode).to_s, :time => Time.parse(event.get(:properties, :timestamp)) ).valid_state end if(evt_id) idx = events.index{|d| e.id == evt_id} idx = idx ? idx + 1 : 0 events.slice(idx, events.size) else events end end end |
#event_all_new(events) ⇒ Array<Models::Orchestration::Stack::Event>
Return all new events for event collection
457 458 459 |
# File 'lib/miasma/contrib/azure/orchestration.rb', line 457 def event_all_new(events) event_all(events.stack, events.all.first.id) end |
#event_reload(event) ⇒ Models::Orchestration::Event
Reload the stack event data from the API
465 466 467 468 |
# File 'lib/miasma/contrib/azure/orchestration.rb', line 465 def event_reload(event) event.stack.events.reload event.stack.events.get(event.id) end |
#fetch_all_stacks ⇒ Array<Models::Orchestration::Stack>
Fetch all available stacks
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/miasma/contrib/azure/orchestration.rb', line 137 def fetch_all_stacks result = request( :path => generate_path ) result.fetch(:body, :value, []).map do |item| new_stack = Stack.new(self) new_stack.load_data( :id => item[:id], :name => item[:name], :state => status_to_state( item.get(:properties, :provisioningState), item.get(:tags, :state) ), :status => item.get(:properties, :provisioningState), :tags => item.fetch(:tags, Smash.new), :created => item.get(:tags, :created) ? Time.at(item.get(:tags, :created).to_i).utc : nil, :custom => Smash.new(:base_load => true) ).valid_state end end |
#fetch_single_stack(stack) ⇒ Models::Orchestration::Stack
Populate stack model data
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 124 125 126 127 128 129 130 131 132 |
# File 'lib/miasma/contrib/azure/orchestration.rb', line 66 def fetch_single_stack(stack) unless(stack.custom[:base_load]) n_stack = fetch_all_stacks.detect do |s| s.name == stack.name || s.id == stack.id end if(n_stack) stack.data.deep_merge!(n_stack.attributes) else stack.state = :delete_complete stack.status = 'Deleted' return stack end end stack.custom.delete(:base_load) result = request( :path => generate_path(stack), :expects => [200, 404] ) if(result[:response].code == 404) if(stack. && state = stack.[:state]) case state when 'create' stack.status = 'Creating' stack.state = :create_in_progress else stack.status = 'Deleting' stack.state = :delete_in_progress end else stack.data.merge!( :state => :unknown, :status => 'Unknown' ) end stack.valid_state else item = result[:body] deployment_id = item[:id] stack_id = deployment_id.sub(/\/providers\/microsoft.resources.+/i, '') stack_name = File.basename(stack_id) stack.data.merge!( :id => stack_id, :name => stack_name, :parameters => Smash[ item.fetch(:properties, :parameters, {}).map do |p_name, p_value| [p_name, p_value[:value]] end ], :outputs => item.fetch(:properties, :outputs, {}).map{ |o_name, o_value| Stack::Output.new(stack, :key => o_name, :value => o_value[:value] ) }, :template_url => item.get(:properties, :templateLink, :uri), :state => status_to_state( item.get(:properties, :provisioningState), stack.[:state] ), :status => item.get(:properties, :provisioningState), :updated => Time.parse(item.get(:properties, :timestamp)), :custom => item ) stack.valid_state end end |
#generate_path(stack = nil) ⇒ String
Generate the URL path required for given stack
29 30 31 32 33 |
# File 'lib/miasma/contrib/azure/orchestration.rb', line 29 def generate_path(stack=nil) path = "/subscriptions/#{azure_subscription_id}/resourcegroups" path << "/#{stack.name}/providers/microsoft.resources/deployments/miasma-stack" if stack path end |
#load_stack_data(stack = nil) ⇒ Array<Models::Orchestration::Stack>
Fetch stacks or update provided stack data
52 53 54 55 56 57 58 59 60 |
# File 'lib/miasma/contrib/azure/orchestration.rb', line 52 def load_stack_data(stack=nil) if(stack) fetch_single_stack(stack) else fetch_all_stacks.map do |n_stack| fetch_single_stack(n_stack) end end end |
#resource_all(stack) ⇒ Array<Models::Orchestration::Stack::Resource>
Return all resources for stack
371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 |
# File 'lib/miasma/contrib/azure/orchestration.rb', line 371 def resource_all(stack) if(stack.persisted?) result = request( :path => [generate_path, stack.name, 'resources'].join('/'), ) result.fetch(:body, :value, []).map do |res| info = Smash.new( :id => res[:id], :type => res[:type], :name => res[:name], :logical_id => res[:name], :state => :unknown, :status => 'Unknown' ) evt = stack.events.all.detect do |event| event.resource_id == res[:id] end if(evt) info = info.merge( Smash.new( :state => evt.resource_state, :status => evt.resource_status, :status_reason => evt.resource_status_reason, :updated => evt.time ) ) end Stack::Resource.new(stack, info).valid_state end else [] end end |
#resource_reload(resource) ⇒ Models::Orchestration::Resource
Reload the stack resource data from the API
409 410 411 412 |
# File 'lib/miasma/contrib/azure/orchestration.rb', line 409 def resource_reload(resource) resource.stack.resources.reload resource.stack.resources.get(resource.id) end |
#stack_all ⇒ Array<Models::Orchestration::Stack>
check if we need any mappings on state set
Return all stacks
363 364 365 |
# File 'lib/miasma/contrib/azure/orchestration.rb', line 363 def stack_all load_stack_data end |
#stack_destroy(stack) ⇒ TrueClass, FalseClass
Delete the stack
270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 |
# File 'lib/miasma/contrib/azure/orchestration.rb', line 270 def stack_destroy(stack) if(stack.persisted?) request( :path => [generate_path, stack.name].join('/'), :method => :patch, :json => { :tags => stack..merge( :updated => Time.now.to_i, :state => 'delete' ) } ) delete_template!(stack) request( :method => :delete, :expects => [202, 204], :path => generate_path(stack) ) request( :path => [generate_path, stack.name].join('/'), :method => :delete, :expects => 202 ) true else false end end |
#stack_get(ident) ⇒ Stack
Return single stack
352 353 354 355 356 |
# File 'lib/miasma/contrib/azure/orchestration.rb', line 352 def stack_get(ident) i = Stack.new(self) i.id = i.name = ident i.reload end |
#stack_reload(stack) ⇒ Models::Orchestration::Stack
Reload the stack data from the API
259 260 261 262 263 264 |
# File 'lib/miasma/contrib/azure/orchestration.rb', line 259 def stack_reload(stack) if(stack.persisted?) load_stack_data(stack) end stack end |
#stack_save(stack) ⇒ Models::Orchestration::Stack
Save the stack
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 |
# File 'lib/miasma/contrib/azure/orchestration.rb', line 162 def stack_save(stack) store_template!(stack) unless(stack.persisted?) request( :path => [generate_path, stack.name].join('/'), :method => :put, :json => { :location => azure_region, :tags => { :created => Time.now.to_i, :state => 'create' } }, :expects => [200, 201] ) else request( :path => [generate_path, stack.name].join('/'), :method => :patch, :json => { :tags => stack..merge( :updated => Time.now.to_i, :state => 'update' ) } ) end result = request( :path => generate_path(stack), :method => :put, :expects => [200, 201], :json => { :properties => { :templateLink => { :uri => stack.template_url, :contentVersion => '1.0.0.0' }, :parameters => Smash[ stack.parameters.map do |p_key, p_value| [p_key, :value => p_value] end ], :mode => 'Complete' } } ) deployment_id = result.get(:body, :id) stack_id = deployment_id.sub(/\/providers\/microsoft.resources.+/i, '') stack_name = File.basename(stack_id) stack.id = stack_id stack.name = stack_name stack.valid_state stack end |
#stack_template_load(stack) ⇒ Smash
Fetch stack template
303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 |
# File 'lib/miasma/contrib/azure/orchestration.rb', line 303 def stack_template_load(stack) if(stack.persisted?) if(stack.template_url) storage = api_for(:storage) location = URI.parse(stack.template_url) bucket, file = location.path.sub('/', '').split('/', 2) file = storage.buckets.get(bucket).files.get(file) file.body.rewind MultiJson.load(file.body.read).to_smash else Smash.new end else Smash.new end end |
#stack_template_validate(stack) ⇒ NilClass, String
Validate stack template
324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 |
# File 'lib/miasma/contrib/azure/orchestration.rb', line 324 def stack_template_validate(stack) begin result = request( :path => [generate_path(stack), 'validate'].join('/'), :method => :post, :json => { :properties => { :template => stack.template, :parameters => stack.parameters, :mode => 'Complete' } } ) nil rescue Error::ApiError::RequestError => e begin error = MultiJson.load(e.response.body.to_s).to_smash "#{error.get(:error, :code)} - #{error.get(:error, :message)}" rescue "Failed to extract error information! - #{e.response.body.to_s}" end end end |
#status_to_state(val, modifier = nil) ⇒ Symbol
Convert given status value to correct state value
40 41 42 43 44 45 46 |
# File 'lib/miasma/contrib/azure/orchestration.rb', line 40 def status_to_state(val, modifier=nil) val = STATUS_MAP.fetch(val, :create_in_progress) if(modifier && modifier.to_s != 'create' && val.to_s.start_with?('create')) val = val.to_s.sub('create', modifier).to_sym end val end |
#store_template!(stack) ⇒ Models::Orchestration::Stack
Store the stack template in the object store for future reference
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 |
# File 'lib/miasma/contrib/azure/orchestration.rb', line 222 def store_template!(stack) storage = api_for(:storage) bucket = storage.buckets.get(azure_root_orchestration_container) unless(bucket) bucket = storage.buckets.build bucket.name = azure_root_orchestration_container bucket.save end file = bucket.files.build file.name = "#{stack.name}-#{attributes.checksum}.json" file.body = MultiJson.dump(stack.template) file.save stack.template_url = file.url stack.template = nil stack end |