File: create_stream.rb

package info (click to toggle)
ruby-fog-aws 3.18.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 8,140 kB
  • sloc: ruby: 73,328; javascript: 14; makefile: 9; sh: 4
file content (70 lines) | stat: -rw-r--r-- 2,252 bytes parent folder | download | duplicates (5)
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
module Fog
  module AWS
    class Kinesis
      class Real
        # Creates a Amazon Kinesis stream.
        #
        # ==== Options
        # * ShardCount<~Number>: The number of shards that the stream will use.
        # * StreamName<~String>: A name to identify the stream.
        # ==== Returns
        # * response<~Excon::Response>:
        #
        # ==== See Also
        # https://docs.aws.amazon.com/kinesis/latest/APIReference/API_CreateStream.html
        #
        def create_stream(options={})
          body = {
            "ShardCount" => options.delete("ShardCount") || 1,
            "StreamName" => options.delete("StreamName")
          }.reject{ |_,v| v.nil? }

          request({
                    'X-Amz-Target' => "Kinesis_#{@version}.CreateStream",
                    :body          => body,
                  }.merge(options))
        end
      end

      class Mock
        def create_stream(options={})
          stream_name = options.delete("StreamName")
          shard_count = options.delete("ShardCount") || 1
          stream_arn = "arn:aws:kinesis:#{@region}:#{@account_id}:stream/#{stream_name}"

          if data[:kinesis_streams].detect{ |s| s["StreamName"] == stream_name }
            raise Fog::AWS::Kinesis::ResourceInUse.new("Stream #{stream_name} under account #{@account_id} already exists.")
          end

          shards = (0...shard_count).map do |shard|
            {
              "HashKeyRange"=>{
                "EndingHashKey"=>"340282366920938463463374607431768211455",
                "StartingHashKey"=>"0"
              },
              "SequenceNumberRange"=>{
                "StartingSequenceNumber"=> next_sequence_number
              },
              "ShardId"=>next_shard_id,
              "Records" => []
            }
          end

          data[:kinesis_streams] = [{
              "HasMoreShards" => false,
              "StreamARN" => stream_arn,
              "StreamName" => stream_name,
              "StreamStatus" => "ACTIVE",
              "Shards" => shards,
              "Tags" => {}
            }]

          response = Excon::Response.new
          response.status = 200
          response.body = ""
          response
        end
      end
    end
  end
end