|
1 | 1 | import logging |
2 | 2 |
|
3 | | -from ..common import union, parse_base_object_ref, ApplicationBackupSet, Object |
| 3 | +from ..common import union, parse_base_object_ref, ApplicationBackupSet, PolicyRuleConverter, Object |
4 | 4 | from ..exception import CTERAException |
5 | 5 | from .base_command import BaseCommand |
6 | 6 | from . import query |
@@ -113,15 +113,35 @@ def _create_template_firmware(platform, base_object_ref): |
113 | 113 | param.firmware = base_object_ref |
114 | 114 | return param |
115 | 115 |
|
116 | | - def list_templates(self, include=None): |
| 116 | + def by_name(self, names, include=None): |
| 117 | + """ |
| 118 | + Get Templates by their names |
| 119 | +
|
| 120 | + :param list[str],optional names: List of names of templates |
| 121 | + :param list[str],optional include: List of fields to retrieve, defaults to ['name'] |
| 122 | + :param list[cterasdk.core.query.FilterBuilder],optional filters: List of additional filters, defaults to None |
| 123 | +
|
| 124 | + :return: Iterator for all matching Templates |
| 125 | + :rtype: cterasdk.lib.iterator.Iterator |
| 126 | + """ |
| 127 | + filters = [query.FilterBuilder('name').eq(name) for name in names] |
| 128 | + return self.list_templates(include, filters) |
| 129 | + |
| 130 | + def list_templates(self, include=None, filters=None): |
117 | 131 | """ |
118 | 132 | List Configuration Templates.\n |
119 | 133 | To retrieve templates, you must first browse the tenant, using: `GlobalAdmin.portals.browse()` |
120 | 134 |
|
121 | 135 | :param list[str],optional include: List of fields to retrieve, defaults to ``['name']`` |
| 136 | + :param list[],optional filters: List of additional filters, defaults to None |
122 | 137 | """ |
123 | 138 | include = union(include or [], Templates.default) |
124 | | - param = query.QueryParamBuilder().include(include).build() |
| 139 | + builder = query.QueryParamBuilder().include(include) |
| 140 | + if filters: |
| 141 | + for query_filter in filters: |
| 142 | + builder.addFilter(query_filter) |
| 143 | + builder.orFilter((len(filters) > 1)) |
| 144 | + param = builder.build() |
125 | 145 | return query.iterator(self._portal, '/deviceTemplates', param) |
126 | 146 |
|
127 | 147 | def delete(self, name): |
@@ -168,6 +188,51 @@ def remove_default(self, name, wait=False): |
168 | 188 |
|
169 | 189 | class TemplateAutoAssignPolicy(BaseCommand): |
170 | 190 |
|
| 191 | + def get_policy(self): |
| 192 | + """ |
| 193 | + Get templates auto assignment policy |
| 194 | + """ |
| 195 | + return self._portal.execute('', 'getAutoAssignmentRules') |
| 196 | + |
| 197 | + def set_policy(self, rules, apply_default=None, default=None, apply_changes=True): |
| 198 | + """ |
| 199 | + Set templates auto assignment policy |
| 200 | +
|
| 201 | + :param list[cterasdk.common.types.PolicyRule] rules: List of policy rules |
| 202 | + :param bool,optional apply_default: If no match found, apply default template. If not passed, the current config will be kept |
| 203 | + :param str,optional default: Name of a template to assign if no match found. Ignored unless the ``apply_default`` is set to ``True`` |
| 204 | + :param bool,optional apply_changes: Apply changes upon update, defaults to ``True`` |
| 205 | + """ |
| 206 | + templates = {rule.assignment for rule in rules} |
| 207 | + if default: |
| 208 | + templates.add(default) |
| 209 | + templates = list(templates) |
| 210 | + portal_templates = {template.name: template for template in self._portal.templates.by_name(templates, ['baseObjectRef'])} |
| 211 | + |
| 212 | + not_found = [template for template in templates if template not in portal_templates.keys()] |
| 213 | + if not_found: |
| 214 | + logging.getLogger().error('Could not find one or more templates. %s', {'templates': not_found}) |
| 215 | + raise CTERAException('Could not find one or more templates', None, templates=not_found) |
| 216 | + |
| 217 | + policy = self.get_policy() |
| 218 | + |
| 219 | + if apply_default is False: |
| 220 | + policy.defaultTemplate = None |
| 221 | + elif apply_default is True and default: |
| 222 | + policy.defaultTemplate = portal_templates.get(default).baseObjectRef |
| 223 | + |
| 224 | + policy_rules = [PolicyRuleConverter.convert(rule, 'DeviceTemplateAutoAssignmentRule', 'template', |
| 225 | + portal_templates.get(rule.assignment).baseObjectRef) for rule in rules] |
| 226 | + policy.deviceTemplatesAutoAssignmentRules = policy_rules |
| 227 | + |
| 228 | + response = self._portal.execute('', 'setAutoAssignmentRules', policy) |
| 229 | + logging.getLogger().info('Set templates auto assignment rules.') |
| 230 | + |
| 231 | + if apply_changes: |
| 232 | + self.apply_changes(True) |
| 233 | + |
| 234 | + return response |
| 235 | + |
171 | 236 | def apply_changes(self, wait=False): |
172 | 237 | """ |
173 | 238 | Apply provisioning changes.\n |
|
0 commit comments