Class: Backends::Ec2::Network

Inherits:
Base
  • Object
show all
Includes:
Helpers::NetworkCreateHelper, Helpers::NetworkDeleteHelper, Helpers::NetworkDummyHelper, Helpers::NetworkParseHelper
Defined in:
lib/backends/ec2/network.rb

Constant Summary collapse

NETWORK_DUMMIES =
['public', 'private'].freeze

Constants inherited from Base

Base::API_VERSION, Base::IMAGE_FILTERING_POLICIES

Instance Method Summary collapse

Methods included from Helpers::NetworkDummyHelper

#get_dummy_private, #get_dummy_public

Methods included from Helpers::NetworkCreateHelper

#create_add_igw

Methods included from Helpers::NetworkDeleteHelper

#delete_acls, #delete_dhcp_options, #delete_internet_gateways, #delete_route_tables, #delete_security_groups, #delete_subnets, #delete_vpn_connections, #delete_vpn_connections_wait4detach, #delete_vpn_gateways, #delete_vpn_gateways_wait4detach

Methods included from Helpers::NetworkParseHelper

#parse_backend_obj

Methods inherited from Base

#add_other_backend, #initialize

Methods included from Helpers::ExtensionsHelper

#read_extensions

Methods included from Helpers::JsonCollectionHelper

#read_from_json

Constructor Details

This class inherits a constructor from Backends::Ec2::Base

Instance Method Details

#create(network) ⇒ String

Instantiates a new network instance from ::Occi::Infrastructure::Network. ID given in the occi.core.id attribute is optional and can be changed inside this method. Final occi.core.id must be returned as a String. If the requested instance cannot be created, an error describing the problem must be raised, @see Backends::Errors.

Examples:

network = ::Occi::Infrastructure::Network.new
network_id = create(network)
    #=> "65d4f65adfadf-ad2f4ad-daf5ad-f5ad4fad4ffdf"

Parameters:

  • network (::Occi::Infrastructure::Network)

    network instance containing necessary attributes

Returns:

  • (String)

    final identifier of the new network instance

Server-side Effects:

  • Creates a VPC

  • Creates a subnet and creates tags for it

  • Creates a new Internet gateway nd creates tags for it

  • Attaches the Internet gateway to the VPC


96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/backends/ec2/network.rb', line 96

def create(network)
  fail Backends::Errors::UserNotAuthorizedError, "Creating networks has been disabled in server's configuration!" \
    unless @options.network_create_allowed

  fail Backends::Errors::ResourceNotValidError, "Network address in CIDR notation is required!" if network.address.blank?
  tags = []
  tags << { key: 'Name', value: (network.title || "rOCCI-server VPC #{network.address}") }

  vpc = nil
  Backends::Ec2::Helpers::AwsConnectHelper.rescue_aws_service(@logger) do
    vpc = @ec2_client.create_vpc(
      cidr_block: network.address,
      instance_tenancy: "default"
    ).vpc

    vpc_subnet = @ec2_client.create_subnet(
      vpc_id: vpc[:vpc_id],
      cidr_block: network.address,
      availability_zone: @options.aws_availability_zone
    ).subnet

    @ec2_client.create_tags(
      resources: [vpc[:vpc_id], vpc_subnet[:subnet_id]],
      tags: tags
    )
  end

  create_add_igw(vpc[:vpc_id], tags)

  vpc[:vpc_id]
end

#delete(network_id) ⇒ true, false

Deletes a specific network instance, instance to be deleted is specified by an ID, this ID must match the occi.core.id attribute of the deleted instance. If the requested instance cannot be deleted, an error describing the problem must be raised, @see Backends::Errors.

Examples:

delete("65d4f65adfadf-ad2f4ad-daf5ad-f5ad4fad4ffdf") #=> true

Parameters:

  • network_id (String)

    an identifier of a network instance to be deleted

Returns:

  • (true, false)

    result of the operation

Server-side Effects:

  • Gets status of the given VPC

  • Deletes all items for a given VPC (security groups, internet gateways, VPN gateways, ACLs, routing tables, subnets, and DHCP options)

  • Deletes the given VPC


164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/backends/ec2/network.rb', line 164

def delete(network_id)
  fail Backends::Errors::UserNotAuthorizedError, "Deleting networks has been disabled in server's configuration!" \
    unless @options.network_destroy_allowed

  fail Backends::Errors::UserNotAuthorizedError, "You cannot delete EC2-provided networks! [#{network_id.inspect}]" \
    if NETWORK_DUMMIES.include?(network_id)

  vpc = get_raw(network_id)
  fail Backends::Errors::ResourceNotFoundError, "The VPC #{network_id.inspect} does not exist." unless vpc

  delete_security_groups(vpc)
  delete_internet_gateways(vpc)
  delete_vpn_gateways(vpc)
  delete_acls(vpc)
  delete_route_tables(vpc)
  delete_subnets(vpc)

  Backends::Ec2::Helpers::AwsConnectHelper.rescue_aws_service(@logger) do
    @ec2_client.delete_vpc(vpc_id: network_id)
  end

  delete_dhcp_options(vpc)

  true
