Class: HammerCLICsv::CsvCommand::OrganizationsCommand

Inherits:
BaseCommand
  • Object
show all
Defined in:
lib/hammer_cli_csv/organizations.rb

Constant Summary collapse

ORGANIZATION =
'Organization'
DESCRIPTION =
'Description'

Constants inherited from BaseCommand

BaseCommand::COUNT, BaseCommand::NAME

Instance Method Summary collapse

Methods inherited from BaseCommand

#associate_locations, #associate_organizations, #build_os_name, #collect_column, #execute, #export_column, #foreman_architecture, #foreman_domain, #foreman_environment, #foreman_filter, #foreman_location, #foreman_operatingsystem, #foreman_organization, #foreman_partitiontable, #foreman_permission, #foreman_role, #foreman_template_kind, #hammer, #hammer_context, #katello_contentview, #katello_hostcollection, #katello_repository, #katello_subscription, #labelize, #lifecycle_environment, #namify, #pluralize, #split_os_name, #thread_import

Instance Method Details

#create_organizations_from_csv(line) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/hammer_cli_csv/organizations.rb', line 84

def create_organizations_from_csv(line)
  line[COUNT].to_i.times do |number|
    name = namify(line[NAME], number)
    label = namify(line[ORGANIZATION], number)
    if !@existing.include? name
      print "Creating organization '#{name}'... " if option_verbose?
      @api.resource(:organizations).call(:create, {
                                   'name' => name,
                                   'label' => label,
                                   'description' => line[DESCRIPTION]
                                 })
    else
      print "Updating organization '#{name}'... " if option_verbose?
      @api.resource(:organizations).call(:update, {
                                   'id' => label,
                                   'description' => line[DESCRIPTION]
                                 })
    end
    print "done\n" if option_verbose?
  end
end

#exportObject



45
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
71
# File 'lib/hammer_cli_csv/organizations.rb', line 45

def export
  CSV.open(option_csv_file || '/dev/stdout', 'wb', {:force_quotes => true}) do |csv|
    csv << [NAME, COUNT, ORGANIZATION, DESCRIPTION]
    if option_sam?
      server = option_server || HammerCLI::Settings.get(:csv, :host)
      username = option_username || HammerCLI::Settings.get(:csv, :username)
      password = option_password || HammerCLI::Settings.get(:csv, :password)
      url = "#{server}/api/organizations"
      uri = URI(url)
      Net::HTTP.start(uri.host, uri.port,
                      :use_ssl => uri.scheme == 'https',
                      :verify_mode => OpenSSL::SSL::VERIFY_NONE) do |http|
        request = Net::HTTP::Get.new uri.request_uri
        request.basic_auth(username, password)
        response = http.request(request)

        JSON.parse(response.body).each do |organization|
          csv << [organization['name'], 1, organization['label'], organization['description']]
        end
      end
    else
      @api.resource(:organizations).call(:index, {:per_page => 999999})['results'].each do |organization|
        csv << [organization['name'], 1, organization['label'], organization['description']]
      end
    end
  end
end

#importObject



73
74
75
76
77
78
79
80
81
82
# File 'lib/hammer_cli_csv/organizations.rb', line 73

def import
  @existing = {}
  @api.resource(:organizations).call(:index, {:per_page => 999999})['results'].each do |organization|
    @existing[organization['name']] = organization['id'] if organization
  end

  thread_import do |line|
    create_organizations_from_csv(line)
  end
end