Skip to content

Fixes AzureRM create_object_model util #56379

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

Merged
merged 8 commits into from
Apr 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion salt/utils/azurearm.py
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ def create_object_model(module_name, object_name, **kwargs):
if "_attribute_map" in dir(Model):
for attr, items in Model._attribute_map.items():
param = kwargs.get(attr)
if param:
if param is not None:
if items["type"][0].isupper() and isinstance(param, dict):
object_kwargs[attr] = create_object_model(
module_name, items["type"], **param
Expand Down
59 changes: 59 additions & 0 deletions tests/unit/utils/test_azurearm.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# -*- coding: utf-8 -*-

# import Python Libs
from __future__ import absolute_import, print_function, unicode_literals

import logging

# Import Salt Libs
import salt.utils.azurearm as azurearm

# Import Salt Testing Libs
from tests.support.unit import TestCase, skipIf

# Azure libs
# pylint: disable=import-error
HAS_LIBS = False
try:
import azure.mgmt.compute.models # pylint: disable=unused-import
import azure.mgmt.network.models # pylint: disable=unused-import

HAS_LIBS = True
except ImportError:
pass

# pylint: enable=import-error

log = logging.getLogger(__name__)

MOCK_CREDENTIALS = {
"client_id": "CLIENT_ID",
"secret": "SECRET",
"subscription_id": "SUBSCRIPTION_ID",
"tenant": "TENANT",
}


@skipIf(HAS_LIBS is False, "The azure.mgmt.network module must be installed.")
class AzureRmUtilsTestCase(TestCase):
def test_create_object_model_vnet(self):
module_name = "network"
object_name = "VirtualNetwork"
vnet = {
"address_space": {"address_prefixes": ["10.0.0.0/8"]},
"enable_ddos_protection": False,
"enable_vm_protection": True,
"tags": {"contact_name": "Elmer Fudd Gantry"},
}
model = azurearm.create_object_model(module_name, object_name, **vnet)
self.assertEqual(vnet, model.as_dict())

def test_create_object_model_nic_ref(self):
module_name = "compute"
object_name = "NetworkInterfaceReference"
ref = {
"id": "/subscriptions/aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa/resourceGroups/rg/providers/Microsoft.Network/networkInterfaces/nic",
"primary": False,
}
model = azurearm.create_object_model(module_name, object_name, **ref)
self.assertEqual(ref, model.as_dict())