Class: DigitalOceanUpdater

Inherits:
ProviderUpdater show all
Includes:
RegionArea
Defined in:
app/provider_updaters/digital_ocean_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

#initializeDigitalOceanUpdater

Returns a new instance of DigitalOceanUpdater.


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

def initialize
  super
  @prefixes = {
      'nyc' => 'US',
      'sfo' => 'US',
      'tor' => 'US',
      'lon' => 'EU',
      'fra' => 'EU',
      'ams' => 'EU',
      'sgp' => 'ASIA',
      'blr' => 'ASIA',
  }
end

Instance Method Details

#performObject


18
19
20
21
# File 'app/provider_updaters/digital_ocean_updater.rb', line 18

def perform
  @provider = Provider.find_or_create_by(name: 'Digital Ocean')
  update_compute_batch
end

#update_computeObject


29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'app/provider_updaters/digital_ocean_updater.rb', line 29

def update_compute
  token = ENV['DIGITALOCEAN_TOKEN']
  uri = URI('https://api.digitalocean.com/v2/sizes')

  request = Net::HTTP::Get.new(uri)
  request['Authorization']="Bearer #{token}"
  response = Net::HTTP.start(uri.hostname, uri.port, use_ssl: uri.scheme == 'https') { |http| http.request(request) }
  response.value

  pricelist = JSON.parse(response.body)
  @provider.more_attributes['pricelist'] = pricelist
  @provider.save!

  pricelist['sizes'].each do |instance_type|
    resource_id = instance_type['slug']

    instance_type['regions'].each do |region|
      resource = @provider.resources.find_or_create_by(name: resource_id, region: region)

      resource.resource_type = 'compute'
      resource.more_attributes['cores'] = instance_type['vcpus']
      resource.more_attributes['mem_gb'] = BigDecimal.new(instance_type['memory']) / 1024
      resource.more_attributes['price_per_hour'] = instance_type['price_hourly']
      resource.more_attributes['price_per_month'] = instance_type['price_monthly']
      resource.region_area = extract_region_area(region)
      resource.save!
    end
  end
end

#update_compute_batchObject


23
24
25
26
27
# File 'app/provider_updaters/digital_ocean_updater.rb', line 23

def update_compute_batch
  ActiveRecord::Base.transaction do
    update_compute
  end
end