Class: Fog::Network::OpenStack::Mock

Inherits:
Object
  • Object
show all
Defined in:
lib/fog/openstack/network.rb,
lib/fog/openstack/requests/network/get_port.rb,
lib/fog/openstack/requests/network/get_router.rb,
lib/fog/openstack/requests/network/get_subnet.rb,
lib/fog/openstack/requests/network/list_ports.rb,
lib/fog/openstack/requests/network/set_tenant.rb,
lib/fog/openstack/requests/network/create_port.rb,
lib/fog/openstack/requests/network/delete_port.rb,
lib/fog/openstack/requests/network/get_network.rb,
lib/fog/openstack/requests/network/update_port.rb,
lib/fog/openstack/requests/network/list_routers.rb,
lib/fog/openstack/requests/network/list_subnets.rb,
lib/fog/openstack/requests/network/create_router.rb,
lib/fog/openstack/requests/network/create_subnet.rb,
lib/fog/openstack/requests/network/delete_router.rb,
lib/fog/openstack/requests/network/delete_subnet.rb,
lib/fog/openstack/requests/network/list_networks.rb,
lib/fog/openstack/requests/network/update_router.rb,
lib/fog/openstack/requests/network/update_subnet.rb,
lib/fog/openstack/requests/network/create_network.rb,
lib/fog/openstack/requests/network/delete_network.rb,
lib/fog/openstack/requests/network/update_network.rb,
lib/fog/openstack/requests/network/get_floating_ip.rb,
lib/fog/openstack/requests/network/list_floating_ips.rb,
lib/fog/openstack/requests/network/create_floating_ip.rb,
lib/fog/openstack/requests/network/delete_floating_ip.rb,
lib/fog/openstack/requests/network/add_router_interface.rb,
lib/fog/openstack/requests/network/associate_floating_ip.rb,
lib/fog/openstack/requests/network/remove_router_interface.rb,
lib/fog/openstack/requests/network/disassociate_floating_ip.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Mock

Returns a new instance of Mock.



89
90
91
92
# File 'lib/fog/openstack/network.rb', line 89

def initialize(options={})
  @openstack_username = options[:openstack_username]
  @openstack_tenant   = options[:openstack_tenant]
end

Class Method Details

.dataObject



73
74
75
76
77
78
79
80
81
82
83
# File 'lib/fog/openstack/network.rb', line 73

def self.data
  @data ||= Hash.new do |hash, key|
    hash[key] = {
      :networks => {},
      :ports => {},
      :subnets => {},
      :floating_ips => {},
      :routers => {},
    }
  end
end

.resetObject



85
86
87
# File 'lib/fog/openstack/network.rb', line 85

def self.reset
  @data = nil
end

Instance Method Details

#add_router_interface(router_id, subnet_id, options = {}) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/fog/openstack/requests/network/add_router_interface.rb', line 21

def add_router_interface(router_id, subnet_id, options = {})
  response = Excon::Response.new
  response.status = 201
  data = {
    'status' => 'ACTIVE',
    'name' => '',
    'admin_state_up' => true,
    'network_id' => '5307648b-e836-4658-8f1a-ff7536870c64',
    'tenant_id' => '6b96ff0cb17a4b859e1e575d221683d3',
    'device_owner' => 'network:router_interface',
    'mac_address' => 'fa:16:3e:f7:d1:9c',
    'fixed_ips' => {
      'subnet_id' => 'a2f1f29d-571b-4533-907f-5803ab96ead1',
      'ip_address' => '10.1.1.1'
    },
    'id' => '3a44f4e5-1694-493a-a1fb-393881c673a4',
    'device_id' => '7177abc4-5ae9-4bb7-b0d4-89e94a4abf3b'
  }

  self.data[:routers][data['router_id']] = data
  response.body = { 'router' => data }
  response
end

#associate_floating_ip(floating_ip_id, port_id, options = {}) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/fog/openstack/requests/network/associate_floating_ip.rb', line 28

def associate_floating_ip(floating_ip_id, port_id, options = {})
  response = Excon::Response.new
  response.status = 201
  data = {
    'id'                  => '00000000-0000-0000-0000-000000000000',
    'router_id'           => '00000000-0000-0000-0000-000000000000',
    'tenant_id'           => options["tenant_id"],
    'floating_network_id' => options["floating_network_id"],
    'fixed_ip_address'    => options["fixed_ip_address"],
    'floating_ip_address' => options["floating_ip_address"],
    'port_id'             => port_id,
  }

  self.data[:floating_ips][data['floating_ip_id']] = data
  response.body = { 'floatingip' => data }
  response
