pulp 0.0.8

Sign up to get free protection for your applications and to get access to all the features.
Files changed (53) hide show
  1. data/.document +5 -0
  2. data/.rspec +1 -0
  3. data/Gemfile +19 -0
  4. data/Gemfile.lock +42 -0
  5. data/LICENSE.txt +20 -0
  6. data/README.rdoc +147 -0
  7. data/Rakefile +49 -0
  8. data/VERSION +1 -0
  9. data/examples/repositories.rb +205 -0
  10. data/examples/test_pulp.yml +4 -0
  11. data/lib/pulp.rb +26 -0
  12. data/lib/pulp/cds.rb +25 -0
  13. data/lib/pulp/common/debug.rb +37 -0
  14. data/lib/pulp/common/lifecycle.rb +166 -0
  15. data/lib/pulp/common/lifecycle/create.rb +16 -0
  16. data/lib/pulp/common/lifecycle/delete.rb +23 -0
  17. data/lib/pulp/common/lifecycle/get.rb +22 -0
  18. data/lib/pulp/common/lifecycle/update.rb +23 -0
  19. data/lib/pulp/connection/base.rb +84 -0
  20. data/lib/pulp/connection/handler.rb +59 -0
  21. data/lib/pulp/consumer.rb +23 -0
  22. data/lib/pulp/consumergroup.rb +22 -0
  23. data/lib/pulp/content.rb +14 -0
  24. data/lib/pulp/distribution.rb +11 -0
  25. data/lib/pulp/errata.rb +17 -0
  26. data/lib/pulp/event.rb +8 -0
  27. data/lib/pulp/filter.rb +19 -0
  28. data/lib/pulp/package.rb +20 -0
  29. data/lib/pulp/package_group.rb +8 -0
  30. data/lib/pulp/package_group_category.rb +7 -0
  31. data/lib/pulp/repository.rb +114 -0
  32. data/lib/pulp/service.rb +51 -0
  33. data/lib/pulp/task.rb +37 -0
  34. data/lib/pulp/task_history.rb +10 -0
  35. data/lib/pulp/task_snapshot.rb +9 -0
  36. data/lib/pulp/user.rb +12 -0
  37. data/spec/pulp/common/debug_spec.rb +42 -0
  38. data/spec/pulp/common/lifecycle/create_spec.rb +21 -0
  39. data/spec/pulp/common/lifecycle/delete_spec.rb +40 -0
  40. data/spec/pulp/common/lifecycle/get_spec.rb +42 -0
  41. data/spec/pulp/common/lifecycle/update_spec.rb +48 -0
  42. data/spec/pulp/common/lifecycle_spec.rb +393 -0
  43. data/spec/pulp/connection/base_spec.rb +312 -0
  44. data/spec/pulp/connection/handler_spec.rb +123 -0
  45. data/spec/pulp/content_spec.rb +21 -0
  46. data/spec/pulp/package_spec.rb +14 -0
  47. data/spec/pulp/repository_spec.rb +14 -0
  48. data/spec/pulp/service_spec.rb +85 -0
  49. data/spec/pulp/task_spec.rb +48 -0
  50. data/spec/pulp_spec.rb +4 -0
  51. data/spec/spec.opts +6 -0
  52. data/spec/spec_helper.rb +32 -0
  53. metadata +252 -0
