Class: OodCore::OpenStackHelper
- Inherits:
-
Object
- Object
- OodCore::OpenStackHelper
- Defined in:
- lib/ood_core/helpers/openstack.rb
Instance Attribute Summary collapse
-
#auth_url ⇒ Object
readonly
Returns the value of attribute auth_url.
-
#openstack_instance ⇒ Object
readonly
Returns the value of attribute openstack_instance.
Instance Method Summary collapse
-
#access_token ⇒ String
Get access token from loaded credentials.
-
#fetch_all_flavors ⇒ Array<Array>
Fetch all flavors across all projects for a user.
-
#fetch_user_projects ⇒ Array<Hash>
Fetch all projects for the authenticated user.
-
#initialize(token_file:, openstack_instance:) ⇒ OpenStackHelper
constructor
A new instance of OpenStackHelper.
-
#load_projects_and_flavors ⇒ Array
Convenience method that returns both projects and flavors.
-
#load_token_data ⇒ Hash
Load token data from the token file.
-
#scope_token_to_project(access_token, project_id) ⇒ String
Scope token to a specific project.
-
#user_id ⇒ String
Get user ID from loaded credentials.
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_url ⇒ Object (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_instance ⇒ Object (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_token ⇒ String
Get access token from loaded credentials
27 28 29 |
# File 'lib/ood_core/helpers/openstack.rb', line 27 def access_token load_token_data&.[]("id") end |
#fetch_all_flavors ⇒ Array<Array>
Fetch all flavors across all projects for a user
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_projects ⇒ Array<Hash>
Fetch all projects for the authenticated user
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_flavors ⇒ Array
Convenience method that returns both projects and 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_data ⇒ Hash
Load token data from the token file
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
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_id ⇒ String
Get user ID from loaded credentials
33 34 35 |
# File 'lib/ood_core/helpers/openstack.rb', line 33 def user_id load_token_data&.[]("user_id") end |