Class: RackspaceUpdater

Inherits:
ProviderUpdater show all
Includes:
RegionArea
Defined in:
app/provider_updaters/rackspace_updater.rb

Constant Summary

Constants included from RegionArea

RegionArea::UNKNOWN

Instance Method Summary collapse

Methods included from RegionArea

#extract_region_area

Methods inherited from ProviderUpdater

#extract_sla, load_providers, #logger, providers, update_providers

Constructor Details

#initializeRackspaceUpdater

Returns a new instance of RackspaceUpdater.


6
7
8
9
10
11
12
13
14
15
16
# File 'app/provider_updaters/rackspace_updater.rb', line 6

def initialize
  super
  @prefixes = {
      'DFW' => 'US',
      'IAD' => 'US',
      'ORD' => 'US',
      'LON' => 'EU',
      'HKG' => 'ASIA',
      'SYD' => 'ASIA'
  }
end

Instance Method Details

#get_pricing_docObject (private)


116
117
118
119
120
121
# File 'app/provider_updaters/rackspace_updater.rb', line 116

def get_pricing_doc
  # Rackspace does not have a pricing API so we need to scrape their website.
  uri = URI('https://www.rackspace.com/cloud/public-pricing')
  doc = Nokogiri::HTML(open(uri))
  doc
end

#number_from(field) ⇒ Object (private)


123
124
125
# File 'app/provider_updaters/rackspace_updater.rb', line 123

def number_from(field)
  field.text.delete('^0-9.').to_d
end

#performObject


18
19
20
21
22
23
# File 'app/provider_updaters/rackspace_updater.rb', line 18

def perform
  doc = get_pricing_doc
  update_provider
  update_compute_batch(doc)
  # update_storage(doc)
end

#update_compute(doc) ⇒ Object (private)


75
76
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
103
104
105
106
107
108
109
110
111
112
113
114
# File 'app/provider_updaters/rackspace_updater.rb', line 75

def update_compute(doc)
  # Rackspace does not differentiate prices between different regions, hence the regions are not crawble from the pricing tables
  # We hardcode the regions here, taken from https://www.rackspace.com/about/datacenters
  regions = ['LON','SYD','DFW','IAD','ORD','HKG']
  pricelist = {}

  pricing_rows = doc.css('[data-currency="USD"]').css('.pricing-row-linux')
  pricing_rows.each do |row|
    data = row.css('td')
    resource_id = data[0].text
    price_per_hour = number_from(data.css('.pricing-col-raw.pricing-col-hourly'))
    price_per_hour += number_from(data.css('.pricing-col-inf.pricing-col-hourly'))
    price_per_month = number_from(data.css('.pricing-col-raw.pricing-col-monthly'))
    price_per_month += number_from(data.css('.pricing-col-inf.pricing-col-monthly'))
    pricelist[resource_id] = {
        'type' => 'compute',
        'mem_gb' => data[1].text.to_d.to_s,
        'cores' => data[2].text,
        'price_per_hour' => price_per_hour.to_s,
        'price_per_month' => price_per_month.to_s,
        'bandwidth_mbps' => number_from(data[5]).to_s,
    }
  end

  provider = Provider.find_by(name: 'Rackspace')
  provider.more_attributes['pricelist']['compute'] = pricelist
  provider.save!

  pricelist.each_pair do |resource_id, data|
    regions.each do |region|
      resource = provider.resources.find_or_create_by(name: resource_id, region: region)
      resource.region = region
      resource.region_area = extract_region_area(region)
      resource.more_attributes = data
      resource.resource_type = 'compute'
      resource.save!
    end
  end

end

#update_compute_batch(doc) ⇒ Object (private)


69
70
71
72
73
# File 'app/provider_updaters/rackspace_updater.rb', line 69

def update_compute_batch(doc)
  ActiveRecord::Base.transaction do
    update_compute(doc)
  end
end

#update_providerObject (private)


27
28
29
30
31
32
33
34
# File 'app/provider_updaters/rackspace_updater.rb', line 27

def update_provider
  provider = Provider.find_or_create_by(name: 'Rackspace')
  provider.more_attributes['pricelist'] ||= {}
  provider.more_attributes['sla'] = {
      uri: 'https://www.rackspace.com/information/legal/cloud/sla'
  }
  provider.save!
end

#update_storage(doc) ⇒ Object (private)


36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'app/provider_updaters/rackspace_updater.rb', line 36

def update_storage(doc)
  table = doc.css('div.horizontal-scroll')[4].css('.cloud-pricing-table')[0]
  resource_id = 'cloud-files'

  # Rackspace does not differentiate prices between different regions, hence the regions are not crawble from the pricing tables
  # We hardcode the regions here, taken from https://www.rackspace.com/about/datacenters
  regions = ['LON','SYD','DFW','IAD','ORD','HKG']

  price_per_month_gb =  number_from(table.css('.pricing-col-monthly')[0])
  price_per_hour_gb = number_from(table.css('.pricing-col-hourly')[0])

  pricelist = {
      'type' => 'storage',
      'price_per_hour_gb' => price_per_hour_gb,
      'price_per_month_gb' => price_per_month_gb
  }

  regions.each do |region|
    provider = Provider.find_by(name: 'Rackspace')
    resource = provider.resources.find_or_create_by(name: resource_id, region: region)
    resource.more_attributes['price_per_hour_gb'] = price_per_hour_gb
    resource.more_attributes['price_per_month_gb'] = price_per_month_gb
    resource.region_area = extract_region_area(region)
    resource.resource_type = 'storage'
    resource.save!

  end

  provider = Provider.find_by(name: 'Rackspace')
  provider.more_attributes['pricelist']['storage'] = pricelist
  provider.save!
end