Module: DLDInternet::AWS::EC2::Instance_Types::MixIns::EC2_Instance_Types

Included in:
DLDInternet::AWS::EC2::Instance_Types
Defined in:
lib/dldinternet/aws/ec2/instance_types/mixins/ec2_instance_types.rb

Instance Method Summary collapse

Instance Method Details

#abort!(msg) ⇒ Object



78
79
80
# File 'lib/dldinternet/aws/ec2/instance_types/mixins/ec2_instance_types.rb', line 78

def abort!(msg)
  raise DLDInternet::AWS::EC2::Instance_Types::Error.new msg
end

#get_ec2_instance_typesObject

noinspection RubyDefParenthesesInspection



65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/dldinternet/aws/ec2/instance_types/mixins/ec2_instance_types.rb', line 65

def get_ec2_instance_types()

  client = DLDInternet::AWS::EC2::Instance_Types::AWSPricingAPIClient.new()

  begin
    # noinspection RubyParenthesesAfterMethodCallInspection
    return client.get_instance_types()
  rescue Exception => e
    puts "Unable to retrieve instance type details. Giving up ...".light_red
    return nil
  end
end

#get_file_format(path) ⇒ Object



14
15
16
17
18
19
20
21
22
23
# File 'lib/dldinternet/aws/ec2/instance_types/mixins/ec2_instance_types.rb', line 14

def get_file_format(path)
  format = case File.extname(File.basename(path)).downcase
             when /json|js/
               'json'
             when /yaml|yml/
               'yaml'
             else
               raise DLDInternet::AWS::EC2::Instance_Types::Error.new("Unsupported file type: #{path}")
           end
end

#load_ec2_instance_types(path) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/dldinternet/aws/ec2/instance_types/mixins/ec2_instance_types.rb', line 46

def load_ec2_instance_types(path)
  format = get_file_format(path)
  spec = File.read(path)
  case format
    when /json/
      JSON.parse(spec)
    #when /yaml/
    else
      begin
        YAML.load(spec)
      rescue Psych::SyntaxError => e
        abort! "Error in the template specification: #{e.message}\n#{spec.split(/\n/).map{|l| "#{i+=1}: #{l}"}.join("\n")}"
      end
    # else
    #   abort! "Unsupported file type: #{path}"
  end
end

#save_ec2_instance_types(path, it) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/dldinternet/aws/ec2/instance_types/mixins/ec2_instance_types.rb', line 25

def save_ec2_instance_types(path, it)
  format = get_file_format(path)
  begin
    File.open path, File::CREAT|File::TRUNC|File::RDWR, 0644 do |f|
      case format
        when /yaml/
          f.write it.to_yaml line_width: 1024, indentation: 4, canonical: false
        when /json/
          f.write JSON.pretty_generate(it, { indent: "\t", space: ' '})
        else
          f.write YAML::dump(it)
          # abort! "Unsupported save format #{format}!"
      end
      f.close
    end
  rescue
    abort! "!!! Could not write file #{path}: \nException: #{$!}\nParent directory exists? #{File.directory?(File.dirname(path))}\n"
  end
  0
end