Module: CloudstackCloner::OptionResolver
- Included in:
- Cli
- Defined in:
- lib/cloudstack_cloner/option_resolver.rb
Instance Method Summary collapse
- #resolve_account(options = options) ⇒ Object
- #resolve_compute_offering(options = options) ⇒ Object
- #resolve_disk_offering(options = options) ⇒ Object
- #resolve_domain(options = options) ⇒ Object
- #resolve_iso(options = options) ⇒ Object
- #resolve_networks(options = options) ⇒ Object
- #resolve_project(options = options) ⇒ Object
- #resolve_snapshot(options = options) ⇒ Object
- #resolve_template(options = options) ⇒ Object
- #resolve_virtual_machine(options = options.dup) ⇒ Object
- #resolve_zone(options = options) ⇒ Object
- #vm_options_to_params(options = options) ⇒ Object
Instance Method Details
#resolve_account(options = options) ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/cloudstack_cloner/option_resolver.rb', line 58 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 #{options[:account]} not found.", :red exit 1 end end end |
#resolve_compute_offering(options = options) ⇒ Object
135 136 137 138 139 140 141 142 143 144 145 |
# File 'lib/cloudstack_cloner/option_resolver.rb', line 135 def resolve_compute_offering( = ) if [:offering] if offering = client.list_service_offerings(name: [:offering]).first [:service_offering_id] = offering['id'] else say "Error: Offering #{options[:offering]} not found.", :red exit 1 end end end |
#resolve_disk_offering(options = options) ⇒ Object
147 148 149 150 151 152 153 154 155 156 |
# File 'lib/cloudstack_cloner/option_resolver.rb', line 147 def resolve_disk_offering( = ) if [:disk_offering] unless disk_offering = client.list_disk_offerings(name: [:disk_offering]).first say "Error: Disk offering '#{options[:disk_offering]}' not found.", :red exit 1 end [:disk_offering_id] = disk_offering['id'] end end |
#resolve_domain(options = options) ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/cloudstack_cloner/option_resolver.rb', line 32 def resolve_domain( = ) if [:domain] if domain = client.list_domains(name: [:domain]).first [:domain_id] = domain['id'] else say "Error: Domain #{options[:domain]} not found.", :red exit 1 end end end |
#resolve_iso(options = options) ⇒ Object
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/cloudstack_cloner/option_resolver.rb', line 100 def resolve_iso( = ) if [:iso] unless iso = client.list_isos( name: [:iso], project_id: [:project_id] ).first say "Error: Iso '#{options[:iso]}' is invalid.", :red exit 1 end unless [:diskoffering_id] say "Error: a disk offering is required when using iso.", :red exit 1 end [:template_id] = iso['id'] ['hypervisor'] = ([:hypervisor] || 'vmware') end end |
#resolve_networks(options = options) ⇒ Object
71 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 |
# File 'lib/cloudstack_cloner/option_resolver.rb', line 71 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: options[:project_id]).find { # |n| n['isdefault'] == true } 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(options = options) ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/cloudstack_cloner/option_resolver.rb', line 44 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 #{options[:project]} not found.", :red exit 1 end end end |
#resolve_snapshot(options = options) ⇒ Object
171 172 173 174 175 176 177 178 179 180 181 182 |
# File 'lib/cloudstack_cloner/option_resolver.rb', line 171 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 '#{options[:snapshot]}' not found.", :red exit 1 end [:snapshot_id] = snapshot['id'] end end |
#resolve_template(options = options) ⇒ Object
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/cloudstack_cloner/option_resolver.rb', line 119 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 #{options[:template]} not found.", :red exit 1 end end end |
#resolve_virtual_machine(options = options.dup) ⇒ Object
158 159 160 161 162 163 164 165 166 167 168 169 |
# File 'lib/cloudstack_cloner/option_resolver.rb', line 158 def resolve_virtual_machine( = .dup) if [:virtual_machine] args = { name: [:virtual_machine], listall: true } args[:project_id] = [:project_id] unless vm = client.list_virtual_machines(args).first say "Error: VM '#{options[:virtual_machine]}' not found.", :red exit 1 end [:virtual_machine_id] = vm['id'] end end |
#resolve_zone(options = options) ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/cloudstack_cloner/option_resolver.rb', line 18 def resolve_zone( = ) if [:zone] zones = client.list_zones zone = zones.find {|z| z['name'] == [:zone] } if !zone msg = [:zone] ? "Zone '#{options[:zone]}' is invalid." : "No zone found." say "Error: #{msg}", :red exit 1 end [:zone_id] = zone['id'] end end |
#vm_options_to_params(options = options) ⇒ Object
4 5 6 7 8 9 10 11 12 13 14 15 16 |
# File 'lib/cloudstack_cloner/option_resolver.rb', line 4 def ( = ) resolve_zone() resolve_project() resolve_compute_offering() resolve_template() resolve_disk_offering() resolve_iso() unless [:template_id] say "Error: Template or ISO is required.", :red exit 1 end resolve_networks() end |