miq_dev_util 0.1.1 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 33a5c74387d9a90323882a0e7f42bfd9c5f9b6a4
4
- data.tar.gz: ef62be82dd8cdbe7a413c8b475525601635bbbe5
3
+ metadata.gz: f2483591e6af4106bc28390033ee7eb5e2777a69
4
+ data.tar.gz: ed101b6f73c32ce124b2ce6da506959a9e79dde8
5
5
  SHA512:
6
- metadata.gz: 52e186e3600569868cb0d81df87fca5c07ca228f7f167f1ea1678ffb51d35d10b573e313ac31ab906ecd8d685fc23177d03b4a0f149ec87793c180317bd1faa2
7
- data.tar.gz: 7b313077919931919e36539d471fd4d783d32e33fd5e487161b30a2a153cd371aed45127b633b46939eb5f22901511a5cae93812269b1c1c543dd2b029004adc
6
+ metadata.gz: 348952bfb3fa0fc9fdd08e9e27e66678bcb54832458361fd69ec553207f23f3c031bb042d45deead4dc0a000f05766c2a9f11dcc0b9043bd0ce039290e29ff2f
7
+ data.tar.gz: 467a0d3fecf09f34da2c85ecc8fd387f18970b529dcae44d20fa57055d68685b67973ccf36f7215b9d41a84f0398053b8c59c0a813d325942a34c230142b3325
data/.gitignore CHANGED
@@ -8,3 +8,4 @@
8
8
  /spec/reports/
9
9
  /tmp/
10
10
  *.swp
11
+ .DS_Store
data/Gemfile CHANGED
@@ -2,3 +2,4 @@ source 'https://rubygems.org'
2
2
 
3
3
  # Specify your gem's dependencies in miq_dev_util.gemspec
4
4
  gemspec
5
+ gem 'simplecov', :require => false, :group => :test
data/README.md CHANGED
@@ -4,13 +4,87 @@ 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
+
7
8
  ## Installation
8
9
 
9
- $ gem install miq_dev_util
10
+ ```
11
+ $ gem install miq_dev_util
12
+ ```
13
+
10
14
 
11
15
  ## Usage
12
16
 
13
- require 'miq_dev_util'
17
+ ```ruby
18
+ # Pull the gem in to we can use it
19
+ require 'miq_dev_util'
20
+ ```
21
+
22
+ ### Logging ###
23
+
24
+ ```ruby
25
+ @logger = MiqDevUtil::Logger.new($evm, 'my_method_name')
26
+ @logger.log(:info, 'Hello World')
27
+
28
+ @logger.dump_root
29
+
30
+ @logger.dump_attributes($evm.root['vm'], 'root vm')
31
+ @logger.dump_associations($evm.root['vm'], 'root vm')
32
+ @logger.dump_virtual_columns($evm.root['vm'], 'root vm')
33
+
34
+ @logger.dump_info($evm.root['vm'], 'root vm') # dumps attributes,
35
+ # associations and
36
+ # virtual_columns
37
+ ```
38
+
39
+ ### EMS Credentials ###
40
+
41
+ ```ruby
42
+ vm = $evm.root['vm']
43
+ credentials = MiqDevUtil::EMS.get_credentials(vm.ext_management_system)
44
+ vim = RbVmomi::VIM.connect credentials
45
+ ```
46
+
47
+ ### Automate Model ###
48
+
49
+ ```ruby
50
+ automate_helper = MiqDevUtil::Automate.new($evm)
51
+
52
+ # Instantiate an automate instance at path or raise an exception with the
53
+ # message provided if the instantiation returns nil (not found).
54
+ automate_helper.instantiate_or_raise(path, message)
55
+
56
+ # This is a hacky workaround used to get an instance without executing the
57
+ # methods on it. It fails if a message is passed in the path or if the
58
+ # message field on the any of the methods are *.
59
+ automate_helper.get_instance_with_attributes(path)
60
+
61
+
62
+ # Condense multiple types of VM lookups into one call. This is useful when
63
+ # making an Automate method generic enough to be used during provisioning,
64
+ # with a custom button, or as a catalog item.
65
+ #
66
+ # Lookup methods used and their order can be overridden by specifying
67
+ # :lookup_order. The default is [:rootvm, :dialog_id, :provision]
68
+ #
69
+ # * :rootvm = $evm.root['vm']
70
+ # * :dialog_id = look up the VM in vmdb using the VMDB ID from a dialog
71
+ # * :provision = $evm.root['miq_provision'].vm
72
+ #
73
+ #
74
+ # The dialog name that may hold the miq ID is specified via :dialog_name
75
+ #
76
+
77
+ vm = resolve_vm
78
+ vm = resolve_vm(lookup_order: [:dialog_id], dialog_name: 'dialog_vm_id')
79
+ ```
80
+
81
+ ### Generic Code ###
82
+
83
+ ```ruby
84
+ # Perform a deep copy on objects that support marshalling.
85
+ MiqDevUtil::Code.deep_copy(object)
86
+ ```
87
+
14
88
 
