foreman_rh_cloud 10.0.6 → 10.0.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: bcf5c802bcf566375518a4f83d5a5fae3ec98c5554e89dea57af748af526f998
4
- data.tar.gz: 1c8d77163d1cac3161813bf9aab5f70f3a8496ce9351a5e2a4d071aaecfb1ad6
3
+ metadata.gz: 65251fdae0087f5b4a2b1601fa9fb40ac63874fb48280614de3efb7fb2807e11
4
+ data.tar.gz: 654748bf9d1270e27118968d2b7ab8dfa99b133087312006f8cea4a745e94543
5
5
  SHA512:
6
- metadata.gz: 807e09c7f560217178221cf3a2c7eda52aee500ea50fe18d56b86f53cffee23d0d7ca50e6b4bd174a7b3c038273158a41cb6c5d60339fd121e5d0c06e0010788
7
- data.tar.gz: ad3a808a270922e52e25778f9101db3a358fbe99b0ded5f01fab67c5d1a398cc7c997ed816f909b528af579a47f44f5b97a9a6c5621d4fa97d59dc8f43851a3c
6
+ metadata.gz: da7ed0469f82a183a824b52a72fc6c85885fea0491bef66b4a473eeb113a6b80aba0b719e91d928ab9dc2b658ebf632f2cb80babaf918002a5f44dd54565ab46
7
+ data.tar.gz: e8c36cf6761e02085bc28c925095b9581eb5f94718bab25ed7bf083bb8d1cb79c33f3a95249a051379aef29ab38ea623b38e8df194bccdb659ad2a183ef7b5b7
@@ -1,3 +1,3 @@
1
1
  module ForemanRhCloud
2
- VERSION = '10.0.6'.freeze
2
+ VERSION = '10.0.7'.freeze
3
3
  end
@@ -1,64 +1,132 @@
1
1
  require 'io/console'
2
+ require 'uri'
2
3
 
3
- namespace :rh_cloud do |args|
4
- desc 'Register Satellite Organization with Hybrid Cloud API. \
5
- Specify org_id=x replace your organization ID with x. \
6
- Specify SATELLITE_RH_CLOUD_URL=https://x with the Hybrid Cloud endpoint you are connecting to.'
4
+ def logger
5
+ @logger ||= Logger.new(STDOUT)
6
+ end
7
+
8
+ namespace :rh_cloud do
9
+ desc 'Register Satellite Organization with Hybrid Cloud API.'
10
+ # This task registers the Satellite Organization with the Hybrid Cloud API.
11
+ # It requires the user to input their organization ID, Insights URL, and token.
12
+ # The task will then send a POST request to the Hybrid Cloud API to register the organization.
13
+ # The response will be logged, and any errors will be caught and logged as well.
14
+ # The task will exit with an error message if the organization does not have a manifest imported or if the token is not entered.
15
+ # The task will also log a warning if the custom URL is not set and the default one is used.
7
16
  task hybridcloud_register: [:environment] do
8
17
  include ::ForemanRhCloud::CertAuth
9
18
  include ::InsightsCloud::CandlepinCache
10
19
 
11
- def logger
12
- @logger ||= Logger.new(STDOUT)
20
+ def default_registrations_url
21
+ URI.join(ForemanRhCloud.base_url, '/api/identity/certificate/registrations').to_s
13
22
  end
14
23
 
15
- def registrations_url
16
- logger.warn("Custom url is not set, using the default one: #{ForemanRhCloud.base_url}") if ENV['SATELLITE_RH_CLOUD_URL'].empty?
17
- ForemanRhCloud.base_url + '/api/identity/certificate/registrations'
24
+ # Helper method to get the registrations URL, with a warning for default usage
25
+ def registrations_url(custom_url)
26
+ if custom_url.empty?
27
+ logger.warn("Custom url is not set, using the default one: #{default_registrations_url}")
28
+ default_registrations_url
29
+ else
30
+ if URI(custom_url).scheme.nil?
31
+ logger.warn("Custom URL lacks a scheme; prepending https:// prefix.")
32
+ custom_url = "https://" + custom_url
33
+ end
34
+ custom_url
35
+ end
18
36
  end
19
37
 
20
- if ENV['org_id'].nil?
21
- logger.error('ERROR: org_id needs to be specified.')
22
- exit(1)
38
+ def get_organization(user_org_id)
39
+ maybe_organization = Organization.find_by(id: user_org_id)
40
+ if maybe_organization.nil?
41
+ logger.error("Organization with ID '#{user_org_id}' not found.")
42
+ exit(1)
43
+ end
44
+ maybe_organization
23
45
  end
24
46
 
