Module: Backends::Ec2::Helpers::ComputeNetworkHelper

Included in:
Compute
Defined in:
lib/backends/ec2/helpers/compute_network_helper.rb

Instance Method Summary collapse

Instance Method Details

#attach_network_private(networkinterface) ⇒ Object


42
43
44
45
# File 'lib/backends/ec2/helpers/compute_network_helper.rb', line 42

def attach_network_private(networkinterface)
  # TODO: explore possible solutions, do not forget to update the effects tag for attach_network()
  fail Backends::Errors::ResourceCreationError, 'Network "private" cannot be attached manually!'
end

#attach_network_public(networkinterface) ⇒ Object


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
# File 'lib/backends/ec2/helpers/compute_network_helper.rb', line 5

def attach_network_public(networkinterface)
  compute_id = networkinterface.source.split('/').last

  compute_instance = get(compute_id)
  fail Backends::Errors::ResourceCreationError, "Resource #{compute_id.inspect} already has a public network attached!" \
    if attach_network_public_has_elastic?(compute_id)

  is_vpc = compute_instance.links.to_a.select { |l| l.target.split('/').last.include?('vpc-') }.any?

  addr_opts = {}
  addr_opts[:instance_id] = compute_id

  Backends::Ec2::Helpers::AwsConnectHelper.rescue_aws_service(@logger) do
    if is_vpc
      addr_opts[:allocation_id] = @ec2_client.allocate_address(domain: 'vpc')[:allocation_id]
    else
      addr_opts[:public_ip] = @ec2_client.allocate_address(domain: 'standard')[:public_ip]
    end

    begin
      @ec2_client.associate_address(addr_opts)
    rescue => e
      @logger.warn "[Backends] [Ec2] An attempt to associate #{addr_opts.inspect} failed!"

      if is_vpc
        @ec2_client.release_address(allocation_id: addr_opts[:allocation_id])
      else
        @ec2_client.release_address(public_ip: addr_opts[:public_ip])
      end

      fail Backends::Errors::ResourceCreationError, e.message
    end
  end

  "compute_#{compute_id}_nic_eni-0"
end

#attach_network_vpc(networkinterface) ⇒ Object


47
48
49
50
# File 'lib/backends/ec2/helpers/compute_network_helper.rb', line 47

def attach_network_vpc(networkinterface)
  # TODO: explore possible solutions, do not forget to update the effects tag for attach_network()
  fail Backends::Errors::ResourceCreationError, "VPC networks cannot be attached to existing instances!"
end

#detach_network_private(networkinterface) ⇒ Object


82
83
84
# File 'lib/backends/ec2/helpers/compute_network_helper.rb', line 82

def detach_network_private(networkinterface)
  fail Backends::Errors::ResourceCreationError, 'Network "private" cannot be detached manually!'
end

#detach_network_public(networkinterface) ⇒ Object


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
# File 'lib/backends/ec2/helpers/compute_network_helper.rb', line 52

def detach_network_public(networkinterface)
  ec2_allocation = networkinterface.attributes.occi!.networkinterface!.address
  fail Backends::Errors::ResourceCreationError, 'Interfaces without an address cannot be detached!' if ec2_allocation.blank?
  ec2_aux = attach_network_public_get_as_al(ec2_allocation)

  addr_opts = {}
  if ec2_aux && ec2_aux[:association_id]
    addr_opts[:association_id] = ec2_aux[:association_id]
  else
    addr_opts[:public_ip] = ec2_allocation
  end

  addr_opts_al = {}
  if ec2_aux && ec2_aux[:allocation_id]
    addr_opts_al[:allocation_id] = ec2_aux[:allocation_id]
  else
    addr_opts_al[:public_ip] = ec2_allocation
  end

  Backends::Ec2::Helpers::AwsConnectHelper.rescue_aws_service(@logger) do
    begin
      @ec2_client.disassociate_address(addr_opts)
      @ec2_client.release_address(addr_opts_al)
    rescue ::Aws::EC2::Errors::AuthFailure => e
      @logger.warn "[Backends] [Ec2] An attempt to release #{ec2_allocation.inspect} failed!"
      fail Backends::Errors::UserNotAuthorizedError, e.message
    end
  end
end

#detach_network_vpc(networkinterface) ⇒ Object


86
87
88
89
# File 'lib/backends/ec2/helpers/compute_network_helper.rb', line 86

def detach_network_vpc(networkinterface)
  # TODO: explore possible solutions
  fail Backends::Errors::ResourceCreationError, "VPC networks cannot be detached from existing instances!"
end