Skip to content

Commit

Permalink
Removed the old changes as requested in PR
Browse files Browse the repository at this point in the history
  • Loading branch information
mostaffatarek124eru committed Feb 6, 2025
1 parent 7864437 commit 0a9d25b
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 58 deletions.
2 changes: 1 addition & 1 deletion cloudinit/config/cc_rsyslog.py
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ def __str__(self):
else:
buf += self.addr

if self.port is not None:
if self.port:
buf += ":%s" % self.port

if self.name:
Expand Down
101 changes: 44 additions & 57 deletions cloudinit/config/cc_ubuntu_pro.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import json
import logging
import re
from typing import Any, Dict, List, Union
from typing import Any, List
from urllib.parse import urlparse

from cloudinit import performance, subp, util
Expand Down Expand Up @@ -196,69 +196,56 @@ def configure_pro(token, enable=None):

try:
enable_resp = json.loads(enable_stdout)
handle_enable_errors(enable_resp)

except json.JSONDecodeError as e:
raise RuntimeError(
f"Pro response was not json: {enable_stdout}"
) from e

# At this point we were able to load the json response from Pro. This
# response contains a list of errors under the key 'errors'. E.g.
#
# {
# "errors": [
# {
# "message": "UA Apps: ESM is already enabled ...",
# "message_code": "service-already-enabled",
# "service": "esm-apps",
# "type": "service"
# },
# {
# "message": "Cannot enable unknown service 'asdf' ...",
# "message_code": "invalid-service-or-failure",
# "service": null,
# "type": "system"
# }
# ]
# }
#
# From our pov there are two type of errors, service and non-service
# related. We can distinguish them by checking if `service` is non-null
# or null respectively.

enable_errors: List[dict] = []
for error in enable_resp.get("errors", []):
if error["message_code"] == "service-already-enabled":
LOG.debug("Service `%s` already enabled.", error["service"])
continue
enable_errors.append(error)

def handle_enable_errors(enable_resp: Union[List[Any], Dict[str, Any]]):
if isinstance(enable_resp, list):
LOG.warning(
"Unexpected list response from pro enable: %s", enable_resp
)
return
elif isinstance(enable_resp, dict) and "errors" in enable_resp:
# At this point we were able to load the json response from Pro. This
# response contains a list of errors under the key 'errors'. E.g.
#
# {
# "errors": [
# {
# "message": "UA Apps: ESM is already enabled ...",
# "message_code": "service-already-enabled",
# "service": "esm-apps",
# "type": "service"
# },
# {
# "message": "Cannot enable unknown service 'asdf' ...",
# "message_code": "invalid-service-or-failure",
# "service": null,
# "type": "system"
# }
# ]
# }
#
# From our pov there are two type of errors, service and non-service
# related. We can distinguish them by checking if `service` is non-null
# or null respectively.
enable_errors: List[Dict[str, Any]] = []
for err in enable_resp.get("errors", []):
if err["message_code"] == "service-already-enabled":
LOG.debug("Service `%s` already enabled.", err["service"])
continue
enable_errors.append(err)

if enable_errors:
error_services: List[str] = []
for err in enable_errors:
service = err.get("service")
if service is not None:
error_services.append(service)
msg = f'Failure enabling `{service}`: {err["message"]}'
else:
msg = f'Failure of type `{err["type"]}`: {err["message"]}'
util.logexc(LOG, msg)
if enable_errors:
error_services: List[str] = []
for error in enable_errors:
service = error.get("service")
if service is not None:
error_services.append(service)
msg = f'Failure enabling `{service}`: {error["message"]}'
else:
msg = f'Failure of type `{error["type"]}`: {error["message"]}'
util.logexc(LOG, msg)

raise RuntimeError(
"Failure enabling Ubuntu Pro service(s): "
+ ", ".join(error_services)
)
else:
LOG.warning(
"Unexpected response format from pro enable: %s", enable_resp
raise RuntimeError(
"Failure enabling Ubuntu Pro service(s): "
+ ", ".join(error_services)
)


Expand Down

0 comments on commit 0a9d25b

Please sign in to comment.