Module: KatelloForemanEngine::Bindings

Defined in:
lib/katello_foreman_engine/bindings.rb

Class Method Summary collapse

Class Method Details

.architectureObject



27
28
29
# File 'lib/katello_foreman_engine/bindings.rb', line 27

def architecture
  resource(ForemanApi::Resources::Architecture)
end

.architecture_create(name) ⇒ Object



115
116
117
# File 'lib/katello_foreman_engine/bindings.rb', line 115

def architecture_create(name)
  without_root_key { architecture.create('architecture' => {'name' => name}) }
end

.architecture_find(name) ⇒ Object



111
112
113
# File 'lib/katello_foreman_engine/bindings.rb', line 111

def architecture_find(name)
  find_resource(architecture, "name = #{name}")
end

.baseObject



19
20
21
# File 'lib/katello_foreman_engine/bindings.rb', line 19

def base
  resource(ForemanApi::Base)
end

.client_configObject



8
9
10
11
12
13
14
15
16
17
# File 'lib/katello_foreman_engine/bindings.rb', line 8

def client_config
  {
    :base_url => Settings['foreman_url'],
    :enable_validations => false,
    :oauth => {
      :consumer_key => Settings['oauth_consumer_key'],
      :consumer_secret => Settings['oauth_consumer_secret']
    },
  }
end

.config_templateObject



39
40
41
# File 'lib/katello_foreman_engine/bindings.rb', line 39

def config_template
  resource(ForemanApi::Resources::ConfigTemplate)
end

.environmentObject



23
24
25
# File 'lib/katello_foreman_engine/bindings.rb', line 23

def environment
  resource(ForemanApi::Resources::Environment)
end

.environment_create(content_view_id, org_label, env_label, content_view_label = nil) ⇒ Object



82
83
84
85
86
87
88
# File 'lib/katello_foreman_engine/bindings.rb', line 82

def environment_create(content_view_id, org_label, env_label, content_view_label = nil)
  params = {:org => org_label, :env => env_label, :content_view => content_view_label, :content_view_id => content_view_id}
  if foreman_org = organization_find(org_label)
    params[:org_id] = foreman_org['organization']['id']
  end
  base.http_call('post', '/foreman_katello_engine/api/environments', params)
end

.environment_destroy(id) ⇒ Object



90
91
92
# File 'lib/katello_foreman_engine/bindings.rb', line 90

def environment_destroy(id)
  environment.destroy('id' => id)
end

.environment_find(org_label, env_label, content_view_label = nil) ⇒ Object



74
75
76
77
78
79
80
# File 'lib/katello_foreman_engine/bindings.rb', line 74

def environment_find(org_label, env_label, content_view_label = nil)
  env, _ = base.http_call('get', '/foreman_katello_engine/api/environments/show',
                          {:org => org_label, :env => env_label, :content_view => content_view_label})
  return env
rescue RestClient::ResourceNotFound => e
  return nil
end

.mediumObject



43
44
45
# File 'lib/katello_foreman_engine/bindings.rb', line 43

def medium
  resource(ForemanApi::Resources::Medium)
end

.medium_create(name, path, org_label) ⇒ Object



178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/katello_foreman_engine/bindings.rb', line 178

def medium_create(name, path, org_label)
  params = {
    'name' => name,
    'path' => path,
    'os_family' => Settings['foreman_os_family']
  }
  if foreman_org = organization_find(org_label)
    params['organization_ids'] = [foreman_org['organization']['id']]
  end
  without_root_key do
    self.medium.create('medium' => params)
  end
end

.medium_destroy(id) ⇒ Object



192
193
194
# File 'lib/katello_foreman_engine/bindings.rb', line 192

def medium_destroy(id)
  self.medium.destroy('id' => id)
end

.medium_find(path) ⇒ Object



174
175
176
# File 'lib/katello_foreman_engine/bindings.rb', line 174

def medium_find(path)
  find_resource(medium, %{path = "#{path}"})
end

.operating_systemObject



35
36
37
# File 'lib/katello_foreman_engine/bindings.rb', line 35

def operating_system
  resource(ForemanApi::Resources::OperatingSystem)
end

.operating_system_create(name, major, minor) ⇒ Object



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/katello_foreman_engine/bindings.rb', line 123

