-
Notifications
You must be signed in to change notification settings - Fork 120
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add EnvironmentService class to list environments
Address other PR feedback
- Loading branch information
Showing
5 changed files
with
184 additions
and
61 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
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,55 @@ | ||
# Copyright (c) 2017 Red Hat, Inc. | ||
# | ||
# This software is licensed to you under the GNU General Public License, | ||
# version 2 (GPLv2). There is NO WARRANTY for this software, express or | ||
# implied, including the implied warranties of MERCHANTABILITY or FITNESS | ||
# FOR A PARTICULAR PURPOSE. You should have received a copy of GPLv2 | ||
# along with this software; if not, see | ||
# http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt. | ||
# | ||
# Red Hat trademarks are not licensed under GPLv2. No permission is | ||
# granted to use or replicate Red Hat trademarks that are incorporated | ||
# in this software or its documentation. | ||
import logging | ||
from typing import List | ||
|
||
|
||
from rhsm.connection import UEPConnection | ||
|
||
from subscription_manager import injection as inj | ||
|
||
log = logging.getLogger(__name__) | ||
|
||
|
||
class EnvironmentService: | ||
""" | ||
Class for listing environments | ||
""" | ||
|
||
def __init__(self, cp: UEPConnection) -> None: | ||
""" | ||
Initialization of EnvironmentService instance | ||
:param cp: instance of connection? | ||
""" | ||
self.plugin_manager = inj.require(inj.PLUGIN_MANAGER) | ||
self.cp = cp | ||
|
||
def list(self, org_id: str, typed_environments: bool = True) -> List[dict]: | ||
""" | ||
Method for listing environments | ||
:param org_id: organization to list environments for | ||
:param typed_environments: Whether output should include typed environments | ||
:return: List of environments. | ||
""" | ||
|
||
if typed_environments: | ||
has_typed_environments = self.cp.has_capability("typed_environments") | ||
|
||
if not has_typed_environments: | ||
log.debug("candlepin does not have typed_environments capability") | ||
|
||
environments = self.cp.getEnvironmentList(org_id, list_all=has_typed_environments) | ||
else: | ||
environments = self.cp.getEnvironmentList(org_id) | ||
|
||
return environments |
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,98 @@ | ||
# Copyright (c) 2017 Red Hat, Inc. | ||
# | ||
# This software is licensed to you under the GNU General Public License, | ||
# version 2 (GPLv2). There is NO WARRANTY for this software, express or | ||
# implied, including the implied warranties of MERCHANTABILITY or FITNESS | ||
# FOR A PARTICULAR PURPOSE. You should have received a copy of GPLv2 | ||
# along with this software; if not, see | ||
# http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt. | ||
# | ||
# Red Hat trademarks are not licensed under GPLv2. No permission is | ||
# granted to use or replicate Red Hat trademarks that are incorporated | ||
# in this software or its documentation. | ||
from unittest import mock | ||
|
||
from test.rhsmlib.base import InjectionMockingTest | ||
|
||
from rhsm import connection | ||
|
||
from rhsmlib.services import environment | ||
|
||
|
||
ENVIRONMENTS_JSON = [ | ||
{ | ||
"created": "2024-10-03T18:12:56+0000", | ||
"updated": "2024-10-03T18:12:56+0000", | ||
"id": "8bdf14cf9e534119a1fe617c03304768", | ||
"name": "template 1", | ||
"type": "content-template", | ||
"description": "my template", | ||
"owner": { | ||
"id": "8ad980939253781c01925378340e0002", | ||
"key": "content-sources-test", | ||
"displayName": "ContentSourcesTest", | ||
"href": "/owners/content-sources-test", | ||
"contentAccessMode": "org_environment" | ||
}, | ||
"environmentContent": [ | ||
{ | ||
"contentId": "11055", | ||
"enabled": True | ||
}, | ||
{ | ||
"contentId": "56a3a98c76ea4e16bd68424a2c9cc1c1", | ||
"enabled": True | ||
}, | ||
{ | ||
"contentId": "11049", | ||
"enabled": True | ||
}, | ||
] | ||
}, | ||
{ | ||
"created": "2024-10-09T19:08:14+0000", | ||
"updated": "2024-10-09T19:08:14+0000", | ||
"id": "6c62889601be41128fe2fece53141fd4", | ||
"name": "template 2", | ||
"type": "content-template", | ||
"description": "my template", | ||
"owner": { | ||
"id": "8ad980939253781c01925378340e0002", | ||
"key": "content-sources-test", | ||
"displayName": "ContentSourcesTest", | ||
"href": "/owners/content-sources-test", | ||
"contentAccessMode": "org_environment" | ||
}, | ||
"environmentContent": [ | ||
{ | ||
"contentId": "11055", | ||
"enabled": True | ||
}, | ||
{ | ||
"contentId": "11049", | ||
"enabled": True | ||
}, | ||
] | ||
} | ||
] | ||
|
||
|
||
class TestEnvironmentService(InjectionMockingTest): | ||
def setUp(self): | ||
super(TestEnvironmentService, self).setUp() | ||
self.mock_cp = mock.Mock(spec=connection.UEPConnection, name="UEPConnection") | ||
|
||
def injection_definitions(self, *args, **kwargs): | ||
return None | ||
|
||
def test_list_environments(self): | ||
self.mock_cp.getEnvironmentList.return_value = ENVIRONMENTS_JSON | ||
|
||
result = environment.EnvironmentService(self.mock_cp).list("org") | ||
self.assertEqual(ENVIRONMENTS_JSON, result) | ||
|
||
def test_list_environments_without_typed_environments(self): | ||
self.mock_cp.getEnvironmentList.return_value = ENVIRONMENTS_JSON | ||
|
||
result = environment.EnvironmentService(self.mock_cp).list("org", typed_environments=False) | ||
self.assertEqual(ENVIRONMENTS_JSON, result) |