Module: CloudstackCli::OptionResolver

Included in:
Base
Defined in:
lib/cloudstack-cli/option_resolver.rb

Instance Method Summary collapse

Instance Method Details

#resolve_accountObject



59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/cloudstack-cli/option_resolver.rb', line 59

def 
  if options[:account]
    if  = client.list_accounts(name: options[:account], listall: true).first
      options[:account_id] = ['id']
      options[:domain_id] = ['domainid']
    else
      say "Error: Account #{options[:account]} not found.", :red
      exit 1
    end
  end
  options
end

#resolve_compute_offeringObject



149
150
151
152
153
154
155
156
157
# File 'lib/cloudstack-cli/option_resolver.rb', line 149

def resolve_compute_offering
  if offering = client.list_service_offerings(name: options[:offering]).first
    options[:service_offering_id] = offering['id']
  else
    say "Error: Offering #{options[:offering]} not found.", :red
    exit 1
  end
  options
end

#resolve_disk_offeringObject



159
160
161
162
163
164
165
166
167
168
# File 'lib/cloudstack-cli/option_resolver.rb', line 159

def resolve_disk_offering
  if options[:disk_offering]
    unless disk_offering = client.list_disk_offerings(name: options[:disk_offering]).first
      say "Error: Disk offering '#{options[:disk_offering]}' not found.", :red
      exit 1
    end
    options[:disk_offering_id] = disk_offering['id']
  end
  options
end

#resolve_domainObject



33
34
35
36
37
38
39
40
41
42
43
# File 'lib/cloudstack-cli/option_resolver.rb', line 33

def resolve_domain
  if options[:domain]
    if domain = client.list_domains(name: options[:domain]).first
      options[:domain_id] = domain['id']
    else
      say "Error: Domain #{options[:domain]} not found.", :red
      exit 1
    end
  end
  options
end

#resolve_host(type = "routing") ⇒ Object



196
197
198
199
200
201
202
203
204
205
206
# File 'lib/cloudstack-cli/option_resolver.rb', line 196

def resolve_host(type = "routing")
  if options[:host]
    args = { name: options[:host], type: type, listall: true }
    unless host = client.list_hosts(args).first
      say "Error: Host '#{options[:host]}' not found.", :red
      exit 1
    end
    options[:host_id] = host['id']
  end
  options
end

#resolve_isoObject



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/cloudstack-cli/option_resolver.rb', line 101

def resolve_iso
  if options[:iso]
    iso = false
    %w(self featured community).each do |iso_filter|
      iso = client.list_isos(
          name: options[:iso],
          project_id: options[:project_id],
          isofilter: iso_filter
      ).first
      break if iso
    end
    if iso
      options[:iso_id] = iso["id"]
    else
      say "Error: Iso '#{options[:iso]}' is invalid.", :red
      exit 1
    end
  end
  options
end

#resolve_iso_for_vm_deploymentObject



122
123
124
125
126
127
128
129
130
131
# File 'lib/cloudstack-cli/option_resolver.rb', line 122

def resolve_iso_for_vm_deployment
  unless options[:disk_offering_id]
    say "Error: a disk offering is required when using iso.", :red
    exit 1
  end
  resolve_iso
  options[:template_id] = options[:iso_id]
  options['hypervisor'] = (options[:hypervisor] || 'vmware')
  options
end

#resolve_networksObject



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/cloudstack-cli/option_resolver.rb', line 72

def resolve_networks
  networks = []
  available_networks = network = client.list_networks(
    zone_id: options[:zone_id],
    project_id: options[:project_id]
  )
  if options[:networks]
    options[:networks].each do |name|
      unless network = available_networks.find { |n| n['name'] == name }
        say "Error: Network '#{name}' not found.", :red
        exit 1
      end
      networks << network['id'] rescue nil
    end
  end
  networks.compact!
  if networks.empty?
    #unless default_network = client.list_networks(project_id: options[:project_id]).find {
    #  |n| n['isdefault'] == true }
    unless default_network = client.list_networks(project_id: options[:project_id]).first
      say "Error: No default network found.", :red
      exit 1
    end
    networks << available_networks.first['id'] rescue nil
  end
  options[:network_ids] = networks.join(',')
  options
end

#resolve_projectObject



45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/cloudstack-cli/option_resolver.rb', line 45

def resolve_project
  if options[:project]
    if %w(ALL -1).include? options[:project]
      options[:project_id] = "-1"
    elsif project = client.list_projects(name: options[:project], listall: true).first
      options[:project_id] = project['id']
    else
      say "Error: Project #{options[:project]} not found.", :red
      exit 1
    end
  end
  options
end

#resolve_snapshotObject



183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/cloudstack-cli/option_resolver.rb', line 183

def resolve_snapshot
  if options[:snapshot]
    args = { name: options[:snapshot], listall: true }
    args[:project_id] = options[:project_id]
    unless snapshot = client.list_snapshots(args).first
      say "Error: Snapshot '#{options[:snapshot]}' not found.", :red
      exit 1
    end
    options[:snapshot_id] = snapshot['id']
  end
  options
end

#resolve_templateObject



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/cloudstack-cli/option_resolver.rb', line 133

def resolve_template
  if options[:template]
    if template = client.list_templates(
        name: options[:template],
        template_filter: "executable",
        project_id: options[:project_id]
      ).first
      options[:template_id] = template['id']
    else
      say "Error: Template #{options[:template]} not found.", :red
      exit 1
    end
  end
  options
end

#resolve_virtual_machineObject



170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/cloudstack-cli/option_resolver.rb', line 170

def resolve_virtual_machine
  if options[:virtual_machine]
    args = { name: options[:virtual_machine], listall: true }
    args[:project_id] = options[:project_id]
    unless vm = client.list_virtual_machines(args).first
      say "Error: VM '#{options[:virtual_machine]}' not found.", :red
      exit 1
    end
    options[:virtual_machine_id] = vm['id']
  end
  options
end

#resolve_zoneObject



19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/cloudstack-cli/option_resolver.rb', line 19

def resolve_zone
  if options[:zone]
    zones = client.list_zones
    zone = zones.find {|z| z['name'] == options[:zone] }
    if !zone
      msg = options[:zone] ? "Zone '#{options[:zone]}' is invalid." : "No zone found."
      say "Error: #{msg}", :red
      exit 1
    end
    options[:zone_id] = zone['id']
  end
  options
end

#vm_options_to_paramsObject



4
5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/cloudstack-cli/option_resolver.rb', line 4

def vm_options_to_params
  resolve_zone
  resolve_project
  resolve_compute_offering
  resolve_template
  resolve_disk_offering
  resolve_iso_for_vm_deployment if options[:iso]
  options[:size] = options[:disk_size] if options[:disk_size]
  unless options[:template_id]
    say "Error: Template or ISO is required.", :red
    exit 1
  end
  resolve_networks
end