Sha256: 1e6b5ab4bb3a35a75f0d387c5d4582b20f9125348516f5d89109639987de301f

Contents?: true

Size: 1.67 KB

Versions: 2

Compression:

Stored size: 1.67 KB

Contents

require_relative 'ionoscloud_base'

class Chef
  class Knife
    class IonoscloudGroupUserAdd < Knife
      include Knife::IonoscloudBase

      banner 'knife ionoscloud group user add USER_ID [USER_ID] (options)'

      option :group_id,
              short: '-G GROUP_ID',
              long: '--group-id GROUP_ID',
              description: 'ID of the group.'

      def initialize(args = [])
        super(args)
        @description =
        'Use this operation to add an existing user to a group.'
        @directory = 'user'
        @required_options = [:group_id, :ionoscloud_username, :ionoscloud_password]
      end

      def run
        $stdout.sync = true
        handle_extra_config
        validate_required_params(@required_options, config)

        user_management_api = Ionoscloud::UserManagementApi.new(api_client)

        request_ids_to_wait = []

        @name_args.each do |user_id|
          begin
            _, _, headers = user_management_api.um_groups_users_post_with_http_info(
              config[:group_id],
              { id: user_id },
            )
            request_id = get_request_id headers
            request_ids_to_wait.append(request_id)

            ui.info("Added User #{user_id} to the Group #{config[:group_id]}. Request ID: #{request_id}.")
          rescue Ionoscloud::ApiError => err
            raise err unless err.code == 404
            ui.error("User ID #{user_id} not found. Skipping.")
            next
          end
        end

        request_ids_to_wait.each { |request_id| api_client.wait_for { is_done? request_id } }

        print_group(user_management_api.um_groups_find_by_id(config[:group_id], depth: 1))
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
knife-ionoscloud-6.1.1 lib/chef/knife/ionoscloud_group_user_add.rb
knife-ionoscloud-6.1.0 lib/chef/knife/ionoscloud_group_user_add.rb