fog-aws 0.5.0 → 0.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,44 @@
1
+ module Fog
2
+ module AWS
3
+ class Lambda
4
+ class Real
5
+ require 'fog/aws/parsers/lambda/base'
6
+
7
+ # Returns the configuration information of the Lambda function.
8
+ # http://docs.aws.amazon.com/lambda/latest/dg/API_GetFunction.html
9
+ # ==== Parameters
10
+ # * FunctionName <~String> - Lambda function name.
11
+ # ==== Returns
12
+ # * response<~Excon::Response>:
13
+ # * body<~Hash>:
14
+ # * 'CodeSize' <~Integer> - size, in bytes, of the function .zip file you uploaded.
15
+ # * 'Description' <~String> - user-provided description.
16
+ # * 'FunctionArn' <~String> - Amazon Resource Name (ARN) assigned to the function.
17
+ # * 'FunctionName' <~String> - name of the function.
18
+ # * 'Handler' <~String> - function Lambda calls to begin executing your function.
19
+ # * 'LastModified' <~Time> - timestamp of the last time you updated the function.
20
+ # * 'Memorysize' <~String> - memory size, in MB, you configured for the function.
21
+ # * 'Role' <~String> - ARN of the IAM role that Lambda assumes when it executes your function to access any other AWS resources.
22
+ # * 'Runtime' <~String> - runtime environment for the Lambda function.
23
+ # * 'Timeout' <~Integer> - function execution time at which Lambda should terminate the function.
24
+ def get_function_configuration(params={})
25
+ function_name = params.delete('FunctionName')
26
+ request({
27
+ :method => 'GET',
28
+ :path => "/functions/#{function_name}/versions/HEAD/configuration",
29
+ :parser => Fog::AWS::Parsers::Lambda::Base.new
30
+ }.merge(params))
31
+ end
32
+ end
33
+
34
+ class Mock
35
+ def get_function_configuration(params={})
36
+ response = self.get_function(params)
37
+ function_configuration = response.body['Configuration']
38
+ response.body = function_configuration
39
+ response
40
+ end
41
+ end
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,52 @@
1
+ module Fog
2
+ module AWS
3
+ class Lambda
4
+ class Real
5
+ require 'fog/aws/parsers/lambda/base'
6
+
7
+ # Returns the access policy, containing a list of permissions granted via the AddPermission API, associated with the specified bucket.
8
+ # http://docs.aws.amazon.com/lambda/latest/dg/API_GetPolicy.html
9
+ # ==== Parameters
10
+ # * FunctionName <~String> - Function name whose access policy you want to retrieve.
11
+ # ==== Returns
12
+ # * response<~Excon::Response>:
13
+ # * body<~Hash>:
14
+ # * 'Policy' <~Hash> - The access policy associated with the specified function.
15
+ def get_policy(params={})
16
+ function_name = params.delete('FunctionName')
17
+ request({
18
+ :method => 'GET',
19
+ :path => "/functions/#{function_name}/versions/HEAD/policy",
20
+ :parser => Fog::AWS::Parsers::Lambda::Base.new
21
+ }.merge(params))
22
+ end
23
+ end
24
+
25
+ class Mock
26
+ def get_policy(params={})
27
+ response = Excon::Response.new
28
+
29
+ function = self.get_function_configuration(params).body
30
+ function_arn = function['FunctionArn']
31
+ statements = self.data[:permissions][function_arn] || []
32
+
33
+ if statements.empty?
34
+ message = "ResourceNotFoundException => "
35
+ message << "The resource you requested does not exist."
36
+ raise Fog::AWS::Lambda::Error, message
37
+ end
38
+
39
+ policy = {
40
+ 'Version' => '2012-10-17',
41
+ 'Statement' => statements,
42
+ 'Id' => 'default'
43
+ }
44
+
45
+ response.status = 200
46
+ response.body = { 'Policy' => policy }
47
+ response
48
+ end
49
+ end
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,85 @@
1
+ module Fog
2
+ module AWS
3
+ class Lambda
4
+ class Real
5
+
6
+ # Invokes a specified Lambda function.
7
+ # http://docs.aws.amazon.com/lambda/latest/dg/API_Invoke.html
8
+ # ==== Parameters
9
+ # * ClientContext <~Hash> - client-specific information to the Lambda function you are invoking.
10
+ # * FunctionName <~String> - Lambda function name.
11
+ # * InvocationType <~String> - function invocation type.
12
+ # * LogType <~String> - logs format for function calls of "RequestResponse" invocation type.
13
+ # * Payload <~Integer> - Lambda function input.
14
+ # ==== Returns
15
+ # * response<~Excon::Response>:
16
+ # * body<~Hash> - JSON representation of the object returned by the Lambda function.
17
+ def invoke(params={})
18
+ headers = {}
19
+ if client_context = params.delete('ClientContext')
20
+ headers['X-Amz-Client-Context'] =
21
+ Base64::encode64(Fog::Json.encode(client_context))
22
+ end
23
+ if client_type = params.delete('InvocationType')
24
+ headers['X-Amz-Client-Type'] = client_type
25
+ end
26
+ if log_type = params.delete('LogType')
27
+ headers['X-Amz-Log-Type'] = log_type
28
+ end
29
+ payload = Fog::JSON.encode(params.delete('Payload'))
30
+ function_name = params.delete('FunctionName')
31
+
32
+ request({
33
+ :method => 'POST',
34
+ :path => "/functions/#{function_name}/invocations",
35
+ :headers => headers,
36
+ :body => payload,
37
+ :expects => [200, 202, 204]
38
+ }.merge(params))
39
+ end
40
+ end
41
+
42
+ class Mock
43
+ def invoke(params={})
44
+ response = Excon::Response.new
45
+ response.status = 200
46
+ response.body = ''
47
+
48
+ unless function_id = params.delete('FunctionName')
49
+ message = 'AccessDeniedException => '
50
+ message << 'Unable to determine service/operation name to be authorized'
51
+ raise Fog::AWS::Lambda::Error, message
52
+ end
53
+
54
+ client_context = params.delete('ClientContext')
55
+ client_type = params.delete('InvocationType')
56
+ log_type = params.delete('LogType')
57
+ payload = params.delete('Payload')
58
+
59
+ if (client_context || client_type || log_type)
60
+ message = "invoke parameters handling are not yet mocked [light_black](#{caller.first})[/]"
61
+ Fog::Logger.warning message
62
+ Fog::Mock.not_implemented
63
+ end
64
+
65
+ if payload
66
+ message = "payload parameter is ignored since we are not really "
67
+ message << "invoking a function [light_black](#{caller.first})[/]"
68
+ Fog::Logger.warning message
69
+ end
70
+
71
+ function = self.get_function_configuration('FunctionName' => function_id).body
72
+
73
+ if function.is_a?(Hash) && function.has_key?('FunctionArn')
74
+ response.body = "\"Imagine #{function['FunctionArn']} was invoked\""
75
+ else
76
+ message = "ResourceNotFoundException => Function not found: #{function_id}"
77
+ raise Fog::AWS::Lambda::Error, message
78
+ end
79
+
80
+ response
81
+ end
82
+ end
83
+ end
84
+ end
85
+ end
@@ -0,0 +1,69 @@
1
+ module Fog
2
+ module AWS
3
+ class Lambda
4
+ class Real
5
+ # Returns a list of event source mappings where you can identify a stream as an event source.
6
+ # http://docs.aws.amazon.com/lambda/latest/dg/API_ListEventSourceMappings.html
7
+ # ==== Parameters
8
+ # * EventSourceArn <~String> - Amazon Resource Name (ARN) of the stream.
9
+ # * FunctionName <~String> - name of the Lambda function.
10
+ # * Marker <~String> - opaque pagination token returned from a previous ListEventSourceMappings operation.
11
+ # * MaxItems <~Integer> - maximum number of event sources to return in response.
12
+ # ==== Returns
13
+ # * response<~Excon::Response>:
14
+ # * body<~Hash>:
15
+ # * 'EventSourceMappings' <~Array> - array of EventSourceMappingConfiguration objects.
16
+ # * 'NextMarker' <~String> - present if there are more event source mappings.
17
+ def list_event_source_mappings(params={})
18
+ event_source_arn = params.delete('EventSourceArn')
19
+ function_name = params.delete('FunctionName')
20
+ marker = params.delete('Marker')
21
+ max_items = params.delete('MaxItems')
22
+
23
+ query = {}
24
+ query.merge!('EventSourceArn' => event_source_arn) if event_source_arn
25
+ query.merge!('FunctionName' => function_name) if function_name
26
+ query.merge!('Marker' => marker) if marker
27
+ query.merge!('MaxItems' => max_items) if max_items
28
+
29
+ request({
30
+ :method => 'GET',
31
+ :path => '/event-source-mappings/',
32
+ :query => query
33
+ }.merge(params))
34
+ end
35
+ end
36
+
37
+ class Mock
38
+ def list_event_source_mappings(params={})
39
+ response = Excon::Response.new
40
+ response.status = 200
41
+
42
+ function_name = params.delete('FunctionName')
43
+
44
+ begin
45
+ function = self.get_function_configuration('FunctionName' => function_name).body
46
+ function_arn = function['FunctionArn']
47
+ rescue Fog::AWS::Lambda::Error => e
48
+ # interestingly enough, if you try to do a list_event_source_mappings
49
+ # on a nonexisting function, Lambda API endpoint doesn't return
50
+ # error, just an empty array.
51
+ end
52
+
53
+ event_source_mappings = []
54
+ if function_arn
55
+ event_source_mappings = self.data[:event_source_mappings].values.select do |m|
56
+ m['FunctionArn'].eql?(function_arn)
57
+ end
58
+ end
59
+
60
+ response.body = {
61
+ 'EventSourceMappings' => event_source_mappings,
62
+ 'NextMarker' => nil
63
+ }
64
+ response
65
+ end
66
+ end
67
+ end
68
+ end
69
+ end
@@ -0,0 +1,39 @@
1
+ module Fog
2
+ module AWS
3
+ class Lambda
4
+ class Real
5
+ require 'fog/aws/parsers/lambda/base'
6
+
7
+ # Returns a list of your Lambda functions.
8
+ # http://docs.aws.amazon.com/lambda/latest/dg/API_ListFunctions.html
9
+ # ==== Parameters
10
+ # * Marker <~String> - opaque pagination token returned from a previous ListFunctions operation. If present, indicates where to continue the listing.
11
+ # * MaxItems <~Integer> - Specifies the maximum number of AWS Lambda functions to return in response.
12
+ # ==== Returns
13
+ # * response<~Excon::Response>:
14
+ # * body<~Hash>:
15
+ # * 'Functions' <~Array> - list of Lambda functions.
16
+ # * 'NextMarker' <~String> - present if there are more functions.
17
+ def list_functions(params={})
18
+ request({
19
+ :method => 'GET',
20
+ :path => '/functions/',
21
+ :parser => Fog::AWS::Parsers::Lambda::Base.new
22
+ }.merge(params))
23
+ end
24
+ end
25
+
26
+ class Mock
27
+ def list_functions(params={})
28
+ response = Excon::Response.new
29
+ response.status = 200
30
+ response.body = {
31
+ 'Functions' => self.data[:functions].values,
32
+ 'NextMarker' => nil
33
+ }
34
+ response
35
+ end
36
+ end
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,56 @@
1
+ module Fog
2
+ module AWS
3
+ class Lambda
4
+ class Real
5
+
6
+ # Remove individual permissions from an access policy associated with a Lambda function by providing a Statement ID.
7
+ # http://docs.aws.amazon.com/lambda/latest/dg/API_RemovePermission.html
8
+ # ==== Parameters
9
+ # * FunctionName <~String> - Lambda function whose access policy you want to remove a permission from.
10
+ # * StatementId <~String> - Statement ID of the permission to remove.
11
+ # ==== Returns
12
+ # * response<~Excon::Response>:
13
+ # * body<~String>:
14
+ def remove_permission(params={})
15
+ function_name = params.delete('FunctionName')
16
+ statement_id = params.delete('StatementId')
17
+ request({
18
+ :method => 'DELETE',
19
+ :path => "/functions/#{function_name}/versions/HEAD/policy/#{statement_id}",
20
+ :expects => 204
21
+ }.merge(params))
22
+ end
23
+ end
24
+
25
+ class Mock
26
+ def remove_permission(params={})
27
+ function_name = params.delete('FunctionName')
28
+ opts = { 'FunctionName' => function_name }
29
+ function = self.get_function_configuration(opts).body
30
+ function_arn = function['FunctionArn']
31
+
32
+ statement_id = params.delete('StatementId')
33
+ message = 'Statement ID cannot be blank'
34
+ raise Fog::AWS::Lambda::Error, message unless statement_id
35
+
36
+ permissions_qty = self.data[:permissions][function_arn].size
37
+
38
+ self.data[:permissions][function_arn].delete_if do |s|
39
+ s['Sid'].eql?(statement_id)
40
+ end
41
+
42
+ if self.data[:permissions][function_arn].size.eql?(permissions_qty)
43
+ message = "ResourceNotFoundException => "
44
+ message << "The resource you requested does not exist."
45
+ raise Fog::AWS::Lambda::Error, message
46
+ end
47
+
48
+ response = Excon::Response.new
49
+ response.status = 204
50
+ response.body = ''
51
+ response
52
+ end
53
+ end
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,86 @@
1
+ module Fog
2
+ module AWS
3
+ class Lambda
4
+ class Real
5
+ # Change the parameters of the existing mapping without losing your position in the stream.
6
+ # http://docs.aws.amazon.com/lambda/latest/dg/API_UpdateEventSourceMapping.html
7
+ # ==== Parameters
8
+ # * UUID <~String> - event source mapping identifier.
9
+ # * BatchSize <~Integer> - maximum number of stream records that can be sent to your Lambda function for a single invocation.
10
+ # * Enabled <~Boolean> - specifies whether AWS Lambda should actively poll the stream or not.
11
+ # * FunctionName <~String> - Lambda function to which you want the stream records sent.
12
+ # ==== Returns
13
+ # * response<~Excon::Response>:
14
+ # * body<~Hash>:
15
+ # * 'BatchSize' <~Integer> - largest number of records that AWS Lambda will retrieve from your event source at the time of invoking your function.
16
+ # * 'EventSourceArn' <~String> - Amazon Resource Name (ARN) of the stream that is the source of events.
17
+ # * 'FunctionArn' <~String> - Lambda function to invoke when AWS Lambda detects an event on the stream.
18
+ # * 'LastModified' <~Time> - UTC time string indicating the last time the event mapping was updated.
19
+ # * 'LastProcessingResult' <~String> - result of the last AWS Lambda invocation of your Lambda function.
20
+ # * 'State' <~String> - state of the event source mapping.
21
+ # * 'StateTransitionReason' <~String> - reason the event source mapping is in its current state.
22
+ # * 'UUID' <~String> - AWS Lambda assigned opaque identifier for the mapping.
23
+ def update_event_source_mapping(params={})
24
+ function_name = params.delete('FunctionName')
25
+ mapping_id = params.delete('UUID')
26
+
27
+ batch_size = params.delete('BatchSize')
28
+ enabled = params.delete('Enabled')
29
+
30
+ update = {}
31
+ update.merge!('BatchSize' => batch_size) if batch_size
32
+ update.merge!('Enabled' => enabled) if !enabled.nil?
33
+ update.merge!('FunctionName' => function_name) if function_name
34
+
35
+ request({
36
+ :method => 'PUT',
37
+ :path => "/event-source-mappings/#{mapping_id}",
38
+ :expects => 202,
39
+ :body => Fog::JSON.encode(update)
40
+ }.merge(params))
41
+ end
42
+ end
43
+
44
+ class Mock
45
+ def update_event_source_mapping(params={})
46
+ mapping_id = params.delete('UUID')
47
+ mapping = self.data[:event_source_mappings][mapping_id]
48
+
49
+ unless mapping
50
+ message = 'ResourceNotFoundException => '
51
+ message << 'The resource you requested does not exist.'
52
+ raise Fog::AWS::Lambda::Error, message
53
+ end
54
+
55
+ function_name = params.delete('FunctionName')
56
+ function = {}
57
+ if function_name
58
+ function_opts = { 'FunctionName' => function_name }
59
+ function = self.get_function_configuration(function_opts).body
60
+ function_arn = function['FunctionArn']
61
+ end
62
+
63
+ batch_size = params.delete('BatchSize')
64
+ enabled = params.delete('Enabled')
65
+
66
+ update = {}
67
+
68
+ if function_name && !function.empty? && function_arn
69
+ update.merge!('FunctionArn' => function_arn)
70
+ end
71
+ update.merge!('BatchSize' => batch_size) if batch_size
72
+ update.merge!('Enabled' => enabled) if !enabled.nil?
73
+
74
+ mapping.merge!(update)
75
+ mapping['State'] = 'Disabling' if enabled.eql?(false)
76
+ mapping['State'] = 'Enabling' if enabled.eql?(true)
77
+
78
+ response = Excon::Response.new
79
+ response.status = 202
80
+ response.body = mapping
81
+ response
82
+ end
83
+ end
84
+ end
85
+ end
86
+ end
@@ -0,0 +1,75 @@
1
+ module Fog
2
+ module AWS
3
+ class Lambda
4
+ class Real
5
+ require 'fog/aws/parsers/lambda/base'
6
+
7
+ # Updates the code for the specified Lambda function.
8
+ # http://docs.aws.amazon.com/lambda/latest/dg/API_UpdateFunctionCode.html
9
+ # ==== Parameters
10
+ # * FunctionName <~String> - existing Lambda function name whose code you want to replace.
11
+ # * S3Bucket <~String> - Amazon S3 bucket name where the .zip file containing your deployment package is stored.
12
+ # * S3Key <~String> - Amazon S3 object (the deployment package) key name you want to upload.
13
+ # * S3ObjectVersion <~String> - Amazon S3 object (the deployment package) version you want to upload.
14
+ # * ZipFile <~String> - Based64-encoded .zip file containing your packaged source code.
15
+ # ==== Returns
16
+ # * response<~Excon::Response>:
17
+ # * body<~Hash>:
18
+ # * 'CodeSize' <~Integer> - size, in bytes, of the function .zip file you uploaded.
19
+ # * 'Description' <~String> - user-provided description.
20
+ # * 'FunctionArn' <~String> - Amazon Resource Name (ARN) assigned to the function.
21
+ # * 'FunctionName' <~String> - name of the function.
22
+ # * 'Handler' <~String> - function Lambda calls to begin executing your function.
23
+ # * 'LastModified' <~Time> - timestamp of the last time you updated the function.
24
+ # * 'Memorysize' <~String> - memory size, in MB, you configured for the function.
25
+ # * 'Role' <~String> - ARN of the IAM role that Lambda assumes when it executes your function to access any other AWS resources.
26
+ # * 'Runtime' <~String> - runtime environment for the Lambda function.
27
+ # * 'Timeout' <~Integer> - function execution time at which Lambda should terminate the function.
28
+ def update_function_code(params={})
29
+ function_name = params.delete('FunctionName')
30
+
31
+ s3_bucket = params.delete('S3Bucket')
32
+ s3_key = params.delete('S3Key')
33
+ s3_object_ver = params.delete('S3ObjectVersion')
34
+ zip_file = params.delete('ZipFile')
35
+
36
+ update = {}
37
+ update.merge!('S3Bucket' => s3_bucket) if s3_bucket
38
+ update.merge!('S3Key' => s3_key) if s3_key
39
+ update.merge!('S3ObjectVersion' => s3_object_ver) if s3_object_ver
40
+ update.merge!('ZipFile' => zip_file) if zip_file
41
+
42
+ request({
43
+ :method => 'PUT',
44
+ :path => "/functions/#{function_name}/versions/HEAD/code",
45
+ :body => Fog::JSON.encode(update),
46
+ :parser => Fog::AWS::Parsers::Lambda::Base.new
47
+ }.merge(params))
48
+ end
49
+ end
50
+
51
+ class Mock
52
+ def update_function_code(params={})
53
+ response = self.get_function_configuration(params)
54
+
55
+ request_data = []
56
+ %w(S3Bucket S3Key S3ObjectVersion ZipFile).each do |p|
57
+ request_data << params.delete(p) if params.has_key?(p)
58
+ end
59
+
60
+ message = 'Please provide a source for function code.'
61
+ raise Fog::AWS::Lambda::Error, message if request_data.empty?
62
+ # we ignore any parameters since we are not uploading any code
63
+
64
+ function_arn = response.body['FunctionArn']
65
+
66
+ response = Excon::Response.new
67
+ response.status = 200
68
+ response.body = self.data[:functions][function_arn]
69
+
70
+ response
71
+ end
72
+ end
73
+ end
74
+ end
75
+ end