Class: MistAws::Ec2
- Inherits:
-
Object
- Object
- MistAws::Ec2
- Defined in:
- lib/mist_aws/ec2.rb
Instance Attribute Summary collapse
-
#credentials ⇒ Object
readonly
Returns the value of attribute credentials.
-
#ec2 ⇒ Object
readonly
Returns the value of attribute ec2.
-
#ec2_client ⇒ Object
readonly
Returns the value of attribute ec2_client.
-
#iam ⇒ Object
readonly
Returns the value of attribute iam.
-
#logger ⇒ Object
readonly
Returns the value of attribute logger.
-
#profile_name ⇒ Object
readonly
These are read-only accessor and are initializeds by initialize method.
-
#region ⇒ Object
readonly
Returns the value of attribute region.
Instance Method Summary collapse
-
#authorize_security_group_ingress(opts = {}) ⇒ Object
Note the keys for opts must be strings not symbols.
- #create_security_group(group_name, vpc_id, description = group_name) ⇒ Object
- #create_vpc(cdr_block) ⇒ Object
- #delete_security_group(group_name, vpc_id) ⇒ Object
- #delete_vpc(vpc_id) ⇒ Object
- #get_security_group(group_name, vpc_id) ⇒ Object
- #get_security_group_id(group_name, vpc_id) ⇒ Object
-
#initialize(opts = {}) ⇒ Ec2
constructor
A new instance of Ec2.
- #security_group_exists?(group_name, vpc_id) ⇒ Boolean
Constructor Details
#initialize(opts = {}) ⇒ Ec2
16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/mist_aws/ec2.rb', line 16 def initialize(opts={}) # Ruby 1.9 backwards compatability opts = {profile_name: nil, region: nil, logger: ::Logger.new(STDERR)}.merge(opts) opts.each do |key, value| instance_variable_set "@#{key}", value end @iam = Iam.new(opts) @ec2_client = Aws::EC2::Client.new(credentials: @iam.credentials, region: @iam.region) @ec2 = Aws::EC2::Resource.new(client: @ec2_client) end |
Instance Attribute Details
#credentials ⇒ Object (readonly)
Returns the value of attribute credentials.
9 10 11 |
# File 'lib/mist_aws/ec2.rb', line 9 def credentials @credentials end |
#ec2 ⇒ Object (readonly)
Returns the value of attribute ec2.
14 15 16 |
# File 'lib/mist_aws/ec2.rb', line 14 def ec2 @ec2 end |
#ec2_client ⇒ Object (readonly)
Returns the value of attribute ec2_client.
13 14 15 |
# File 'lib/mist_aws/ec2.rb', line 13 def ec2_client @ec2_client end |
#iam ⇒ Object (readonly)
Returns the value of attribute iam.
12 13 14 |
# File 'lib/mist_aws/ec2.rb', line 12 def iam @iam end |
#logger ⇒ Object (readonly)
Returns the value of attribute logger.
11 12 13 |
# File 'lib/mist_aws/ec2.rb', line 11 def logger @logger end |
#profile_name ⇒ Object (readonly)
These are read-only accessor and are initializeds by initialize method
8 9 10 |
# File 'lib/mist_aws/ec2.rb', line 8 def profile_name @profile_name end |
#region ⇒ Object (readonly)
Returns the value of attribute region.
10 11 12 |
# File 'lib/mist_aws/ec2.rb', line 10 def region @region end |
Instance Method Details
#authorize_security_group_ingress(opts = {}) ⇒ Object
Note the keys for opts must be strings not symbols
60 61 62 63 64 65 |
# File 'lib/mist_aws/ec2.rb', line 60 def (opts={}) begin ec2_client.(opts) rescue Aws::EC2::Errors::InvalidPermissionDuplicate end end |
#create_security_group(group_name, vpc_id, description = group_name) ⇒ Object
50 51 52 53 54 55 56 57 |
# File 'lib/mist_aws/ec2.rb', line 50 def create_security_group(group_name, vpc_id, description=group_name) begin ec2_client.create_security_group(group_name: group_name, vpc_id: vpc_id, description: description) rescue Aws::EC2::Errors::InvalidGroupDuplicate end # Seem to need to fetch it to get a valid security group struct get_security_group(group_name, vpc_id) end |
#create_vpc(cdr_block) ⇒ Object
67 68 69 70 71 72 73 74 |
# File 'lib/mist_aws/ec2.rb', line 67 def create_vpc(cdr_block) result = ec2_client.describe_vpcs(filters: [{ name: "cidr", values: [cdr_block]}]) if result.vpcs == [] ec2_client.create_vpc(cidr_block: cdr_block).vpc else result.vpcs.first end end |
#delete_security_group(group_name, vpc_id) ⇒ Object
45 46 47 48 |
# File 'lib/mist_aws/ec2.rb', line 45 def delete_security_group(group_name, vpc_id) group_id = get_security_group_id(group_name, vpc_id) ec2_client.delete_security_group(group_id: group_id) if group_id end |
#delete_vpc(vpc_id) ⇒ Object
76 77 78 79 80 81 82 |
# File 'lib/mist_aws/ec2.rb', line 76 def delete_vpc(vpc_id) begin ec2_client.delete_vpc(vpc_id: vpc_id) rescue Aws::EC2::Errors::InvalidVpcIDNotFound nil end end |
#get_security_group(group_name, vpc_id) ⇒ Object
29 30 31 |
# File 'lib/mist_aws/ec2.rb', line 29 def get_security_group(group_name, vpc_id) ec2_client.describe_security_groups(filters: [{ name: "vpc-id", values: [vpc_id]}]).security_groups.detect { |g| g.group_name == group_name } end |
#get_security_group_id(group_name, vpc_id) ⇒ Object
33 34 35 36 37 38 39 |
# File 'lib/mist_aws/ec2.rb', line 33 def get_security_group_id(group_name, vpc_id) if (group = get_security_group(group_name, vpc_id)) group.group_id else nil end end |
#security_group_exists?(group_name, vpc_id) ⇒ Boolean
41 42 43 |
# File 'lib/mist_aws/ec2.rb', line 41 def security_group_exists?(group_name, vpc_id) get_security_group(group_name, vpc_id) end |