Repository: cloud-custodian/cloud-custodian

Issue:
azure - lower az log verbosity when running custodian commands without -v
currently, c7n_azure will log info about the session it's using with normal c7n commands and is much more verbose than what it used to be, moved the log level from info to debug so it's still accessible with the -v flag

```
$ custodian schema network-addr.filters.shield-enabled -v
2018-06-19 09:42:36,028: cli.azure.cli.core:DEBUG Current cloud config:
AzureCloud
2018-06-19 09:42:36,029: custodian.azure.session:DEBUG Creating session with Azure CLI Authentication
2018-06-19 09:42:36,029: custodian.azure.session:DEBUG Session using Subscription ID: xxxxxxxxxxxxxxxxxxxxxxxxxxx

Help
----

The most base type

Schema
------

{
    "additionalProperties": false,
    "required": [
        "type"
    ],
    "type": "object",
    "properties": {
        "state": {
            "type": "boolean"
        },
        "type": {
            "enum": [
                "shield-enabled"
            ]
        }
    }
}
```


Discussion:
Concern was that with the CLI functionality with automatic auth to multiple subscriptions that users would be error prone without seeing it, but if there are downsides to the logging we can hide it or I can refactor to make a single compact logline.    Perhaps I could also find a way to prevent it from logging on future session creation in the middle of an operation.
@stefangordon i'll close out this PR as the lazy init in #2561 should solve this issue

Resolving patch:
diff --git a/tools/c7n_azure/c7n_azure/session.py b/tools/c7n_azure/c7n_azure/session.py
--- a/tools/c7n_azure/c7n_azure/session.py
+++ b/tools/c7n_azure/c7n_azure/session.py
@@ -24,6 +24,19 @@
 class Session(object):
 
     def __init__(self, subscription_id=None):
+        """
+        :param subscription_id: If provided overrides environment variables.
+
+        """
+
+        self.log = logging.getLogger('custodian.azure.session')
+        self._provider_cache = {}
+        self.subscription_id_override = subscription_id
+        self.credentials = None
+        self.subscription_id = None
+        self.tenant_id = None
+
+    def _initialize_session(self):
         """
         Creates a session using available authentication type.
 
@@ -32,11 +45,11 @@ def __init__(self, subscription_id=None):
         2. Tenant Auth
         3. Azure CLI Auth
 
-        :param subscription_id: If provided, overrides environment variables.
         """
 
-        self.log = logging.getLogger('custodian.azure.session')
-        self._provider_cache = {}
+        # Only run once
+        if self.credentials is not None:
+            return
 
         tenant_auth_variables = [
             'AZURE_TENANT_ID', 'AZURE_SUBSCRIPTION_ID',
@@ -73,8 +86,8 @@ def __init__(self, subscription_id=None):
             self.log.info("Creating session with Azure CLI Authentication")
 
         # Let provided id parameter override everything else
-        if subscription_id is not None:
-            self.subscription_id = subscription_id
+        if self.subscription_id_override is not None:
+            self.subscription_id = self.subscription_id_override
 
         self.log.info("Session using Subscription ID: %s" % self.subscription_id)
 
@@ -82,6 +95,7 @@ def __init__(self, subscription_id=None):
             self.log.error('Unable to locate credentials for Azure session.')
 
     def client(self, client):
+        self._initialize_session()
         service_name, client_name = client.rsplit('.', 1)
         svc_module = importlib.import_module(service_name)
         klass = getattr(svc_module, client_name)


Test patch: