def source_type(self): return self.data.get('source', 'describe-azure') def resources(self, query=None): key = self.get_cache_key(query) resources = self.augment(self.source.get_resources(query)) self._cache.save(key, resources) return self.filter_resources(resources) def get_resources(self, resource_ids, **params): resource_client = self.get_client() m = self.resource_type get_client, get_op, extra_args = m.get_spec if extra_args: params.update(extra_args) op = getattr(getattr(resource_client, get_client), get_op) data = [op(rid, **params) for rid in resource_ids] return [r.serialize(True) for r in data] @staticmethod def register_actions_and_filters(registry, _): for resource in registry.keys(): klass = registry.get(resource) klass.action_registry.register('notify', Notify) resources.subscribe(resources.EVENT_FINAL, QueryResourceManager.register_actions_and_filters)
) def augment(self, resources): for resource in resources: if 'id' in resource: resource['resourceGroup'] = ResourceIdParser.get_resource_group(resource['id']) return resources def get_resources(self, resource_ids): resource_client = self.get_client('azure.mgmt.resource.ResourceManagementClient') session = local_session(self.session_factory) data = [ resource_client.resources.get_by_id(rid, session.resource_api_version(rid)) for rid in resource_ids ] return [r.serialize(True) for r in data] @staticmethod def register_arm_specific(registry, _): for resource in registry.keys(): klass = registry.get(resource) if issubclass(klass, ArmResourceManager): klass.action_registry.register('tag', Tag) klass.action_registry.register('untag', RemoveTag) klass.action_registry.register('auto-tag-user', AutoTagUser) klass.action_registry.register('tag-trim', TagTrim) klass.filter_registry.register('metric', MetricFilter) resources.subscribe(resources.EVENT_FINAL, ArmResourceManager.register_arm_specific)
resource_class.action_registry.register('mark-for-op', TagDelayedAction) if resource_class.type != 'armresource': resource_class.filter_registry.register('cost', CostFilter) resource_class.filter_registry.register('metric', MetricFilter) resource_class.filter_registry.register('policy-compliant', PolicyCompliantFilter) resource_class.filter_registry.register('resource-lock', ResourceLockFilter) resource_class.action_registry.register('lock', LockAction) resource_class.filter_registry.register('offhour', AzureOffHour) resource_class.filter_registry.register('onhour', AzureOnHour) resource_class.action_registry.register('delete', DeleteAction) if resource_class.resource_type.diagnostic_settings_enabled: resource_class.filter_registry.register('diagnostic-settings', DiagnosticSettingsFilter) class ChildArmResourceManager(ChildResourceManager, ArmResourceManager, metaclass=QueryMeta): class resource_type(ChildTypeInfo, ArmTypeInfo): pass resources.subscribe(ArmResourceManager.register_arm_specific)
key: extra_args[key](parent_resource) for key in extra_args.keys() }) params.update(type_info.extra_args(parent_resource)) # Some resources might not have enum_op piece (non-arm resources) if enum_op: op = getattr(getattr(client, enum_op), list_op) else: op = getattr(client, list_op) return [r.serialize(True) for r in op(**params)] @staticmethod def register_child_specific(registry, _): for resource in registry.keys(): klass = registry.get(resource) if issubclass(klass, ChildResourceManager): # If Child Resource doesn't annotate parent, there is no way to filter based on # parent properties. if klass.resource_type.annotate_parent: klass.filter_registry.register('parent', ParentFilter) resources.subscribe(resources.EVENT_FINAL, QueryResourceManager.register_actions_and_filters) resources.subscribe(resources.EVENT_FINAL, ChildResourceManager.register_child_specific)
@resources.register('appserviceplan') class AppServicePlan(ArmResourceManager): class resource_type(ArmResourceManager.resource_type): service = 'azure.mgmt.web' client = 'WebSiteManagementClient' enum_spec = ('app_service_plans', 'list', None) default_report_fields = ('name', 'location', 'resourceGroup', 'kind') @staticmethod def register(registry, _): # Additional filters/actions registered for this resource type AppServicePlan.filter_registry.register("offhour", AzureOffHour) AppServicePlan.filter_registry.register("onhour", AzureOnHour) resources.subscribe(resources.EVENT_FINAL, AppServicePlan.register) @AppServicePlan.action_registry.register('resize-plan') class ResizePlan(AzureBaseAction): """Resize App Service Plans .. code-block:: yaml policies: - name: azure-resize-plan resource: azure.appserviceplan actions: - type: resize-plan size: F1 # F1, D1, S1, S2, S3, P1, P2, P3 count: 1
# Some resources might not have enum_op piece (non-arm resources) if enum_op: op = getattr(getattr(client, enum_op), list_op) else: op = getattr(client, list_op) result = op(**params) if isinstance(result, Iterable): return [r.serialize(True) for r in result] elif hasattr(result, 'value'): return [r.serialize(True) for r in result.value] raise TypeError("Enumerating resources resulted in a return" "value which could not be iterated.") @staticmethod def register_child_specific(registry, resource_class): if not issubclass(resource_class, ChildResourceManager): return # If Child Resource doesn't annotate parent, there is no way to filter based on # parent properties. if resource_class.resource_type.annotate_parent: resource_class.filter_registry.register('parent', ParentFilter) resources.subscribe(QueryResourceManager.register_actions_and_filters) resources.subscribe(ChildResourceManager.register_child_specific)
default_report_fields = ( 'name', 'location', 'resourceGroup', 'properties.hardwareProfile.vmSize', ) resource_type = 'Microsoft.Compute/virtualMachines' @staticmethod def register(registry, _): # Additional filters/actions registered for this resource type VirtualMachine.filter_registry.register("offhour", AzureOffHour) VirtualMachine.filter_registry.register("onhour", AzureOnHour) resources.subscribe(resources.EVENT_FINAL, VirtualMachine.register) @VirtualMachine.filter_registry.register('instance-view') class InstanceViewFilter(ValueFilter): schema = type_schema('instance-view', rinherit=ValueFilter.schema) schema_alias = True def __call__(self, i): if 'instanceView' not in i: client = self.manager.get_client() instance = ( client.virtual_machines .get(i['resourceGroup'], i['name'], expand='instanceview') .instance_view )
class resource_type(object): service = 'azure.mgmt.resource' client = 'ResourceManagementClient' enum_spec = ('resources', 'list') id = 'id' name = 'name' default_report_fields = ( 'name', 'location', 'resourceGroup' ) def augment(self, resources): for resource in resources: if 'id' in resource: resource['resourceGroup'] = ResourceIdParser.get_resource_group(resource['id']) return resources @staticmethod def register_arm_specific(registry, _): for resource in registry.keys(): klass = registry.get(resource) if issubclass(klass, ArmResourceManager): klass.action_registry.register('tag', Tag) klass.action_registry.register('untag', RemoveTag) klass.action_registry.register('auto-tag-user', AutoTagUser) klass.action_registry.register('tag-trim', TagTrim) resources.subscribe(resources.EVENT_FINAL, ArmResourceManager.register_arm_specific)
if first_op is not None: user = first_op.caller # issue tag action to label user try: tag_action({'tag': tag_key, 'value': user}, self.manager).process([resource]) except CloudError as e: # resources can be locked if e.inner_exception.error == 'ScopeLocked': pass @staticmethod def get_first_operation(logs, operation_name): first_operation = None for l in logs: if l.operation_name.value == operation_name: first_operation = l return first_operation @staticmethod def add_auto_tag_user(registry, _): for resource in registry.keys(): klass = registry.get(resource) if klass.action_registry.get('tag') and not klass.action_registry.get('auto-tag-user'): klass.action_registry.register('auto-tag-user', AutoTagUser) # Add the AutoTagUser action to all resources that support tagging resources.subscribe(resources.EVENT_FINAL, AutoTagUser.add_auto_tag_user)
enum_spec = ('app_service_plans', 'list', None) default_report_fields = ( 'name', 'location', 'resourceGroup', 'kind' ) @staticmethod def register(registry, _): # Additional filters/actions registered for this resource type AppServicePlan.filter_registry.register("offhour", AzureOffHour) AppServicePlan.filter_registry.register("onhour", AzureOnHour) resources.subscribe(resources.EVENT_FINAL, AppServicePlan.register) @AppServicePlan.action_registry.register('resize-plan') class ResizePlan(AzureBaseAction): """Resize App Service Plans .. code-block:: yaml policies: - name: azure-resize-plan resource: azure.appserviceplan actions: - type: resize-plan size: F1 count: 1
diagnostic_settings_enabled = False default_report_fields = ( 'name', 'location', 'resourceGroup', 'properties.hardwareProfile.vmSize', ) @staticmethod def register(registry, _): # Additional filters/actions registered for this resource type VirtualMachine.filter_registry.register("offhour", AzureOffHour) VirtualMachine.filter_registry.register("onhour", AzureOnHour) resources.subscribe(resources.EVENT_FINAL, VirtualMachine.register) @VirtualMachine.filter_registry.register('instance-view') class InstanceViewFilter(ValueFilter): schema = type_schema('instance-view', rinherit=ValueFilter.schema) def __call__(self, i): if 'instanceView' not in i: client = self.manager.get_client() instance = ( client.virtual_machines .get(i['resourceGroup'], i['name'], expand='instanceview') .instance_view ) i['instanceView'] = instance.serialize()
try: tag_action({ 'tag': tag_key, 'value': user }, self.manager).process([resource]) except CloudError as e: # resources can be locked if e.inner_exception.error == 'ScopeLocked': pass @staticmethod def get_first_operation(logs, operation_name): first_operation = None for l in logs: if l.operation_name.value == operation_name: first_operation = l return first_operation @staticmethod def add_auto_tag_user(registry, _): for resource in registry.keys(): klass = registry.get(resource) if klass.action_registry.get( 'tag') and not klass.action_registry.get('auto-tag-user'): klass.action_registry.register('auto-tag-user', AutoTagUser) # Add the AutoTagUser action to all resources that support tagging resources.subscribe(resources.EVENT_FINAL, AutoTagUser.add_auto_tag_user)
def resources(self, query=None): key = self.get_cache_key(query) resources = self.augment(self.source.get_resources(query)) self._cache.save(key, resources) return self.filter_resources(resources) def get_resources(self, resource_ids, **params): resource_client = self.get_client() m = self.resource_type get_client, get_op, extra_args = m.get_spec if extra_args: params.update(extra_args) op = getattr(getattr(resource_client, get_client), get_op) data = [ op(rid, **params) for rid in resource_ids ] return [r.serialize(True) for r in data] @staticmethod def register_actions_and_filters(registry, _): for resource in registry.keys(): klass = registry.get(resource) klass.action_registry.register('notify', Notify) resources.subscribe(resources.EVENT_FINAL, QueryResourceManager.register_actions_and_filters)