-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
custom_service.rb
94 lines (78 loc) · 2.66 KB
/
custom_service.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# frozen_string_literal: true
require 'json'
require 'cgi'
module BuildTools
class CustomService
# @option options [required, String] :service_name
# @option options [String] :gem_version
# @option options [String] :gem_name
# @option options [required, String] :model_path
# @option options [required, String] :default_endpoint
def initialize(options = {})
@svc_name = validate(options.fetch(:service_name))
@default_endpoint = options.fetch(:default_endpoint)
@model_path = options.fetch(:model_path)
# Optional
@gem_version = options[:gem_version] || '1.0.0'
@gem_name = options[:gem_name] || "#{@svc_name.downcase}-sdk"
@output_dir = options[:output_dir] || File.expand_path('../../gems', __FILE__)
end
attr_reader :svc_name
def build
AwsSdkCodeGenerator::Service.new(
name: @svc_name,
module_name: @svc_name, # avoid AWS prefix
gem_name: @gem_name,
gem_version: @gem_version,
api: load_json(@model_path), # contains both api and docs
gem_dependencies: gem_dependencies,
default_endpoint: @default_endpoint,
add_plugins: add_plugins([
"#{@svc_name}::Plugins::Authorizer",
"#{@svc_name}::Plugins::APIGEndpoint"
]),
remove_plugins: [
'Aws::Plugins::UserAgent',
'Aws::Plugins::RegionalEndpoint',
'Aws::Plugins::EndpointDiscovery',
'Aws::Plugins::EndpointPattern',
'Aws::Plugins::CredentialsConfiguration',
'Aws::Plugins::DefaultsMode'
]
)
end
private
def load_json(model_dir)
JSON.load_file(model_path(model_dir))
end
def model_path(model_dir)
path = File.expand_path("#{model_dir}/service-2.json", __FILE__)
File.exist?(path) ? path : nil
end
def gem_dependencies
{
'aws-sdk-core' => '>= 3.46.1\', \'< 4.0',
'aws-sigv4' => '~> 1.0'
}
end
def add_plugins(plugins)
plugins.inject({}) do |hash, plugin|
hash[plugin] = plugin_path(plugin)
hash
end
end
def plugin_path(plugin_name)
parts = plugin_name.split('::')
parts = parts.map { |part| AwsSdkCodeGenerator::Underscore.underscore(part) }
parts.shift # Shift off service module then append gem path
(["#{@output_dir}/#{@gem_name}/lib/#{@gem_name}"] + parts).join('/') + '.rb'
end
def validate(svc_name)
# replace all non alphanumber with space, and make camel case string
raw = CGI::unescape(svc_name)
raw.gsub(/[^0-9a-zA-Z]/i, ' ').split(' ').each do |part|
part[0] = part[0].upcase
end.join
end
end
end