File: global_configuration.rb

package info (click to toggle)
ruby-aws-sdk-core 3.104.3-3%2Bdeb11u2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 1,444 kB
  • sloc: ruby: 11,201; makefile: 4
file content (90 lines) | stat: -rw-r--r-- 2,596 bytes parent folder | download | duplicates (2)
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
# frozen_string_literal: true

require 'set'

module Aws
  module Plugins

    # This plugin provides the ability to provide global configuration for
    # all AWS classes or specific ones.
    #
    # ## Global AWS configuration
    #
    # You can specify global configuration defaults via `Aws.config`
    #
    #     Aws.config[:region] = 'us-west-2'
    #
    # Options applied to `Aws.config` are merged with constructed
    # service interfaces.
    #
    #     # uses the global configuration
    #     Aws::EC2.new.config.region #=> 'us-west-2'
    #
    #     # constructor args have priority over global configuration
    #     Aws::EC2.new(region: 'us-east-1').config.region #=> 'us-east-1'
    #
    # ## Service Specific Global Configuration
    #
    # Some services have very specific configuration options that are not
    # shared by other services.
    #
    #     # oops, this option is only recognized by Aws::S3
    #     Aws.config[:force_path_style] = true
    #     Aws::EC2.new
    #     #=> raises ArgumentError: invalid configuration option `:force_path_style'
    #
    # To avoid this issue, you can nest service specific options
    #
    #     Aws.config[:s3] = { force_path_style: true }
    #
    #     Aws::EC2.new # no error this time
    #     Aws::S3.new.config.force_path_style #=> true
    #
    # @api private
    class GlobalConfiguration < Seahorse::Client::Plugin

      @identifiers = Set.new()

      # @api private
      def before_initialize(client_class, options)
        # apply service specific defaults before the global aws defaults
        apply_service_defaults(client_class, options)
        apply_aws_defaults(client_class, options)
      end

      private

      def apply_service_defaults(client_class, options)
        if defaults = Aws.config[client_class.identifier]
          defaults.each do |option_name, default|
            options[option_name] = default unless options.key?(option_name)
          end
        end
      end

      def apply_aws_defaults(client_class, options)
        Aws.config.each do |option_name, default|
          next if self.class.identifiers.include?(option_name)
          next if options.key?(option_name)
          options[option_name] = default
        end
      end

      class << self

        # Registers an additional service identifier.
        # @api private
        def add_identifier(identifier)
          @identifiers << identifier
        end

        # @return [Set<String>]
        # @api private
        def identifiers
          @identifiers
        end

      end
    end
  end
end