Project

General

Profile

Download (2.55 KB) Statistics
| Branch: | Tag: | Revision:

hammer-cli-csv / lib / hammer_cli_csv / compute_resources.rb @ f92e695a

1
require 'hammer_cli'
2
require 'json'
3
require 'csv'
4

    
5
module HammerCLICsv
6
  class CsvCommand
7
    class ComputeResourcesCommand < BaseCommand
8
      command_name 'compute-resources'
9
      desc 'import or export compute resources'
10

    
11
      ORGANIZATIONS = 'Organizations'
12
      LOCATIONS = 'Locations'
13
      DESCRIPTION = 'Description'
14
      PROVIDER = 'Provider'
15
      URL = 'URL'
16

    
17
      def export(csv)
18
        csv << [NAME, ORGANIZATIONS, LOCATIONS, DESCRIPTION, PROVIDER, URL]
19
        @api.resource(:compute_resources).call(:index, {
20
            :per_page => 999999,
21
            :search => option_search
22
        })['results'].each do |compute_resource|
23
          compute_resource = @api.resource(:compute_resources).call(:show, {'id' => compute_resource['id']})
24

    
25
          name = compute_resource['name']
26
          organizations = export_column(compute_resource, 'organizations', 'name')
27
          locations = export_column(compute_resource, 'locations', 'name')
28
          description = compute_resource['description']
29
          provider = compute_resource['provider']
30
          url = compute_resource['url']
31
          csv << [name, organizations, locations, description, provider, url]
32
        end
33
      end
34

    
35
      def import
36
        @existing = {}
37
        @api.resource(:compute_resources).call(:index, {:per_page => 999999})['results'].each do |compute_resource|
38
          @existing[compute_resource['name']] = compute_resource['id'] if compute_resource
39
        end
40

    
41
        thread_import do |line|
42
          create_compute_resources_from_csv(line)
43
        end
44
      end
45

    
46
      def create_compute_resources_from_csv(line)
47
        count(line[COUNT]).times do |number|
48
          name = namify(line[NAME], number)
49
          params = {
50
            'compute_resource' => {
51
              'name' => name,
52
              'url' => line[URL],
53
              'provider' => line[PROVIDER]
54
            }
55
          }
56
          if !@existing.include? name
57
            print _("Creating compute resource '%{name}'...") % {:name => name} if option_verbose?
58
            id = @api.resource(:compute_resources).call(:create, params)['id']
59
          else
60
            print _("Updating compute resource '%{name}'...") % {:name => name} if option_verbose?
61
            id = @existing[name]
62
            params['id'] = id
63
            @api.resource(:compute_resources).call(:update, params)
64
          end
65

    
66
          associate_organizations(id, line[ORGANIZATIONS], 'compute_resource')
67
          associate_locations(id, line[LOCATIONS], 'compute_resource')
68

    
69
          puts _("done") if option_verbose?
70
        end
71
      end
72
    end
73
  end
74
end