Sha256: 474839555011b254efe2e263e8c27e39800f02faa56b7506789fc89ebf1f48c0

Contents?: true

Size: 1.83 KB

Versions: 12

Compression:

Stored size: 1.83 KB

Contents

require 'io/console'

namespace :rh_cloud do |args|
  desc 'Register Satellite Organization with Hybrid Cloud API. \
        Specify org_id=x replace your organization ID with x. \
        Specify SATELLITE_RH_CLOUD_URL=https://x with the Hybrid Cloud endpoint you are connecting to.'
  task hybridcloud_register: [:environment] do
    include ::ForemanRhCloud::CertAuth
    include ::InsightsCloud::CandlepinCache

    def logger
      @logger ||= Logger.new(STDOUT)
    end

    def registrations_url
      logger.warn("Custom url is not set, using the default one: #{ForemanRhCloud.base_url}") if ENV['SATELLITE_RH_CLOUD_URL'].empty?
      ForemanRhCloud.base_url + '/api/identity/certificate/registrations'
    end

    if ENV['org_id'].nil?
      logger.error('ERROR: org_id needs to be specified.')
      exit(1)
    end

    @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.
    @uid = cp_owner_id(@organization)
    @hostname = ForemanRhCloud.foreman_host_name
    logger.error('Organization provided does not have a manifest imported.') + exit(1) if @uid.nil?

    puts 'Paste your token, output will be hidden.'
    @token = STDIN.noecho(&:gets).chomp
    logger.error('Token was not entered.') + exit(1) if @token.empty?

    def headers
      {
        Authorization: "Bearer #{@token}",
      }
    end

    def payload
      {
        "uid": @uid,
        "display_name": "#{@hostname}+#{@organization.label}",
      }
    end

    def method
      :post
    end

    begin
      response = execute_cloud_request(
        organization: @organization,
        method: method,
        url: registrations_url,
        headers: headers,
        payload: payload.to_json
      )
      logger.debug(response)
    rescue StandardError => ex
      logger.error(ex)
    end
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
foreman_rh_cloud-11.1.0 lib/tasks/hybrid_cloud.rake
foreman_rh_cloud-10.0.3 lib/tasks/hybrid_cloud.rake
foreman_rh_cloud-11.0.3 lib/tasks/hybrid_cloud.rake
foreman_rh_cloud-9.0.59 lib/tasks/hybrid_cloud.rake
foreman_rh_cloud-11.0.2 lib/tasks/hybrid_cloud.rake
foreman_rh_cloud-11.0.1 lib/tasks/hybrid_cloud.rake
foreman_rh_cloud-11.0.0 lib/tasks/hybrid_cloud.rake
foreman_rh_cloud-9.0.58 lib/tasks/hybrid_cloud.rake
foreman_rh_cloud-10.0.2 lib/tasks/hybrid_cloud.rake
foreman_rh_cloud-9.0.57 lib/tasks/hybrid_cloud.rake
foreman_rh_cloud-10.0.1 lib/tasks/hybrid_cloud.rake
foreman_rh_cloud-9.0.56 lib/tasks/hybrid_cloud.rake