File: partition.rb

package info (click to toggle)
ruby-aws-partitions 1.354.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 352 kB
  • sloc: ruby: 567; makefile: 5
file content (108 lines) | stat: -rw-r--r-- 3,317 bytes parent folder | download
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# frozen_string_literal: true

module Aws
  module Partitions
    class Partition
      # @option options [required, String] :name
      # @option options [required, Hash<String,Region>] :regions
      # @option options [required, Hash<String,Service>] :services
      # @api private
      def initialize(options = {})
        @name = options[:name]
        @regions = options[:regions]
        @services = options[:services]
      end

      # @return [String] The partition name, e.g. "aws", "aws-cn", "aws-us-gov".
      attr_reader :name

      # @param [String] region_name The name of the region, e.g. "us-east-1".
      # @return [Region]
      # @raise [ArgumentError] Raises `ArgumentError` for unknown region name.
      def region(region_name)
        if @regions.key?(region_name)
          @regions[region_name]
        else
          msg = "invalid region name #{region_name.inspect}; valid region "\
                "names include #{@regions.keys.join(', ')}"
          raise ArgumentError, msg
        end
      end

      # @return [Array<Region>]
      def regions
        @regions.values
      end

      # @param [String] region_name The name of the region, e.g. "us-east-1".
      # @return [Boolean] true if the region is in the partition.
      def region?(region_name)
        @regions.key?(region_name)
      end

      # @param [String] service_name The service module name.
      # @return [Service]
      # @raise [ArgumentError] Raises `ArgumentError` for unknown service name.
      def service(service_name)
        if @services.key?(service_name)
          @services[service_name]
        else
          msg = "invalid service name #{service_name.inspect}; valid service "\
                "names include #{@services.keys.join(', ')}"
          raise ArgumentError, msg
        end
      end

      # @return [Array<Service>]
      def services
        @services.values
      end

      # @param [String] service_name The service module name.
      # @return [Boolean] true if the service is in the partition.
      def service?(service_name)
        @services.key?(service_name)
      end

      class << self
        # @api private
        def build(partition)
          Partition.new(
            name: partition['partition'],
            regions: build_regions(partition),
            services: build_services(partition)
          )
        end

        private

        # @param [Hash] partition
        # @return [Hash<String,Region>]
        def build_regions(partition)
          partition['regions'].each_with_object({}) do
            |(region_name, region), regions|
            next if region_name == "#{partition['partition']}-global"

            regions[region_name] = Region.build(
              region_name, region, partition
            )
          end
        end

        # @param [Hash] partition
        # @return [Hash<String,Service>]
        def build_services(partition)
          Partitions.service_ids.each_with_object({}) do
            |(service_name, service), services|
            service_data = partition['services'].fetch(
              service, 'endpoints' => {}
            )
            services[service_name] = Service.build(
              service_name, service_data, partition
            )
          end
        end
      end
    end
  end
end