Permalink
Browse files

[aws]Add in subnets

  • Loading branch information...
1 parent 6aee48a commit f281bfb16383bbfe1e11739f5d07d9dac4d29bf8 Eric Stonfer committed Mar 8, 2012
View
@@ -29,6 +29,8 @@ class AWS < Fog::Service
collection :volumes
model :spot_request
collection :spot_requests
+ model :subnet
+ collection :subnets
model :vpc
collection :vpcs
@@ -44,6 +46,7 @@ class AWS < Fog::Service
request :create_security_group
request :create_snapshot
request :create_spot_datafeed_subscription
+ request :create_subnet
request :create_tags
request :create_volume
request :create_vpc
@@ -52,6 +55,7 @@ class AWS < Fog::Service
request :delete_placement_group
request :delete_snapshot
request :delete_spot_datafeed_subscription
+ request :delete_subnet
request :delete_tags
request :delete_volume
request :delete_vpc
@@ -71,6 +75,7 @@ class AWS < Fog::Service
request :describe_spot_datafeed_subscription
request :describe_spot_instance_requests
request :describe_spot_price_history
+ request :describe_subnets
request :describe_tags
request :describe_volumes
request :describe_vpcs
@@ -0,0 +1,58 @@
+require 'fog/core/model'
+
+module Fog
+ module Compute
+ class AWS
+
+ class Subnet < Fog::Model
+
+ identity :subnet_id, :aliases => 'subnetId'
+ attribute :state
+ attribute :vpc_id, :aliases => 'vpcId'
+ attribute :cidr_block, :aliases => 'cidrBlock'
+ attribute :available_ip_address_count, :aliases => 'availableIpAddressCount'
+ attribute :availability_zone, :aliases => 'availabilityZone'
+ attribute :tag_set, :aliases => 'tagSet'
+
+
+ # Removes an existing subnet
+ #
+ # subnet.destroy
+ #
+ # ==== Returns
+ #
+ # True or false depending on the result
+ #
+
+ def destroy
+ requires :subnet_id
+
+ connection.delete_subnet(subnet_id)
+ true
+ end
+
+ # Create a subnet
+ #
+ # >> g = AWS.subnets.new(:vpc_id => "vpc-someId", :cidr_block => "10.0.0.0/24")
+ # >> g.save
+ #
+ # == Returns:
+ #
+ # requestId and a subnetSet object
+ #
+
+ def save
+ requires :vpc_id, :cidr_block
+ data = connection.create_subnet(vpc_id, cidr_block).body['subnetSet'].first
+ puts data
+ new_attributes = data.reject {|key,value| key == 'requestId'}
+ merge_attributes(new_attributes)
+ true
+
+ true
+ end
+
+ end
+ end
+ end
+end
@@ -0,0 +1,99 @@
+require 'fog/core/collection'
+require 'fog/aws/models/compute/subnet'
+
+module Fog
+ module Compute
+ class AWS
+
+ class Subnets < Fog::Collection
+
+ attribute :filters
+
+ model Fog::Compute::AWS::Subnet
+
+ # Creates a new subnet
+ #
+ # AWS.subnets.new
+ #
+ # ==== Returns
+ #
+ # Returns the details of the new Subnet
+ #
+ #>> AWS.subnets.new
+ # <Fog::AWS::Compute::Subnet
+ # subnet_id=subnet-someId,
+ # state=[pending|available],
+ # vpc_id=vpc-someId
+ # cidr_block=someIpRange
+ # available_ip_address_count=someInt
+ # tagset=nil
+ # >
+ #
+
+ def initialize(attributes)
+ self.filters ||= {}
+ super
+ end
+
+ # Returns an array of all Subnets that have been created
+ #
+ # AWS.subnets.all
+ #
+ # ==== Returns
+ #
+ # Returns an array of all VPCs
+ #
+ #>> AWS.subnets.all
+ # <Fog::AWS::Compute::Subnet
+ # filters={}
+ # [
+ # subnet_id=subnet-someId,
+ # state=[pending|available],
+ # vpc_id=vpc-someId
+ # cidr_block=someIpRange
+ # available_ip_address_count=someInt
+ # tagset=nil
+ # ]
+ # >
+ #
+
+ def all(filters = filters)
+ unless filters.is_a?(Hash)
+ Fog::Logger.warning("all with #{filters.class} param is deprecated, use all('subnet-id' => []) instead [light_black](#{caller.first})[/]")
+ filters = {'subnet-id' => [*filters]}
+ end
+ self.filters = filters
+ data = connection.describe_subnets(filters).body
+ load(data['subnetSet'])
+ end
+
+ # Used to retrieve a Subnet
+ # subnet-id is required to get the associated VPC information.
+ #
+ # You can run the following command to get the details:
+ # AWS.subnets.get("subnet-12345678")
+ #
+ # ==== Returns
+ #
+ #>> AWS.subnets.get("subnet-12345678")
+ # <Fog::AWS::Compute::Subnet
+ # subnet_id=subnet-someId,
+ # state=[pending|available],
+ # vpc_id=vpc-someId
+ # cidr_block=someIpRange
+ # available_ip_address_count=someInt
+ # tagset=nil
+ # >
+ #
+
+ def get(subnet_id)
+ if subnet_id
+ self.class.new(:connection => connection).all('subnet-id' => subnet_id).first
+ end
+ end
+
+ end
+
+ end
+ end
+end
@@ -6,13 +6,13 @@ class AWS
class VPC < Fog::Model
- identity :id, :aliases => 'vpcId'
+ identity :id, :aliases => 'vpcId'
attribute :state
- attribute :cidr_block, :aliases => 'cidrBlock'
- attribute :dhcp_options_id, :aliases => 'dhcpOptionsId'
- attribute :tags, :aliases => 'tagSet'
- attribute :tenancy, :aliases => 'instanceTenancy'
+ attribute :cidr_block, :aliases => 'cidrBlock'
+ attribute :dhcp_options_id, :aliases => 'dhcpOptionsId'
+ attribute :tags, :aliases => 'tagSet'
+ attribute :tenancy, :aliases => 'instanceTenancy'
def initialize(attributes={})
self.dhcp_options_id ||= "default"
@@ -0,0 +1,51 @@
+module Fog
+ module Parsers
+ module Compute
+ module AWS
+
+ class CreateSubnet < Fog::Parsers::Base
+
+ def reset
+ @subnet = { 'tagSet' => {} }
+ @response = { 'subnetSet' => [] }
+ @tag = {}
+ end
+
+ def start_element(name, attrs = [])
+ super
+ case name
+ when 'tagSet'
+ @in_tag_set = true
+ end
+ end
+
+ def end_element(name)
+ if @in_tag_set
+ case name
+ when 'item'
+ @subnet['tagSet'][@tag['key']] = @tag['value']
+ @tag = {}
+ when 'key', 'value'
+ @tag[name] = value
+ when 'tagSet'
+ @in_tag_set = false
+ end
+ else
+ case name
+ when 'subnetId', 'state', 'vpcId', 'cidrBlock', 'availableIpAddressCount', 'availabilityZone'
+ @subnet[name] = value
+ when 'subnet'
+ @response['subnetSet'] << @subnet
+ @subnet = { 'tagSet' => {} }
+ when 'requestId'
+ @response[name] = value
+ end
+ end
+ end
+ end
+ end
+ end
+ end
+end
+
+
@@ -0,0 +1,24 @@
+module Fog
+ module Parsers
+ module Compute
+ module AWS
+
+ class DeleteSubnet < Fog::Parsers::Base
+
+ def end_element(name)
+ case name
+ when 'requestId'
+ @response[name] = value
+ when 'return'
+ if value == 'true'
+ @response[name] = true
+ else
+ @response[name] = false
+ end
+ end
+ end
+ end
+ end
+ end
+ end
+end
@@ -0,0 +1,49 @@
+module Fog
+ module Parsers
+ module Compute
+ module AWS
+
+ class DescribeSubnets < Fog::Parsers::Base
+
+ def reset
+ @subnet = { 'tagSet' => {} }
+ @response = { 'subnetSet' => [] }
+ @tag = {}
+ end
+
+ def start_element(name, attrs = [])
+ super
+ case name
+ when 'tagSet'
+ @in_tag_set = true
+ end
+ end
+
+ def end_element(name)
+ if @in_tag_set
+ case name
+ when 'item'
+ @subnet['tagSet'][@tag['key']] = @tag['value']
+ @tag = {}
+ when 'key', 'value'
+ @tag[name] = value
+ when 'tagSet'
+ @in_tag_set = false
+ end
+ else
+ case name
+ when 'subnetId', 'state', 'vpcId', 'cidrBlock', 'availableIpAddressCount', 'availabilityZone'
+ @subnet[name] = value
+ when 'item'
+ @response['subnetSet'] << @subnet
+ @subnet = { 'tagSet' => {} }
+ when 'requestId'
+ @response[name] = value
+ end
+ end
+ end
+ end
+ end
+ end
+ end
+end
Oops, something went wrong.

0 comments on commit f281bfb

Please sign in to comment.