25
- @organization = Organization.find_by(id: ENV['org_id'].to_i) # saw this coming in as a string, so making sure it gets passed as an integer.
26
- @uid = cp_owner_id(@organization)
27
- @hostname = ForemanRhCloud.foreman_host_name
28
- logger.error('Organization provided does not have a manifest imported.') + exit(1) if @uid.nil?
29
-
30
- puts 'Paste your token, output will be hidden.'
31
- @token = STDIN.noecho(&:gets).chomp
32
- logger.error('Token was not entered.') + exit(1) if @token.empty?
33
-
34
- def headers
35
- {
36
- Authorization: "Bearer #{@token}",
37
- }
47
+ def get_uid(organization)
48
+ maybe_uid = cp_owner_id(organization)
49
+ if maybe_uid.nil?
50
+ logger.error("Organization '#{organization}' does not have a manifest imported.")
51
+ exit(1)
52
+ end
53
+ maybe_uid
38
54
  end
39
55
 
40
- def payload
41
- {
42
- "uid": @uid,
43
- "display_name": "#{@hostname}+#{@organization.label}",
44
- }
56
+ # --- Input Collection ---
57
+ puts "Paste in your organization ID, this can be retrieved with the command: hammer organization list"
58
+ loop do
59
+ input = STDIN.gets.chomp
60
+ if input.match?(/^\d+$/) # Checks if input consists only of digits
61
+ @user_org_id = input.to_i
62
+ break
63
+ else
64
+ puts "Invalid input. Please enter a numeric organization ID."
65
+ end
45
66
  end
46
67
 
47
- def method
48
- :post
68
+ puts "\n" + "-" * 50 + "\n\n"
69
+ puts "Paste in your custom Insights URL. If nothing is entered, the default will be used (#{default_registrations_url})."
70
+ insights_user_input = STDIN.gets.chomp
71
+
72
+ puts "\n" + "-" * 50 + "\n\n"
73
+ puts 'Paste in your Hybrid Cloud API token, output will be hidden.'
74
+ puts 'This token can be retrieved from the Hybrid Cloud console.'
75
+ token = STDIN.noecho(&:gets).chomp
76
+ if token.empty?
77
+ logger.error('Token was not entered.')
78
+ exit(1)
49
79
  end
50
80
 
81
+ # --- Data Preparation ---
82
+
83
+ organization = get_organization(@user_org_id)
84
+ uid = get_uid(organization)
85
+ hostname = ForemanRhCloud.foreman_host_name
86
+ insights_url = registrations_url(insights_user_input)
87
+
88
+ # --- API Request ---
89
+
90
+ headers = {
91
+ Authorization: "Bearer #{token}",
92
+ }
93
+
94
+ payload = {
95
+ 'uid': uid,
96
+ "display_name": "#{hostname}+#{organization.label}",
97
+ }
98
+
51
99
  begin
52
100
  response = execute_cloud_request(
53
- organization: @organization,
54
- method: method,
55
- url: registrations_url,
101
+ organization: organization,
102
+ method: :post,
103
+ url: insights_url,
56
104
  headers: headers,
57
105
  payload: payload.to_json
58
106
  )
59
- logger.debug(response)
107
+ logger.debug("Cloud request completed: status=#{response.code}, body_preview=#{response.body&.slice(0, 200)}")
108
+ rescue RestClient::Unauthorized => _ex
109
+ # Add a more specific rescue for 401 Unauthorized errors
110
+ logger.error('Registration failed: Your token is invalid or unauthorized. Please check your token and try again.')
111
+ # Optionally, you can still log the full debug info if helpful for advanced troubleshooting
112
+ # logger.debug(ex.backtrace.join("\n"))
113
+ exit(1)
114
+ rescue RestClient::ExceptionWithResponse => ex
115
+ # This catches any RestClient exception that has a response (like 400, 403, 404, 500, etc.)
116
+ status_code = begin
117
+ ex.response.code
118
+ rescue StandardError
119
+ "unknown"
120
+ end
121
+ logger.error("Registration failed with HTTP status #{status_code}: #{ex.message}")
122
+ logger.debug("Response body (if available): #{ex.response.body}")
123
+ exit(1)
60
124
  rescue StandardError => ex
61
- logger.error(ex)
125
+ # This is the catch-all for any other unexpected errors
126
+ logger.error("An unexpected error occurred during registration: #{ex.message}")
127
+ exit(1)
62
128
  end
129
+
130
+ logger.info("Satellite Organization '#{organization.label}' (ID: #{@user_org_id}) successfully registered.")
63
131
  end
64
132
  end
data/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "foreman_rh_cloud",
3
- "version": "10.0.6",
3
+ "version": "10.0.7",
4
4
  "description": "Inventory Upload =============",
5
5
  "main": "index.js",
6
6
  "scripts": {
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: foreman_rh_cloud
3
3
  version: !ruby/object:Gem::Version
4
- version: 10.0.6
4
+ version: 10.0.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Foreman Red Hat Cloud team
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2025-06-04 00:00:00.000000000 Z
11
+ date: 2025-08-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: foreman_ansible