# File lib/hammer_cli_csv/puppet_environments.rb, line 59
      def create_environments_from_csv(line)
        line[COUNT].to_i.times do |number|
          name = namify(line[NAME], number)
          if !@existing.include? name
            print "Creating environment '#{name}'..." if option_verbose?
            id = @api.resource(:environments).call(:create, {
                                             'environment' => {
                                               'name' => name
                                             }
                                           })['id']
          else
            print "Updating environment '#{name}'..." if option_verbose?
            id = @api.resource(:environments).call(:update, {
                                             'id' => @existing[name],
                                             'environment' => {
                                               'name' => name
                                             }
                                           })['environment']['id']
          end

          # Update associated resources
          # TODO: Bug #4738: organization json does not include puppet environments
          #       http://projects.theforeman.org/issues/4738#change-15319
          #       Update below to match style of domains
          organization_ids = CSV.parse_line(line[ORGANIZATIONS]).collect do |organization|
            foreman_organization(:name => organization)
          end
          organization_ids += @api.resource(:environments).call(:show, {'id' => id})['organizations'].collect do |organization|
            organization['id']
          end
          organization_ids.uniq!

          @api.resource(:environments).call(:update, {
                                              'id' => id,
                                              'environment' => {
                                                'organization_ids' => organization_ids
                                              }
                                            })

          print "done\n" if option_verbose?
        end
      rescue RuntimeError => e
        raise "#{e}\n       #{line}"
      end