end

#create_floating_ip(floating_network_id, options = {}) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/fog/openstack/requests/network/create_floating_ip.rb', line 31

def create_floating_ip(floating_network_id, options = {})
  response = Excon::Response.new
  response.status = 201
  data = {
    'id'                  => floating_network_id,
    'floating_network_id' => floating_network_id,
    'port_id'             => options[:port_id],
    'tenant_id'           => options[:tenant_id],
    'fixed_ip_address'    => options[:fixed_ip_address],
    'router_id'           => nil,
  }
  self.data[:floating_ips][data['id']] = data
  response.body = { 'floatingip' => data }
  response
end

#create_network(options = {}) ⇒ Object



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
# File 'lib/fog/openstack/requests/network/create_network.rb', line 58

def create_network(options = {})
  response = Excon::Response.new
  response.status = 201
  data = {
    'id'             => Fog::Mock.random_numbers(6).to_s,
    'name'           => options[:name],
    'shared'         => options[:shared],
    'subnets'        => [],
    'status'         => 'ACTIVE',
    'admin_state_up' => options[:admin_state_up],
    'tenant_id'      => options[:tenant_id],
  }

  # Add provider specific attributes when found
  #
  provider_options = [
    :router_external,
    :provider_network_type,
    :provider_segmentation_id,
    :provider_physical_network
  ]
  aliases = {
    :provider_network_type     => 'provider:network_type',
    # Not applicable to the "local" or "gre" network types
    :provider_physical_network => 'provider:physical_network',
    :provider_segmentation_id  => 'provider:segmentation_id',
    :router_external           => 'router:external'
  }
  provider_options.reject{ |o| options[o].nil? }.each do |key|
    aliased_key = aliases[key] || key
    data[aliased_key] = options[key]
  end

  self.data[:networks][data['id']] = data
  response.body = { 'network' => data }
  response
end

#create_port(network_id, options = {}) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/fog/openstack/requests/network/create_port.rb', line 29

def create_port(network_id, options = {})
  response = Excon::Response.new
  response.status = 201
  data = {
    'id'             => Fog::Mock.random_numbers(6).to_s,
    'name'           => options[:name],
    'network_id'     => network_id,
    'fixed_ips'      => options[:fixed_ips],
    'mac_address'    => options[:mac_address],
    'status'         => 'ACTIVE',
    'admin_state_up' => options[:admin_state_up],
    'device_owner'   => options[:device_owner],
    'device_id'      => options[:device_id],
    'tenant_id'      => options[:tenant_id],
  }
  self.data[:ports][data['id']] = data
  response.body = { 'port' => data }
  response
end

#create_router(name, options = {}) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/fog/openstack/requests/network/create_router.rb', line 36

def create_router(name, options = {})
  response = Excon::Response.new
  response.status = 201
  data = {
    'router' => {
      'status' => 'ACTIVE',
      'external_gateway_info' => nil,
      'name' => 'another_router',
      'admin_state_up' => true,
      'tenant_id' => '6b96ff0cb17a4b859e1e575d221683d3',
      'id' => '8604a0de-7f6b-409a-a47c-a1cc7bc77b2e'
    }
  }
  self.data['routers'] ||= []
  self.data['routers'] << data['router']
  response.body = data
  response
end

#create_subnet(network_id, cidr, ip_version, options = {}) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/fog/openstack/requests/network/create_subnet.rb', line 32

def create_subnet(network_id, cidr, ip_version, options = {})
  response = Excon::Response.new
  response.status = 201
  data = {
    'id'               => Fog::Mock.random_numbers(6).to_s,
    'name'             => options[:name],
    'network_id'       => network_id,
    'cidr'             => cidr,
    'ip_version'       => ip_version,
    'gateway_ip'       => options[:gateway_ip],
    'allocation_pools' => options[:allocation_pools],
    'dns_nameservers'  => options[:dns_nameservers],
    'host_routes'      => options[:host_routes],
    'enable_dhcp'      => options[:enable_dhcp],
    'tenant_id'        => options[:tenant_id],
  }
  self.data[:subnets][data['id']] = data
  response.body = { 'subnet' => data }
  response
end

#credentialsObject



102
103
104
105
106
107
# File 'lib/fog/openstack/network.rb', line 102

