|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +module AwsSdkCodeGenerator |
| 4 | + class EndpointParameter |
| 5 | + def initialize(name, definition, service, operation=nil) |
| 6 | + @name = name |
| 7 | + |
| 8 | + @value, @source = endpoint_parameter_value(name, definition, service, operation) |
| 9 | + |
| 10 | + @type = definition['type'] |
| 11 | + @built_in = definition['builtIn'] |
| 12 | + @default = definition['default'] |
| 13 | + @required = definition['required'] |
| 14 | + @documentation = "# @!attribute #{key}\n" |
| 15 | + if definition['documentation'] |
| 16 | + @documentation += " # #{definition['documentation']}\n" |
| 17 | + end |
| 18 | + if deprecated = definition['deprecated'] |
| 19 | + @documentation += " #\n # @deprecated\n" |
| 20 | + if deprecated['message'] |
| 21 | + @documentation += " # #{deprecated['message']}\n" |
| 22 | + end |
| 23 | + if deprecated['since'] |
| 24 | + @documentation += " # Since: #{deprecated['since']}\n" |
| 25 | + end |
| 26 | + end |
| 27 | + @documentation += " #\n # @return [#{@type}]\n #" |
| 28 | + end |
| 29 | + |
| 30 | + # @return [String] |
| 31 | + attr_reader :name |
| 32 | + |
| 33 | + # @return [String] |
| 34 | + attr_reader :documentation |
| 35 | + |
| 36 | + # @return [Boolean] |
| 37 | + attr_reader :required |
| 38 | + |
| 39 | + # @return [String] |
| 40 | + attr_reader :source |
| 41 | + |
| 42 | + # @return [String] |
| 43 | + attr_reader :value |
| 44 | + |
| 45 | + # @return [String,Boolean,Array] |
| 46 | + def default |
| 47 | + case @default |
| 48 | + when String |
| 49 | + "\"#{@default}\"" |
| 50 | + else |
| 51 | + @default.to_s |
| 52 | + end |
| 53 | + end |
| 54 | + |
| 55 | + def default? |
| 56 | + !@default.nil? |
| 57 | + end |
| 58 | + |
| 59 | + def validate_required? |
| 60 | + required && !default? |
| 61 | + end |
| 62 | + |
| 63 | + def key |
| 64 | + Underscore.underscore(name) |
| 65 | + end |
| 66 | + |
| 67 | + private |
| 68 | + |
| 69 | + # Most to least |
| 70 | + # staticContextParams |
| 71 | + # contextParam |
| 72 | + # operationContextParams |
| 73 | + # clientContextParams (always sourced from config) |
| 74 | + # Built-In Bindings (sourced from config in most cases, context in some cases to allow operation level overrides) |
| 75 | + # Built-in binding default values |
| 76 | + # @retyrn [value, source]. source may be one of [operation, config, default] |
| 77 | + def endpoint_parameter_value(param_name, param_data, service, operation) |
| 78 | + unless operation.nil? |
| 79 | + value, source = [ |
| 80 | + static_context_param(operation, param_name), 'operation' |
| 81 | + ] |
| 82 | + value, source = [ |
| 83 | + context_param_value(service, operation, param_name), 'operation' |
| 84 | + ] unless value |
| 85 | + value, source = [ |
| 86 | + operation_context_param_value(operation, param_name), 'operation' |
| 87 | + ] unless value |
| 88 | + end |
| 89 | + |
| 90 | + value, source = [ |
| 91 | + client_context_param_value(service, param_name, param_data), |
| 92 | + 'config' |
| 93 | + ] unless value |
| 94 | + |
| 95 | + |
| 96 | + # built-ins may be sourced from operation context in some cases |
| 97 | + unless value |
| 98 | + value, source = built_in_param_value(service, param_data) |
| 99 | + end |
| 100 | + |
| 101 | + [value || 'nil', source || 'default'] |
| 102 | + end |
| 103 | + |
| 104 | + def client_context_param_value(service, param_name, param_data) |
| 105 | + if service.api['clientContextParams']&.key?(param_name) && |
| 106 | + !param_data['builtIn'] |
| 107 | + "config.#{Underscore.underscore(param_name)}" |
| 108 | + end |
| 109 | + end |
| 110 | + |
| 111 | + # built-ins may be sourced from operation context in some cases |
| 112 | + def built_in_param_value(service, param_data) |
| 113 | + source = 'config' |
| 114 | + value = |
| 115 | + case param_data['builtIn'] |
| 116 | + when 'AWS::Region' |
| 117 | + 'config.region' |
| 118 | + when 'AWS::UseFIPS' |
| 119 | + 'config.use_fips_endpoint' |
| 120 | + when 'AWS::UseDualStack' |
| 121 | + if service.name == 'S3' || service.name == 'S3Control' |
| 122 | + source = 'operation' |
| 123 | + 'context[:use_dualstack_endpoint]' |
| 124 | + else |
| 125 | + 'config.use_dualstack_endpoint' |
| 126 | + end |
| 127 | + when 'AWS::Auth::AccountId' |
| 128 | + 'config.credentials.credentials.account_id' |
| 129 | + when 'AWS::Auth::AccountIdEndpointMode' |
| 130 | + 'config.account_id_endpoint_mode' |
| 131 | + when 'AWS::STS::UseGlobalEndpoint' |
| 132 | + "config.sts_regional_endpoints == 'legacy'" |
| 133 | + when 'AWS::S3::UseGlobalEndpoint' |
| 134 | + "config.s3_us_east_1_regional_endpoint == 'legacy'" |
| 135 | + when 'AWS::S3::Accelerate' |
| 136 | + if service.name == 'S3' || service.name == 'S3Control' |
| 137 | + source = 'operation' |
| 138 | + 'context[:use_accelerate_endpoint]' |
| 139 | + else |
| 140 | + 'config.use_accelerate_endpoint' |
| 141 | + end |
| 142 | + when 'AWS::S3::ForcePathStyle' |
| 143 | + 'config.force_path_style' |
| 144 | + when 'AWS::S3::UseArnRegion', 'AWS::S3Control::UseArnRegion' |
| 145 | + 'config.s3_use_arn_region' |
| 146 | + when 'AWS::S3::DisableMultiRegionAccessPoints' |
| 147 | + 'config.s3_disable_multiregion_access_points' |
| 148 | + when 'SDK::Endpoint' |
| 149 | + '(config.endpoint.to_s unless config.regional_endpoint)' |
| 150 | + else |
| 151 | + source = nil |
| 152 | + nil # no value, not a default |
| 153 | + end |
| 154 | + [value, source] |
| 155 | + end |
| 156 | + |
| 157 | + def context_param_value(service, operation, param_name) |
| 158 | + return nil unless operation['input'] |
| 159 | + |
| 160 | + input_shape = operation['input']['shape'] |
| 161 | + members = service.api['shapes'][input_shape].fetch('members', {}) |
| 162 | + members.detect do |(member_name, member)| |
| 163 | + context_param = member.fetch('contextParam', {}) |
| 164 | + if context_param.fetch('name', nil) == param_name |
| 165 | + break "context.params[:#{Underscore.underscore(member_name)}]" |
| 166 | + end |
| 167 | + end |
| 168 | + end |
| 169 | + |
| 170 | + def operation_context_param_value(operation, param_name) |
| 171 | + return nil unless operation['input'] |
| 172 | + |
| 173 | + binding = operation.fetch('operationContextParams', {})[param_name] |
| 174 | + |
| 175 | + return nil unless binding |
| 176 | + |
| 177 | + "JMESPath.search(\"#{Underscore.underscore_jmespath(binding['path'])}\", context.params)" |
| 178 | + end |
| 179 | + |
| 180 | + def static_context_param(operation, param_name) |
| 181 | + value = operation.fetch('staticContextParams', {}) |
| 182 | + .fetch(param_name, {}).fetch('value', nil) |
| 183 | + if !value.nil? && value.is_a?(String) |
| 184 | + "\"#{value}\"" |
| 185 | + else |
| 186 | + value |
| 187 | + end |
| 188 | + end |
| 189 | + end |
| 190 | +end |
0 commit comments