15
89
  ## Development
16
90
 
@@ -18,6 +92,7 @@ After checking out the repo, run `bin/setup` to install dependencies. Then, run
18
92
 
19
93
  To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
20
94
 
95
+
21
96
  ## Contributing
22
97
 
23
98
  Bug reports and pull requests are welcome on GitHub at https://github.com/ewannema/miq_dev_util.
@@ -1,34 +1,77 @@
1
- # The Automate class is intended to hold methods that are useful when
2
- # interacting with the ManageIQ automate system directly.
1
+ module MiqDevUtil
2
+ # The Automate class is intended to hold methods that are useful when
3
+ # interacting with the ManageIQ automate system directly.
4
+ class Automate
5
+ def initialize(evm)
6
+ @evm = evm
7
+ end
3
8
 
4
- class MiqDevUtil::Automate
9
+ # Instantiate an automate instance at path or raise an exception with the
10
+ # message provided if the instantiation returns nil (not found).
11
+ def instantiate_or_raise(path, message)
12
+ object = @evm.instantiate(path)
13
+ if object.nil?
14
+ raise message
15
+ end
5
16
 
6
- def initialize(evm)
7
- @evm = evm
8
- end
17
+ object
18
+ end
9
19
 
10
- # Instantiate an automate instance at path or raise an exception with the
11
- # message provided if the instantiation returns nil (not found).
12
- def instantiate_or_raise(path, message)
13
- object = @evm.instantiate(path)
14
- if object.nil?
15
- raise message
20
+ # This is a hacky workaround used to get an instance without executing the
21
+ # methods on it. It fails if a message is passed in the path or if the
22
+ # message field on the any of the methods are *.
23
+ def get_instance_with_attributes(path)
24
+ if path =~ /#/
25
+ raise "Does not work with messages in the path."
26
+ end
27
+ fake_message = "callingWithAFakeMessage"
28
+ empty_instance = @evm.instantiate("#{path}##{fake_message}")
29
+ instance_name = empty_instance.name
30
+ @evm.instance_get(instance_name)
16
31
  end
17
32
 
18
- object
19
- end
33
+ # Condense multiple types of VM lookups into one call. This is useful when
34
+ # making an Automate method generic enough to be used during provisioning,
35
+ # with a custom button, or as a catalog item.
36
+ #
37
+ # Lookup methods used and their order can be overridden by specifying
38
+ # :lookup_order
39
+ #
40
+ # The dialog name that may hold the miq ID is specified via :dialog_name
41
+ def resolve_vm(lookup_order: [:rootvm, :dialog_id, :provision],
42
+ dialog_name: 'dialog_target_server')
43
+
44
+ vm = nil
45
+ lookup_order.each do |lu_method|
46
+ vm = vm_lookup_by(lu_method, dialog_name)
47
+
48
+ # If we found a VM we can stop looking
49
+ break unless vm.nil?
50
+ end
51
+
52
+ vm
53
+ end
54
+
55
+ private
56
+
57
+ def vm_lookup_by(lu_method, dialog_name = nil)
58
+ case lu_method
59
+ when :rootvm then @evm.root['vm']
60
+ when :dialog_id then vm_by_id(@evm.root[dialog_name])
61
+ when :provision then provision_vm
62
+ else fail "unknown lookup method #{lu_method}"
63
+ end
64
+ end
20
65
 
21
- # This is a hacky workaround used to get an instance without executing the
22
- # methods on it. It fails if a message is passed in the path or if the
23
- # message field on the any of the methods are *.
24
- def get_instance_with_attributes(path)
25
- if path =~ /#/
26
- raise "Does not work with messages in the path."
66
+ def vm_by_id(vm_id)
67
+ @evm.vmdb('vm_or_template', vm_id)
68
+ rescue StandardError
69
+ nil
70
+ end
71
+
72
+ def provision_vm
73
+ return nil unless @evm.root['miq_provision'].respond_to?('vm')
74
+ @evm.root['miq_provision'].vm
27
75
  end
28
- fake_message = "callingWithAFakeMessage"
29
- empty_instance = @evm.instantiate("#{path}##{fake_message}")
30
- instance_name = empty_instance.name
31
- @evm.instance_get(instance_name)
32
76
  end
33
77
  end
34
-
@@ -1,3 +1,3 @@
1
1
  module MiqDevUtil
2
- VERSION = "0.1.1"
2
+ VERSION = "0.2.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: miq_dev_util
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.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-07-30 00:00:00.000000000 Z
11
+ date: 2015-10-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler