uc3-dmp-dynamo 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 58ed099abf0a13ea6a5e296d5b9cb1705e91323ecf0c11175aaa26548e601df4
4
+ data.tar.gz: e071931916566776591f79c380750834fc2474a2233ae15ddfb76dbd06dc594d
5
+ SHA512:
6
+ metadata.gz: c74365c564c6762dc2429583611cfb69daec13fa6c3df7e1d624a1c8630df8e5440c31cac301a65f4182f9fdc168c165bf132ecff60f4381bbc906eb5d79aeb8
7
+ data.tar.gz: aa410b9a0fb4f84df58a4c6e5c8ef495359e290bbfeb5a50a45da8aa9544764d023d21d06282e7c350f729e3b7bca2a10a7cf05cc7f8e494113b5fd1d73c8a8c
data/README.md ADDED
@@ -0,0 +1,3 @@
1
+ # Uc3DmpDynamo
2
+
3
+ Helper methods for accessing the Dynamo DB
@@ -0,0 +1,83 @@
1
+ # frozen_string_literal: true
2
+
3
+
4
+ # TODO: Be sure to update the API functions so that they call cleanse_dmp_json before
5
+ # calling Uc3DmpApiCore::Responder.respond !!!!!!!!!!
6
+
7
+
8
+ module Uc3DmpDynamo
9
+ class Uc3DmpDynamoError < StandardError; end
10
+
11
+ # Helper functions for working with Dynamo JSON
12
+ class Client
13
+ SOURCE = 'Uc3DmpDynamo::Client'
14
+
15
+ MSG_INVALID_KEY = 'Invalid key specified. Expecting Hash containing `PK` and `SK`'
16
+ MSG_MISSING_TABLE = 'No Dynamo Table defined! Looking for `ENV[\'DYNAMO_TABLE\']`'
17
+ MSG_DYNAMO_ERROR = 'Dynamo DB Table Error - %{msg} - %{trace}'
18
+
19
+ attr_accessor: :connection, :table, :log_writer
20
+
21
+ def initialize(**args)
22
+ @table = ENV['DYNAMO_TABLE']
23
+ raise Uc3DmpDynamoError, MSG_MISSING_TABLE if @table.nil?
24
+
25
+ @log_writer = args[:log_writer]
26
+ @connection = args.fetch(:client, Aws::DynamoDB::Client.new(region: ENV.fetch('AWS_REGION', 'us-west-2')))
27
+ end
28
+
29
+ # Fetch a single item
30
+ def get_item(key:, debug: false)
31
+ raise Uc3DmpDynamoError, MSG_INVALID_KEY unless key.is_a?(Hash)
32
+
33
+ resp = @client.get_item(
34
+ { table_name: @table,
35
+ key: key,
36
+ consistent_read: false,
37
+ return_consumed_capacity: debug ? 'TOTAL' : 'NONE'
38
+ }
39
+ )
40
+ # If debug is enabled then write the response to the LogWriter
41
+ if debug && @log_writer
42
+ @log_writer.log_message(source: SOURCE, message: "get_item - #{key}", details: resp[:item].first)
43
+ end
44
+ resp[:item].first
45
+ rescue Aws::Errors::ServiceError => e
46
+ raise Uc3DmpDynamoError, MSG_DYNAMO_ERROR % { msg: e.message, trace: e.backtrace }
47
+ end
48
+
49
+ # Perform a table scan if a filter was specified.
50
+ # For example:
51
+ # key_conditions: { PK: { attribute_value_list: ['DMP#12345'] }, comparison_operator: 'EQ' }
52
+ # projection_expression: 'title, dmp_id, modified'
53
+ #
54
+ # See the DynamoDB docs for examples of key_conditions and projection_expressions
55
+ def query(debug: false, **args)
56
+ raise Uc3DmpDynamoError, MSG_INVALID_KEY unless args[:key_conditions].is_a?(Hash) &&
57
+ !args[:key_conditions].keys.empty?
58
+
59
+ hash = {
60
+ table_name: @table,
61
+ key_conditions: args[:key_conditions],
62
+ consistent_read: false,
63
+ return_consumed_capacity: debug ? 'TOTAL' : 'NONE'
64
+ }
65
+ # Look for and add any other filtering or projection args
66
+ %i[filter_expression, expression_attribute_values, projection_expression, scan_index_forward].each do |key|
67
+ next if args[key.to_sym].nil?
68
+
69
+ hash[key.to_sym] = args[key.to_sym]
70
+ end
71
+
72
+ resp = @client.query(hash)
73
+ # If debug is enabled then write the response to the LogWriter
74
+ if debug && @log_writer
75
+ msg = "query - key_conditions: #{key_conditions}, projection_expression: #{projection_expression}"
76
+ @log_writer.log_message(source: SOURCE, message: msg, details: resp.items)
77
+ end
78
+ resp.items
79
+ rescue Aws::Errors::ServiceError => e
80
+ raise Uc3DmpDynamoError, MSG_DYNAMO_ERROR % { msg: e.message, trace: e.backtrace }
81
+ end
82
+ end
83
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Uc3DmpDynamo
4
+ VERSION = '0.0.1'
5
+ end
@@ -0,0 +1,11 @@
1
+ # rubocop:disable Naming/FileName
2
+ # frozen_string_literal: true
3
+
4
+ require 'aws-sdk-dynamodb'
5
+
6
+ require 'uc3-dmp-dynamo/client'
7
+
8
+ module Uc3DmpDynamo
9
+
10
+ end
11
+ # rubocop:enable Naming/FileName
metadata ADDED
@@ -0,0 +1,146 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: uc3-dmp-dynamo
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Brian Riley
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2023-05-09 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: json
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.6'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.6'
27
+ - !ruby/object:Gem::Dependency
28
+ name: logger
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.4'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.4'
41
+ - !ruby/object:Gem::Dependency
42
+ name: aws-sdk-dynamodb
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.83'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.83'
55
+ - !ruby/object:Gem::Dependency
56
+ name: byebug
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '='
60
+ - !ruby/object:Gem::Version
61
+ version: 11.1.3
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '='
67
+ - !ruby/object:Gem::Version
68
+ version: 11.1.3
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '='
74
+ - !ruby/object:Gem::Version
75
+ version: 3.9.0
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - '='
81
+ - !ruby/object:Gem::Version
82
+ version: 3.9.0
83
+ - !ruby/object:Gem::Dependency
84
+ name: rubocop
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - '='
88
+ - !ruby/object:Gem::Version
89
+ version: 1.50.2
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - '='
95
+ - !ruby/object:Gem::Version
96
+ version: 1.50.2
97
+ - !ruby/object:Gem::Dependency
98
+ name: rubocop-rspec
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - '='
102
+ - !ruby/object:Gem::Version
103
+ version: 2.20.0
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - '='
109
+ - !ruby/object:Gem::Version
110
+ version: 2.20.0
111
+ description: Helpers for Dynamo DB Table access
112
+ email:
113
+ - brian.riley@ucop.edu
114
+ executables: []
115
+ extensions: []
116
+ extra_rdoc_files: []
117
+ files:
118
+ - README.md
119
+ - lib/uc3-dmp-dynamo.rb
120
+ - lib/uc3-dmp-dynamo/client.rb
121
+ - lib/uc3-dmp-dynamo/version.rb
122
+ homepage: https://github.com/CDLUC3/dmp-hub-cfn/blob/main/src/sam/gems/uc3-dmp-dynamo
123
+ licenses:
124
+ - MIT
125
+ metadata:
126
+ rubygems_mfa_required: 'false'
127
+ post_install_message:
128
+ rdoc_options: []
129
+ require_paths:
130
+ - lib
131
+ required_ruby_version: !ruby/object:Gem::Requirement
132
+ requirements:
133
+ - - ">="
134
+ - !ruby/object:Gem::Version
135
+ version: '2.7'
136
+ required_rubygems_version: !ruby/object:Gem::Requirement
137
+ requirements:
138
+ - - ">="
139
+ - !ruby/object:Gem::Version
140
+ version: '0'
141
+ requirements: []
142
+ rubygems_version: 3.1.6
143
+ signing_key:
144
+ specification_version: 4
145
+ summary: DMPTool gem that provides support for Dynamo DB
146
+ test_files: []