Class: KnifeCloudstack::CsTemplateCreate

Inherits:
Chef::Knife show all
Includes:
Chef::Knife::KnifeCloudstackBase
Defined in:
lib/chef/knife/cs_template_create.rb

Instance Method Summary collapse

Methods included from Chef::Knife::KnifeCloudstackBase

included

Instance Method Details

#runObject



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/chef/knife/cs_template_create.rb', line 81

def run
  validate_base_options

  Chef::Log.debug("Validate hostname and options")
  if  locate_config_value(:name)
    templatename = locate_config_value(:name)
  else
    templatename = @name_args.first
    unless /^[a-zA-Z0-9][a-zA-Z0-9_\-#]*$/.match templatename then
      ui.error "Invalid templatename. Please specify a simple name without any spaces"
      exit 1
    end
  end

  print "#{ui.color("Creating template: #{templatename}", :magenta)}\n"

  params = {
    'command' => 'createTemplate',
    'name' => templatename,
    'displaytext' => locate_config_value(:displaytext),
    'ostypeid' => locate_config_value(:ostypeid),
    'volumeid' => locate_config_value(:volumeid),
  }
  params['ispublic'] = locate_config_value(:ispublic) if locate_config_value(:ispublic)
  params['isfeatured'] = locate_config_value(:isfeatured) if locate_config_value(:isfeatured)
  params['passwordenabled'] = locate_config_value(:passwordenabled) if locate_config_value(:passwordenabled)
  params['extractable'] = locate_config_value(:extractable) if locate_config_value(:extractable)
  json = connection.send_request(params)

  if ! json then
    ui.error("Unable to create template")
  exit 1
  end

  print "Template #{json['id']} is being created in the background\n";

  return json['id']
end