def credentials
  { :provider                 => 'openstack',
    :openstack_auth_url       => @openstack_auth_uri.to_s,
    :openstack_auth_token     => @auth_token,
    :openstack_management_url => @openstack_management_url }
end

#dataObject



94
95
96
# File 'lib/fog/openstack/network.rb', line 94

def data
  self.class.data["#{@openstack_username}-#{@openstack_tenant}"]
end

#delete_floating_ip(floating_ip_id) ⇒ Object



16
17
18
19
20
21
22
23
24
25
# File 'lib/fog/openstack/requests/network/delete_floating_ip.rb', line 16

def delete_floating_ip(floating_ip_id)
  response = Excon::Response.new
  if list_floating_ips.body['floatingips'].map { |r| r['id'] }.include? floating_ip_id
    self.data[:floating_ips].delete(floating_ip_id)
    response.status = 204
    response
  else
    raise Fog::Network::OpenStack::NotFound
  end
end

#delete_network(network_id) ⇒ Object



16
17
18
19
20
21
22
23
24
25
# File 'lib/fog/openstack/requests/network/delete_network.rb', line 16

def delete_network(network_id)
  response = Excon::Response.new
  if list_networks.body['networks'].map { |r| r['id'] }.include? network_id
    self.data[:networks].delete(network_id)
    response.status = 204
    response
  else
    raise Fog::Network::OpenStack::NotFound
  end
end

#delete_port(port_id) ⇒ Object



16
17
18
19
20
21
22
23
24
25
# File 'lib/fog/openstack/requests/network/delete_port.rb', line 16

def delete_port(port_id)
  response = Excon::Response.new
  if list_ports.body['ports'].map { |r| r['id'] }.include? port_id
    self.data[:ports].delete(port_id)
    response.status = 204
    response
  else
    raise Fog::Network::OpenStack::NotFound
  end
end

#delete_router(router_id) ⇒ Object



16
17
18
19
20
21
22
23
24
25
# File 'lib/fog/openstack/requests/network/delete_router.rb', line 16

def delete_router(router_id)
  response = Excon::Response.new
  if list_routers.body['routers'].map { |r| r['id'] }.include? router_id
    self.data[:routers].delete(router_id)
    response.status = 204
    response
  else
    raise Fog::Network::OpenStack::NotFound
  end
end

#delete_subnet(subnet_id) ⇒ Object



16
17
18
19
20
21
22
23
24
25
# File 'lib/fog/openstack/requests/network/delete_subnet.rb', line 16

def delete_subnet(subnet_id)
  response = Excon::Response.new
  if list_subnets.body['subnets'].map { |r| r['id'] }.include? subnet_id
    self.data[:subnets].delete(subnet_id)
    response.status = 204
    response
  else
    raise Fog::Network::OpenStack::NotFound
  end
end

#disassociate_floating_ip(floating_ip_id, options = {}) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/fog/openstack/requests/network/disassociate_floating_ip.rb', line 28

def disassociate_floating_ip(floating_ip_id, options = {})
  response = Excon::Response.new
  response.status = 200
  data = {
    'id'                  => '00000000-0000-0000-0000-000000000000',
    'router_id'           => nil,
    'tenant_id'           => options["tenant_id"],
    'floating_network_id' => options["floating_network_id"],
    'fixed_ip_address'    => nil,
    'floating_ip_address' => options["floating_ip_address"],
    'port_id'             => options["port_id"],
  }

  self.data[:floating_ips][data['floating_ip_id']] = data
  response.body = { 'floatingip' => data }
  response
end

#get_floating_ip(floating_ip_id) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/fog/openstack/requests/network/get_floating_ip.rb', line 16

def get_floating_ip(floating_ip_id)
  response = Excon::Response.new
  if data = self.data[:floating_ips][floating_ip_id]
    response.status = 200
    response.body = {
      "floatingip" => {
        "id"                  => "00000000-0000-0000-0000-000000000000",
        # changed
        # "floating_ip_id" => floating_ip_id,
        "port_id"             => data["port_id"],
        "tenant_id"           => data["tenant_id"],
        "fixed_ip_address"    => data["fixed_ip_address"],
        "router_id"           => "00000000-0000-0000-0000-000000000000",
        "floating_ip_address" => data["floating_ip_address"],
      }
    }
    response
  else
    raise Fog::Network::OpenStack::NotFound
  end
end

#get_network(network_id) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/fog/openstack/requests/network/get_network.rb', line 16

