File: create_launch_configuration.rb

package info (click to toggle)
ruby-fog-aws 3.3.0-5
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 7,816 kB
  • sloc: ruby: 68,587; makefile: 6
file content (119 lines) | stat: -rw-r--r-- 5,839 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
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
109
110
111
112
113
114
115
116
117
118
119
module Fog
  module AWS
    class AutoScaling
      class Real
        require 'fog/aws/parsers/auto_scaling/basic'

        # Creates a new launch configuration. When created, the new launch
        # configuration is available for immediate use.
        #
        # ==== Parameters
        # * image_id<~String> - Unique ID of the Amazon Machine Image (AMI)
        #   which was assigned during registration.
        # * instance_type<~String> - The instance type of the EC2 instance.
        # * launch_configuration_name<~String> - The name of the launch
        #   configuration to create.
        # * options<~Hash>:
        #   * 'BlockDeviceMappings'<~Array>:
        #     * 'DeviceName'<~String> - The name of the device within Amazon
        #       EC2.
        #     * 'Ebs.SnapshotId'<~String> - The snapshot ID.
        #     * 'Ebs.VolumeSize'<~Integer> - The volume size, in GigaBytes.
        #     * 'VirtualName'<~String> - The virtual name associated with the
        #       device.
        #   * 'IamInstanceProfile'<~String> The name or the Amazon Resource
        #     Name (ARN) of the instance profile associated with the IAM role
        #     for the instance.
        #   * 'InstanceMonitoring.Enabled'<~Boolean> - Enables detailed
        #     monitoring, which is enabled by default.
        #   * 'KernelId'<~String> - The ID of the kernel associated with the
        #     Amazon EC2 AMI.
        #   * 'KeyName'<~String> - The name of the Amazon EC2 key pair.
        #   * 'RamdiskId'<~String> - The ID of the RAM disk associated with the
        #     Amazon EC2 AMI.
        #   * 'SecurityGroups'<~Array> - The names of the security groups with
        #     which to associate Amazon EC2 or Amazon VPC instances.
        #   * 'SpotPrice'<~String> - The maximum hourly price to be paid for
        #     any Spot Instance launched to fulfill the request. Spot Instances
        #     are launched when the price you specify exceeds the current Spot
        #     market price.
        #   * 'UserData'<~String> - The user data available to the launched
        #     Amazon EC2 instances.
        #   * 'EbsOptimized'<~Boolean> - Whether the instance is optimized for
        #     EBS I/O. Not required, default false.
        #   * 'PlacementTenancy'<~String> - The tenancy of the instance. Valid
        #     values: default | dedicated.  Default: default
        #
        # ==== Returns
        # * response<~Excon::Response>:
        #   * body<~Hash>:
        #     * 'ResponseMetadata'<~Hash>:
        #       * 'RequestId'<~String> - Id of request
        #
        # ==== See Also
        # http://docs.amazonwebservices.com/AutoScaling/latest/APIReference/API_CreateLaunchConfiguration.html
        #
        def create_launch_configuration(image_id, instance_type, launch_configuration_name, options = {})
          if block_device_mappings = options.delete('BlockDeviceMappings')
            block_device_mappings.each_with_index do |mapping, i|
              for key, value in mapping
                options.merge!({ format("BlockDeviceMappings.member.%d.#{key}", i+1) => value })
              end
            end
          end
          if security_groups = options.delete('SecurityGroups')
             options.merge!(AWS.indexed_param('SecurityGroups.member.%d', [*security_groups]))
          end

          if classic_link_groups = options.delete('ClassicLinkVPCSecurityGroups')
            options.merge!(AWS.indexed_param('ClassicLinkVPCSecurityGroups.member.%d', [*classic_link_groups]))
          end
          
          if options['UserData']
            options['UserData'] = Base64.encode64(options['UserData'])
          end
          request({
            'Action'                  => 'CreateLaunchConfiguration',
            'ImageId'                 => image_id,
            'InstanceType'            => instance_type,
            'LaunchConfigurationName' => launch_configuration_name,
            :parser                   => Fog::Parsers::AWS::AutoScaling::Basic.new
          }.merge!(options))
        end
      end

      class Mock
        def create_launch_configuration(image_id, instance_type, launch_configuration_name, options = {})
          if self.data[:launch_configurations].key?(launch_configuration_name)
            raise Fog::AWS::AutoScaling::IdentifierTaken.new("Launch Configuration by this name already exists - A launch configuration already exists with the name #{launch_configuration_name}")
          end
          self.data[:launch_configurations][launch_configuration_name] = {
            'AssociatePublicIpAddress' => nil,
            'BlockDeviceMappings'     => [],
            'CreatedTime'             => Time.now.utc,
            'EbsOptimized'            => false,
            'IamInstanceProfile'      => nil,
            'ImageId'                 => image_id,
            'InstanceMonitoring'      => {'Enabled' => true},
            'InstanceType'            => instance_type,
            'KernelId'                => nil,
            'KeyName'                 => nil,
            'LaunchConfigurationARN'  => Fog::AWS::Mock.arn('autoscaling', self.data[:owner_id], "launchConfiguration:00000000-0000-0000-0000-000000000000:launchConfigurationName/#{launch_configuration_name}", @region),
            'LaunchConfigurationName' => launch_configuration_name,
            'PlacementTenancy'        => nil,
            'RamdiskId'               => nil,
            'SecurityGroups'          => [],
            'UserData'                => nil
          }.merge!(options)

          response = Excon::Response.new
          response.status = 200
          response.body = {
            'ResponseMetadata' => { 'RequestId' => Fog::AWS::Mock.request_id }
          }
          response
        end
      end
    end
  end
end