Skip to content

Commit

Permalink
[1LP][RFR] Azure iot client, create/add iothub (#419)
Browse files Browse the repository at this point in the history
* add iot client

* create iothub

* create/delete/check

* changed doc
  • Loading branch information
psimovec authored and mshriver committed Sep 27, 2019
1 parent 533f580 commit 63a8f93
Showing 1 changed file with 32 additions and 1 deletion.
33 changes: 32 additions & 1 deletion wrapanapi/systems/msazure.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from azure.common.credentials import ServicePrincipalCredentials
from azure.common.exceptions import CloudError
from azure.mgmt.compute import ComputeManagementClient
from azure.mgmt.iothub import IotHubClient
from azure.mgmt.network import NetworkManagementClient
from azure.mgmt.network.models import NetworkSecurityGroup, SecurityRule
from azure.mgmt.resource import ResourceManagementClient, SubscriptionClient
Expand Down Expand Up @@ -512,7 +513,7 @@ def can_pause(self):
def __setattr__(self, key, value):
"""If the subscription_id is changed, invalidate client caches"""
if key in ['credentials', 'subscription_id']:
for client in ['compute_client', 'resource_client', 'network_client',
for client in ['compute_client', 'iot_client', 'resource_client', 'network_client',
'subscription_client', 'storage_client']:
if getattr(self, client, False):
del self.__dict__[client]
Expand All @@ -525,6 +526,10 @@ def __setattr__(self, key, value):
def compute_client(self):
return ComputeManagementClient(self.credentials, self.subscription_id)

@cached_property
def iot_client(self):
return IotHubClient(self.credentials, self.subscription_id)

@cached_property
def resource_client(self):
return ResourceManagementClient(self.credentials, self.subscription_id)
Expand Down Expand Up @@ -552,6 +557,32 @@ def vms_collection(self):
def create_vm(self, vm_name, *args, **kwargs):
raise NotImplementedError

def create_iothub(self, name, sku_name='F1', sku_capacity=1):
"""
Create an iothub in Azure with the specified name.
sku_name and sku_capacity are required for the creation
and defaults to 'F1' and '1' for free pricing.
"""
async_iot_hub = self.iot_client.iot_hub_resource.create_or_update(
self.resource_group,
name,
{'location': self.region,
'subscriptionid': self.subscription_id,
'resourcegroup': self.resource_group,
'sku': {
'name': sku_name,
'capacity': sku_capacity
},
'features': 'None'}
)
return async_iot_hub.result()

def delete_iothub(self, name):
self.iot_client.iot_hub_resource.delete(self.resource_group, name)

def has_iothub(self):
return any(True for _ in self.iot_client.iot_hub_resource.list_by_subscription())

def find_vms(self, name=None, resource_group=None):
"""
Returns list of Instances in current Region
Expand Down

0 comments on commit 63a8f93

Please sign in to comment.