Method: Chef::ChefFS::ChefFSDataStore#list

Defined in:
lib/chef/chef_fs/chef_fs_data_store.rb

#list(path) ⇒ Object



483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
# File 'lib/chef/chef_fs/chef_fs_data_store.rb', line 483

def list(path)
  if use_memory_store?(path)
    @memory_store.list(path)

  # LIST /policies
  elsif path == [ "policies" ]
    with_entry([ path[0] ]) do |policies|

      policies.children.map { |policy| policy.name[0..-6].rpartition("-")[0] }.uniq
    rescue Chef::ChefFS::FileSystem::NotFoundError
      []

    end

  # LIST /policies/POLICY/revisions
  elsif path[0] == "policies" && path[2] == "revisions" && path.length == 3
    with_entry([ path[0] ]) do |policies|
      # /policies:
      #   - a-1.0.0.json
      #   - a-1.0.1.json
      #   - b-2.0.0.json
      revisions = []
      policies.children.each do |policy|
        name, dash, revision = policy.name[0..-6].rpartition("-")
        revisions << revision if name == path[1]
      end
      raise ChefZero::DataStore::DataNotFoundError.new(path) if revisions.empty?

      revisions
    end

  elsif path[0] == "policy_groups" && path.length == 2
    with_entry(path) do |entry|
      [ "policies" ]
    end

  elsif path[0] == "policy_groups" && path[2] == "policies" && path.length == 3
    with_entry(path[0..1]) do |entry|
      policies = Chef::JSONCompat.parse(entry.read)["policies"] || {}
      policies.keys
    end

  elsif %w{cookbooks cookbook_artifacts}.include?(path[0]) && path.length == 1
    with_entry(path) do |entry|

      if path[0] == "cookbook_artifacts"
        entry.children.map { |child| child.name.rpartition("-")[0] }.uniq
      elsif chef_fs.versioned_cookbooks
        # /cookbooks/name-version -> /cookbooks/name
        entry.children.map { |child| split_name_version(child.name)[0] }.uniq
      else
        entry.children.map(&:name)
      end
    rescue Chef::ChefFS::FileSystem::NotFoundError
      # If the cookbooks dir doesn't exist, we have no cookbooks (not 404)
      []

    end

  elsif %w{cookbooks cookbook_artifacts}.include?(path[0]) && path.length == 2
    if chef_fs.versioned_cookbooks || path[0] == "cookbook_artifacts"
      result = with_entry([ path[0] ]) do |entry|
        # list /cookbooks/name = filter /cookbooks/name-version down to name
        entry.children.map { |child| split_name_version(child.name) }
          .select { |name, version| name == path[1] }
          .map { |name, version| version }
      end
      if result.empty?
        raise ChefZero::DataStore::DataNotFoundError.new(path)
      end

      result
    else
      # list /cookbooks/name = <single version>
      version = get_single_cookbook_version(path)
      [version]
    end

  else
    result = with_entry(path) do |entry|

      entry.children.map { |c| zero_filename(c) }.sort
    rescue Chef::ChefFS::FileSystem::NotFoundError => e
      # /cookbooks, /data, etc. never return 404
      if path_always_exists?(path)
        []
      else
        raise ChefZero::DataStore::DataNotFoundError.new(to_zero_path(e.entry), e)
      end

    end

    # Older versions of chef-zero do not understand policies and cookbook_artifacts,
    # don't give that stuff to them
    if path == [] && ChefZero::VERSION.to_f < 4.4
      result.reject! { |child| %w{policies policy_data cookbook_artifacts}.include?(child) }
    end
    result
  end
end