Class: VagrantPlugins::SoftLayer::Action::CreateInstance

Inherits:
Object
  • Object
show all
Includes:
Util::Network, Util::Warden
Defined in:
lib/vagrant-softlayer/action/create_instance.rb

Overview

This creates a new instance.

Instance Method Summary collapse

Methods included from Util::Warden

#sl_warden

Methods included from Util::Network

#hostname, #ip_address, #ip_address_id, #ip_address_record, #ssh_keys

Constructor Details

#initialize(app, env) ⇒ CreateInstance

Returns a new instance of CreateInstance.



9
10
11
# File 'lib/vagrant-softlayer/action/create_instance.rb', line 9

def initialize(app, env)
  @app = app
end

Instance Method Details

#call(env) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/vagrant-softlayer/action/create_instance.rb', line 13

def call(env)
  @env = env

  @env[:ui].info I18n.t("vagrant_softlayer.vm.creating")

  sl_warden { env[:sl_product_order].verifyOrder(env[:sl_virtual_guest].generateOrderTemplate(order_template)) }

  result = sl_warden { env[:sl_virtual_guest].createObject(order_template) }
  @env[:machine].id = result["id"].to_s

  @app.call(@env)
end

#get_hostnameObject



26
27
28
# File 'lib/vagrant-softlayer/action/create_instance.rb', line 26

def get_hostname
  @env[:machine].provider_config.hostname || @env[:machine].config.vm.hostname
end

#get_vlan_id(vlan_name, vlan_space) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/vagrant-softlayer/action/create_instance.rb', line 30

def get_vlan_id(vlan_name, vlan_space)
  return vlan_name unless vlan_name.kind_of?(String)

  routers = @env[:sl_account].object_mask("mask[routers,routers.datacenter,routers.networkVlans,routers.networkVlans.networkSpace,routers.networkVlans.type]").getObject["routers"]

  routers.each do |router|
    next if @env[:machine].provider_config.datacenter && router["datacenter"]["name"] != @env[:machine].provider_config.datacenter
    router["networkVlans"].each do |vlan|
      vlan_qualified_name = [ router["hostname"].split('.').reverse.join('.'), vlan["vlanNumber"] ].join('.')
      return vlan["id"] if vlan.has_key?("name") && vlan["type"]["keyName"] == "STANDARD" && vlan["networkSpace"] == vlan_space.to_s.upcase && (vlan["name"] == vlan_name || vlan_qualified_name == vlan_name)
    end
  end

  raise Errors::SLVlanIdNotFound, :vlan_space => vlan_space.to_s, :vlan_name => vlan_name.inspect
end

#order_templateObject



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/vagrant-softlayer/action/create_instance.rb', line 46

def order_template
  template = {
    "dedicatedAccountHostOnlyFlag" => @env[:machine].provider_config.dedicated,
    "domain"                       => @env[:machine].provider_config.domain,
    "hostname"                     => get_hostname,
    "hourlyBillingFlag"            => @env[:machine].provider_config.hourly_billing,
    "localDiskFlag"                => @env[:machine].provider_config.local_disk,
    "maxMemory"                    => @env[:machine].provider_config.max_memory,
    "networkComponents"            => [ { :maxSpeed => @env[:machine].provider_config.network_speed } ],
    "privateNetworkOnlyFlag"       => @env[:machine].provider_config.private_only,
    "sshKeys"                      => ssh_keys(@env),
    "startCpus"                    => @env[:machine].provider_config.start_cpus
  }

  template["blockDevices"]                   =  @env[:machine].provider_config.disk_capacity.map{ |key,value| { "device"=> key.to_s, "diskImage" => { "capacity" => value.to_s } } } if @env[:machine].provider_config.disk_capacity
  template["blockDeviceTemplateGroup"]       = { :globalIdentifier => @env[:machine].provider_config.image_guid } if @env[:machine].provider_config.image_guid
  template["datacenter"]                     = { :name => @env[:machine].provider_config.datacenter } if @env[:machine].provider_config.datacenter
  template["operatingSystemReferenceCode"]   = @env[:machine].provider_config.operating_system if !@env[:machine].provider_config.image_guid
  template["postInstallScriptUri"]           = @env[:machine].provider_config.post_install if @env[:machine].provider_config.post_install
  template["primaryBackendNetworkComponent"] = { :networkVlan => { :id => get_vlan_id(@env[:machine].provider_config.vlan_private, :private) } } if @env[:machine].provider_config.vlan_private
  template["primaryNetworkComponent"]        = { :networkVlan => { :id => get_vlan_id(@env[:machine].provider_config.vlan_public,  :public) } } if @env[:machine].provider_config.vlan_public
  template["userData"]                       = [ { :value => @env[:machine].provider_config.user_data } ] if @env[:machine].provider_config.user_data

  return template
end