Class: Iota::Function

Inherits:
Object
  • Object
show all
Defined in:
lib/iota/function.rb

Constant Summary collapse

DEFAULT_HANDLER =
{
  'nodejs' => {
    handler_name: 'index.handler',
    file_name: 'index.js'
  },
  'python2.7' => {
    handler_name: 'lambda_function.lambda_handler',
    file_name: 'lambda_function.py'
  }
}
RUNTIMES =
DEFAULT_HANDLER.keys
ENVIRONMENT_ALIAS_NAME_MAP =
{
  production: 'PROD',
  development: 'DEV'
}
PROD_VERSION_ALIAS_NAME =

TODO: multiple last_versions, multiple rollback

ENVIRONMENT_ALIAS_NAME_MAP[:production]
DEV_VERSION_ALIAS_NAME =
ENVIRONMENT_ALIAS_NAME_MAP[:development]
LAST_VERSION_SUFFIX =
'_LAST'
IGNORES =
%w(
  node_modules
  bin
  src
  test
  iota_conf.rb
)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, path) ⇒ Function

Returns a new instance of Function.



42
43
44
45
# File 'lib/iota/function.rb', line 42

def initialize(name, path)
  @name = name
  @path = path
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



40
41
42
# File 'lib/iota/function.rb', line 40

def name
  @name
end

#pathObject (readonly)

Returns the value of attribute path.



40
41
42
# File 'lib/iota/function.rb', line 40

def path
  @path
end

Instance Method Details

#create_alias(alias_name, version = '$LATEST') ⇒ Object



143
144
145
146
147
148
149
# File 'lib/iota/function.rb', line 143

def create_alias(alias_name, version='$LATEST')
  client.create_alias({
    function_name: name,
    name: alias_name,
    function_version: version
  })
end

#create_function(runtime, role) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/iota/function.rb', line 47

def create_function(runtime, role)
  if RUNTIMES.include? runtime
    Dir.mktmpdir do |tmpdir|
      zip_file_path = package_function(tmpdir)

      handler = DEFAULT_HANDLER[runtime]

      client.create_function({
        function_name: name,
        runtime: runtime,
        handler: handler[:handler_name],
        role: role || ENV['AWS_LAMBDA_IAM_ROLE'],
        code: {
          zip_file: File.open(zip_file_path, "rb")
        }
      })
    end
  end
end

#create_local(runtime) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/iota/function.rb', line 67

def create_local(runtime)
  Dir.mkdir path
  handler = DEFAULT_HANDLER[runtime]

  # write function
  File.open(File.join(path, handler[:file_name]), "w") do |file|
    template = File.read(File.join(templates_path, template_file_name[runtime]))
    template_code = ERB.new(template).result(binding)
    file.write(template_code)
  end

  # write config
  # TODO: 'iota.conf' is hard-coded
  File.open(File.join(path, 'iota.conf'), "w") do |file|
    role = ENV['AWS_LAMBDA_IAM_ROLE'] || ''
    template = File.read(File.join(templates_path, template_file_name['config']))
    template_config = ERB.new(template).result(binding)
    file.write(template_config)
  end

  path
end

#exists?Boolean

Returns:

  • (Boolean)


104
105
106
107
108
109
110
# File 'lib/iota/function.rb', line 104

def exists?
  begin
    !!remote
  rescue Aws::Lambda::Errors::ResourceNotFoundException
    false
  end
end

#list_aliasesObject

list aliases



98
99
100
101
102
# File 'lib/iota/function.rb', line 98

def list_aliases
  client.list_aliases({
    function_name: name
  }).aliases
end

#list_versionsObject

list versions



91
92
93
94
95
# File 'lib/iota/function.rb', line 91

def list_versions
  client.list_versions_by_function({
    function_name: name
  }).versions
end

#publish_versionObject



137
138
139
140
141
# File 'lib/iota/function.rb', line 137

def publish_version
  client.publish_version({
    function_name: name
  })
end

#remoteObject



112
113
114
115
116
# File 'lib/iota/function.rb', line 112

def remote
  client.get_function({
      function_name: name
  })
end

#update_alias(alias_, target_version) ⇒ Object



118
119
120
121
122
123
124
# File 'lib/iota/function.rb', line 118

def update_alias(alias_, target_version)
  client.update_alias({
    function_name: name,
    name: alias_,
    function_version: target_version
  })
end

#update_codeObject



126
127
128
129
130
131
132
133
134
135
# File 'lib/iota/function.rb', line 126

def update_code
  Dir.mktmpdir do |tmpdir|
    zip_file_path = package_function(tmpdir)
    resp = client.update_function_code(
      function_name: name,
      zip_file: File.open(zip_file_path, "rb"),
    )
    return resp
  end
end