def operating_system_create(name, major, minor)
  data = {
    'name' => name,
    'major' => major.to_s,
    'minor' => minor.to_s,
    'family' => Settings['foreman_os_family']
  }
  provisioning_template_name = if name == 'RedHat'
                                 Settings['foreman_os_rhel_provisioning_template']
                               else
                                 Settings['foreman_os_provisioning_template']
                               end
  templates_to_add = [template_find(provisioning_template_name),
                      template_find(Settings['foreman_os_pxe_template'])].compact
  data['os_default_templates_attributes'] = templates_to_add.map do |template|
    {
      "config_template_id" => template["id"],
      "template_kind_id" => template["template_kind"]["id"],
    }
  end

  if ptable = ptable_find(Settings['foreman_os_ptable'])
    data['ptable_ids'] = [ptable['id']]
  end

  os = without_root_key { operating_system.create('operatingsystem' => data) }

  oss, _ = operating_system.index
  os_ids = oss.map { |o| o['operatingsystem']['id'] }
  templates_to_add.each do |template|
    # Because of http://projects.theforeman.org/issues/2500 we
    # have not way to add only the created os to the template.
    # As a workaround, we add all os to the template for now.
    config_template.update("id" => template['id'],
                           'config_template' => {
                             "operatingsystem_ids" => os_ids
                           })
  end
  return os
end

.operating_system_find(name, major, minor) ⇒ Object



119
120
121
# File 'lib/katello_foreman_engine/bindings.rb', line 119

def operating_system_find(name, major, minor)
  find_resource(operating_system, %{name = "#{name}" AND major = "#{major}" AND minor = "#{minor}"})
end

.operating_system_update(id, data) ⇒ Object



164
165
166
167
168
# File 'lib/katello_foreman_engine/bindings.rb', line 164

def operating_system_update(id, data)
  without_root_key do
    operating_system.update('id' => id, 'operatingsystem' => data)
  end
end

.organization_create(name) ⇒ Object



63
64
65
66
67
# File 'lib/katello_foreman_engine/bindings.rb', line 63

def organization_create(name)
  base.http_call('post', '/api/organizations',
                 :organization => {:name => name,
                   :ignore_types => %w[User SmartProxy Subnet ComputeResource ConfigTemplate Domain] })
end

.organization_destroy(id) ⇒ Object



69
70
71
# File 'lib/katello_foreman_engine/bindings.rb', line 69

def organization_destroy(id)
  base.http_call('delete', "/api/organizations/#{id}")
end

.organization_find(kt_label) ⇒ Object



57
58
59
60
61
# File 'lib/katello_foreman_engine/bindings.rb', line 57

def organization_find(kt_label)
  name = "KT-[#{kt_label}]"
  orgs, _ = base.http_call('get', '/api/organizations', 'search' => "name = #{name}")
  return orgs.first
end

.ptableObject



31
32
33
# File 'lib/katello_foreman_engine/bindings.rb', line 31

def ptable
  resource(ForemanApi::Resources::Ptable)
end

.ptable_find(name) ⇒ Object



196
197
198
# File 'lib/katello_foreman_engine/bindings.rb', line 196

def ptable_find(name)
  find_resource(ptable, %{name = "#{name}"})
end

.template_find(name) ⇒ Object



170
171
172
# File 'lib/katello_foreman_engine/bindings.rb', line 170

def template_find(name)
  find_resource(config_template, %{name = "#{name}"})
end

.user(username = nil) ⇒ Object



47
48
49
50
51
52
53
54
55
# File 'lib/katello_foreman_engine/bindings.rb', line 47

def user(username = nil)
  user_resource = resource(ForemanApi::Resources::User)
  if username && User.current.username == username
    # this means the first user is created: use the predefined
    # Foreman admin user for this cases.
    user_resource.client.options[:headers][:foreman_user] = 'admin'
  end
  return user_resource
end

.user_create(username, email, admin) ⇒ Object



98
99
100
101
102
103
104
105
# File 'lib/katello_foreman_engine/bindings.rb', line 98

def user_create(username, email, admin)
  user(username).create({ 'user' => {
                            'login' => username,
                            'mail' => email,
                            'password' => Password.generate_random_string(25),
                            'admin' => admin,
                            'auth_source_id' => 1}})
end

.user_destroy(id) ⇒ Object



107
108
109
# File 'lib/katello_foreman_engine/bindings.rb', line 107

def user_destroy(id)
  user.destroy('id' => id)
end

.user_find(username) ⇒ Object



94
95
96
# File 'lib/katello_foreman_engine/bindings.rb', line 94

def user_find(username)
  find_resource(user(username), "login = #{username}")
end