Repository: Azure/iotedgedev

Issue:
AZ IOT HUB apply-configuration needs hubname.
If user has old version of az cli iot extension installed they get this: 

`az iot hub apply-configuration: error: argument --hub-name/-n is required
`

- add the -n parameter to the apply-configuration call.  you can get it in IOTHUB_CONNECTION_INFO.HostName

apply-configuration might need ONLY hubname, but HostName has [name].azuredevices.net.

Therefore, You might have to split the ConnectionString.HostName property and add a new property to that class called HubName.


Discussion:

Patch:
diff --git a/iotedgedev/azurecli.py b/iotedgedev/azurecli.py
index c5bce70..6bce331 100644
--- a/iotedgedev/azurecli.py
+++ b/iotedgedev/azurecli.py
@@ -226,10 +226,10 @@ class AzureCli:
 
         return result
 
-    def apply_configuration(self, deviceId, connection_string, config):
-        self.output.status(f("Deploying '{config}' to '{deviceId}'..."))
+    def apply_configuration(self, device_id, connection_string, hub_name, config):
+        self.output.status(f("Deploying '{config}' to '{device_id}'..."))
 
-        return self.invoke_az_cli_outproc(["iot", "hub", "apply-configuration", "-d", deviceId, "-k", config, "-l", connection_string], error_message=f("Failed to deploy '{config}' to '{deviceId}'..."), suppress_output=True)
+        return self.invoke_az_cli_outproc(["iot", "hub", "apply-configuration", "-d", device_id, "-n", hub_name, "-k", config, "-l", connection_string], error_message=f("Failed to deploy '{config}' to '{device_id}'..."), suppress_output=True)
 
     def get_free_iothub(self):
         with output_io_cls() as io:
diff --git a/iotedgedev/connectionstring.py b/iotedgedev/connectionstring.py
index cc29b68..2c8c19e 100644
--- a/iotedgedev/connectionstring.py
+++ b/iotedgedev/connectionstring.py
@@ -1,10 +1,10 @@
 class ConnectionString:
     def __init__(self, value):
-        self.value = value
+        self.ConnectionString = value
         self.data = dict()
 
-        if self.value: 
-            parts = value.split(';')
+        if self.ConnectionString: 
+            parts = self.ConnectionString.split(';')
             if len(parts) > 0:
                 for part in parts:
                     subpart = part.split('=', 1)
@@ -13,6 +13,8 @@ class ConnectionString:
 
             if self.data:
                 self.HostName = self["hostname"]
+                if self.HostName:
+                    self.HubName = self.HostName.split('.')[0]
                 self.SharedAccessKey = self["sharedaccesskey"]
 
     def __getitem__(self, key):
@@ -23,7 +25,7 @@ class IoTHubConnectionString(ConnectionString):
     def __init__(self, value):
         ConnectionString.__init__(self, value)
 
-        if self.value:
+        if self.ConnectionString:
             self.SharedAccessKeyName = self["sharedaccesskeyname"]
 
 
@@ -31,5 +33,5 @@ class DeviceConnectionString(ConnectionString):
     def __init__(self, value):
         ConnectionString.__init__(self, value)
 
-        if self.value:
+        if self.ConnectionString:
             self.DeviceId = self["deviceid"]
diff --git a/iotedgedev/edge.py b/iotedgedev/edge.py
index 6e71ba0..4d20943 100644
--- a/iotedgedev/edge.py
+++ b/iotedgedev/edge.py
@@ -10,11 +10,11 @@ class Edge:
 
         self.output.header("DEPLOYING CONFIGURATION")
 
-        self.envvars.verify_envvar_has_val("IOTHUB_CONNECTION_STRING", self.envvars.IOTHUB_CONNECTION_STRING)
-        self.envvars.verify_envvar_has_val("DEVICE_CONNECTION_STRING", self.envvars.DEVICE_CONNECTION_STRING)
+        self.envvars.verify_envvar_has_val("IOTHUB_CONNECTION_INFO", self.envvars.IOTHUB_CONNECTION_INFO)
+        self.envvars.verify_envvar_has_val("DEVICE_CONNECTION_INFO", self.envvars.DEVICE_CONNECTION_INFO)
         self.envvars.verify_envvar_has_val("DEPLOYMENT_CONFIG_FILE", self.envvars.DEPLOYMENT_CONFIG_FILE)
 
-        self.azure_cli.apply_configuration(self.envvars.DEVICE_CONNECTION_INFO.DeviceId, self.envvars.IOTHUB_CONNECTION_STRING, self.envvars.DEPLOYMENT_CONFIG_FILE_PATH)
+        self.azure_cli.apply_configuration(self.envvars.DEVICE_CONNECTION_INFO.DeviceId, self.envvars.IOTHUB_CONNECTION_INFO.ConnectionString, self.envvars.IOTHUB_CONNECTION_INFO.HubName, self.envvars.DEPLOYMENT_CONFIG_FILE_PATH)
 
         self.output.footer("DEPLOYMENT COMPLETE")
         
