Class: Fog::Network::OpenStack::Real

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_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_subnets.rb,
lib/fog/openstack/requests/network/create_subnet.rb,
lib/fog/openstack/requests/network/delete_subnet.rb,
lib/fog/openstack/requests/network/list_networks.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/associate_floating_ip.rb,
lib/fog/openstack/requests/network/disassociate_floating_ip.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Real

Returns a new instance of Real.



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
127
128
129
130
131
132
133
134
# File 'lib/fog/openstack/network.rb', line 102

def initialize(options={})
  require 'multi_json'

  @openstack_auth_token = options[:openstack_auth_token]

  unless @openstack_auth_token
    missing_credentials = Array.new
    @openstack_api_key  = options[:openstack_api_key]
    @openstack_username = options[:openstack_username]

    missing_credentials << :openstack_api_key  unless @openstack_api_key
    missing_credentials << :openstack_username unless @openstack_username
    raise ArgumentError, "Missing required arguments: #{missing_credentials.join(', ')}" unless missing_credentials.empty?
  end

  @openstack_tenant               = options[:openstack_tenant]
  @openstack_auth_uri             = URI.parse(options[:openstack_auth_url])
  @openstack_management_url       = options[:openstack_management_url]
  @openstack_must_reauthenticate  = false
  @openstack_service_type         = options[:openstack_service_type] || ['network']
  @openstack_service_name         = options[:openstack_service_name]
  @openstack_endpoint_type        = options[:openstack_endpoint_type] || 'adminURL'

  @connection_options = options[:connection_options] || {}

  @current_user = options[:current_user]
  @current_tenant = options[:current_tenant]

  authenticate

  @persistent = options[:persistent] || false
  @connection = Fog::Connection.new("#{@scheme}://#{@host}:#{@port}", @persistent, @connection_options)
end

Instance Attribute Details

#current_tenantObject (readonly)

Returns the value of attribute current_tenant.



100
101
102
# File 'lib/fog/openstack/network.rb', line 100

def current_tenant
  @current_tenant
end

#current_userObject (readonly)

Returns the value of attribute current_user.



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

def current_user
  @current_user
end

Instance Method Details

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



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/fog/openstack/requests/network/associate_floating_ip.rb', line 6

def associate_floating_ip(floating_ip_id, port_id, options = {})
  data = {
    'floatingip' => {
      'port_id'    => port_id,
    }
  }

  vanilla_options = [:fixed_ip_address]
  vanilla_options.reject{ |o| options[o].nil? }.each do |key|
    data['floatingip'][key] = options[key]
  end

  request(
    :body     => Fog::JSON.encode(data),
    :expects  => [200],
    :method   => 'PUT',
    :path     => "floatingips/#{floating_ip_id}"
  )
end

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



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/fog/openstack/requests/network/create_floating_ip.rb', line 6

def create_floating_ip(floating_network_id, options = {})
  data = {
    'floatingip' => {
      'floating_network_id' => floating_network_id,
      'port_id' => options[:port_id],
      'tenant_id' => options[:tenant_id],
      'fixed_ip_address' => options[:fixed_ip_address],
    }
  }

  vanilla_options = [:port_id, :tenant_id, :fixed_ip_address ]
  vanilla_options.reject{ |o| options[o].nil? }.each do |key|
    data['floatingip'][key] = options[key]
  end

  request(
    :body     => Fog::JSON.encode(data),
    :expects  => [201],
    :method   => 'POST',
    :path     => 'floatingips'
  )
end

#create_network(options = {}) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/fog/openstack/requests/network/create_network.rb', line 6

def create_network(options = {})
  data = { 'network' => {} }

  vanilla_options = [:name, :shared, :admin_state_up, :tenant_id]
  vanilla_options.reject{ |o| options[o].nil? }.each do |key|
    data['network'][key] = options[key]
  end

  request(
    :body     => Fog::JSON.encode(data),
    :expects  => [201],
    :method   => 'POST',
    :path     => 'networks'
  )
end

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



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/fog/openstack/requests/network/create_port.rb', line 6

def create_port(network_id, options = {})
  data = {
    'port' => {
      'network_id' => network_id,
    }
  }

  vanilla_options = [:name, :fixed_ips, :mac_address, :admin_state_up,
                     :device_owner, :device_id, :tenant_id]
  vanilla_options.reject{ |o| options[o].nil? }.each do |key|
    data['port'][key] = options[key]
  end

  request(
    :body     => Fog::JSON.encode(data),
    :expects  => [201],
    :method   => 'POST',
    :path     => 'ports'
  )
