Module: CloudstackCli::OptionResolver
- Included in:
- Base
- Defined in:
- lib/cloudstack-cli/option_resolver.rb
Instance Method Summary collapse
- #resolve_account ⇒ Object
- #resolve_cluster ⇒ Object
- #resolve_compute_offering ⇒ Object
- #resolve_disk_offering ⇒ Object
- #resolve_domain ⇒ Object
- #resolve_host(type = "routing") ⇒ Object
- #resolve_ip_network_list ⇒ Object
- #resolve_iso ⇒ Object
- #resolve_iso_for_vm_deployment ⇒ Object
- #resolve_networks ⇒ Object
- #resolve_project ⇒ Object
- #resolve_snapshot ⇒ Object
- #resolve_template ⇒ Object
- #resolve_virtual_machine(return_vm = false) ⇒ Object
- #resolve_zone ⇒ Object
- #vm_options_to_params ⇒ Object
Instance Method Details
#resolve_account ⇒ Object
64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/cloudstack-cli/option_resolver.rb', line 64 def resolve_account if [:account] if account = client.list_accounts(name: [:account], listall: true).first [:account_id] = account['id'] [:domain_id] = account['domainid'] else say "Error: Account #{[:account]} not found.", :red exit 1 end end end |
#resolve_cluster ⇒ Object
244 245 246 247 248 249 250 251 252 253 |
# File 'lib/cloudstack-cli/option_resolver.rb', line 244 def resolve_cluster if [:cluster] unless cluster = client.list_clusters(name: [:cluster]).first say "Error: Cluster '#{[:cluster]}' not found.", :red exit 1 end [:cluster_id] = cluster['id'] end end |
#resolve_compute_offering ⇒ Object
178 179 180 181 182 183 184 185 186 |
# File 'lib/cloudstack-cli/option_resolver.rb', line 178 def resolve_compute_offering if offering = client.list_service_offerings(name: [:offering]).first [:service_offering_id] = offering['id'] else say "Error: Offering #{[:offering]} not found.", :red exit 1 end end |
#resolve_disk_offering ⇒ Object
188 189 190 191 192 193 194 195 196 197 |
# File 'lib/cloudstack-cli/option_resolver.rb', line 188 def resolve_disk_offering if [:disk_offering] unless disk_offering = client.list_disk_offerings(name: [:disk_offering]).first say "Error: Disk offering '#{[:disk_offering]}' not found.", :red exit 1 end [:disk_offering_id] = disk_offering['id'] end end |
#resolve_domain ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/cloudstack-cli/option_resolver.rb', line 38 def resolve_domain if [:domain] if domain = client.list_domains(name: [:domain]).first [:domain_id] = domain['id'] else say "Error: Domain #{[:domain]} not found.", :red exit 1 end end end |
#resolve_host(type = "routing") ⇒ Object
232 233 234 235 236 237 238 239 240 241 242 |
# File 'lib/cloudstack-cli/option_resolver.rb', line 232 def resolve_host(type = "routing") if [:host] args = { name: [:host], type: type, listall: true } unless host = client.list_hosts(args).first say "Error: Host '#{[:host]}' not found.", :red exit 1 end [:host_id] = host['id'] end end |
#resolve_ip_network_list ⇒ Object
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/cloudstack-cli/option_resolver.rb', line 104 def resolve_ip_network_list network_list = [] available_networks = network = client.list_networks( zone_id: [:zone_id], project_id: [:project_id] ) if [:ip_network_list] [:ip_network_list].each do |item| unless network = available_networks.find { |n| n['name'] == item["name"] } say "Error: Network '#{item["name"]}' not found.", :red exit 1 end item.delete("name") network_list << {networkid: network["id"]}.merge(item) end end network_list.compact! if network_list.empty? say "Error: IP network list can't be empty.", :red exit 1 end [:ip_to_network_list] = network_list [:network_ids, :ip_address].each { |k| .delete(k) } end |
#resolve_iso ⇒ Object
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 |
# File 'lib/cloudstack-cli/option_resolver.rb', line 130 def resolve_iso if [:iso] iso = false %w(self featured community).each do |iso_filter| iso = client.list_isos( name: [:iso], project_id: [:project_id], isofilter: iso_filter ).first break if iso end if iso [:iso_id] = iso["id"] else say "Error: Iso '#{[:iso]}' is invalid.", :red exit 1 end end end |
#resolve_iso_for_vm_deployment ⇒ Object
151 152 153 154 155 156 157 158 159 160 |
# File 'lib/cloudstack-cli/option_resolver.rb', line 151 def resolve_iso_for_vm_deployment unless [:disk_offering_id] say "Error: a disk offering is required when using iso.", :red exit 1 end resolve_iso [:template_id] = [:iso_id] ['hypervisor'] = ([:hypervisor] || 'vmware') end |
#resolve_networks ⇒ Object
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/cloudstack-cli/option_resolver.rb', line 77 def resolve_networks networks = [] available_networks = network = client.list_networks( zone_id: [:zone_id], project_id: [:project_id] ) if [:networks] [: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: [:project_id]).first say "Error: No default network found.", :red exit 1 end networks << available_networks.first['id'] rescue nil end [:network_ids] = networks.join(',') end |
#resolve_project ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/cloudstack-cli/option_resolver.rb', line 50 def resolve_project if [:project] if %w(ALL -1).include? [:project] [:project_id] = "-1" elsif project = client.list_projects(name: [:project], listall: true).first [:project_id] = project['id'] else say "Error: Project #{[:project]} not found.", :red exit 1 end end end |
#resolve_snapshot ⇒ Object
219 220 221 222 223 224 225 226 227 228 229 230 |
# File 'lib/cloudstack-cli/option_resolver.rb', line 219 def resolve_snapshot if [:snapshot] args = { name: [:snapshot], listall: true } args[:project_id] = [:project_id] unless snapshot = client.list_snapshots(args).first say "Error: Snapshot '#{[:snapshot]}' not found.", :red exit 1 end [:snapshot_id] = snapshot['id'] end end |
#resolve_template ⇒ Object
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 |
# File 'lib/cloudstack-cli/option_resolver.rb', line 162 def resolve_template if [:template] if template = client.list_templates( name: [:template], template_filter: "executable", project_id: [:project_id] ).first [:template_id] = template['id'] else say "Error: Template #{[:template]} not found.", :red exit 1 end end end |
#resolve_virtual_machine(return_vm = false) ⇒ Object
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 |
# File 'lib/cloudstack-cli/option_resolver.rb', line 199 def resolve_virtual_machine(return_vm = false) if [:virtual_machine] unless vm = client.list_virtual_machines( name: [:virtual_machine], listall: true, project_id: [:project_id] ).find {|vm| vm["name"] == [:virtual_machine] } say "Error: VM '#{[:virtual_machine]}' not found.", :red exit 1 end if return_vm return vm else [:virtual_machine_id] = vm["id"] end end end |
#resolve_zone ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/cloudstack-cli/option_resolver.rb', line 24 def resolve_zone if [:zone] zones = client.list_zones zone = zones.find {|z| z['name'] == [:zone] } if !zone msg = [:zone] ? "Zone '#{[:zone]}' is invalid." : "No zone found." say "Error: #{msg}", :red exit 1 end [:zone_id] = zone['id'] end end |
#vm_options_to_params ⇒ Object
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/cloudstack-cli/option_resolver.rb', line 4 def resolve_zone resolve_project resolve_compute_offering resolve_template resolve_disk_offering resolve_iso_for_vm_deployment if [:iso] [:size] = [:disk_size] if [:disk_size] unless [:template_id] say "Error: Template or ISO is required.", :red exit 1 end if [:ip_network_list] resolve_ip_network_list else resolve_networks end end |