Skip to content

Commit

Permalink
Merge 7b9ad70 into 1605177
Browse files Browse the repository at this point in the history
  • Loading branch information
hartmantis committed Aug 1, 2015
2 parents 1605177 + 7b9ad70 commit c5b38fa
Show file tree
Hide file tree
Showing 8 changed files with 97 additions and 61 deletions.
19 changes: 10 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,27 +93,28 @@ Attributes:

***vmware_fusion_config***

Used to manage the configuration of VMware Fusion.
An execute resource customized to handle the configuration of VMware Fusion
through its included initialization script.

Syntax:

vmware_fusion_config 'default' do
license 'abc123'
action :configure
action :create
end

Actions:

| Action | Description |
|--------------|-------------------|
| `:configure` | Configure the app |
| Action | Description |
|-----------|-------------------|
| `:create` | Configure the app |

Attributes:

| Attribute | Default | Description |
|------------|----------------|-------------------------|
| license | `nil` | An optional license key |
| action | `:configure` | Action(s) to perform |
| Attribute | Default | Description |
|------------|-----------|-------------------------|
| license | `nil` | An optional license key |
| action | `:create` | Action(s) to perform |

Providers
=========
Expand Down
2 changes: 1 addition & 1 deletion libraries/provider_vmware_fusion.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def whyrun_supported?
action :configure do
vmware_fusion_config new_resource.name do
license new_resource.license
action :configure
action :create
end
end

Expand Down
41 changes: 22 additions & 19 deletions libraries/provider_vmware_fusion_config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,41 +18,44 @@
# limitations under the License.
#

require 'net/http'
require 'chef/provider/lwrp_base'
require 'chef/provider/execute'
require_relative 'provider_vmware_fusion'
require_relative 'resource_vmware_fusion_config'

class Chef
class Provider
# A Chef provider for VMWare Fusion configuration.
# A Chef provider for VMWare Fusion configuration. Since that config is
# just an initialization script, build based off an execute resource.
#
# @author Jonathan Hartman <j@p4nt5.com>
class VmwareFusionConfig < Provider::LWRPBase
use_inline_resources

class VmwareFusionConfig < Provider::Execute
provides :vmware_fusion_config, platform_family: 'mac_os_x'

alias_method :action_create, :action_run

private

#
# WhyRun is supported by this provider.
# Overload the command string to call the VMware initialization tool.
#
# @return [TrueClass, FalseClass]
# (see Execute#command)
#
def whyrun_supported?
true
def command
path = ::File.join(VmwareFusion::PATH,
'Contents/Library/Initialize VMware Fusion.tool')
"#{path.gsub(' ', '\\ ')} set '' '' '' '#{new_resource.license}'"
end

#
# Use an execute resource to call the VMF initialize script.
# Redact the license key from the command's description.
#
action :configure do
path = ::File.join(VmwareFusion::PATH,
'Contents/Library/Initialize VMware Fusion.tool')
cmd = "#{path.gsub(' ', '\\ ')} set '' '' '#{new_resource.license}'"
execute 'Initialize VMware Fusion' do
command cmd
sensitive(new_resource.license.nil? ? false : true)
action :run
# (see Execute#description)
#
def description
if new_resource.license.nil?
super
else
super.gsub(new_resource.license, '*' * 16)
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion libraries/resource_vmware_fusion.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class VmwareFusion < Resource::LWRPBase
# (see Resource#to_text)
#
def to_text
license.nil? ? super : super.gsub(license, '****************')
license.nil? ? super : super.gsub(license, '*' * 16)
end
end
end
Expand Down
21 changes: 13 additions & 8 deletions libraries/resource_vmware_fusion_config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,30 +18,35 @@
# limitations under the License.
#

require 'chef/resource/lwrp_base'
require 'chef/resource/execute'

class Chef
class Resource
# A Chef resource for VMWare Fusion configuration.
# A Chef resource for VMware Fusion configuration. Since that config is
# just an initialization script, build based off an execute resource.
#
# @author Jonathan Hartman <j@p4nt5.com>
class VmwareFusionConfig < Resource::LWRPBase
class VmwareFusionConfig < Resource::Execute
self.resource_name = :vmware_fusion_config
actions :configure
default_action :configure
self.allowed_actions = [:nothing, :create]
default_action :create