\ No newline at end of file


Test patch:
diff --git a/tests/test_connectionstring.py b/tests/test_connectionstring.py
new file mode 100644
index 0000000..21d0dc9
--- /dev/null
+++ b/tests/test_connectionstring.py
@@ -0,0 +1,78 @@
+import os
+import pytest
+from dotenv import load_dotenv
+from iotedgedev.connectionstring import ConnectionString, IoTHubConnectionString, DeviceConnectionString
+
+emptystring = ""
+valid_connectionstring = "HostName=testhub.azure-devices.net;SharedAccessKey=gibberish"
+valid_iothub_connectionstring = "HostName=testhub.azure-devices.net;SharedAccessKeyName=iothubowner;SharedAccessKey=moregibberish"
+valid_device_connectionstring = "HostName=testhub.azure-devices.net;DeviceId=testdevice;SharedAccessKey=othergibberish"
+invalid_connectionstring = "HostName=azure-devices.net;SharedAccessKey=gibberish"
+invalid_iothub_connectionstring = "HostName=testhub.azure-devices.net;SharedAccessKey=moregibberish"
+invalid_device_connectionstring = "HostName=testhub.azure-devices.net;DeviceId=;SharedAccessKey=othergibberish"
+
+def test_empty_connectionstring():
+    connectionstring = ConnectionString(emptystring)
+    assert not connectionstring.data
+
+def test_empty_iothub_connectionstring():
+    connectionstring = IoTHubConnectionString(emptystring)
+    assert not connectionstring.data
+
+def test_empty_device_connectionstring():
+    connectionstring = DeviceConnectionString(emptystring)
+    assert not connectionstring.data
+
+def test_valid_connectionstring():
+    connectionstring = ConnectionString(valid_connectionstring)
+    assert connectionstring.HostName == "testhub.azure-devices.net"
+    assert connectionstring.HubName == "testhub"
+    assert connectionstring.SharedAccessKey == "gibberish"
+
+def test_valid_iothub_connectionstring():
+    connectionstring = IoTHubConnectionString(valid_iothub_connectionstring)
+    assert connectionstring.HostName == "testhub.azure-devices.net"
+    assert connectionstring.HubName == "testhub"
+    assert connectionstring.SharedAccessKeyName == "iothubowner"
+    assert connectionstring.SharedAccessKey == "moregibberish"
+
+def test_valid_devicehub_connectionstring():
+    connectionstring = DeviceConnectionString(valid_device_connectionstring)
+    assert connectionstring.HostName == "testhub.azure-devices.net"
+    assert connectionstring.HubName == "testhub"
+    assert connectionstring.DeviceId == "testdevice"
+    assert connectionstring.SharedAccessKey == "othergibberish"
+
+def test_invalid_connectionstring():
+    connectionstring = ConnectionString(invalid_connectionstring)
+    assert connectionstring.HubName != "testhub"
+
+def test_invalid_iothub_connectionstring():
+    with pytest.raises(KeyError):
+        IoTHubConnectionString(invalid_iothub_connectionstring)
+
+def test_invalid_devicehub_connectionstring():
+    connectionstring = DeviceConnectionString(invalid_device_connectionstring)
+    assert connectionstring.HostName == "testhub.azure-devices.net"
+    assert connectionstring.HubName == "testhub"
+    assert not connectionstring.DeviceId
+    assert connectionstring.SharedAccessKey == "othergibberish"
+
+def test_valid_env_iothub_connectionstring():
+    load_dotenv(".env")
+    env_iothub_connectionstring = os.getenv("IOTHUB_CONNECTION_STRING")
+    connectionstring = IoTHubConnectionString(env_iothub_connectionstring)
+    assert connectionstring.HostName
+    assert connectionstring.HubName
+    assert connectionstring.SharedAccessKey
+    assert connectionstring.SharedAccessKeyName
+
+def test_valid_env_device_connectionstring():
+    load_dotenv(".env")
+    env_device_connectionstring = os.getenv("DEVICE_CONNECTION_STRING")
+    connectionstring = DeviceConnectionString(env_device_connectionstring)
+    assert connectionstring.HostName
+    assert connectionstring.HubName
+    assert connectionstring.SharedAccessKey
+    assert connectionstring.DeviceId
+    
\ No newline at end of file
diff --git a/tests/test_iotedgedev.py b/tests/test_iotedgedev.py
index 2d08bad..c809c15 100644
--- a/tests/test_iotedgedev.py
+++ b/tests/test_iotedgedev.py
@@ -153,7 +153,7 @@ def test_monitor(request, capfd):
     print (err)
     print (result.output)
 
-    assert 'application properties' in out
+    assert 'timeCreated' in out
 
 
 @pytest.fixture