Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Red Hat Satellite subscription module #52578

Open
wants to merge 12 commits into
base: devel
from

fix pylint errors

  • Loading branch information...
Luc Stroobant
Luc Stroobant committed Feb 20, 2019
commit c7179d49f68239caa26b8c2013124a2425d8ce6f
@@ -16,7 +16,7 @@
def host_list(sat_url, user, password, org, verify, content_host=None):
"""Get a list of hosts from the satellite"""

apicall = "{}/api/hosts".format(sat_url)
apicall = "{0}/api/hosts".format(sat_url)
params = dict(
organization_id=org,
sort_by="name",
@@ -28,17 +28,19 @@ def host_list(sat_url, user, password, org, verify, content_host=None):
params["search"] = str(content_host)
else:
# treat other patterns as hostnames
params["search"] = "name={}".format(content_host)
params["search"] = "name={0}".format(content_host)

hosts = request(apicall, user, password, verify, True, params)

return hosts


def request(url, user, password, verify=True, pagination=False, params={}):
def request(url, user, password, verify=True, pagination=False, params=None):
""""Execute a Satellite GET API request"""

per_page = 200
if not params:
params = dict()
if params != {} or pagination:
params["page"] = 1
params["paged"] = True
@@ -91,7 +91,7 @@
- product_id=RH000XX
This conversation was marked as resolved by stroobl

This comment has been minimized.

Copy link
@dagwieers

dagwieers Feb 21, 2019

Member

Would this not be better a list of dicts ?

Also, is the number of possible keys in that dict limited to e.g. product_id and name, or is it free-form ?
Because you can actually enforce certain values in dictionary lists from the argspec.

This comment has been minimized.

Copy link
@stroobl

stroobl Feb 21, 2019

Author

It's not necessary a key-value. In the example on line 123 you have:

subscription:
 - "Red Hat Enterprise Linux Server Entry Level, Self-support"

as far as I can test, the api is not 100% consistent there, it doesn't accept a search key if you search for a name. (so name=Red Hat Enterprise Linux Server Entry Level, Self-support doesn't work).

- name=EPEL
- name=Extra
unique: True
unique: true
This conversation was marked as resolved by stroobl

This comment has been minimized.

Copy link
@dagwieers

dagwieers Feb 20, 2019

Member
Suggested change
unique: true
unique: yes

This comment has been minimized.

Copy link
@gforster

gforster Feb 21, 2019

Contributor

Interesting. Defaults for molecule/ansible-lint prefer true/false.

This comment has been minimized.

Copy link
@dagwieers

dagwieers Feb 22, 2019

Member

Yup, there is an action item to change ansible-lint and Molecule.

Strict YAML 1.2 only accepts true/false, but normal YAML accepts too many options as booleans (y/n, true/false, yes/no, on/off). Obviously we don't want to have all these in our examples, as this leads to confusion.

We decided that we would allow both yes/no and true/false, but for documentation and examples would stick to yes/no. The main reason for this is that it is easier for non-native English speakers. (Since YAML interpretes the boolean values, we don't know exactly what the DOCUMENTATION specified, so we can't just reproduce what was stated and have to decide on one.)

organization: 1
delegate_to: localhost
@@ -104,7 +104,7 @@
content_host:
- "{{ inventory_hostname }}"
subscription: "{{ redhat_subscriptions }}"
unique: True
unique: true
organization: 1
delegate_to: localhost
@@ -198,10 +198,10 @@ def subscription_assign(sat_url, user, password, matched_subscriptions, host, mo
data = {"id": host["id"], "subscriptions": [{"id": available_subscription["id"], "quantity": 1}]}

# attach the available subscription to this host
apicall = "{}/api/hosts/{}/subscriptions/add_subscriptions".format(sat_url, host["id"])
apicall = "{0}/api/hosts/{1}/subscriptions/add_subscriptions".format(sat_url, host["id"])
put(apicall, user, password, data, verify)

result.append([host["name"], "Assigned: {}".format(sub["name"])])
result.append([host["name"], "Assigned: {0}".format(sub["name"])])

return result, error

@@ -212,8 +212,8 @@ def subscription_search(sat_url, user, password, org, subscription, module, veri
matched_subscriptions = {}
for subsearch in subscription:
# only search for normal subscriptions to avoid returning assigned ones
params = {"search": "type=NORMAL and ({})".format(subsearch)}
apicall = "{}/katello/api/organizations/{}/subscriptions".format(sat_url, org)
params = {"search": "type=NORMAL and ({0})".format(subsearch)}
apicall = "{0}/katello/api/organizations/{1}/subscriptions".format(sat_url, org)
matched_subscriptions[subsearch] = request(apicall, user, password, verify, True, params)
if matched_subscriptions[subsearch] == []:
errors = "Sorry, no subscriptions matched. Please adjust your subscription search"
@@ -225,7 +225,7 @@ def subscription_search(sat_url, user, password, org, subscription, module, veri
def subscription_remove(sat_url, user, password, host, sub, verify):
"""Remove a subscription from a host"""

apicall = "{}/api/hosts/{}/subscriptions/remove_subscriptions".format(sat_url, host["id"])
apicall = "{0}/api/hosts/{1}/subscriptions/remove_subscriptions".format(sat_url, host["id"])
data = {"id": host["id"], "subscriptions": [{"id": sub["id"], "quantity": sub["quantity_consumed"]}]}
result = put(apicall, user, password, data, verify)

@@ -243,7 +243,7 @@ def subscription_configure_autoattach(sat_url, user, password, host, autoheal, v
"service_level": "",
},
}
apicall = "{}/api/hosts/{}".format(sat_url, host["id"])
apicall = "{0}/api/hosts/{1}".format(sat_url, host["id"])
req = put(apicall, user, password, data, verify)

return req
@@ -253,7 +253,7 @@ def subscription_autoattach(sat_url, user, password, host, verify):
"""Run subscription autoattach for host"""

data = {"id": host["id"]}
apicall = "{}/api/hosts/{}/subscriptions/auto_attach".format(sat_url, host["id"])
apicall = "{0}/api/hosts/{1}/subscriptions/auto_attach".format(sat_url, host["id"])
req = put(apicall, user, password, data, verify)

return req["results"]
@@ -296,7 +296,7 @@ def main():
changed = False
result = []

sat_url = "https://{}".format(sat)
sat_url = "https://{0}".format(sat)

# get the list of hosts to work on
hosts = []
@@ -338,12 +338,12 @@ def main():

if unique:
# get existing subscriptions on host
apicall = "{}/api/hosts/{}/subscriptions".format(sat_url, host["id"])
apicall = "{0}/api/hosts/{1}/subscriptions".format(sat_url, host["id"])
hostsubs = request(apicall, user, password, verify)
# and remove them
for sub in hostsubs["results"]:
subscription_remove(sat_url, user, password, host, sub, verify)
result.append([host["name"], "Removed: {}".format(sub["name"])])
result.append([host["name"], "Removed: {0}".format(sub["name"])])

# search for our subscriptions
matched_subscriptions = subscription_search(sat_url, user, password, org, subscription, module, verify)
@@ -360,7 +360,7 @@ def main():
# loop over hosts to remove
for host in hosts:
# get existing subscriptions on host
apicall = "{}/api/hosts/{}/subscriptions".format(sat_url, host["id"])
apicall = "{0}/api/hosts/{1}/subscriptions".format(sat_url, host["id"])
hostsubs = request(apicall, user, password, verify)
for sub in hostsubs["results"]:
for subsearch in subscription:
@@ -369,31 +369,31 @@ def main():
(key, value) = subsearch.split("=")
if value in sub[key]:
subscription_remove(sat_url, user, password, host, sub, verify)
result.append([host["name"], "Removed: {}".format(sub["name"])])
result.append([host["name"], "Removed: {0}".format(sub["name"])])
changed = True
else:
# only match on name if no key is given
if subsearch in sub["name"]:
subscription_remove(sat_url, user, password, host, sub, verify)
result.append([host["name"], "Removed: {}".format(sub["name"])])
result.append([host["name"], "Removed: {0}".format(sub["name"])])
changed = True

elif state == "vdcguests":
# loop over hypervisors
for host in hosts:
# request detailed info for host
apicall = "{}/api/hosts/{}".format(sat_url, host["id"])
apicall = "{0}/api/hosts/{1}".format(sat_url, host["id"])
host_info = request(apicall, user, password, verify, False)
# loop over guests on hypervisor
for guest in host_info["subscription_facet_attributes"]["virtual_guests"]:
# get existing guest subscriptions
apicall = "{}/api/hosts/{}/subscriptions".format(sat_url, guest["id"])
apicall = "{0}/api/hosts/{1}/subscriptions".format(sat_url, guest["id"])
guest_subs = request(apicall, user, password, verify)
# remove existing Red Hat subscriptions, except the ones from the hypervisor
for sub in guest_subs["results"]:
if "hypervisor" not in sub and sub["account_number"]:
subscription_remove(sat_url, user, password, guest, sub, verify)
result.append([guest["name"], "removed: {}".format(sub["name"])])
result.append([guest["name"], "removed: {0}".format(sub["name"])])
# run autoattach
subscription_autoattach(sat_url, user, password, guest, verify)

@@ -405,7 +405,7 @@ def main():
continue
else:
# get existing subscriptions on host
apicall = "{}/api/hosts/{}/subscriptions".format(sat_url, host["id"])
apicall = "{0}/api/hosts/{1}/subscriptions".format(sat_url, host["id"])
hostsubs = request(apicall, user, password, verify)
for sub in hostsubs["results"]:
# remove subscription if it's using an entitlment per socket
@@ -429,7 +429,7 @@ def main():
module.fail_json(msg=result)
# remove the original subscription
subscription_remove(sat_url, user, password, host, sub, verify)
result.append([host["name"], "Removed: {} {}".format(sub["quantity_consumed"], sub["name"])])
result.append([host["name"], "Removed: {0} {1}".format(sub["quantity_consumed"], sub["name"])])
changed = True
# skip to next host
break
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.