Class: Miasma::Models::Orchestration::OpenStack

Inherits:
Miasma::Models::Orchestration show all
Includes:
Contrib::OpenStackApiCore::ApiCommon
Defined in:
lib/miasma/contrib/open_stack/orchestration.rb

Constant Summary collapse

RESOURCE_MAPPING =

Returns external to internal resource mapping.

Returns:

  • (Smash)

    external to internal resource mapping

Smash.new(
  'OS::Nova::Server' => Smash.new(
    :api => :compute,
    :collection => :servers
  )
  # 'OS::Heat::AutoScalingGroup' => Smash.new(
  #   :api => :auto_scale,
  #   :collection => :groups
  # )
)

Instance Method Summary collapse

Methods included from Contrib::OpenStackApiCore::ApiCommon

#connection, #endpoint, included, #open_stack_api, #token

Instance Method Details

#event_all(stack, marker = nil) ⇒ Array<Models::Orchestration::Stack::Event>

Return all events for stack

Parameters:

  • stack (Models::Orchestration::Stack)

Returns:

  • (Array<Models::Orchestration::Stack::Event>)


212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
# File 'lib/miasma/contrib/open_stack/orchestration.rb', line 212

def event_all(stack, marker = nil)
  params = marker ? {:marker => marker} : {}
  result = request(
    :path => "/stacks/#{stack.name}/#{stack.id}/events",
    :method => :get,
    :expects => 200,
    :params => params
  )
  result.fetch(:body, :events, []).map do |event|
    Stack::Event.new(
      stack,
      :id => event[:id],
      :resource_id => event[:physical_resource_id],
      :resource_name => event[:resource_name],
      :resource_logical_id => event[:logical_resource_id],
      :resource_state => event[:resource_status].downcase.to_sym,
      :resource_status => event[:resource_status],
      :resource_status_reason => event[:resource_status_reason],
      :time => Time.parse(event[:event_time])
    ).valid_state
  end
end

#event_all_new(events) ⇒ Array<Models::Orchestration::Stack::Event>

Return all new events for event collection

Parameters:

  • events (Models::Orchestration::Stack::Events)

Returns:

  • (Array<Models::Orchestration::Stack::Event>)


239
240
241
# File 'lib/miasma/contrib/open_stack/orchestration.rb', line 239

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

Parameters:

  • resource (Models::Orchestration::Stack::Event)

Returns:

  • (Models::Orchestration::Event)


247
248
249
250
# File 'lib/miasma/contrib/open_stack/orchestration.rb', line 247

def event_reload(event)
  event.stack.events.reload
  event.stack.events.get(event.id)
end

#resource_all(stack) ⇒ Array<Models::Orchestration::Stack::Resource>

Return all resources for stack

Parameters:

  • stack (Models::Orchestration::Stack)

Returns:

  • (Array<Models::Orchestration::Stack::Resource>)


178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/miasma/contrib/open_stack/orchestration.rb', line 178

def resource_all(stack)
  result = request(
    :method => :get,
    :path => "/stacks/#{stack.name}/#{stack.id}/resources",
    :expects => 200
  )
  result.fetch(:body, :resources, []).map do |resource|
    Stack::Resource.new(
      stack,
      :id => resource[:physical_resource_id],
      :name => resource[:resource_name],
      :type => resource[:resource_type],
      :logical_id => resource[:logical_resource_id],
      :state => resource[:resource_status].downcase.to_sym,
      :status => resource[:resource_status],
      :status_reason => resource[:resource_status_reason],
      :updated => Time.parse(resource[:updated_time])
    ).valid_state
  end
end

#resource_reload(resource) ⇒ Models::Orchestration::Resource

Reload the stack resource data from the API

Parameters:

  • resource (Models::Orchestration::Stack::Resource)

Returns:

  • (Models::Orchestration::Resource)


203
204
205
206
# File 'lib/miasma/contrib/open_stack/orchestration.rb', line 203

def resource_reload(resource)
  resource.stack.resources.reload
  resource.stack.resources.get(resource.id)
end

#stack_all(options = {}) ⇒ Array<Models::Orchestration::Stack>

TODO:

check if we need any mappings on state set

Return all stacks

Parameters:

  • options (Hash) (defaults to: {})

    filter

Returns:

  • (Array<Models::Orchestration::Stack>)


154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/miasma/contrib/open_stack/orchestration.rb', line 154

