Class: OpenStudioAmis

Inherits:
Object
  • Object
show all
Includes:
Logging
Defined in:
lib/openstudio/lib/ami_list.rb

Overview

Class for managing the AMI ids based on the openstudio version and the openstudio-server version

Constant Summary collapse

VALID_OPTIONS =
[
  :openstudio_version, :openstudio_server_version, :host, :url, :stable
]

Instance Method Summary collapse

Methods included from Logging

configure_logger_for, #logger, logger_for

Constructor Details

#initialize(version = 1, options = {}) ⇒ OpenStudioAmis

Initializer for listing the AMIs and grabbing the correct version of the AMIs based on the OpenStudio version or OpenStudio Server version.

Parameters:

  • version (Double) (defaults to: 1)

    Version of the JSON to return. Currently only version 1 or 2

  • options (Hash) (defaults to: {})

    Values to operate on

Options Hash (options):

  • :openstudio_version (String)

    Version of OpenStudio to perform the AMI lookup

  • :openstudio_server_version (String)

    Version of the OpenStudio to perform the AMI lookup



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/openstudio/lib/ami_list.rb', line 52

def initialize(version = 1, options = {})
  invalid_options = options.keys - VALID_OPTIONS
  if invalid_options.any?
    fail ArgumentError, "invalid option(s): #{invalid_options.join(', ')}"
  end

  if options[:openstudio_version] && options[:openstudio_server_version]
    fail 'Must pass only an openstudio_version or openstudio_server_version when looking up AMIs'
  end

  # merge in some defaults
  defaults = {
    openstudio_version: 'default',
    openstudio_server_version: 'default',
    # host: 'developer.nrel.gov',
    # url: '/downloads/buildings/openstudio/api'
    host: 's3.amazonaws.com',
    url: '/openstudio-resources/server/api'
  }

  @version = version
  @options = defaults.merge(options)

  if @options[:openstudio_version] != 'default' && @options[:openstudio_server_version] != 'default'
    fail 'Must pass either the openstudio_version or openstudio_server_version when looking up AMIs, not both'
  end
end

Instance Method Details

#get_amisObject

Return the AMIs for the specific openstudio_version or openstudio_server_version



97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/openstudio/lib/ami_list.rb', line 97

def get_amis
  amis = nil
  command = "get_ami_version_#{@version}"
  if OpenStudioAmis.method_defined?(command)
    amis = send(command)
  else
    fail "Unknown api version command #{command}"
  end

  fail "Could not find any amis for #{@version}" if amis.nil?

  amis
end

#listObject

List the AMIs based on the version and host. This method does catch old ‘developer.nrel.gov’ hosts and formats the endpoint using the old ‘<host>/<url>/amis_#version.json’ instead of the new, more restful, syntax of <host>/<url>/v#version/amis.json



83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/openstudio/lib/ami_list.rb', line 83

def list
  endpoint = nil

  # for backwards compatibility with developer
  if @options[:host] =~ /developer.nrel/
    endpoint = "#{@options[:url]}/amis_v#{@version}.json"
  else
    endpoint = "#{@options[:url]}/v#{@version}/amis.json"
  end

  retrieve_json(endpoint)
end