Class: Chef::Knife::Cloud::OpenstackService

Inherits:
FogService
  • Object
show all
Defined in:
lib/chef/knife/cloud/openstack_service.rb

Instance Method Summary collapse

Constructor Details

#initialize(config:, **kwargs) ⇒ OpenstackService

Returns a new instance of OpenstackService.



28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/chef/knife/cloud/openstack_service.rb', line 28

def initialize(config:, **kwargs)
  super(config: config, **kwargs)

  Chef::Log.debug("openstack_username #{config[:openstack_username]}")
  Chef::Log.debug("openstack_auth_url #{config[:openstack_auth_url]}")
  Chef::Log.debug("openstack_tenant #{config[:openstack_tenant]}")
  Chef::Log.debug("openstack_endpoint_type #{config[:openstack_endpoint_type] || "publicURL"}")
  Chef::Log.debug("openstack_insecure #{config[:openstack_insecure]}")
  Chef::Log.debug("openstack_region #{config[:openstack_region]}")

  @auth_params = get_auth_params
end

Instance Method Details

#add_api_endpointObject

add alternate user defined api_endpoint value.



42
43
44
# File 'lib/chef/knife/cloud/openstack_service.rb', line 42

def add_api_endpoint
  @auth_params.merge!(openstack_auth_url: config[:api_endpoint]) unless config[:api_endpoint].nil?
end

#get_auth_paramsObject



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/chef/knife/cloud/openstack_service.rb', line 64

def get_auth_params
  params = {
    provider: "OpenStack",
    connection_options: {
      ssl_verify_peer: !config[:openstack_insecure],
    },
  }

  (
    Fog::OpenStack::Compute.requirements +
    Fog::OpenStack::Compute.recognized -
    [:openstack_api_key]
  ).each do |k|
    next unless k.to_s.start_with?("openstack")

    params[k] = config[k]
  end
  params[:openstack_api_key] = config[:openstack_password] || config[:openstack_api_key]

  params
end

#get_server(search_term) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/chef/knife/cloud/openstack_service.rb', line 46

def get_server(search_term)
  if server = connection.servers.get(search_term)
    return server
  end

  if servers = connection.servers.all(name: search_term)
    if servers.length > 1
      error_message = "Multiple server matches found for '#{search_term}', use an instance_id to be more specific."
      ui.fatal(error_message)
      raise CloudExceptions::ValidationError, error_message
    else
      servers.first
    end
  end
rescue Excon::Errors::BadRequest => e
  handle_excon_exception(CloudExceptions::KnifeCloudError, e)
end