miq_dev_util 0.2.0 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: f2483591e6af4106bc28390033ee7eb5e2777a69
4
- data.tar.gz: ed101b6f73c32ce124b2ce6da506959a9e79dde8
3
+ metadata.gz: e8a90e284793baf979cfe5c906008182dd3f9c51
4
+ data.tar.gz: f6ee7f7670c173d08c69815b56b33119e69b4052
5
5
  SHA512:
6
- metadata.gz: 348952bfb3fa0fc9fdd08e9e27e66678bcb54832458361fd69ec553207f23f3c031bb042d45deead4dc0a000f05766c2a9f11dcc0b9043bd0ce039290e29ff2f
7
- data.tar.gz: 467a0d3fecf09f34da2c85ecc8fd387f18970b529dcae44d20fa57055d68685b67973ccf36f7215b9d41a84f0398053b8c59c0a813d325942a34c230142b3325
6
+ metadata.gz: 61d09ccfe9ba091900db987e2fb093f67cc9ee0e6259b4e26735b945dbf9148ab9ed584309087b06ac47d4820a6bbe5d4353ddb85f1b79e25eb3186fa1b83e94
7
+ data.tar.gz: 06d86d98fbbeedfa6c01e098f9f0685e60b2c8cb2409ee3931ed36301074dd1dcad36827f0b36f9a8cfd3616e1acdb70113dffc1e981ef2c07c7cdf8904fae50
data/README.md CHANGED
@@ -4,6 +4,9 @@ This is a set of helper classes to make developing in the ManageIQ automate
4
4
  model less cumbersome. By putting helper code and commonly used methods in this
5
5
  gem we can reduce the amount of code copied and pasted between methods.
6
6
 
7
+ The master branch and version 1.x are being targeted for CloudForms 4.1. If you
8
+ want support for CloudForms 3.2, please use the 0.x releases based upon the 3.2
9
+ branch.
7
10
 
8
11
  ## Installation
9
12
 
@@ -25,6 +28,11 @@ require 'miq_dev_util'
25
28
  @logger = MiqDevUtil::Logger.new($evm, 'my_method_name')
26
29
  @logger.log(:info, 'Hello World')
27
30
 
31
+ # There are also shortcuts for different log levels.
32
+ @logger.info('Hello info')
33
+ @logger.warn('Uh oh')
34
+ @logger.error('Something went really wrong.')
35
+
28
36
  @logger.dump_root
29
37
 
30
38
  @logger.dump_attributes($evm.root['vm'], 'root vm')
@@ -74,8 +82,49 @@ automate_helper.get_instance_with_attributes(path)
74
82
  # The dialog name that may hold the miq ID is specified via :dialog_name
75
83
  #
76
84
 
