Module: Addax::Commands

Defined in:
lib/addax/commands.rb

Constant Summary collapse

OUTPUTS =
{
  :generic_error          => "ERROR. Something went wrong with the server.",
  :not_found              => "ERROR. Resource not found.",
  :platform_data_missing  => "ERROR. Platform data file (format: zip) is missing.",
  :created_resource       => "Successfully created resource.",
  :updated_resource       => "Successfully updated resource.",
  :destroyed_resource     => "Successfully destroyed resource.",
  :security_tokens_error  => "ERROR. Could not fetch security tokens from the server."
}

Class Method Summary collapse

Class Method Details

.create_driver(params) ⇒ Object



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/addax/commands.rb', line 120

def self.create_driver(params)
  param_hash = normalize_params(params)

  authenticity_token, cookie = security_tokens

  begin
    response = RestClient.post "#{SERVER_URL}/drivers.json",
                               {:driver => param_hash, :authenticity_token => authenticity_token},
                               {:cookies => cookie}
  rescue => e
    abort OUTPUTS[:generic_error] + '\n' + format_error(e.response.body)
  else
    puts OUTPUTS[:updated_resource]
    format(response.body)
  end
end

.create_operating_system(params) ⇒ Object Also known as: create_os



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/addax/commands.rb', line 137

def self.create_operating_system(params)
  param_hash = normalize_params(params)

  authenticity_token, cookie = security_tokens

  begin
    response = RestClient.post "#{SERVER_URL}/operating_systems.json",
                               {:operating_system => param_hash,:authenticity_token => authenticity_token},
                               {:cookies => cookie}
  rescue => e
    abort OUTPUTS[:generic_error] + '\n' + format_error(e.response.body)
  else
    puts OUTPUTS[:created_resource]
    format(response.body)
  end
end

.create_platform(params) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/addax/commands.rb', line 83

def self.create_platform(params)
  param_hash = normalize_params(params)

  authenticity_token, cookie = security_tokens

  # The params should at least have a platform_data param
  platform_data = param_hash.delete(:platform_data)
  unless platform_data
    abort OUTPUTS[:platform_data_missing]
  end

  begin
    load_platform_data_response = RestClient.post "#{SERVER_URL}/platforms/load_file.json",
                                                  {:platform_data => File.new(platform_data, 'rb'), :authenticity_token => authenticity_token},
                                                  {:cookies => cookie}
  rescue => e
    abort OUTPUTS[:generic_error] + '\n' + format_error(e.response.body)
  end

  encoded_cache_name = load_platform_data_response.body

  if param_hash[:root_component]
    begin
      create_platform_response = RestClient.post "#{SERVER_URL}/platforms.json?cache=#{encoded_cache_name}",
                                                 {:platform => param_hash, :authenticity_token => authenticity_token},
                                                 {:cookies => cookie}
    rescue => e
      abort OUTPUTS[:generic_error] + '\n' + format_error(e.response.body)
    else
      puts OUTPUTS[:created_resource]
      format(create_platform_response.body)
    end
  else
    abort "ERROR: Root component choice not yet implemented!"
  end
end

.destroy(object_type, object_id) ⇒ Object



169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/addax/commands.rb', line 169

def self.destroy(object_type, object_id)
  authenticity_token, cookie = security_tokens
  begin
    response = RestClient.delete "#{SERVER_URL}/#{object_type}s/#{object_id}.json", {:cookies => cookie}
  rescue => e
    case e.response.code
      when 404 then
        abort OUTPUTS[:not_found]
      else
        abort OUTPUTS[:generic_error]
      end
  else
    puts OUTPUTS[:destroyed_resource]
  end
end

.fetch_driver(object_id, file_path) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/addax/commands.rb', line 60

def self.fetch_driver(object_id, file_path)
  begin
    response = RestClient.get "#{SERVER_URL}/drivers/#{object_id}/show_attachment"
  rescue => e
    case e.response.code
      when 404 then
        abort OUTPUTS[:not_found]
      else
        abort OUTPUTS[:generic_error]
    end
  else
    file_name = response.headers[:content_disposition].scan(/filename="(.+)"/)[0][0] + ".xml"
    File.open File.join(file_path, file_name), "w" do |f|
      f.write(response.body)
    end
    puts "File was written to #{File.join file_path, file_name}"
  end
end

.fetch_platform(object_id, file_path) ⇒ Object



79
80
81
# File 'lib/addax/commands.rb', line 79

def self.fetch_platform(object_id, file_path)
  abort "ERROR: Platform data fetch not implemented yet!"
end

.generate(driver_id, file_path) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/addax/commands.rb', line 41

def self.generate(driver_id, file_path)
  begin
    response = RestClient.post "#{SERVER_URL}/drivers/#{driver_id}/generate_sources", {}
  rescue => e
    case e.response.code
      when 404 then
        abort OUTPUTS[:not_found]
      else
        abort OUTPUTS[:generic_error]
    end
  else
    file_name = response.headers[:content_disposition].scan(/filename="(.+)"/)[0][0] + ".zip"
    File.open File.join(file_path, file_name), "w" do |f|
      f.write(response.body)
    end
    puts "File was written to #{File.join(file_path, file_name)}"
  end
end

.list(type) ⇒ Object



14
15
16
17
18
19
20
21
22
23
# File 'lib/addax/commands.rb', line 14

def self.list(type)
  type = type == 'oses' ? 'operating_systems' : type
  begin
    response = RestClient.get "#{SERVER_URL}/#{type}.json"
  rescue => e
    abort OUTPUTS[:generic_error]
  else
    format(response.body)
  end
end

.show(model, id) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/addax/commands.rb', line 25

def self.show(model, id)
  type = type == 'os' ? 'operating_system' : type
  begin
    response = RestClient.get "#{SERVER_URL}/#{model}s/#{id}.json"
  rescue => e
    case e.response.code
      when 404 then
        abort OUTPUTS[:not_found]
      else
        abort OUTPUTS[:generic_error]
    end
  else
    format(response.body)
  end
end

.update(object_type, object_id, params) ⇒ Object



154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/addax/commands.rb', line 154

def self.update(object_type, object_id, params)
  object_type = object_type == "os" ? "operating_system" : object_type
  param_hash = normalize_params(params)

  begin
    response = RestClient.put "#{SERVER_URL}/#{object_type}s/#{object_id}.json",
                              {object_type.to_sym => param_hash}
  rescue => e
    abort OUTPUTS[:generic_error] + '\n' + format_error(e.response.body)
  else
    puts OUTPUTS[:updated_resource]
    format(response.body)
  end
end