end

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



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/fog/openstack/requests/network/create_subnet.rb', line 6

def create_subnet(network_id, cidr, ip_version, options = {})
  data = {
    'subnet' => {
      'network_id' => network_id,
      'cidr'       => cidr,
      'ip_version' => ip_version,
    }
  }

  vanilla_options = [:name, :gateway_ip, :allocation_pools,
                     :dns_nameservers, :host_routes, :enable_dhcp,
                     :tenant_id]
  vanilla_options.reject{ |o| options[o].nil? }.each do |key|
    data['subnet'][key] = options[key]
  end

  request(
    :body     => Fog::JSON.encode(data),
    :expects  => [201],
    :method   => 'POST',
    :path     => 'subnets'
  )
end

#credentialsObject



136
137
138
139
140
141
142
143
# File 'lib/fog/openstack/network.rb', line 136

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

#delete_floating_ip(floating_ip_id) ⇒ Object



6
7
8
9
10
11
12
# File 'lib/fog/openstack/requests/network/delete_floating_ip.rb', line 6

def delete_floating_ip(floating_ip_id)
  request(
    :expects  => 204,
    :method   => 'DELETE',
    :path     => "floatingips/#{floating_ip_id}"
  )
end

#delete_network(network_id) ⇒ Object



6
7
8
9
10
11
12
# File 'lib/fog/openstack/requests/network/delete_network.rb', line 6

def delete_network(network_id)
  request(
    :expects  => 204,
    :method   => 'DELETE',
    :path     => "networks/#{network_id}"
  )
end

#delete_port(port_id) ⇒ Object



6
7
8
9
10
11
12
# File 'lib/fog/openstack/requests/network/delete_port.rb', line 6

def delete_port(port_id)
  request(
    :expects  => 204,
    :method   => 'DELETE',
    :path     => "ports/#{port_id}"
  )
end

#delete_subnet(subnet_id) ⇒ Object



6
7
8
9
10
11
12
# File 'lib/fog/openstack/requests/network/delete_subnet.rb', line 6

def delete_subnet(subnet_id)
  request(
    :expects  => 204,
    :method   => 'DELETE',
    :path     => "subnets/#{subnet_id}"
  )
end

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



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/fog/openstack/requests/network/disassociate_floating_ip.rb', line 6

def disassociate_floating_ip(floating_ip_id, options = {})
  data = {
    'floatingip' => {
      'port_id'    => nil,
    }
  }

  vanilla_options = [:fixed_ip_address]
  vanilla_options.reject{ |o| options[o].nil? }.each do |key|
    data['floatingip'][key] = options[key]
  end

  request(
    :body     => Fog::JSON.encode(data),
    :expects  => [200],
    :method   => 'PUT',
    :path     => "floatingips/#{floating_ip_id}"
  )
end

#get_floating_ip(floating_ip_id) ⇒ Object



6
7
8
9
10
11
12
# File 'lib/fog/openstack/requests/network/get_floating_ip.rb', line 6

def get_floating_ip(floating_ip_id)
  request(
    :expects => [200],
    :method  => 'GET',
    :path    => "floatingips/#{floating_ip_id}"
  )
end

#get_network(network_id) ⇒ Object



6
7
8
9
10
11
12
# File 'lib/fog/openstack/requests/network/get_network.rb', line 6

def get_network(network_id)
  request(
    :expects => [200],
    :method  => 'GET',
    :path    => "networks/#{network_id}"
  )
end

#get_port(port_id) ⇒ Object



6
7
8
9
10
11
12
# File 'lib/fog/openstack/requests/network/get_port.rb', line 6

def get_port(port_id)
  request(
    :expects => [200],
    :method  => 'GET',
    :path    => "ports/#{port_id}"
  )
end

#get_subnet(subnet_id) ⇒ Object



6
7
8
9
10
11
12
# File 'lib/fog/openstack/requests/network/get_subnet.rb', line 6

def get_subnet(subnet_id)
  request(
    :expects => [200],
    :method  => 'GET',
    :path    => "subnets/#{subnet_id}"
  )
end

#list_floating_ips(filters = {}) ⇒ Object



