第4章 Satellite 6 API への移行
Red Hat Satellite 6 が Satellite 5 と大きく違う点の 1 つに API が挙げられます。Satellite 5 は XMLRPC ベースの API を使用しますが、Satellite 6 は REST ベースの API を使用します。この基本的な違いにより、Satellite 6 REST API を使用する前に、Satellite 5 API と統合した既存のスクリプトまたはツールを確認するか、少なくても部分的に書き直す必要があります。
本セクションは、各製品内で同じユースケースを実現するための比較を紹介します。このチュートリアルは、特定のプログラミング言語を対象にしておらず、どのスクリプトも HTTPS で安全を確保していません。ここでは、Satellite 6 API への移行を開始するために Satellite 5 API スクリプトをメンテナンスするところから説明します。
Satellite 6 移行ツールは、Satellite 5 API またはスクリプトを Satellite 6 に移行しません。移行プロセスを開始するには、このセクションから始めてください。
詳しい情報
API の詳しい情報は『API ガイド』を参照してください。お使いの Satellite Server では、以下の場所で参照できます。
- Satellite 6: https://satellite6.example.com/apidoc/v2.html
- Satellite 5: https://satellite5.example.com/rpc/api
4.1. API スクリプトの例
本セクションでは、例を挙げて、以下の点を説明します。
Red Hat Satellite のシステムおよびホスト
- 認証し、ログインするユーザーに利用可能なシステム (Satellite 5) またはホスト (Satellite 6) の一覧を要求する
ユーザーとロール
- 認証し、ログインしたユーザーに表示可能なユーザーの一覧を要求する
- example ユーザーが存在する場合は削除する
- example ユーザーを新規作成し、そのロールを Administrator に設定する
本セクションでは、同じ結果を実現するための方法を全部で 5 つ紹介します。そのうち 2 つは Satellite 5 用で、残りの 3 つは Satellite 6 用です。
- Satellite 5 の Python スクリプト
- Satellite 6 Python スクリプト
- Satellite 6 の Ruby スクリプト
-
Satellite 5 の
spacecmdの例 -
Satellite 6 の
hammerの例
spacecmd コマンドは、Satellite 5.6 ではサポートされていません。Satellite 5.7 ではサポートされているため、完全を期すためにここに示します。
全部で 10 個の例が提供されます。最初に、システムおよびホストを一覧を表示する簡単なユースケースを紹介し、その後、ユーザやロールなどの複雑なユースケースを紹介します。
4.1.1. システムおよびホストの一覧表示
本セクションでは、例を挙げて、ログインしたユーザーに利用可能なシステムおよびホストの一覧を表示する方法を説明します。
Satellite 5 で Python を使用して利用可能なシステムを一覧表示
この例では、Python スクリプトを使用して Satellite 5 に接続し、認証し、利用可能なシステムの一覧を取得します。
#!/usr/bin/python
import xmlrpclib
# Define Satellite location and login details
SATELLITE_URL = "http://localhost/rpc/api"
SATELLITE_LOGIN = "admin"
SATELLITE_PASSWORD = "password"
client = xmlrpclib.Server(SATELLITE_URL, verbose=0)
# Authenticate and get session key
key = client.auth.login(SATELLITE_LOGIN, SATELLITE_PASSWORD)
# Get list of systems available to user
list = client.system.listSystems(key)
for system in list:
print system.get('id')
print system.get('name')
# Logout
client.auth.logout(key)Satellite 6 で Python を使用して利用可能なホストを一覧表示
この例では、Python スクリプトを使用して Satellite 6 に接続し、認証し、利用可能なシステムの一覧を取得します。
#!/usr/bin/python
import json
import requests
# Define Satellite location and login details
SAT_API = "https://localhost/api/v2/"
USERNAME = "admin"
PASSWORD = "changeme"
SSL_VERIFY = False
def get_json(location):
"""
Performs a GET using the passed URL location
"""
r = requests.get(location, auth=(USERNAME, PASSWORD), verify=SSL_VERIFY)
return r.json()
def main():
# List all hosts available to the user
hosts = get_json(SAT_API + "hosts/")
# Pretty Print the returned JSON of Hosts
print json.dumps(hosts, sort_keys=True, indent=4)
if __name__ == "__main__":
main()Satellite 6 で Ruby を使用して利用可能なホストを一覧表示
この例では、Ruby スクリプトを使用して Satellite 6 に接続し、認証し、利用可能なシステムの一覧を取得します。
#!/usr/bin/env ruby
require 'json'
require 'rest-client'
url = 'https://localhost/api/v2/'
$username = 'admin'
$password = 'changeme'
def get_json(location)
response = RestClient::Request.new(
:method => :get,
:url => location,
:user => $username,
:password => $password,
:headers => { :accept => :json,
:content_type => :json }
).execute
results = JSON.parse(response.to_str)
end
hosts = get_json(url+"hosts/")
#puts JSON.pretty_generate(hosts)
puts "Hosts within Satellite are:"
hosts['results'].each do |name|
puts name['name']
end
exit()Satellite 5 でコマンドラインを使用して利用可能なシステムを一覧表示
Satellite 5 で以下のコマンドを使用して、利用なシステムを一覧表示します。
# spacecmd -u username -p password system_list
セッションは以下のようになります。
# spacecmd -u admin -p password system_list INFO: Spacewalk Username: admin INFO: Connected to https://localhost/rpc/api as admin test_02.example.com
Satellite 6 でコマンドラインを使用して利用可能なホストを一覧表示
Satellite 6 で以下のコマンドを使用して、利用可能なホストを一覧表示します。
# hammer host list
セッションは以下のようになります。
# hammer host list [Foreman] password for admin: ---|-----------------|-------------------|------------|--------------|------------------ ID | NAME | OPERATING SYSTEM | HOST GROUP | IP | MAC ---|-----------------|-------------------|------------|--------------|------------------ 1 | test.example.com | RHEL Server 6.5 | | 10.34.34.235 | e4:1f:13:6b:ed:0c
4.1.2. ユーザーの削除および作成
本セクションでは、ユーザーの検索、作成、削除する方法を説明します。
Satellite 5 で Python を使用してユーザーを管理
この例では、Python スクリプトを使用して Satellite 5 Server に接続し、認証する方法を説明します。ここでは、特定ユーザー (example) を検索し、そのユーザーが存在する場合は削除して再作成し、管理者権限を付与する方法を説明します。
#!/usr/bin/python
import xmlrpclib
# Define Satellite location and login details
SATELLITE_URL = "http://localhost/rpc/api"
SATELLITE_LOGIN = "admin"
SATELLITE_PASSWORD = "password"
client = xmlrpclib.Server(SATELLITE_URL, verbose=0)
# Authenticate and get session key
key = client.auth.login(SATELLITE_LOGIN, SATELLITE_PASSWORD)
# Get list of users
list = client.user.list_users(key)
print "Existing users in Satellite:"
for user in list:
print user.get('login')
# Look for user example and if found, delete the user
for user in list:
if user.get('login') == 'example':
deleteuser = client.user.delete(key, 'example')
if deleteuser == 1:
print "User example deleted"
# Create a user called example
createuser = client.user.create(key, 'example', 'password', 'Example', 'User', "root@localhost")
if createuser == 1:
print "User example created"
# Admin Org Admin role to the example user
adminrole = client.user.addRole(key, 'example', 'org_admin')
if adminrole == 1:
print "Made example an Org Admin"
# Logout
client.auth.logout(key)Satellite 6 で Python を使用してユーザーを管理
ここでは、先ほどの例と同じタスクを行います。つまり、Python スクリプトを使用して Satellite 6 Server サーバーに接続し、認証する方法を説明します。特定のユーザーを検索し、そのユーザーが存在する場合は削除して再作成し、管理者権限を付与する方法を説明します。
#!/usr/bin/python
import json
import requests
# Define Satellite location and login details
SAT_API = "https://localhost/api/v2/"
POST_HEADERS = {'content-type': 'application/json'}
USERNAME = "admin"
PASSWORD = "changeme"
SSL_VERIFY = False
def get_json(location):
"""
Performs a GET using the passed URL location
"""
r = requests.get(location, auth=(USERNAME, PASSWORD), verify=SSL_VERIFY)
return r.json()
def post_json(location, json_data):
"""
Performs a POST and passes the data to the URL location
"""
result = requests.post(
location,
data=json_data,
auth=(USERNAME, PASSWORD),
verify=SSL_VERIFY,
headers=POST_HEADERS)
return result.json()
def delete_json(location):
"""
Performs a DELETE and passes the id to the URL location
"""
result = requests.delete(
location,
auth=(USERNAME, PASSWORD),
verify=SSL_VERIFY)
return result.json()
def main():
# List all users within the Satellite
users = get_json(SAT_API + "users/")
#print json.dumps(users, indent=4)
print "Users known are:"
for login in users['results']:
print login['login']
# Look for user example and if found, delete the user
for delete in users['results']:
if delete['login'] == 'example':
id = delete ['id']
id = str(id)
delete = delete_json(SAT_API + "/users/" + id)
#print json.dumps(delete, indent=4)
print "User example deleted"
# Create a user called example as admin role
createuser = post_json(SAT_API + "/users/", json.dumps({ "mail": "root@localhost", "firstname": "Example", "lastname": "User", "login": "example", "password": "redhat", "admin": 'true', "auth_source_id": 1 }))
#print json.dumps(createuser, indent=4)
print "Admin user example created"
if __name__ == "__main__":
main()Satellite 6 で Ruby を使用してユーザーを管理
次に、Ruby を使用して、先ほどの例と同じタスクを実行します。
#!/usr/bin/env ruby
require 'json'
require 'rest-client'
url = 'https://localhost/api/v2/'
$username = 'admin'
$password = 'changeme'
def get_json(location)
response = RestClient::Request.new(
:method => :get,
:url => location,
:user => $username,
:password => $password,
:headers => { :accept => :json,
:content_type => :json }
).execute
results = JSON.parse(response.to_str)
end
def post_json(location, json_data)
response = RestClient::Request.new(
:method => :post,
:url => location,
:user => $username,
:password => $password,
:headers => { :accept => :json,
:content_type => :json},
:payload => json_data
).execute
results = JSON.parse(response.to_str)
end
def delete_json(location)
response = RestClient::Request.new(
:method => :delete,
:url => location,
:user => $username,
:password => $password,
:headers => { :accept => :json,
:content_type => :json }
).execute
results = JSON.parse(response.to_str)
end
# List all users within the Satellite
users = get_json(url+"users/")
#puts JSON.pretty_generate(users)
puts "Users known are:"
users['results'].each do |name|
puts name['login']
end
# Look for user example and if found, delete the user
users['results'].each do |name|
if name['login'] == 'example'
id = name['id']
delete = delete_json(url+"users/"+id.to_s)
#puts JSON.pretty_generate(delete)
puts "User example deleted"
end
end
# Create a user called example as admin role
data = JSON.generate({ :mail => "root@localhost", :firstname => "Example", :lastname => "User", :login => "example", :password => "password", :admin => 'true', :auth_source_id => 1})
createuser = post_json(url+"users/", data)
#puts JSON.pretty_generate(createuser)
puts "Admin user example created"
exit()Satellite 5 でコマンドラインを使用してユーザーを管理
ここでは、spacecmd コマンドを使用して、上の例と同じタスクを実行します。
# spacecmd -u admin -p password user_list # spacecmd -u admin -p password user_delete example # spacecmd -u admin -p password user_create # spacecmd -u admin -p password user_addrole example org_admin
セッションは以下のようになります。
# spacecmd -u admin -p password user_list INFO: Spacewalk Username: admin INFO: Connected to https://localhost/rpc/api as admin admin example # spacecmd -u admin -p password user_delete example INFO: Spacewalk Username: admin INFO: Connected to https://localhost/rpc/api as admin Delete this user [y/N]: y # spacecmd -u admin -p password user_list INFO: Spacewalk Username: admin INFO: Connected to https://localhost/rpc/api as admin admin # spacecmd -u admin -p password user_create INFO: Spacewalk Username: admin INFO: Connected to https://localhost/rpc/api as admin Username: example First Name: Example Last Name: User Email: root@localhost PAM Authentication [y/N]: n Password: Repeat Password: # spacecmd -u admin -p password user_list INFO: Spacewalk Username: admin INFO: Connected to https://localhost/rpc/api as admin admin example # spacecmd -u admin -p password user_addrole example org_admin INFO: Spacewalk Username: admin INFO: Connected to https://localhost/rpc/api as admin # spacecmd -u admin -p password user_details example INFO: Spacewalk Username: admin INFO: Connected to https://localhost/rpc/api as admin Username: example First Name: Example Last Name: User Email Address: root@localhost Organization: MY ORG Last Login: Created: 8/19/14 8:42:52 AM EDT Enabled: True Roles ----- activation_key_admin channel_admin config_admin monitoring_admin org_admin system_group_admin
Satellite 6 でコマンドラインを使用してユーザーを管理
ここでは、 hammer コマンドを使用して、上の例と同じタスクを実行します。
# hammer shell > user list > user delete --id 4 --login example > user create --admin true --firstname Example --lastname User --login example --mail root@localhost --password redhat --auth-source-id 1
セッションは以下のようになります。
# hammer shell [Foreman] password for admin: Welcome to the hammer interactive shell Type 'help' for usage information hammer> user list ---|---------|--------------|--------------- ID | LOGIN | NAME | EMAIL ---|---------|--------------|--------------- 4 | example | Example User | root@localhost 3 | admin | Admin User | root@localhost ---|---------|--------------|--------------- hammer> user delete --id 4 --login example User deleted hammer> user list ---|-------|------------|--------------- ID | LOGIN | NAME | EMAIL ---|-------|------------|--------------- 3 | admin | Admin User | root@localhost ---|-------|------------|--------------- hammer> user create --admin true --firstname Example --lastname User --login example --mail root@localhost --password redhat --auth-source-id 1 User created hammer> user list ---|----------|--------------|--------------- ID | LOGIN | NAME | EMAIL ---|----------|--------------|--------------- 3 | admin | Admin User | root@localhost 5 | example | Example User | root@localhost ---|----------|--------------|--------------- hammer>
4.2. Satellite 6 API のヒント
本セクションでは、Satellite 6 API での体験を最適化するための一般的なヒントを紹介します。
Satellite 6 API の閲覧
Satellite 6 Web UI にログインしている場合は、GET 要求のデフォルトの結果を /api/v2/<API-NAME> で確認できます。以下はその例となります。
コマンドラインで Satellite 6 API 要求の使用
curl コマンドを使用して、Satellite 6 API と対話ができます。以下に例を示します。
例4.1 GET 要求の例
Satellite 6 の組織、ホスト、ユーザーの一覧を表示する GET 要求の例
# SATUSER=admin # SATPASS='changeme' # SATURL="https://localhost" # curl -k -u $SATUSER:$SATPASS -X GET -H \ 'Accept: application/json' $SATURL/api/v2/organizations | json_reformat # curl -k -u $SATUSER:$SATPASS -X GET -H \ 'Accept: application/json' $SATURL/api/v2/hosts | json_reformat # curl -k -u $SATUSER:$SATPASS -X GET -H \ 'Accept: application/json' $SATURL/api/v2/users | json_reformat # curl -k -u $SATUSER:$SATPASS -X GET -H \ 'Accept: application/json' $SATURL/api/v2/users/3 | json_reformat
例4.2 DELETE 要求の例
上のコマンドで得られた結果から、 ID が「9」のユーザーを削除します。
# curl -k -u $SATUSER:$SATPASS -X DELETE -H \ 'Accept: application/json' $SATURL/api/v2/users/9 | json_reformat
例4.3 POST 要求の例
example という名前の新規ユーザーを作成する POST 要求の例。true フラグを渡して、ユーザーに管理者権限を付与します。
# curl -k -u $SATUSER:$SATPASS -X POST \
-d '{ "mail": "root@localhost", "firstname": "Example", \
"lastname": "User", "login": "example", "password": "redhat", \
"admin": 'true', "auth_source_id": 1 }' \
-H 'Accept: application/json' \
-H 'Content-Type: application/json' \
$SATURL/api/v2/users | json_reformat例4.4 PUT 要求の例
ID が「10」の example ユーザーのメールアドレスを example@localhost に変更する PUT 要求の例
# curl -k -u $SATUSER:$SATPASS -X PUT \
-d '{ "id": 10, "mail": "example@localhost" }' \
-H 'Accept: application/json' \
-H 'Content-Type: application/json' \
$SATURL/api/v2/users/10 | json_reformat