def get_network(network_id)
  response = Excon::Response.new
  if data = self.data[:networks][network_id]
    response.status = 200
    response.body = {
      'network' => {
        'id' => 'e624a36d-762b-481f-9b50-4154ceb78bbb',
        'name' => 'network_1',
        'subnets' => [
          '2e4ec6a4-0150-47f5-8523-e899ac03026e'
        ],
        'shared' => false,
        'status' => 'ACTIVE',
        'admin_state_up' => true,
        'tenant_id' => 'f8b26a6032bc47718a7702233ac708b9',
      }
    }
    response
  else
    raise Fog::Network::OpenStack::NotFound
  end
end

#get_port(port_id) ⇒ Object



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
# File 'lib/fog/openstack/requests/network/get_port.rb', line 16

def get_port(port_id)
  response = Excon::Response.new
  if data = self.data[:ports][port_id]
    response.status = 200
    response.body = {
      'port' => {
        'id' => '5c81d975-5fea-4674-9c1f-b8aa10bf9a79',
        'name' => 'port_1',
        'network_id' => 'e624a36d-762b-481f-9b50-4154ceb78bbb',
        'fixed_ips' => [
          {
            'ip_address' => '10.2.2.2',
            'subnet_id' => '2e4ec6a4-0150-47f5-8523-e899ac03026e',
          }
        ],
        'mac_address' => 'fa:16:3e:62:91:7f',
        'status' => 'ACTIVE',
        'admin_state_up' => true,
        'device_id' => 'dhcp724fc160-2b2e-597e-b9ed-7f65313cd73f-e624a36d-762b-481f-9b50-4154ceb78bbb',
        'device_owner' => 'network:dhcp',
        'tenant_id' => 'f8b26a6032bc47718a7702233ac708b9',
      }
    }
    response
  else
    raise Fog::Network::OpenStack::NotFound
  end
end

#get_router(router_id) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/fog/openstack/requests/network/get_router.rb', line 16

def get_router(router_id)
  response = Excon::Response.new
  if data = (self.data['routers'].find { |r| r['id'] == router_id })
    response.status = 200
    response.body = {
      'router' => {
        'status' => 'ACTIVE',
        'external_gateway_info' => {
          'network_id' => '3c5bcddd-6af9-4e6b-9c3e-c153e521cab8'
        },
        'name' => 'router1',
        'admin_state_up' => true,
        'tenant_id' => '33a40233088643acb66ff6eb0ebea679',
        'id' => 'a9254bdb-2613-4a13-ac4c-adc581fba50d'
      }
    }
    response
  else
    raise Fog::Network::OpenStack::NotFound
  end
end

#get_subnet(subnet_id) ⇒ Object



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
# File 'lib/fog/openstack/requests/network/get_subnet.rb', line 16

def get_subnet(subnet_id)
  response = Excon::Response.new
  if data = self.data[:subnets][subnet_id]
    response.status = 200
    response.body = {
      "subnet" => {
        "id" => "2e4ec6a4-0150-47f5-8523-e899ac03026e",
        "name" => "subnet_1",
        "network_id" => "e624a36d-762b-481f-9b50-4154ceb78bbb",
        "cidr" => "10.2.2.0/24",
        "ip_version" => 4,
        "gateway_ip" => "10.2.2.1",
        "allocation_pools" => [
          {
            "start" => "10.2.2.2",
            "end" => "10.2.2.254"
          }
        ],
        "dns_nameservers" => [],
        "host_routes" => [],
        "enable_dhcp" => true,
        "tenant_id" => "f8b26a6032bc47718a7702233ac708b9",
      }
    }
    response
  else
    raise Fog::Network::OpenStack::NotFound
  end
end

#list_floating_ips(filters = {}) ⇒ Object



17
18
19
20
21
22
# File 'lib/fog/openstack/requests/network/list_floating_ips.rb', line 17

def list_floating_ips(filters = {})
  Excon::Response.new(
    :body   => { 'floatingips' => self.data[:floating_ips].values },
    :status => 200
  )
end

#list_networks(filters = {}) ⇒ Object



17
18
19
20
21
22
# File 'lib/fog/openstack/requests/network/list_networks.rb', line 17

def list_networks(filters = {})
  Excon::Response.new(
    :body   => { 'networks' => self.data[:networks].values },
    :status => 200
  )
end

#list_ports(filters = {}) ⇒ Object



17
18
19
20
21
22
# File 'lib/fog/openstack/requests/network/list_ports.rb', line 17

