Skip to content

Commit

Permalink
Added new Managemant clinet function which calls (#119)
Browse files Browse the repository at this point in the history
Added new Managemant client function which calls WebSiteManagementClient

Signed-off-by: Hemant <prasadsai.ahk@gmail.com>
  • Loading branch information
HemantAHK authored Oct 20, 2020
1 parent 1ed41aa commit 82cf883
Show file tree
Hide file tree
Showing 9 changed files with 62 additions and 29 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,14 @@
## [Unreleased][]
[Unreleased]: https://github.com/chaostoolkit-incubator/chaostoolkit-azure/compare/0.8.3...HEAD

### Added

- Individual Azure managemnt clients for website and compute resources

### Removed

- Removed common init_client

## [0.8.3][] - 2020-05-14

[0.8.3]: https://github.com/chaostoolkit-incubator/chaostoolkit-azure/compare/0.8.2...0.8.3
Expand Down
35 changes: 30 additions & 5 deletions chaosazure/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from typing import List

from azure.mgmt.compute import ComputeManagementClient
from azure.mgmt.web import WebSiteManagementClient
from azure.mgmt.resourcegraph import ResourceGraphClient
from chaoslib.discovery import (discover_actions, discover_probes,
initialize_discovery_result)
Expand All @@ -16,8 +17,8 @@
from chaosazure.common.config import load_configuration, load_secrets

__all__ = [
"discover", "__version__", "init_client",
"init_resource_graph_client"
"discover", "__version__", "init_compute_management_client",
"init_website_management_client", "init_resource_graph_client"
]
__version__ = '0.8.3'

Expand All @@ -34,10 +35,13 @@ def discover(discover_system: bool = True) -> Discovery:
return discovery


def init_client(
def init_compute_management_client(
experiment_secrets: Secrets,
experiment_configuration: Configuration) -> ComputeManagementClient:

"""
Initializes Compute management client for virtual machine,
and virtual machine scale sets resources under Azure Resource manager.
"""
secrets = load_secrets(experiment_secrets)
configuration = load_configuration(experiment_configuration)
with auth(secrets) as authentication:
Expand All @@ -50,9 +54,30 @@ def init_client(
return client


def init_website_management_client(
experiment_secrets: Secrets,
experiment_configuration: Configuration) -> WebSiteManagementClient:
"""
Initializes Website management client for webapp resource under Azure
Resource manager.
"""
secrets = load_secrets(experiment_secrets)
configuration = load_configuration(experiment_configuration)
with auth(secrets) as authentication:
base_url = secrets.get('cloud').endpoints.resource_manager
client = WebSiteManagementClient(
credentials=authentication,
subscription_id=configuration.get('subscription_id'),
base_url=base_url)

return client


def init_resource_graph_client(
experiment_secrets: Secrets) -> ResourceGraphClient:

"""
Initializes Resource Graph client.
"""
secrets = load_secrets(experiment_secrets)
with auth(secrets) as authentication:
base_url = secrets.get('cloud').endpoints.resource_manager
Expand Down
4 changes: 2 additions & 2 deletions chaosazure/common/compute/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from chaoslib.exceptions import FailedActivity, InterruptExecution
from logzero import logger

from chaosazure import init_client
from chaosazure import init_compute_management_client
from chaosazure.machine.constants import OS_LINUX, OS_WINDOWS, RES_TYPE_VM
from chaosazure.vmss.constants import RES_TYPE_VMSS_VM

Expand Down Expand Up @@ -41,7 +41,7 @@ def prepare(compute: dict, script: str):

def run(resource_group: str, compute: dict, timeout: int, parameters: dict,
secrets, configuration):
client = init_client(secrets, configuration)
client = init_compute_management_client(secrets, configuration)

compute_type = compute.get('type').lower()
if compute_type == RES_TYPE_VMSS_VM.lower():
Expand Down
4 changes: 2 additions & 2 deletions chaosazure/machine/actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from chaoslib.types import Configuration, Secrets
from logzero import logger

from chaosazure import init_client
from chaosazure import init_compute_management_client
from chaosazure.common import cleanse
from chaosazure.common.compute import command
from chaosazure.machine.constants import RES_TYPE_VM
Expand Down Expand Up @@ -521,4 +521,4 @@ def __fetch_machines(filter, configuration, secrets) -> []:


def __compute_mgmt_client(secrets, configuration):
return init_client(secrets, configuration)
return init_compute_management_client(secrets, configuration)
10 changes: 5 additions & 5 deletions chaosazure/vmss/actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from chaoslib import Configuration, Secrets
from logzero import logger

from chaosazure import init_client
from chaosazure import init_compute_management_client
from chaosazure.common import cleanse
from chaosazure.common.compute import command
from chaosazure.vmss.fetcher import fetch_vmss, fetch_instances
Expand Down Expand Up @@ -48,7 +48,7 @@ def delete_vmss(filter: str = None,
for instance in instances:
logger.debug(
"Deleting instance: {}".format(instance['name']))
client = init_client(secrets, configuration)
client = init_compute_management_client(secrets, configuration)
client.virtual_machine_scale_set_vms.delete(
scale_set['resourceGroup'],
scale_set['name'],
Expand Down Expand Up @@ -90,7 +90,7 @@ def restart_vmss(filter: str = None,
for instance in instances:
logger.debug(
"Restarting instance: {}".format(instance['name']))
client = init_client(secrets, configuration)
client = init_compute_management_client(secrets, configuration)
client.virtual_machine_scale_set_vms.restart(
scale_set['resourceGroup'],
scale_set['name'],
Expand Down Expand Up @@ -154,7 +154,7 @@ def stop_vmss(filter: str = None,
for instance in instances:
logger.debug(
"Stopping instance: {}".format(instance['name']))
client = init_client(secrets, configuration)
client = init_compute_management_client(secrets, configuration)
client.virtual_machine_scale_set_vms.power_off(
scale_set['resourceGroup'],
scale_set['name'],
Expand Down Expand Up @@ -196,7 +196,7 @@ def deallocate_vmss(filter: str = None,
for instance in instances:
logger.debug(
"Deallocating instance: {}".format(instance['name']))
client = init_client(secrets, configuration)
client = init_compute_management_client(secrets, configuration)
client.virtual_machine_scale_set_vms.deallocate(
scale_set['resourceGroup'],
scale_set['name'],
Expand Down
4 changes: 2 additions & 2 deletions chaosazure/vmss/fetcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from chaoslib.exceptions import FailedActivity
from logzero import logger

from chaosazure import init_client
from chaosazure import init_compute_management_client
from chaosazure.common.resources.graph import fetch_resources
from chaosazure.vmss.constants import RES_TYPE_VMSS

Expand Down Expand Up @@ -62,7 +62,7 @@ def fetch_vmss(filter, configuration, secrets) -> List[dict]:
#############################################################################
def __fetch_vmss_instances(choice, configuration, secrets) -> List[Dict]:
vmss_instances = []
client = init_client(secrets, configuration)
client = init_compute_management_client(secrets, configuration)
pages = client.virtual_machine_scale_set_vms.list(
choice['resourceGroup'], choice['name'])
first_page = pages.advance_page()
Expand Down
10 changes: 5 additions & 5 deletions chaosazure/webapp/actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from chaoslib.exceptions import FailedActivity
from logzero import logger

from chaosazure import init_client
from chaosazure import init_website_management_client
from chaosazure.common.resources.graph import fetch_resources
from chaosazure.webapp.constants import RES_TYPE_WEBAPP

Expand Down Expand Up @@ -33,7 +33,7 @@ def stop_webapp(filter: str = None,
choice = __fetch_webapp_at_random(filter, configuration, secrets)

logger.debug("Stopping web app: {}".format(choice['name']))
client = init_client(secrets, configuration)
client = init_website_management_client(secrets, configuration)
client.web_apps.stop(choice['resourceGroup'], choice['name'])


Expand All @@ -58,7 +58,7 @@ def restart_webapp(filter: str = None,
choice = __fetch_webapp_at_random(filter, configuration, secrets)

logger.debug("Restarting web app: {}".format(choice['name']))
client = init_client(secrets, configuration)
client = init_website_management_client(secrets, configuration)
client.web_apps.restart(choice['resourceGroup'], choice['name'])


Expand All @@ -83,7 +83,7 @@ def start_webapp(filter: str = None,
choice = __fetch_webapp_at_random(filter, configuration, secrets)

logger.debug("Starting web app: {}".format(choice['name']))
client = init_client(secrets, configuration)
client = init_website_management_client(secrets, configuration)
client.web_apps.start(choice['resourceGroup'], choice['name'])


Expand Down Expand Up @@ -111,7 +111,7 @@ def delete_webapp(filter: str = None,
choice = __fetch_webapp_at_random(filter, configuration, secrets)

logger.debug("Deleting web app: {}".format(choice['name']))
client = init_client(secrets, configuration)
client = init_website_management_client(secrets, configuration)
client.web_apps.delete(choice['resourceGroup'], choice['name'])


Expand Down
8 changes: 4 additions & 4 deletions tests/vmss/test_vmss_actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

@patch('chaosazure.vmss.actions.fetch_vmss', autospec=True)
@patch('chaosazure.vmss.actions.fetch_instances', autospec=True)
@patch('chaosazure.vmss.actions.init_client', autospec=True)
@patch('chaosazure.vmss.actions.init_compute_management_client', autospec=True)
def test_deallocate_vmss(client, fetch_instances, fetch_vmss):
scale_set = vmss_provider.provide_scale_set()
scale_sets = [scale_set]
Expand All @@ -25,7 +25,7 @@ def test_deallocate_vmss(client, fetch_instances, fetch_vmss):

@patch('chaosazure.vmss.actions.fetch_vmss', autospec=True)
@patch('chaosazure.vmss.actions.fetch_instances', autospec=True)
@patch('chaosazure.vmss.actions.init_client', autospec=True)
@patch('chaosazure.vmss.actions.init_compute_management_client', autospec=True)
def test_stop_vmss(client, fetch_instances, fetch_vmss):
scale_set = vmss_provider.provide_scale_set()
scale_sets = [scale_set]
Expand All @@ -41,7 +41,7 @@ def test_stop_vmss(client, fetch_instances, fetch_vmss):

@patch('chaosazure.vmss.actions.fetch_vmss', autospec=True)
@patch('chaosazure.vmss.actions.fetch_instances', autospec=True)
@patch('chaosazure.vmss.actions.init_client', autospec=True)
@patch('chaosazure.vmss.actions.init_compute_management_client', autospec=True)
def test_restart_vmss(client, fetch_instances, fetch_vmss):
scale_set = vmss_provider.provide_scale_set()
scale_sets = [scale_set]
Expand All @@ -57,7 +57,7 @@ def test_restart_vmss(client, fetch_instances, fetch_vmss):

@patch('chaosazure.vmss.actions.fetch_vmss', autospec=True)
@patch('chaosazure.vmss.actions.fetch_instances', autospec=True)
@patch('chaosazure.vmss.actions.init_client', autospec=True)
@patch('chaosazure.vmss.actions.init_compute_management_client', autospec=True)
def test_delete_vmss(client, fetch_instances, fetch_vmss):
scale_set = vmss_provider.provide_scale_set()
scale_sets = [scale_set]
Expand Down
8 changes: 4 additions & 4 deletions tests/webapp/test_webapp_actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@


@patch('chaosazure.webapp.actions.fetch_webapps', autospec=True)
@patch('chaosazure.webapp.actions.init_client', autospec=True)
@patch('chaosazure.webapp.actions.init_website_management_client', autospec=True)
def test_stop_webapp(init, fetch):
client = MagicMock()
init.return_value = client
Expand All @@ -37,7 +37,7 @@ def test_stop_webapp(init, fetch):


@patch('chaosazure.webapp.actions.fetch_webapps', autospec=True)
@patch('chaosazure.webapp.actions.init_client', autospec=True)
@patch('chaosazure.webapp.actions.init_website_management_client', autospec=True)
def test_restart_webapp(init, fetch):
client = MagicMock()
init.return_value = client
Expand All @@ -53,7 +53,7 @@ def test_restart_webapp(init, fetch):


@patch('chaosazure.webapp.actions.fetch_webapps', autospec=True)
@patch('chaosazure.webapp.actions.init_client', autospec=True)
@patch('chaosazure.webapp.actions.init_website_management_client', autospec=True)
def test_start_webapp(init, fetch):
client = MagicMock()
init.return_value = client
Expand All @@ -69,7 +69,7 @@ def test_start_webapp(init, fetch):


@patch('chaosazure.webapp.actions.fetch_webapps', autospec=True)
@patch('chaosazure.webapp.actions.init_client', autospec=True)
@patch('chaosazure.webapp.actions.init_website_management_client', autospec=True)
def test_delete_webapp(init, fetch):
client = MagicMock()
init.return_value = client
Expand Down

0 comments on commit 82cf883

Please sign in to comment.