Project

General

Profile

Download (5.24 KB) Statistics
| Branch: | Tag: | Revision:

runcible / lib / runcible / extensions / unit.rb @ a5858c9b

1
module Runcible
2
  module Extensions
3
    class Unit < Runcible::Resources::Unit
4
      # The content type (e.g. rpm, errata)
5
      def self.content_type
6
        fail 'Content type not defined'
7
      end
8

    
9
      def content_type
10
        self.class.content_type
11
      end
12

    
13
      # Retrieves all content of a certain @@type
14
      #
15
      # @return [RestClient::Response]  list of all content for the given type
16
      def all
17
        search(content_type, {})
18
      end
19

    
20
      # Retrieves a single content by it's content ID
21
      #
22
      # @param  [Array]                id   the content ID of the content to retrieve
23
      # @return [RestClient::Response]      the requested content
24
      def find(id, optional = {})
25
        find_all([id], optional).first
26
      end
27

    
28
      # Retrieves a set of content by the contents IDs
29
      #
30
      # @param  [Array]                ids  list of content IDs to retrieve
31
      # @return [RestClient::Response]      list of content
32
      def find_all(ids, optional = {})
33
        optional[:include_repos] = optional.fetch(:include_repos, true)
34
        search(content_type, { :filters => {'id' => {'$in' => ids}} }, optional)
35
      end
36

    
37
      # Retrieves a single content by it's unit ID
38
      #
39
      # @param  [Array]                id   the unit ID of the content to retrieve
40
      # @return [RestClient::Response]      the requested content
41
      def find_by_unit_id(id, optional = {})
42
        find_all_by_unit_ids([id], [], optional).first
43
      end
44

    
45
      # Retrieves a set of content by the Pulp unit IDs
46
      #
47
      # @param  [Array]                ids    list of content unit IDs to retrieve
48
      # @param  [Array]                fields optional list of to retrieve
49
      # @return [RestClient::Response]        list of content
50
      def find_all_by_unit_ids(ids, fields = [], optional = {})
51
        optional[:include_repos] = optional.fetch(:include_repos, true)
52
        criteria = { :filters => { :_id => { '$in' => ids } } }
53
        criteria[:fields] = fields unless fields.empty?
54
        search(content_type, criteria, optional)
55
      end
56

    
57
      # unassociates content units from a repository
58
      #
59
      # @param  [String]                repo_id the repository ID to remove units from
60
      # @param  [Hash]                  filters the filters to find the units  this content type to remove
61
      # @return [RestClient::Response]          a task representing the unit unassociate operation
62
      def unassociate_from_repo(repo_id, filters)
63
        criteria = {:type_ids => [content_type]}
64
        criteria[:filters] = filters
65
        Runcible::Extensions::Repository.new(self.config).unassociate_units(repo_id, criteria)
66
      end
67

    
68
      # unassociates content units from a repository
69
      #
70
      # @param  [String]                repo_id the repository ID to remove units from
71
      # @param  [Array]                 ids  list of content unit ids of this
72
      #                                      content type, aka metadata id or content id
73
      #                                       ex: "RHEA-2010:0001" for errata..,
74
      #                                       Note rpms do not have ids, so cant use this.
75
      # @return [RestClient::Response]          a task representing the unit unassociate operation
76
      def unassociate_ids_from_repo(repo_id, ids)
77
        unassociate_from_repo(repo_id, :unit => {'id' => {'$in' => ids}})
78
      end
79

    
80
      # unassociates content units from a repository
81
      #
82
      # @param  [String]                repo_id the repository ID to remove units from
83
      # @param  [Array]                 ids list of the unique hash ids of the content unit
84
      #                                     with respect to this repo. unit_id, id are other names for this.
85
      #                                     for example: "efdd2d63-b275-4728-a298-f68cf4c174e7"
86
      #
87
      # @return [RestClient::Response]          a task representing the unit unassociate operation
88
      def unassociate_unit_ids_from_repo(repo_id, ids)
89
        unassociate_from_repo(repo_id, :association => {'unit_id' => {'$in' => ids}})
90
      end
91

    
92
      #copy contents from source repo to the destination repo
93
      #
94
      # @param  [String]                source_repo_id      the source repository ID
95
      # @param  [String]                destination_repo_id the destination repository ID
96
      # @param  [Hash]                  optional            container for all optional parameters
97
      # @return [RestClient::Response]                      a task representing the unit copy operation
98
      def copy(source_repo_id, destination_repo_id, optional = {})
99
        criteria = {:type_ids => [content_type], :filters => {}}
100
        criteria[:filters]['association'] = {'unit_id' => {'$in' => optional[:ids]}} if optional[:ids]
101
        criteria[:filters] = optional[:filters] if optional[:filters]
102
        criteria[:fields] = {:unit => optional[:fields]} if optional[:fields]
103

    
104
        payload = {:criteria => criteria}
105
        payload[:override_config] = optional[:override_config] if optional.key?(:override_config)
106

    
107
        if optional[:copy_children]
108
          payload[:override_config] ||= {}
109
          payload[:override_config][:recursive] = true
110
        end
111

    
112
        Runcible::Extensions::Repository.new(self.config).unit_copy(destination_repo_id, source_repo_id, payload)
113
      end
114
    end
115
  end
116
end