def list_ports(filters = {})
  Excon::Response.new(
    :body   => { 'ports' => self.data[:ports].values },
    :status => 200
  )
end

#list_routers(filters = {}) ⇒ Object



17
18
19
20
21
22
# File 'lib/fog/openstack/requests/network/list_routers.rb', line 17

def list_routers(filters = {})
  Excon::Response.new(
    :body   => { 'routers' => self.data['routers'] },
    :status => 200
  )
end

#list_subnets(filters = {}) ⇒ Object



17
18
19
20
21
22
# File 'lib/fog/openstack/requests/network/list_subnets.rb', line 17

def list_subnets(filters = {})
  Excon::Response.new(
    :body   => { 'subnets' => self.data[:subnets].values },
    :status => 200
  )
end

#remove_router_interface(router_id, subnet_id, options = {}) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/fog/openstack/requests/network/remove_router_interface.rb', line 21

def remove_router_interface(router_id, subnet_id, options = {})
  response = Excon::Response.new
  response.status = 201
  data = {
    'subnet_id' => 'a2f1f29d-571b-4533-907f-5803ab96ead1'
  }

  self.data[:routers][data['router_id']] = data
  response.body = { 'router' => data }
  response
end

#reset_dataObject



98
99
100
# File 'lib/fog/openstack/network.rb', line 98

def reset_data
  self.class.data.delete("#{@openstack_username}-#{@openstack_tenant}")
end

#set_tenant(tenant) ⇒ Object



14
15
16
# File 'lib/fog/openstack/requests/network/set_tenant.rb', line 14

def set_tenant(tenant)
  true
end

#update_network(network_id, options = {}) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/fog/openstack/requests/network/update_network.rb', line 24

def update_network(network_id, options = {})
  response = Excon::Response.new
  if network = list_networks.body['networks'].detect { |_| _['id'] == network_id }
    network['name']           = options[:name]
    network['shared']         = options[:shared]
    network['admin_state_up'] = options[:admin_state_up]
    response.body = { 'network' => network }
    response.status = 200
    response
  else
    raise Fog::Network::OpenStack::NotFound
  end
end

#update_port(port_id, options = {}) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/fog/openstack/requests/network/update_port.rb', line 25

def update_port(port_id, options = {})
  response = Excon::Response.new
  if port = list_ports.body['ports'].detect { |_| _['id'] == port_id }
    port['name']           = options[:name]
    port['fixed_ips']      = options[:fixed_ips]
    port['admin_state_up'] = options[:admin_state_up]
    port['device_owner']   = options[:device_owner]
    port['device_id']      = options[:device_id]
    response.body = { 'port' => port }
    response.status = 200
    response
  else
    raise Fog::Network::OpenStack::NotFound
  end
end

#update_router(router_id, options = {}) ⇒ Object



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/fog/openstack/requests/network/update_router.rb', line 55

def update_router(router_id, options = {})
  router = self.data['routers'].find { |r| r['id'] == router_id }
  raise Fog::Network::OpenStack::NotFound unless router
  data = { 'router' => router }

  vanilla_options = [:name, :admin_state_up]

  egi = options[:external_gateway_info]
  if egi
    if egi.is_a?(Fog::Network::OpenStack::Network)
      data['router']['external_gateway_info'] = { 'network_id' => egi.id }
    elsif egi.is_a?(Hash) and egi['network_id']
      data['router']['external_gateway_info'] = egi
    else
      raise ArgumentError.new('Invalid external_gateway_info attribute')
    end
  end

  vanilla_options.reject{ |o| options[o].nil? }.each do |key|
    data['router'][key] = options[key]
  end
  response = Excon::Response.new
  response.status = 201
  response.body = data
  response
end

#update_subnet(subnet_id, options = {}) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/fog/openstack/requests/network/update_subnet.rb', line 25

def update_subnet(subnet_id, options = {})
  response = Excon::Response.new
  if subnet = list_subnets.body['subnets'].detect { |_| _['id'] == subnet_id }
    subnet['name']            = options[:name]
    subnet['gateway_ip']      = options[:gateway_ip]
    subnet['dns_nameservers'] = options[:dns_nameservers]
    subnet['host_routes']     = options[:host_routes]
    subnet['enable_dhcp']     = options[:enable_dhcp]
    response.body = { 'subnet' => subnet }
    response.status = 200
    response
  else
    raise Fog::Network::OpenStack::NotFound
  end
end