@@ -0,0 +1,4 @@
1
+ hostname: localhost
2
+ username: admin
3
+ password: admin
4
+ debug: false
@@ -0,0 +1,26 @@
1
+ require 'rest_client'
2
+ require 'json'
3
+ require 'active_support/inflector'
4
+
5
+ require 'pulp/common/debug'
6
+ require 'pulp/common/lifecycle'
7
+ require 'pulp/common/lifecycle/create'
8
+ require 'pulp/common/lifecycle/delete'
9
+ require 'pulp/common/lifecycle/get'
10
+ require 'pulp/common/lifecycle/update'
11
+ require 'pulp/connection/base'
12
+ require 'pulp/connection/handler'
13
+
14
+ # types
15
+ %w{consumer consumergroup content distribution errata event filter task_snapshot task
16
+ cds package package_group package_group_category repository service task_history user}.each {|type| require "pulp/#{type}" }
17
+
18
+
19
+ if file = [ ENV['PULP_YML'], File.expand_path('~/.pulp.yaml'),'/etc/pulp/pulp.yaml' ].find{|f| f && File.exists?(f) }
20
+ require 'yaml'
21
+ global_options = YAML.load_file(file)
22
+ Pulp::Connection::Handler.hostname = global_options['hostname']
23
+ Pulp::Connection::Handler.username = global_options['username']
24
+ Pulp::Connection::Handler.password = global_options['password']
25
+ Pulp::Connection::Handler.debug_enabled = global_options['debug']||false
26
+ end
@@ -0,0 +1,25 @@
1
+ module Pulp
2
+ class Cds < Pulp::Connection::Base
3
+
4
+ has_crud
5
+
6
+ pulp_fields :cluster_id, :description, :hostname, :name, :sync_schedule
7
+
8
+ pulp_locked_fields :repo_ids, :last_sync , :secret, :hearbeat
9
+
10
+ pulp_action :associate, :params => true, :returns => Pulp::Task
11
+ pulp_action :unassociate, :params => true, :returns => Pulp::Task
12
+
13
+ pulp_action :history, :params => true
14
+
15
+ pulp_action :sync, :params => false, :returns => Pulp::Task, :task_list => true
16
+
17
+ def history(action)
18
+ self.class.base_get("history/#{action}/",self.id).collect{|th| Pulp::TaskHistory.new(th) }
19
+ end
20
+
21
+ def repositories
22
+ repo_ids.collect{|repo_id| Pulp::Repository.find_by_id(repo_id).first }.compact
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,37 @@
1
+ module Pulp
2
+ module Common
3
+ module Debug
4
+ def self.included(base)
5
+ base.extend(ClassMethods)
6
+ end
7
+
8
+ def debug(msg)
9
+ self.class.debug(msg)
10
+ end
11
+
12
+ module ClassMethods
13
+ def debug_enabled
14
+ @debug_enabled ||= false
15
+ end
16
+
17
+ def debug_enabled=(enable)
18
+ @debug_enabled = enable
19
+ self.output = self.output # reset output to activate it
20
+ end
21
+
22
+ def output=(o)
23
+ @output = o
24
+ RestClient.log = debug_enabled ? output : nil
25
+ end
26
+
27
+ def output
28
+ @output ||= STDERR
29
+ end
30
+
31
+ def debug(msg)
32
+ output.puts msg if @debug_enabled
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,166 @@
1
+ module Pulp
2
+ module Common
3
+ module Lifecycle
4
+ def self.included(base)
5
+ base.extend ClassMethods
6
+ end
7
+ def fields
8
+ @fields ||= {}
9
+ end
10
+
11
+ def user_fields
12
+ @user_fields ||= {}
13
+ end
14
+ def record_fields
15
+ self.class.record_fields
16
+ end
17
+ def special_fields
18
+ self.class.special_fields
19
+ end
20
+
21
+ def locked_fields
22
+ self.class.locked_fields
23
+ end
24
+
25
+ module ClassMethods
26
+ # Allows for dynamically declaring fields that will come from
27
+ # Pulp.
28
+ #
29
+ def pulp_field(field,options={})
30
+ locked_fields << field if options[:locked]
31
+
32
+ module_eval("def #{field}() user_fields['#{field}'] || fields['#{field}']; end")
33
+ module_eval("def #{field}=(val) user_fields['#{field}'] = val; end") unless options[:locked]
34
+
35
+ record_fields[field] = options
36
+ end
37
+
38
+ # Declare many fields at once.
39
+ def pulp_fields(*fields)
40
+ fields.to_a.each {|field| pulp_field(field) }
41
+ end
42
+
43
+ def pulp_deferred_field(field,options={})
44
+ if options[:array]
45
+ module_eval("def #{field}() self.class.plain_get(fields['#{field}']).collect{|p| #{options[:array]}.new(p) }; end")
46
+ elsif options[:returns] == :plain
47
+ module_eval("def #{field}() self.class.plain_base.connection[fields['#{field}'].sub(/^\\/pulp\\/api\\//,'')].get; end")
48
+ elsif options[:returns]
49
+ module_eval("def #{field}() (res = self.class.plain_get(fields['#{field}'])).empty? ? nil : #{options[:returns]}.new(res); end")
50
+ else
51
+ module_eval("def #{field}() self.class.plain_get(fields['#{field}']); end")
52
+ end
53
+ module_eval("def #{field}_link() fields['#{field}']; end")
54
+ end
55
+
56
+ # Declare many deffered fields at once.
57
+ def pulp_deferred_fields(*fields)
58
+ [*fields].each {|field| pulp_deferred_field(field) }
59
+ end
60
+
61
+ # declare a field that is locked
62
+ def pulp_locked_field(field,options={})
63
+ pulp_field field, options.merge(:locked => true)
64
+ end
65
+
66
+ # Declare many locked fields at once.
67
+ def pulp_locked_fields(*fields)
68
+ [*fields].each {|field| pulp_locked_field(field) }
69
+ end
70
+
71
+ # special fields are locked and registered as being special
72
+ def pulp_special_field(field,options={})
73
+ pulp_locked_field(field,options)
74
+ special_fields << field
75
+ end
76
+
77
+ #Declare multiple special fields at once
78
+ def pulp_special_fields(*fields)
79
+ [*fields].each{|f| pulp_special_field(f) }
80
+ end
81
+
82
+ # optional features
83
+ def has_collection(options={})
84
+
85
+ instance_eval %{ def all
86
+ base_get('').collect {|e| self.new(e) }
87
+ end}
88
+
89
+ options[:all_filters] && options[:all_filters].each do |filter|
90
+ instance_eval %{
91
+ def find_by_#{filter}(f)
92
+ base_get('',nil,{ :#{filter} => f }).collect {|e| self.new(e) }
93
+ end}
94
+ end
95
+ end
96
+
97
+ def has_crud(options={})
98
+ has_collection(options[:collection]||{})
99
+ has_create
100
+ has_get
101
+ has_update
102
+ has_delete
103
+ end
104
+
105
+ def has_delete
106
+ include Pulp::Common::Lifecycle::Delete
107
+ end
108
+
109
+ def has_create
110
+ include Pulp::Common::Lifecycle::Create
111
+ end
112
+
113
+ def has_get
114
+ include Pulp::Common::Lifecycle::Get
115
+ end
116
+
117
+ def has_update
118
+ include Pulp::Common::Lifecycle::Update
119
+ end
120
+
121
+ def pulp_action(action_name, options={})
122
+ options[:method] ||= :post
123
+ options[:params] = true if options[:params].nil?
124
+ # default is true
125
+ slash = (options[:append_slash].nil? || options[:append_slash]) ? '/' : ''
126
+ if options[:returns]
127
+ module_eval %{
128
+ def #{action_name}(#{"params#{"={}" if options[:params] == :optional}" if options[:params]})
129
+ #{options[:returns]}.new(self.class.base_#{(options[:parse] == false) ? 'unparsed_' : '' }#{options[:method]}('#{action_name}#{slash}',self.id,#{options[:params] ? 'params' : 'nil' }))
130
+ end}
131
+ else
132
+ module_eval %{
133
+ def #{action_name}(#{"params#{"={}" if options[:params] == :optional}" if options[:params]})
134
+ self.class.base_#{(options[:parse] == false) ? 'unparsed_' : '' }#{options[:method]}('#{action_name}#{slash}',self.id,#{options[:params] ? 'params' : 'nil' })
135
+ end}
136
+ end
137
+ if options[:task_list]
138
+ module_eval %{
139
+ def #{action_name}_tasks
140
+ self.class.base_get('#{action_name}#{slash}',self.id).collect{|p| Pulp::Task.new(p) }
141
+ end}
142
+ end
143
+ end
144
+
145
+ def pulp_update_action(action_name,options)
146
+ module_eval %{
147
+ def update_#{action_name}(#{[*options[:params]].join(', ')})
148
+ self.class.base_put('',self.id,{ #{[*options[:params]].collect{|p| ":#{p} => #{p}" }.join(', ')} })
149
+ end}
150
+ end
151
+
152
+ def special_fields
153
+ @special_fields ||= []
154
+ end
155
+
156
+ def record_fields
157
+ @record_fields ||= {}
158
+ end
159
+
160
+ def locked_fields
161
+ @locked_fields ||= []
162
+ end
163
+ end
164
+ end
165
+ end
166
+ end
@@ -0,0 +1,16 @@
1
+ module Pulp
2
+ module Common
3
+ module Lifecycle
4
+ module Create
5
+ def self.included(base)
6
+ base.extend ClassMethods
7
+ end
8
+ module ClassMethods
9
+ def create(fields)
10
+ self.new(base_post('',nil,fields))
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,23 @@
1
+ module Pulp
2
+ module Common
3
+ module Lifecycle
4
+ module Delete
5
+ def self.included(base)
6
+ base.extend ClassMethods
7
+ end
8
+ def delete
9
+ self.class.delete(id)
10
+ end
11
+ module ClassMethods
12
+ def delete_all
13
+ base_unparsed_delete('','')
14
+ end
15
+
16
+ def delete(item_id)
17
+ base_delete("",item_id)
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,22 @@
1
+ module Pulp
2
+ module Common
3
+ module Lifecycle
4
+ module Get
5
+ def self.included(base)
6
+ base.extend ClassMethods
7
+ end
8
+
9
+ def refresh
10
+ set_fields(self.class.base_get('',id))
11
+ self
12
+ end
13
+
14
+ module ClassMethods
15
+ def get(item_id)
16
+ self.new(base_get('',item_id))
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,23 @@
1
+ module Pulp
2
+ module Common
3
+ module Lifecycle
4
+ module Update
5
+ def self.included(base)
6
+ base.extend ClassMethods
7
+ end
8
+
9
+ def save
10
+ res = self.class.update(self.id,user_fields.reject{|key,value| self.class.special_fields.include?(key) })
11
+ refresh if self.respond_to?(:refresh)
12
+ res
13
+ end
14
+
15
+ module ClassMethods
16
+ def update(item,fields)
17
+ self.new(base_put('',item,fields))
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,84 @@
1
+ module Pulp
2
+ module Connection
3
+ class Base
4
+ include Pulp::Common::Lifecycle
5
+
6
+ pulp_special_fields :_id, :id
7
+
8
+ def initialize(data={})
9
+ set_fields(data)
10
+ end
11
+
12
+ def set_fields(field_data)
13
+ @fields = field_data
14
+ end
15
+
16
+ class << self
17
+ attr_accessor :hostname, :username, :password, :https
18
+
19
+ def reset
20
+ Pulp::Connection::Handler.reset_instance(self.identifier)
21
+ end
22
+
23
+ def identifier
24
+ @identifier ||= name.demodulize.downcase
25
+ end
26
+
27
+ def plain_unparsed_get(cmd, params=nil)
28
+ plain_base.connection[cmd.sub(/^#{Regexp.escape(plain_base.api_path)}\//,'')].get(merge_params(params)).body
29
+ end
30
+
31
+ def base_unparsed_get(cmd,item=nil,params=nil)
32
+ base.connection[parse_item_cmd(item,cmd)].get(merge_params(params)).body
33
+ end
34
+
35
+ def base_unparsed_delete(cmd,item=nil,params=nil)
36
+ base.connection[parse_item_cmd(item,cmd)].delete(merge_params(params)).body
37
+ end
38
+
39
+ def base_unparsed_post(cmd,item=nil,params=nil)
40
+ base.connection[parse_item_cmd(item,cmd)].post(params.nil? ? nil : params.to_json, :content_type => :json ).body
41
+ end
42
+
43
+ def base_unparsed_put(cmd,item=nil,params=nil)
44
+ base.connection[parse_item_cmd(item,cmd)].put(params.nil? ? nil : params.to_json, :content_type => :json ).body
45
+ end
46
+
47
+ def plain_get(cmd, params=nil)
48
+ plain_base.parsed{|conn| conn[cmd.sub(/^#{Regexp.escape(plain_base.api_path)}\//,'')].get(merge_params(params)) }
49
+ end
50
+
51
+ def base_get(cmd,item=nil,params=nil)
52
+ base.parsed{|conn| conn[parse_item_cmd(item,cmd)].get(merge_params(params)) }
53
+ end
54
+
55
+ def base_delete(cmd,item=nil,params=nil)
56
+ base.parsed{|conn| conn[parse_item_cmd(item,cmd)].delete(merge_params(params)) }
57
+ end
58
+
59
+ def base_post(cmd,item=nil,params=nil)
60
+ base.parsed{|conn| conn[parse_item_cmd(item,cmd)].post(params.nil? ? nil : params.to_json, :content_type => :json ) }
61
+ end
62
+ def base_put(cmd,item=nil,params=nil)
63
+ base.parsed{|conn| conn[parse_item_cmd(item,cmd)].put(params.nil? ? nil : params.to_json, :content_type => :json ) }
64
+ end
65
+
66
+ def base
67
+ Pulp::Connection::Handler.instance_for(self.identifier, hostname, username, password, (https.nil? ? true : https))
68
+ end
69
+
70
+ def plain_base
71
+ Pulp::Connection::Handler.instance_for('', hostname, username, password, (https.nil? ? true : https))
72
+ end
73
+
74
+ def parse_item_cmd(item,cmd)
75
+ "#{"/#{item}" unless item.nil?}#{(cmd =~ /^\//) ? cmd : "/#{cmd}"}"
76
+ end
77
+
78
+ def merge_params(params)
79
+ params.nil? ? {} : { :params => params }
80
+ end
81
+ end
82
+ end
83
+ end
84
+ end
@@ -0,0 +1,59 @@
1
+ module Pulp
2
+ module Connection
3
+ class Handler
4
+
5
+ include Pulp::Common::Debug
6
+
7
+ class << self
8
+ attr_accessor :hostname,:username, :password
9
+
10
+ def instance_for(identifier,h=nil,u=nil,p=nil,https=true)
11
+ instances[identifier] ||= Handler.new(identifier,
12
+ h||hostname,
13
+ u||username,
14
+ p||password,
15
+ https
16
+ )
17
+ end
18
+
19
+ def reset_instance(identifier)
20
+ instances.delete(identifier)
21
+ end
22
+
23
+ def reset_all
24
+ @instances = {}
25
+ end
26
+
27
+ private
28
+ def instances
29
+ @instances ||= {}
30
+ end
31
+ end
32
+
33
+ def initialize(identifier,hostname,username=nil,password=nil,https=true)
34
+ @identifier = identifier
35
+ @hostname = hostname
36
+ @username = username
37
+ @password = password
38
+ @https = https
39
+ end
40
+
41
+ def parsed
42
+ JSON.parse((yield self.connection).body)
43
+ end
44
+
45
+ def connection
46
+ @connection ||= RestClient::Resource.new(url, :user => @username, :password => @password, :headers => { :accept => :json })
47
+ end
48
+
49
+ def api_path
50
+ "/pulp/api"
51
+ end
52
+
53
+ def url
54
+ @url ||= "#{@https ? 'https' : 'http'}://#{@hostname}#{api_path}/#{@identifier.to_s.pluralize}"
55
+ end
56
+
57
+ end
58
+ end
59
+ end