Class: Retrospec::Puppet::Generators::TypeGenerator

Inherits:
Retrospec::Plugins::V1::Plugin
  • Object
show all
Defined in:
lib/retrospec/plugins/v1/plugin/generators/type_generator.rb

Constant Summary collapse

CORE_TYPES =

this is the list of core puppet types that cannot be recreated

%w(augeas computer cron exec file filebucket
group host interface k5login macauthorization mailalias
maillist mcx mount nagios_command nagios_contact
nagios_contactgroup nagios_host nagios_hostdependency
nagios_hostescalation nagios_hostextinfo nagios_hostgroup
nagios_service nagios_servicedependency
nagios_serviceescalation nagios_serviceextinfo
nagios_servicegroup nagios_timeperiod notify package
resources router schedule scheduled_task selboolean
selmodule service ssh_authorized_key sshkey stage
tidy user vlan yumrepo zfs zone zpool)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(module_path, spec_object = {}) ⇒ TypeGenerator

retrospec will initilalize this class so its up to you to set any additional variables you need to get the job done.



24
25
26
27
28
29
30
31
32
33
34
# File 'lib/retrospec/plugins/v1/plugin/generators/type_generator.rb', line 24

def initialize(module_path, spec_object = {})
  super
  # below is the Spec Object which serves as a context for template rendering
  # you will need to initialize this object, so the erb templates can get the binding
  # the SpecObject can be customized to your liking as its different for every plugin gem.
  if CORE_TYPES.include?(spec_object[:name])
    raise Retrospec::Puppet::Generators::CoreTypeException
  end
  @context = OpenStruct.new(:type_name => spec_object[:name], :parameters => spec_object[:parameters],
                            :properties => spec_object[:properties], :providers => spec_object[:providers])
end

Instance Attribute Details

#contextObject (readonly)

Returns the value of attribute context.



7
8
9
# File 'lib/retrospec/plugins/v1/plugin/generators/type_generator.rb', line 7

def context
  @context
end

#template_dirObject (readonly)

Returns the value of attribute template_dir.



7
8
9
# File 'lib/retrospec/plugins/v1/plugin/generators/type_generator.rb', line 7

def template_dir
  @template_dir
end

Class Method Details

.run_cli(global_opts, args = ARGV) ⇒ Object

used to display subcommand options to the cli the global options are passed in for your usage trollop.rubyforge.org all options here are available in the config passed into config object returns the parameters



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/retrospec/plugins/v1/plugin/generators/type_generator.rb', line 49

def self.run_cli(global_opts, args=ARGV)
  sub_command_opts = Trollop.options(args) do
    banner <<-EOS
Generates a new type with the given name, parameters, and properties.

    EOS
    opt :name, 'The name of the type you wish to create', :type => :string, :required => true, :short => '-n'
    opt :parameters, 'A list of parameters to initialize your type with', :type => :strings, :required => false,
                                                                          :short => '-p', :default => ['name']
    opt :properties, 'A list of properties to initialize your type with', :type => :strings, :required => false,
                                                                          :short => '-a', :default => []
    opt :providers, 'A list of providers to create and associate with this type', :type => :strings,
                                                                                  :default => ['default'], :required => false
  end
  unless sub_command_opts[:name]
    Trollop.educate
    exit 1
  end
  plugin_data = global_opts.merge(sub_command_opts)
  plugin_data
end

Instance Method Details

#generate_provider_filesObject



87
88
89
90
91
92
93
94
# File 'lib/retrospec/plugins/v1/plugin/generators/type_generator.rb', line 87

def generate_provider_files
  providers = context.providers
  providers.each do |provider|
    plugin_data = { :name => provider, :type => type_name, :template_dir => config_data[:template_dir] }
    p = Retrospec::Puppet::Generators::ProviderGenerator.new(module_path, plugin_data)
    p.generate_provider_files
  end
end

#generate_type_filesObject



96
97
98
99
100
# File 'lib/retrospec/plugins/v1/plugin/generators/type_generator.rb', line 96

def generate_type_files
  safe_create_template_file(type_name_path, File.join(template_dir, 'type_template.rb.retrospec.erb'), context)
  generate_provider_files
  type_name_path
end

#generate_type_spec_filesObject

this will look through all



103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/retrospec/plugins/v1/plugin/generators/type_generator.rb', line 103

def generate_type_spec_files
  type_files = Dir.glob(File.join(type_dir, '**', '*.rb')).sort
  spec_files = []
  type_files.each do |type_file|
    type_file_data = Retrospec::Puppet::Type.load_type(type_file)
    # because many facts can be in a single file we want to create a unique file for each fact
    type_spec_path = File.join(type_spec_dir, "#{type_file_data.name}_spec.rb")
    spec_files << type_spec_path
    safe_create_template_file(type_spec_path, File.join(template_dir, 'type_spec.rb.retrospec.erb'), type_file_data)
  end
  spec_files
end

#type_dirObject



71
72
73
# File 'lib/retrospec/plugins/v1/plugin/generators/type_generator.rb', line 71

def type_dir
  @type_dir ||= File.join(module_path, 'lib', 'puppet', 'type')
end

#type_nameObject



83
84
85
# File 'lib/retrospec/plugins/v1/plugin/generators/type_generator.rb', line 83

def type_name
  context.type_name
end

#type_name_pathObject



79
80
81
# File 'lib/retrospec/plugins/v1/plugin/generators/type_generator.rb', line 79

def type_name_path
  File.join(type_dir, "#{type_name}.rb")
end

#type_spec_dirObject



75
76
77
# File 'lib/retrospec/plugins/v1/plugin/generators/type_generator.rb', line 75

def type_spec_dir
  @type_spec_dir ||= File.join(module_path, 'spec', 'unit', 'puppet', 'type')
end