77
- vm = resolve_vm
78
- vm = resolve_vm(lookup_order: [:dialog_id], dialog_name: 'dialog_vm_id')
85
+ vm = automate_helper.resolve_vm
86
+ vm = automate_helper.resolve_vm(lookup_order: [:dialog_id],
87
+ dialog_name: 'dialog_vm_id')
88
+ ```
89
+
90
+ Issuing automate requests within Automate.
91
+
92
+ ```ruby
93
+ # Options that are used in the various methods below.
94
+ options = {}
95
+ options[:namespace] = 'MyCustomCode'
96
+ options[:class_name] = 'Methods'
97
+ options[:instance_name] = 'do_something'
98
+ options[:user_id] = $evm.vmdb(:user).find_by_userid('admin').id
99
+
100
+ ah = MiqDevUtil::Automate.new($evm)
101
+
102
+ # This uses an existing version of $evm.execute(create_automation_request) if it
103
+ # already exists (MIQ 5.5+), otherwise it backports a version to use.
104
+
105
+ request = ah.create_automation_request(options)
106
+ # or to specify user and approval
107
+ request = ah.create_automation_request(options, userid = 'admin', auto_approve = true))
108
+
109
+ #
110
+ # To run the automate request in the same zone as the VM.
111
+ #
112
+ ah.zone_aware_vm_automation_request(vm, options)
113
+
114
+ # or to specify user and approval
115
+ request = ah.zone_aware_vm_automation(vm, options, userid = 'admin', auto_approve = true))
116
+
117
+
118
+ #
119
+ # To synchronously wait for an automate request to finish.
120
+ #
121
+ updated_request = ah.wait_for_automation_request(request)
122
+
123
+ # Optional parameters
124
+ # Wait at most max_wait seconds.
125
+ # Check the status ever poll_interval seconds.
126
+ updated_request = ah.wait_for_automation_request(request, max_wait: 600,
127
+ poll_interval: 5)
79
128
  ```
80
129
 
81
130
  ### Generic Code ###
@@ -52,8 +52,67 @@ module MiqDevUtil
52
52
  vm
53
53
  end
54
54
 
55
+ # A wrapper to handle MIQ 5.4 which does not have this built in and 5.5+
56
+ # which does.
57
+ def create_automation_request(options, userid = 'admin', auto_approve = false)
58
+ unless service_method_defined?(:create_automation_request)
59
+ backport_create_automation_request
60
+ end
61
+
62
+ @evm.execute('create_automation_request', options, userid, auto_approve)
63
+ end
64
+
65
+ # Wait until the automation request shows as finished.
66
+ # Wait at most max_wait seconds.
67
+ # Check the status ever poll_interval seconds.
68
+ def wait_for_automation_request(request, options = {})
69
+ max_wait = options['max_wait'] || 600
70
+ poll_interval = options['poll_interval'] || 15
71
+
72
+ start_time = Time.now
73
+
74
+ while (Time.now - start_time) < max_wait
75
+ request = $evm.vmdb(:automation_request).find(request.id)
76
+ return request if request['request_state'] == 'finished'
77
+ sleep(poll_interval)
78
+ end
79
+
80
+ raise 'Automation request wait time exceeded.'
81
+ end
82
+
83
+ # Create an automation request that will run in the same zone as the VM's
84
+ # EMS. Also set some values to make this have similar data to that which
85
+ # a method responding to a button press would have.
86
+ def zone_aware_vm_automation_request(vm, options, userid = 'admin',
87
+ auto_approve = false)
88
+ options[:miq_zone] = vm.ext_management_system.zone_name
89
+ options[:attrs] = {} if options[:attrs].nil?
90
+ options[:attrs][:vm] = vm
91
+ options[:attrs][:vm_id] = vm.id
92
+
93
+ create_automation_request(options, userid, auto_approve)
94
+ end
95
+
55
96
  private
56
97
 
98
+ def service_method_defined?(method)
99
+ @evm.execute(:instance_eval, "respond_to?(:#{method})")
100
+ end
101
+
102
+ def backport_create_automation_request
103
+ @evm.execute(:class_eval, create_automation_request_definition)
104
+ end
105
+
106
+ # This is based on the MIQ 5.5 method, but changed regarding the user/userid
107
+ # since the code in 5.4 uses a different value.
108
+ def create_automation_request_definition
109
+ <<-eos
110
+ def self.create_automation_request(options, userid = "admin", auto_approve = false)
111
+ MiqAeServiceModelBase.wrap_results(AutomationRequest.create_request(options, userid, auto_approve))
112
+ end
113
+ eos
114
+ end
115
+
57
116
  def vm_lookup_by(lu_method, dialog_name = nil)
58
117
  case lu_method
59
118
  when :rootvm then @evm.root['vm']
@@ -10,7 +10,7 @@ class MiqDevUtil::EMS
10
10
  # * :insecure
11
11
  def self.get_credentials(ems, insecure=true)
12
12
  {
13
- host: ems['hostname'],
13
+ host: ems.hostname,
14
14
  user: ems.authentication_userid,
15
15
  password: ems.authentication_password,
16
16
  insecure: insecure
@@ -12,6 +12,22 @@ class MiqDevUtil::Logger
12
12
  @evm.log(level, "#{@method_name} - #{message}")
13
13
  end
14
14
 
15
+ def debug(message)
16
+ log(:debug, message)
17
+ end
18
+
19
+ def error(message)
20
+ log(:error, message)
21
+ end
22
+
23
+ def info(message)
24
+ log(:info, message)
25
+ end
26
+
27
+ def warn(message)
28
+ log(:warn, message)
29
+ end
30
+
15
31
  # Write the attributes of the given object to the log with a prefix of
16
32
  # my_object_name to make finding the entries a little easier.
17
33
  def dump_attributes(my_object, my_object_name)
@@ -1,3 +1,3 @@
1
1
  module MiqDevUtil
2
- VERSION = "0.2.0"
2
+ VERSION = '1.0.0'.freeze
3
3
  end
metadata CHANGED
@@ -1,55 +1,55 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: miq_dev_util
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Eric Wannemacher
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2015-10-01 00:00:00.000000000 Z
11
+ date: 2017-02-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - "~>"
17
+ - - ~>
18
18
  - !ruby/object:Gem::Version
19
19
  version: '1.10'
20
20
  type: :development
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - "~>"
24
+ - - ~>
25
25
  - !ruby/object:Gem::Version
26
26
  version: '1.10'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: rake
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - "~>"
31
+ - - ~>
32
32
  - !ruby/object:Gem::Version
33
33
  version: '10.0'
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - "~>"
38
+ - - ~>
39
39
  - !ruby/object:Gem::Version
40
40
  version: '10.0'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: rspec
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
- - - ">="
45
+ - - '>='
46
46
  - !ruby/object:Gem::Version
47
47
  version: '0'
48
48
  type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
- - - ">="
52
+ - - '>='
53
53
  - !ruby/object:Gem::Version
54
54
  version: '0'
55
55
  description:
@@ -59,9 +59,9 @@ executables: []
59
59
  extensions: []
60
60
  extra_rdoc_files: []
61
61
  files:
62
- - ".gitignore"
63
- - ".rspec"
64
- - ".travis.yml"
62
+ - .gitignore
63
+ - .rspec
64
+ - .travis.yml
65
65
  - Gemfile
66
66
  - LICENSE.txt
67
67
  - README.md
@@ -85,17 +85,17 @@ require_paths:
85
85
  - lib
86
86
  required_ruby_version: !ruby/object:Gem::Requirement
87
87
  requirements:
88
- - - ">="
88
+ - - '>='
89
89
  - !ruby/object:Gem::Version
90
90
  version: '0'
91
91
  required_rubygems_version: !ruby/object:Gem::Requirement
92
92
  requirements:
93
- - - ">="
93
+ - - '>='
94
94
  - !ruby/object:Gem::Version
95
95
  version: '0'
96
96
  requirements: []
97
97
  rubyforge_project:
98
- rubygems_version: 2.2.3
98
+ rubygems_version: 2.0.14.1
99
99
  signing_key:
100
100
  specification_version: 4
101
101
  summary: A set of helper classes to make ManageIQ automate development easier and