6
7
8
9
10
11
12
13
# File 'lib/fog/openstack/requests/network/list_floating_ips.rb', line 6

def list_floating_ips(filters = {})
  request(
    :expects => 200,
    :method  => 'GET',
    :path    => 'floatingips',
    :query   => filters
  )
end

#list_networks(filters = {}) ⇒ Object



6
7
8
9
10
11
12
13
# File 'lib/fog/openstack/requests/network/list_networks.rb', line 6

def list_networks(filters = {})
  request(
    :expects => 200,
    :method  => 'GET',
    :path    => 'networks',
    :query   => filters
  )
end

#list_ports(filters = {}) ⇒ Object



6
7
8
9
10
11
12
13
# File 'lib/fog/openstack/requests/network/list_ports.rb', line 6

def list_ports(filters = {})
  request(
    :expects => 200,
    :method  => 'GET',
    :path    => 'ports',
    :query   => filters
  )
end

#list_subnets(filters = {}) ⇒ Object



6
7
8
9
10
11
12
13
# File 'lib/fog/openstack/requests/network/list_subnets.rb', line 6

def list_subnets(filters = {})
  request(
    :expects => 200,
    :method  => 'GET',
    :path    => 'subnets',
    :query   => filters
  )
end

#reloadObject



145
146
147
# File 'lib/fog/openstack/network.rb', line 145

def reload
  @connection.reset
end

#request(params) ⇒ Object



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/fog/openstack/network.rb', line 149

def request(params)
  begin
    response = @connection.request(params.merge({
      :headers  => {
        'Content-Type' => 'application/json',
        'Accept' => 'application/json',
        'X-Auth-Token' => @auth_token
      }.merge!(params[:headers] || {}),
      :host     => @host,
      :path     => "#{@path}/#{params[:path]}"#,
      # Causes errors for some requests like tenants?limit=1
      # :query    => ('ignore_awful_caching' << Time.now.to_i.to_s)
    }))
  rescue Excon::Errors::Unauthorized => error
    if error.response.body != 'Bad username or password' # token expiration
      @openstack_must_reauthenticate = true
      authenticate
      retry
    else # bad credentials
      raise error
    end
  rescue Excon::Errors::HTTPStatusError => error
    raise case error
    when Excon::Errors::NotFound
      Fog::Network::OpenStack::NotFound.slurp(error)
    else
      error
    end
  end
  unless response.body.empty?
    response.body = MultiJson.decode(response.body)
  end
  response
end

#set_tenant(tenant) ⇒ Object



6
7
8
9
10
# File 'lib/fog/openstack/requests/network/set_tenant.rb', line 6

def set_tenant(tenant)
  @openstack_must_reauthenticate = true
  @openstack_tenant = tenant.to_s
  authenticate
end

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



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/fog/openstack/requests/network/update_network.rb', line 6

def update_network(network_id, options = {})
  data = { 'network' => {} }

  vanilla_options = [:name, :shared, :admin_state_up]
  vanilla_options.select{ |o| options.has_key?(o) }.each do |key|
    data['network'][key] = options[key]
  end

  request(
    :body     => Fog::JSON.encode(data),
    :expects  => 200,
    :method   => 'PUT',
    :path     => "networks/#{network_id}.json"
  )
end

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



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/fog/openstack/requests/network/update_port.rb', line 6

def update_port(port_id, options = {})
  data = { 'port' => {} }

  vanilla_options = [:name, :fixed_ips, :admin_state_up, :device_owner,
                     :device_id]
  vanilla_options.select{ |o| options.has_key?(o) }.each do |key|
    data['port'][key] = options[key]
  end

  request(
    :body     => Fog::JSON.encode(data),
    :expects  => 200,
    :method   => 'PUT',
    :path     => "ports/#{port_id}.json"
  )
end

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



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/fog/openstack/requests/network/update_subnet.rb', line 6

def update_subnet(subnet_id, options = {})
  data = { 'subnet' => {} }

  vanilla_options = [:name, :gateway_ip, :dns_nameservers,
                     :host_routes, :enable_dhcp]
  vanilla_options.select{ |o| options.has_key?(o) }.each do |key|
    data['subnet'][key] = options[key]
  end

  request(
    :body     => Fog::JSON.encode(data),
    :expects  => 200,
    :method   => 'PUT',
    :path     => "subnets/#{subnet_id}"
  )
end