Class: OodCore::OpenStackHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/ood_core/helpers/openstack.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(token_file:, openstack_instance:) ⇒ OpenStackHelper

Returns a new instance of OpenStackHelper.



9
10
11
12
13
# File 'lib/ood_core/helpers/openstack.rb', line 9

def initialize(token_file:, openstack_instance:)
  @token_file = token_file
  @openstack_instance = openstack_instance
  @auth_url = "https://identity.#{openstack_instance}/v3"
end

Instance Attribute Details

#auth_urlObject (readonly)

Returns the value of attribute auth_url.



7
8
9
# File 'lib/ood_core/helpers/openstack.rb', line 7

def auth_url
  @auth_url
end

#openstack_instanceObject (readonly)

Returns the value of attribute openstack_instance.



7
8
9
# File 'lib/ood_core/helpers/openstack.rb', line 7

def openstack_instance
  @openstack_instance
end

Instance Method Details

#access_tokenString

Get access token from loaded credentials

Returns:

  • (String)

    The token ID



27
28
29
# File 'lib/ood_core/helpers/openstack.rb', line 27

def access_token
  load_token_data&.[]("id")
end

#fetch_all_flavorsArray<Array>

Fetch all flavors across all projects for a user

Returns:

  • (Array<Array>)

    Sorted array of [display_string, flavor_name, project_id]



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/ood_core/helpers/openstack.rb', line 51

def fetch_all_flavors
  flavors = []

  fetch_user_projects.each do |project|
    scoped_token = scope_token_to_project(access_token, project['id'])

    compute_connection_params = {
      openstack_auth_url: auth_url,
      openstack_project_name: project['name'],
      openstack_management_url: "https://compute.#{openstack_instance}/v2.1/#{project['id']}",
      openstack_auth_token: scoped_token,
    }
    compute = Fog::OpenStack::Compute.new(compute_connection_params)

    compute.flavors.each do |flavor|
      flavors << [
        "#{flavor.name} - #{flavor.vcpus}VCPUS, #{flavor.ram/1024}GB RAM, #{flavor.disk}GB disk",
        flavor.name,
        project['id']
      ]
    end
  end

  flavors.sort
end

#fetch_user_projectsArray<Hash>

Fetch all projects for the authenticated user

Returns:

  • (Array<Hash>)

    Array of project hashes with id and name



39
40
41
42
43
44
45
46
47
# File 'lib/ood_core/helpers/openstack.rb', line 39

def fetch_user_projects
  connection_params = {
    openstack_auth_url: auth_url,
    openstack_management_url: auth_url,
    openstack_auth_token: access_token,
  }
  identity = Fog::OpenStack::Identity.new(connection_params)
  identity.list_user_projects(user_id).body["projects"]
end

#load_projects_and_flavorsArray

Convenience method that returns both projects and flavors

Returns:

  • (Array)

    Array containing [projects, flavors]



79
80
81
# File 'lib/ood_core/helpers/openstack.rb', line 79

def load_projects_and_flavors
  [fetch_user_projects, fetch_all_flavors]
end

#load_token_dataHash

Load token data from the token file

Returns:

  • (Hash)

    Parsed token JSON or nil if file does not exist



17
18
19
20
21
22
23
# File 'lib/ood_core/helpers/openstack.rb', line 17

def load_token_data
  return nil unless File.exist?(@token_file)
  JSON.parse(File.read(@token_file))
rescue Errno::ENOENT => e
  puts "Error loading token: #{e}"
  nil
end

#scope_token_to_project(access_token, project_id) ⇒ String

Scope token to a specific project

Parameters:

  • access_token (String)

    The unscoped token ID

  • project_id (String)

    The project ID to scope to

Returns:

  • (String)

    The scoped token ID



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/ood_core/helpers/openstack.rb', line 87

def scope_token_to_project(access_token, project_id)
  auth = {
    "auth": {
      "identity": {
        "methods": ["token"],
        "token": { "id": access_token }
      },
      "scope": { "project": { "id": project_id } }
    }
  }

  connection_params = {
    openstack_auth_url: auth_url,
    openstack_management_url: auth_url,
    openstack_auth_token: access_token,
  }
  identity = Fog::OpenStack::Identity.new(connection_params)
  identity.tokens.authenticate(auth)
end

#user_idString

Get user ID from loaded credentials

Returns:

  • (String)

    The user ID



33
34
35
# File 'lib/ood_core/helpers/openstack.rb', line 33

def user_id
  load_token_data&.[]("user_id")
end