#
# Attribute for an optional VMware Fusion license key
# Add an attribute for an optional VMware Fusion license key.
#
attribute :license, kind_of: String, default: nil
# @return [NilClass, String]
#
def license(arg = nil)
set_or_return(:license, arg, kind_of: String, default: nil)
end

#
# Override resource's text rendering to remove license strings.
#
# (see Resource#to_text)
#
def to_text
license.nil? ? super : super.gsub(license, '****************')
license.nil? ? super : super.gsub(license, '*' * 16)
end
end
end
Expand Down
65 changes: 46 additions & 19 deletions spec/libraries/provider_vmware_fusion_config_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,42 +22,69 @@
end
end

describe '#whyrun_supported?' do
it 'returns true' do
expect(provider.whyrun_supported?).to eq(true)
describe '#action_create' do
it 'aliases to the execute resource #action_run method' do
expect_any_instance_of(described_class).to receive(:converge_by)
provider.action_create
end
end

describe '#action_configure' do
describe '#command' do
let(:license) { nil }
let(:new_resource) do
r = super()
r.license(license) unless license.nil?
r
end

shared_examples_for 'any resource' do
it 'uses an execute to initialize VMware Fusion' do
p = provider
expect(p).to receive(:execute).with('Initialize VMware Fusion')
.and_yield
cmd = '/Applications/VMware\\ Fusion.app/Contents/Library/' \
"Initialize\\ VMware\\ Fusion.tool set '' '' '#{license}'"
expect(p).to receive(:command).with(cmd)
expect(p).to receive(:sensitive).with(license.nil? ? false : true)
expect(p).to receive(:action).with(:run)
p.action_configure
context 'a resource without a license' do
let(:license) { nil }

it 'returns the license-less command' do
expected = '/Applications/VMware\\ Fusion.app/Contents/Library/' \
"Initialize\\ VMware\\ Fusion.tool set '' '' '' ''"
expect(provider.send(:command)).to eq(expected)
end
end

context 'an all-default resource' do
it_behaves_like 'any resource'
context 'a resource with a license' do
let(:license) { 'abc123' }

it 'returns the licensed command' do
expected = '/Applications/VMware\\ Fusion.app/Contents/Library/' \
"Initialize\\ VMware\\ Fusion.tool set '' '' '' 'abc123'"
expect(provider.send(:command)).to eq(expected)
end
end
end

context 'a resource with a given license key' do
describe '#description' do
let(:license) { nil }
let(:new_resource) do
r = super()
r.license(license) unless license.nil?
r
end

context 'a resource without a license' do
let(:license) { nil }

it 'returns the regular output' do
expected = '/Applications/VMware\\ Fusion.app/Contents/Library/' \
"Initialize\\ VMware\\ Fusion.tool set '' '' '' ''"
expect(provider.send(:description)).to eq(expected)
end
end

context 'a resource with a license' do
let(:license) { 'abc123' }

it_behaves_like 'any resource'
it 'returns with the redacted license' do
expected = '/Applications/VMware\\ Fusion.app/Contents/Library/' \
'Initialize\\ VMware\\ Fusion.tool set ' \
"'' '' '' '#{'*' * 16}'"
expect(provider.send(:description)).to eq(expected)
end
end
end
end
2 changes: 1 addition & 1 deletion spec/libraries/provider_vmware_fusion_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
p = provider
expect(p).to receive(:vmware_fusion_config).with(name).and_yield
expect(p).to receive(:license).with(license)
expect(p).to receive(:action).with(:configure)
expect(p).to receive(:action).with(:create)
p.action_configure
end
end
Expand Down
6 changes: 3 additions & 3 deletions spec/libraries/resource_vmware_fusion_config_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@
end

it 'sets the correct supported actions' do
expect(resource.allowed_actions).to eq([:nothing, :configure])
expect(resource.allowed_actions).to eq([:nothing, :create])
end

it 'sets the correct default action' do
expect(resource.action).to eq([:configure])
expect(resource.action).to eq([:create])
end
end

Expand Down Expand Up @@ -74,7 +74,7 @@
let(:license) { 'abc123' }

it 'sanitizes the license field' do
expect(resource.to_text).to include('license "****************"')
expect(resource.to_text).to include("license \"#{'*' * 16}\"")
end
end
end
Expand Down

0 comments on commit c5b38fa

Please sign in to comment.