def stack_all(options={})
  result = request(
    :method => :get,
    :path => '/stacks'
  )
  result.fetch(:body, :stacks, []).map do |s|
    Stack.new(
      self,
      :id => s[:id],
      :created => Time.parse(s[:creation_time]),
      :description => s[:description],
      :name => s[:stack_name],
      :state => s[:stack_status].downcase.to_sym,
      :status => s[:stack_status],
      :status_reason => s[:stack_status_reason],
      :updated => s[:updated_time].to_s.empty? ? nil : Time.parse(s[:updated_time])
    ).valid_state
  end
end

#stack_destroy(stack) ⇒ TrueClass, FalseClass

Delete the stack

Parameters:

  • stack (Models::Orchestration::Stack)

Returns:

  • (TrueClass, FalseClass)


101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/miasma/contrib/open_stack/orchestration.rb', line 101

def stack_destroy(stack)
  if(stack.persisted?)
    request(
      :method => :delete,
      :path => "/stacks/#{stack.name}/#{stack.id}",
      :expects => 204
    )
    true
  else
    false
  end
end

#stack_reload(stack) ⇒ Models::Orchestration::Stack

Reload the stack data from the API

Parameters:

  • stack (Models::Orchestration::Stack)

Returns:

  • (Models::Orchestration::Stack)


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
# File 'lib/miasma/contrib/open_stack/orchestration.rb', line 62

def stack_reload(stack)
  if(stack.persisted?)
    result = request(
      :method => :get,
      :path => "/stacks/#{stack.name}/#{stack.id}",
      :expects => 200
    )
    stk = result.get(:body, :stack)
    stack.load_data(
      :id => stk[:id],
      :capabilities => stk[:capabilities],
      :created => Time.parse(stk[:creation_time]),
      :description => stk[:description],
      :disable_rollback => stk[:disable_rollback].to_s.downcase == 'true',
      :notification_topics => stk[:notification_topics],
      :name => stk[:stack_name],
      :state => stk[:stack_status].downcase.to_sym,
      :status => stk[:stack_status],
      :status_reason => stk[:stack_status_reason],
      :template_description => stk[:template_description],
      :timeout_in_minutes => stk[:timeout_mins].to_s.empty? ? nil : stk[:timeout_mins].to_i,
      :updated => stk[:updated_time].to_s.empty? ? nil : Time.parse(stk[:updated_time]),
      :parameters => stk.fetch(:parameters, Smash.new),
      :outputs => stk.fetch(:outputs, []).map{ |output|
        Smash.new(
          :key => output[:output_key],
          :value => output[:output_value],
          :description => output[:description]
        )
      }
    ).valid_state
  end
  stack
end

#stack_save(stack) ⇒ Models::Orchestration::Stack

Save the stack

Parameters:

  • stack (Models::Orchestration::Stack)

Returns:

  • (Models::Orchestration::Stack)


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
# File 'lib/miasma/contrib/open_stack/orchestration.rb', line 26

def stack_save(stack)
  if(stack.persisted?)
    stack.load_data(stack.attributes)
    result = request(
      :expects => 202,
      :method => :put,
      :path => "/stacks/#{stack.name}/#{stack.id}",
      :json => {
        :stack_name => stack.name,
        :template => MultiJson.dump(stack.template),
        :parameters => stack.parameters || {}
      }
    )
    stack.valid_state
  else
    stack.load_data(stack.attributes)
    result = request(
      :expects => 201,
      :method => :post,
      :path => '/stacks',
      :json => {
        :stack_name => stack.name,
        :template => MultiJson.dump(stack.template),
        :parameters => stack.parameters || {},
        :disable_rollback => (!!stack.disable_rollback).to_s
      }
    )
    stack.id = result.get(:body, :stack, :id)
    stack.valid_state
  end
end

#stack_template_load(stack) ⇒ Smash

Fetch stack template

Parameters:

  • stack (Stack)

Returns:

  • (Smash)

    stack template



118
119
120
121
122
123
124
125
126
127
128
# File 'lib/miasma/contrib/open_stack/orchestration.rb', line 118

def stack_template_load(stack)
  if(stack.persisted?)
    result = request(
      :method => :get,
      :path => "/stacks/#{stack.name}/#{stack.id}/template"
    )
    result.fetch(:body, Smash.new)
  else
    Smash.new
  end
end

#stack_template_validate(stack) ⇒ NilClass, String

Validate stack template

Parameters:

  • stack (Stack)

Returns:

  • (NilClass, String)

    nil if valid, string error message if invalid



134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/miasma/contrib/open_stack/orchestration.rb', line 134

def stack_template_validate(stack)
  begin
    result = request(
      :method => :post,
      :path => '/validate',
      :json => Smash.new(
        :template => stack.template
      )
    )
    nil
  rescue Error::ApiError::RequestError => e
    MultiJson.load(e.response.body.to_s).to_smash.get(:error, :message)
  end
end