Sha256: 132e3e0a4cb4e1fda1c1a043223c0edb4ca413db8bf67a18763adb61ee3d8947

Contents?: true

Size: 1.86 KB

Versions: 1

Compression:

Stored size: 1.86 KB

Contents

require "cloud/instancetype/version"
require "json"

module Cloud
  # Describe a public cloud instance type
  class InstanceType
    attr_reader :name, :sort_value, :key, :category, :vcpu_count, :ram_bytes,
      :ram_si_units, :details

    def initialize(key, raw_data)
      @key = key
      @name = raw_data["name"]
      @sort_value = raw_data["sort_value"]
      @vcpu_count = raw_data["vcpu_count"]
      @ram_bytes = raw_data["ram_bytes"]
      @ram_si_units = raw_data["ram_si_units"]
      @details = raw_data["details"]
      return unless raw_data["category_key"]

      @category = Cloud::InstanceCategory.new(
        raw_data["category_key"], 
        raw_data["category"]
      )
    end

    def <=>(other)
      sort_value <=> other.sort_value
    end

    class << self
      def for(cloud)
        load(rails_data_path(cloud))
      end

      def load(data_path)
        raw_collection = get_raw_collection(data_path)
        instance_type_collection_factory(
          raw_collection["instance_types"],
          raw_collection["categories"]
        )
      end

      private

      def rails_data_path(cloud)
        Rails.root.join("config", "data", "#{cloud}-types.json")
      end

      def get_raw_collection(data_path)
        JSON.parse(File.read(data_path))
      end

      def instance_type_collection_factory(raw_types, raw_categories)
        raw_types.collect do |key, values|
          values["category_key"] = values["category"]
          values["category"] = raw_categories[values["category"]]
          InstanceType.new(key, values)
        end.sort!
      end
    end
  end

  # Describe a public cloud category
  class InstanceCategory
    attr_reader :key, :name, :description, :features

    def initialize(key, args)
      @key = key
      @name = args["name"]
      @description = args["description"]
      @features = args["features"]
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
cloud-instancetype-1.0.0 lib/cloud/instancetype.rb