Skip to content

Commit

Permalink
view_info -> public ip address
Browse files Browse the repository at this point in the history
  • Loading branch information
lee212 committed Oct 17, 2016
1 parent 0918f9d commit 6769579
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 2 deletions.
39 changes: 38 additions & 1 deletion docs/azure_quickstart_templates.rst
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ In our example of RHEL, three parameters need to be set before its deployment,
>>> arm.set_parameters(
{"adminPassword":"xxxxx",
"adminUsername":"azureuser",
"vmName":"saz-quickstart"}
"vmName":"simpleazure-quickstart"}
)

{'adminPassword': {'value': 'xxxxx'},
Expand Down Expand Up @@ -332,6 +332,43 @@ Or you can directly deploy a template with parameters.

It takes some time to complete a deployment and get access to a virtual machine.

Access
-------------------------------------------------------------------------------

If a template is deployed with an access to virtual machines i.e. SSH via
public IP addresses, ``view_info()`` returns an ip address in a same resource
group.

::

>>> arm.view_info()
[u'40.77.103.150']


Use the same login user name and password from parameters in a SSH client:

::
$ ssh 40.77.103.150 -l azureuser
The authenticity of host '40.77.103.150 (40.77.103.150)' can't be established.
ECDSA key fingerprint is 64:fc:dd:7c:98:8c:ed:93:63:61:56:31:81:ad:cf:69.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '40.77.103.150' (ECDSA) to the list of known hosts.
azureuser@40.77.103.150's password:
[azureuser@simpleazure-quickstart-rhel ~]$ cat /etc/redhat-release
Red Hat Enterprise Linux Server release 7.2 (Maipo)

Termination
--------------------------------------------------------------------------------

Simple deleting a resource group where deployment is made terminates all
services.

::

>>> arm.remove_resource_group()


.. todo::

access information is required to display
Expand Down
42 changes: 41 additions & 1 deletion simpleazure/arm.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
from . import utils
from template.template import Template

# Test for getting ip address
from azure.mgmt.network import NetworkManagementClient as nmc

class ARM(object):
"""Constructs a :class:`ARM <ARM>`.
Returns :class:`ARM <ARM>` instance.
Expand All @@ -37,6 +40,8 @@ class ARM(object):
template = None
parameters = {}

network = None

def __init__(self, subscription=None, client_id=None, secret=None, tenant=None):
self.sshkey = SSHKey()
self.get_credential(subscription, client_id, secret, tenant)
Expand All @@ -47,6 +52,7 @@ def get_credential(self, subscription=None, client_id=None, secret=None, tenant=
cid = os.getenv('AZURE_CLIENT_ID', client_id)
tid = os.getenv('AZURE_TENANT_ID', tenant)
sec = os.getenv('AZURE_CLIENT_SECRET', secret)
self.subscription_id = sid
self.cred = spc(client_id = cid, secret = sec, tenant = tid)
self.client = rmc(self.cred, sid)

Expand Down Expand Up @@ -149,7 +155,14 @@ def view_template(self):
pass

def view_info(self):
pass
self._load_network_client()
return self.network.get_access_info()

def _load_network_client(self, refresh=False):
if self.network:
return self.network
self.network = NMC(self.cred, self.subscription_id)
self.network.set_resource_group(self.resource_group)
# Tips
#
# DeploymentMode: incremental | complete
Expand All @@ -160,6 +173,33 @@ def view_info(self):
# deleted. The default mode is Incremental.
# source: https://github.com/dx-ted-emea/Azure-Resource-Manager-Documentation/blob/master/ARM/Templates/Template_Deploy.md

class NMC(object):
def __init__(self, credentials = None, subscription_id = None):
if credentials and subscription_id:
self.cred = credentials
self.sid = subscription_id
if self.cred and self.sid:
self.client = nmc(self.cred, self.sid)

def set_credentials(self, credentials, subscription_id):
self.cred = credentials
self.sid = subscription_id

def set_resource_group(self, group_name):
self.resource_group = group_name

# public ip address in this example
def get_access_info(self, resource_group=None):
res = []
try:
ips = self.client.public_ip_addresses.list(resource_group or self.resource_group)
# {'dns_settings': None, 'name': u'fp6mwq3k4ytsypublicip', 'tags': None, 'public_ip_address_version': u'IPv4', 'public_ip_allocation_method': u'Dynamic', 'resource_guid': u'24d9fc7a-8ff0-40da-9b88-e283e105c07c', 'provisioning_state': u'Succeeded', 'ip_address': u'40.83.13.177', 'etag': u'W/"1ea1dc32-6afc-4704-b927-d7f5db69d4da"', 'location': u'centralus', 'ip_configuration': <azure.mgmt.network.models.ip_configuration.IPConfiguration object at 0x7f56bd981890>, 'idle_timeout_in_minutes': 4, 'type': u'Microsoft.Network/publicIPAddresses', 'id': u'/subscriptions/6b3cf2b5-2cc1-4828-b5e0-9f8be72e6e6f/resourceGroups/saz-rg/providers/Microsoft.Network/publicIPAddresses/fp6mwq3k4ytsypublicip'}
for ip in ips:
res.append(ip.ip_address)
return res
except:
return res

#extra
class SSHKey(object):

Expand Down

0 comments on commit 6769579

Please sign in to comment.