end

#delete_all(mixins = nil) ⇒ true, false

Deletes all network instances, instances to be deleted must be filtered by the specified filter, filter (if set) must contain an ::Occi::Core::Mixins instance. If the requested instances cannot be deleted, an error describing the problem must be raised, @see Backends::Errors.

Examples:

delete_all #=> true

mixins = ::Occi::Core::Mixins.new << ::Occi::Core::Mixin.new
delete_all(mixins)  #=> true

Parameters:

  • mixins (::Occi::Core::Mixins) (defaults to: nil)

    a filter containing mixins

Returns:

  • (true, false)

    result of the operation

Server-side Effects:

  • Deletes all items for all VPCs (security groups, internet gateways, VPN gateways, ACLs, routing tables, subnets, and DHCP options)

  • Deletes all VPCs


143
144
145
146
147
148
# File 'lib/backends/ec2/network.rb', line 143

def delete_all(mixins = nil)
  vpc_ids = list_ids(mixins) - NETWORK_DUMMIES
  vpc_ids.each { |vpc_id| delete(vpc_id) }

  true
end

#get(network_id) ⇒ ::Occi::Infrastructure::Network?

Gets a specific network instance as ::Occi::Infrastructure::Network. ID given as an argument must match the occi.core.id attribute inside the returned ::Occi::Infrastructure::Network instance, however it is possible to implement internal mapping to a platform-specific identifier.

Examples:

network = get('65d4f65adfadf-ad2f4ad-daf5ad-f5ad4fad4ffdf')
    #=> #<::Occi::Infrastructure::Network>

Parameters:

  • network_id (String)

    OCCI identifier of the requested network instance

Returns:

  • (::Occi::Infrastructure::Network, nil)

    a network instance or `nil`

Server-side Effects:

  • Gets status of the given VPC


71
72
73
74
75
76
77
# File 'lib/backends/ec2/network.rb', line 71

def get(network_id)
  return get_dummy_public if network_id == 'public'
  return get_dummy_private if network_id == 'private'

  vpc = get_raw(network_id)
  vpc ? parse_backend_obj(vpc) : nil
end

#get_extensions::Occi::Collection

Returns a collection of custom mixins introduced (and specific for) the enabled backend. Only mixins and actions are allowed.

Returns:

  • (::Occi::Collection)

    collection of extensions (custom mixins and/or actions)


272
273
274
# File 'lib/backends/ec2/network.rb', line 272

def get_extensions
  read_extensions 'network', @options.model_extensions_dir
end

#list(mixins = nil) ⇒ ::Occi::Core::Resources

Gets all network instances, instances must be filtered by the specified filter, filter (if set) must contain an ::Occi::Core::Mixins instance. Returned collection must contain ::Occi::Infrastructure::Network instances wrapped in ::Occi::Core::Resources.

Examples:

networks = list #=> #<::Occi::Core::Resources>
networks.first #=> #<::Occi::Infrastructure::Network>

mixins = ::Occi::Core::Mixins.new << ::Occi::Core::Mixin.new
networks = list(mixins) #=> #<::Occi::Core::Resources>

Parameters:

  • mixins (::Occi::Core::Mixins) (defaults to: nil)

    a filter containing mixins

Returns:

  • (::Occi::Core::Resources)

    a collection of network instances

Server-side Effects:

  • Gets the status of existing VPC instances


46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/backends/ec2/network.rb', line 46

def list(mixins = nil)
  networks = ::Occi::Core::Resources.new

  Backends::Ec2::Helpers::AwsConnectHelper.rescue_aws_service(@logger) do
    vpcs = @ec2_client.describe_vpcs.vpcs
    vpcs.each { |vpc| networks << parse_backend_obj(vpc) } if vpcs
  end

  networks << get_dummy_public << get_dummy_private

  networks
end

#list_ids(mixins = nil) ⇒ Array<String>

Gets all network instance IDs, no details, no duplicates. Returned identifiers must correspond to those found in the occi.core.id attribute of ::Occi::Infrastructure::Network instances.

Examples:

list_ids #=> []
list_ids #=> ["65d4f65adfadf-ad2f4ad-daf5ad-f5ad4fad4ffdf",
                         "ggf4f65adfadf-adgg4ad-daggad-fydd4fadyfdfd"]

Parameters:

  • mixins (::Occi::Core::Mixins) (defaults to: nil)

    a filter containing mixins

Returns:

  • (Array<String>)

    IDs for all available network instances

Server-side Effects:

  • Gets the status of existing VPC instances


18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/backends/ec2/network.rb', line 18

def list_ids(mixins = nil)
  id_list = []

  Backends::Ec2::Helpers::AwsConnectHelper.rescue_aws_service(@logger) do
    vpcs = @ec2_client.describe_vpcs.vpcs
    vpcs.each { |vpc| id_list << vpc[:vpc_id] } if vpcs
  end

  id_list.concat(NETWORK_DUMMIES)

  id_list
end

#partial_update(network_id, attributes = nil, mixins = nil, links = nil) ⇒ true, false

TODO:

Not supported

Partially updates an existing network instance, instance to be updated is specified by network_id. If the requested instance cannot be updated, an error describing the problem must be raised, @see Backends::Errors.

Examples:

attributes = ::Occi::Core::Attributes.new
mixins = ::Occi::Core::Mixins.new
links = ::Occi::Core::Links.new
partial_update(network_id, attributes, mixins, links) #=> true

Parameters:

  • network_id (String)

    unique identifier of a network instance to be updated

  • attributes (::Occi::Core::Attributes) (defaults to: nil)

    a collection of attributes to be updated

  • mixins (::Occi::Core::Mixins) (defaults to: nil)

    a collection of mixins to be added

  • links (::Occi::Core::Links) (defaults to: nil)

    a collection of links to be added

Returns:

  • (true, false)

    result of the operation


207
208
209
# File 'lib/backends/ec2/network.rb', line 207

def partial_update(network_id, attributes = nil, mixins = nil, links = nil)
  fail Backends::Errors::MethodNotImplementedError, 'Partial updates are currently not supported!'
end

#trigger_action(network_id, action_instance) ⇒ true, false

TODO:

Not implemented

Triggers an action on an existing network instance, the network instance in question is identified by a network instance ID, action is identified by the action.term attribute of the action instance passed as an argument. If the requested action cannot be triggered, an error describing the problem must be raised, @see Backends::Errors.

Examples:

action_instance = ::Occi::Core::ActionInstance.new
trigger_action("65d4f65adfadf-ad2f4ad-daf5ad-f5ad4fad4ffdf", action_instance)
  #=> true

Parameters:

  • network_id (String)

    network instance identifier

  • action_instance (::Occi::Core::ActionInstance)

    action to be triggered

Returns:

  • (true, false)

    result of the operation


262
263
264
265
266
# File 'lib/backends/ec2/network.rb', line 262

def trigger_action(network_id, action_instance)
  fail Backends::Errors::ActionNotImplementedError,
       "Action #{action_instance.action.type_identifier.inspect} is not implemented!"
  true
end

#trigger_action_on_all(action_instance, mixins = nil) ⇒ true, false

TODO:

Underlying method not implemented

Triggers an action on all existing network instance, instances must be filtered by the specified filter, filter (if set) must contain an ::Occi::Core::Mixins instance, action is identified by the action.term attribute of the action instance passed as an argument. If the requested action cannot be triggered, an error describing the problem must be raised, @see Backends::Errors.

Examples:

action_instance = ::Occi::Core::ActionInstance.new
mixins = ::Occi::Core::Mixins.new << ::Occi::Core::Mixin.new
trigger_action_on_all(action_instance, mixin) #=> true

Parameters:

  • action_instance (::Occi::Core::ActionInstance)

    action to be triggered

  • mixins (::Occi::Core::Mixins) (defaults to: nil)

    a filter containing mixins

Returns:

  • (true, false)

    result of the operation


242
243
244
245
# File 'lib/backends/ec2/network.rb', line 242

def trigger_action_on_all(action_instance, mixins = nil)
  list_ids(mixins).each { |ntwrk| trigger_action(ntwrk, action_instance) }
  true
end

#update(network) ⇒ true, false

TODO:

Not implemented

Updates an existing network instance, instance to be updated is specified using the occi.core.id attribute of the instance passed as an argument. If the requested instance cannot be updated, an error describing the problem must be raised, @see Backends::Errors.

Examples:

network = ::Occi::Infrastructure::Network.new
update(network) #=> true

Parameters:

  • network (::Occi::Infrastructure::Network)

    instance containing updated information

Returns:

  • (true, false)

    result of the operation


223
224
225
# File 'lib/backends/ec2/network.rb', line 223

def update(network)
  fail Backends::Errors::MethodNotImplementedError, 'Not Implemented!'
end