-
Notifications
You must be signed in to change notification settings - Fork 75
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds an api endpoint which returns list of active notices in the machine
- Loading branch information
Showing
8 changed files
with
191 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
Feature: Status notices api | ||
|
||
@uses.config.contract_token | ||
Scenario Outline: Check notices returned by status api | ||
Given a `<release>` `<machine_type>` machine with ubuntu-advantage-tools installed | ||
When I create the file `/tmp/response-overlay.json` with the following: | ||
""" | ||
{ | ||
"https://contracts.canonical.com/v1/context/machines/token": [ | ||
{ | ||
"code": 200, | ||
"response": { | ||
"machineTokenInfo": { | ||
"accountInfo": { | ||
"name": "testName", | ||
"id": "testAccID" | ||
}, | ||
"contractInfo": { | ||
"id": "testCID", | ||
"name": "testName", | ||
"resourceEntitlements": [ | ||
{ | ||
"type": "support", | ||
"affordances": { | ||
"onlySeries": "jammy" | ||
} | ||
} | ||
] | ||
}, | ||
"machineId": "testMID" | ||
} | ||
} | ||
}], | ||
"https://contracts.canonical.com/v1/contracts/testCID/context/machines/testMID": [ | ||
{ | ||
"code": 200, | ||
"response": { | ||
"activityToken": "test-activity-token", | ||
"activityID": "test-activity-id", | ||
"activityPingInterval": 123456789 | ||
} | ||
}], | ||
"https://contracts.canonical.com/v1/contracts/testCID/machine-activity/testMID": [ | ||
{ | ||
"code": 200, | ||
"response": { | ||
"activityToken": "test-activity-token", | ||
"activityID": "test-activity-id", | ||
"activityPingInterval": 123456789 | ||
} | ||
}] | ||
} | ||
""" | ||
And I append the following on uaclient config: | ||
""" | ||
features: | ||
serviceclient_url_responses: "/tmp/response-overlay.json" | ||
""" | ||
When I attach `contract_token` with sudo | ||
Then the machine is attached | ||
When I run `pro api u.pro.status.notices.v1` with sudo | ||
Then API data field output matches regexp: | ||
""" | ||
{ | ||
"attributes": { | ||
"notices": [ | ||
{ | ||
"label": "contract_expired", | ||
"message": ".*", | ||
"order_id": "5" | ||
}, | ||
{ | ||
"label": "limited_to_release", | ||
"message": ".*", | ||
"order_id": "80" | ||
} | ||
] | ||
}, | ||
"meta": { | ||
"environment_vars": [] | ||
}, | ||
"type": "NoticesList" | ||
} | ||
""" | ||
|
||
Examples: ubuntu release | ||
| release | machine_type | | ||
| xenial | lxd-container | | ||
| bionic | lxd-container | | ||
| focal | lxd-container | | ||
| jammy | lxd-container | | ||
| noble | lxd-container | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import logging | ||
from typing import List | ||
|
||
from uaclient.api.api import APIEndpoint | ||
from uaclient.api.data_types import AdditionalInfo | ||
from uaclient.config import UAConfig | ||
from uaclient.data_types import DataObject, Field, StringDataValue, data_list | ||
from uaclient.files.notices import Notice, NoticesManager | ||
|
||
LOG = logging.getLogger("ubuntupro.lib.auto_attach") | ||
|
||
|
||
class NoticeInfo(DataObject): | ||
fields = [ | ||
Field("order_id", StringDataValue, doc="Notice order id"), | ||
Field( | ||
"message", | ||
StringDataValue, | ||
doc="Message to be displayed by the notice", | ||
), | ||
Field("label", StringDataValue, doc="Notice label"), | ||
] | ||
|
||
def __init__(self, order_id: str, message: str, label: str): | ||
self.order_id = order_id | ||
self.message = message | ||
self.label = label | ||
|
||
|
||
class NoticeListResult(DataObject, AdditionalInfo): | ||
fields = [ | ||
Field( | ||
"notices", | ||
data_list(NoticeInfo), | ||
doc="A list of ``Notice`` objects", | ||
), | ||
] | ||
|
||
def __init__(self, notices: List[NoticeInfo]): | ||
self.notices = notices | ||
|
||
|
||
def notice_list() -> NoticeListResult: | ||
return _get_notice_list(cfg=UAConfig()) | ||
|
||
|
||
def _get_notice_list(cfg: UAConfig) -> NoticeListResult: | ||
_notice_cls = NoticesManager() | ||
noticeList = _notice_cls.get_active_notices() | ||
notices = [] | ||
for notice in noticeList: | ||
for n in Notice: | ||
if notice.order_id == n.order_id: | ||
notices.append( | ||
NoticeInfo( | ||
order_id=n.order_id, | ||
message=notice.message, | ||
label=n.label, | ||
) | ||
) | ||
notices.sort(key=lambda x: x.order_id) | ||
return NoticeListResult(notices=notices) | ||
|
||
|
||
endpoint = APIEndpoint( | ||
version="v1", | ||
name="NoticesList", | ||
fn=_get_notice_list, | ||
options_cls=None, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters