Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 15 additions & 17 deletions guides/common/modules/proc_calling-the-api-in-python.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,9 @@ except ImportError:
print("Please install the python-requests module.")
sys.exit(-1)

# URL to your {ProjectX} server
# URL to your {ProjectServer}
URL = "https://{foreman-example-com}"
# URL for the API to your deployed {ProjectX} server
{project-allcaps}_API = f"\{URL}/katello/api/v2/"
# Katello-specific API
FOREMAN_API = f"\{URL}/api/"
KATELLO_API = f"\{URL}/katello/api/"
POST_HEADERS = {'content-type': 'application/json'}
# Default credentials to login to {ProjectX}
Expand Down Expand Up @@ -82,12 +80,12 @@ def main():
"""

# Check if our organization already exists
org = get_json(f"{{project-allcaps}_API}/organizations/\{ORG_NAME}")
org = get_json(f"\{FOREMAN_API}/organizations/\{ORG_NAME}")

# If our organization is not found, create it
if org.get('error', None):
org_id = post_json(
f"{{project-allcaps}_API}/organizations/",
f"\{FOREMAN_API}/organizations/",
json.dumps({"name": ORG_NAME})
)["id"]
print("Creating organization:\t" + ORG_NAME)
Expand All @@ -98,7 +96,7 @@ def main():

# Now, let's fetch all available life cycle environments for this org...
envs = get_json(
f"{{project-allcaps}_API}/organizations/\{org_id}/environments/"
f"\{KATELLO_API}/organizations/\{org_id}/environments/"
)

# ...and add them to a dictionary, with respective 'Prior' environment
Expand All @@ -116,7 +114,7 @@ def main():
# Create life cycle environments
for environment in ENVIRONMENTS:
new_env_id = post_json(
f"{{project-allcaps}_API}/organizations/\{org_id}/environments/",
f"\{KATELLO_API}/organizations/\{org_id}/environments/",
json.dumps({
"name": environment,
"organization_id": org_id,
Expand Down Expand Up @@ -152,10 +150,10 @@ except ImportError:
print("Please install the python-requests module.")
sys.exit(-1)

SAT = "{foreman-example-com}"
# URL for the API to your deployed {ProjectX} server
{project-allcaps}_API = f"https://\{SAT}/api/"
KATELLO_API = f"https://\{SAT}/katello/api/v2/"
HOSTNAME = "{foreman-example-com}"
# URL for the API to your {ProjectServer}
FOREMAN_API = f"https://\{HOSTNAME}/api/"
KATELLO_API = f"https://\{HOSTNAME}/katello/api/v2/"

POST_HEADERS = {'content-type': 'application/json'}
# Default credentials to login to {ProjectX}
Expand Down Expand Up @@ -204,23 +202,23 @@ def display_info_for_subs(url):
print(f"{str(sub['id']):10}{sub['name']:90}{str(sub['start_date']):30}")

def main():
host = SAT
host = HOSTNAME
print(f"Displaying all info for host \{host} ...")
display_all_results({project-allcaps}_API + 'hosts/' + host)
display_all_results(FOREMAN_API + 'hosts/' + host)

print(f"Displaying all facts for host \{host} ...")
display_all_results({project-allcaps}_API + f'hosts/\{host}/facts')
display_all_results(FOREMAN_API + f'hosts/\{host}/facts')

host_pattern = 'example'
print(f"Displaying basic info for hosts matching pattern '\{host_pattern}'...")
display_info_for_hosts({project-allcaps}_API + 'hosts?per_page=1&search=name~' + host_pattern)
display_info_for_hosts(FOREMAN_API + 'hosts?per_page=1&search=name~' + host_pattern)

print(f"Displaying basic info for subscriptions")
display_info_for_subs(KATELLO_API + 'subscriptions')

environment = 'production'
print(f"Displaying basic info for hosts in environment \{environment}...")
display_info_for_hosts({project-allcaps}_API + 'hosts?search=environment=' + environment)
display_info_for_hosts(FOREMAN_API + 'hosts?search=environment=' + environment)


if __name__ == "__main__":
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,25 @@
= Updating the details of multiple operating systems

Use this procedure to update the details of multiple operating systems.
This example shows you how to assign each operating system a partition table called `Kickstart default`, a configuration template called `Kickstart default PXELinux`, and a provisioning template called `Kickstart Default`.
This example shows you how to assign each operating system a partition table called `{client-provisioning-template-type} default`, a configuration template called `{client-provisioning-template-type} default PXELinux`, and a provisioning template called `{client-provisioning-template-type} Default`.

.Procedure

. On {ProjectServer}, run the following Bash script:
+
[source,terminal,options="nowrap" subs="+quotes"]
[source, bash, options="nowrap" subs="+quotes,verbatim,attributes"]
----
PARTID=$(hammer --csv partition-table list | grep "Kickstart default," | cut -d, -f1)
PXEID=$(hammer --csv template list --per-page=1000 | grep "Kickstart default PXELinux" | cut -d, -f1)
SATID=$(hammer --csv template list --per-page=1000 | grep "provision" | grep ",Kickstart default" | cut -d, -f1)
PARTID=$(hammer --csv partition-table list | grep "{client-provisioning-template-type} default," | cut -d, -f1)
PXEID=$(hammer --csv template list --per-page=1000 | grep "{client-provisioning-template-type} default PXELinux" | cut -d, -f1)
{project-allcaps}_ID=$(hammer --csv template list --per-page=1000 | grep "provision" | grep ",{client-provisioning-template-type} default" | cut -d, -f1)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Previously it was named SAT_ID but, isn't this really the TEMPLATE_ID or even PROVISION_TEMPLATE_ID?

Also, this really goes beyond the scope of this PR but I really dislike that it's using grep to "search" in combination with a very large per page number.

I don't have a setup at hand to test it and there there may be a better way, but I'd suggest to try

Suggested change
{project-allcaps}_ID=$(hammer --csv template list --per-page=1000 | grep "provision" | grep ",{client-provisioning-template-type} default" | cut -d, -f1)
PROVISION_TEMPLATE_ID=$(hammer --csv --no-headers template list --search 'name = "{client-provisioning-template-type} default"' --fields=Id --per-page 1)

Perhaps @ofedoren has some better recommendation.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Input on improving the templates is very much appreciated. For this PR, it is out of scope but I will keep this thread open and apply feedback later if necessary.

I have two possible follow-up changes in mind:

  • Extract the actual code into snippets with a specific naming scheme, for example, snip_code_x-y.adoc and write some minor automation to at least lint the code using flake8/black/rubocop. Next step would be to automatically run this code on a Foreman/Katello instance.
  • Deliver this code, similar to bin/katello-certs-check in foreman-installer, via RPM. We actually spoke about automating a procedure, I believe it was Salt or Ubuntu-related, in the past @ekohl.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd prefer to recommend foreman-ansible-modules (or Red Hat's commercially supported branded equivalent). If you look at theforeman/forklift#1856 you can see it's way easier to maintain.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, FAM works great. We use that downstream/internally quite a bit. However, as part of the Foreman API guide, I don't think we want to show FAM examples (even though FAM also uses the Foreman/Katello API internally). I think a FAM example would be nice in https://docs.theforeman.org/nightly/Administering_Project/index-katello.html#Managing_Project_with_Ansible_Collections_admin

Is it OK to open an issue and move this discussion/implementation to another issue/PR?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hammer and FAM both use the API. If you're take that position, I think Hammer shouldn't be in the API guide either. But it's not: https://docs.theforeman.org/nightly/Provisioning_Hosts/index-katello.html#updating-the-details-of-multiple-operating-systems_provisioning

I think guiding users to FAM instead of writing shell scripts is a better solution, but agree it's out of scope for this PR.

for i in $(hammer --no-headers --csv os list | awk -F, {'print $1'})
do
hammer partition-table add-operatingsystem --id="${PARTID}" --operatingsystem-id="${i}"
hammer template add-operatingsystem --id="${PXEID}" --operatingsystem-id="${i}"
hammer os set-default-template --id="${i}" --config-template-id=${PXEID}
hammer os add-config-template --id="${i}" --config-template-id=${SATID}
hammer os set-default-template --id="${i}" --config-template-id=${SATID}
hammer partition-table add-operatingsystem --id="$\{PARTID}" --operatingsystem-id="$\{i}"
hammer template add-operatingsystem --id="$\{PXEID}" --operatingsystem-id="$\{i}"
hammer os set-default-template --id="$\{i}" --config-template-id=$\{PXEID}
hammer os add-config-template --id="$\{i}" --config-template-id=${{project-allcaps}_ID}
hammer os set-default-template --id="$\{i}" --config-template-id=${{project-allcaps}_ID}
done
----

Expand Down