Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/lee212/simpleazure
Browse files Browse the repository at this point in the history
  • Loading branch information
lee212 committed Oct 24, 2016
2 parents f5cc610 + bcd77f2 commit c15fb61
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 69 deletions.
2 changes: 1 addition & 1 deletion docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ From a custom template on github:
::

>> url = "https://raw.githubusercontent.com/Azure-Samples/resource-manager-python-template-deployment/master/templates/template.json"
>> saz.arm.deploy(template = url, param = { "sshKeyData": "ssh-rsa AAAB3Nza..." })
>> saz.arm.deploy(template = url, param = { "sshKeyData": "ssh-rsa AAAB3Nza...", 'dnsLabelPrefix':"simpleazure", 'vmName':'simpleazure-first-vm'}) })

.. note:: For more about using ARM? check out :ref:`ref-arm`
.. note:: For more about deploying a custom Template? check out :ref:`ref-saz-template-deploy`
Expand Down
29 changes: 29 additions & 0 deletions simpleazure/azure_network_management.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Test for getting ip address
from azure.mgmt.network import NetworkManagementClient as nmc

class AzureNetworkManagement(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
91 changes: 25 additions & 66 deletions simpleazure/azure_resource_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,18 @@
from . import config
from . import utils
from template.template import Template

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

class AzureResourceManager(object):
"""Constructs a :class:`ARM <ARM>`.
Returns :class:`ARM <ARM>` instance.
Usage::
>> from simpleazure.arm import ARM as armt
>> arm = armt()
>>> from simpleazure import SimpleAzure as saz
>>> saz_client = saz()
>>> saz_client.arm
<simpleazure.azure_resource_manager.AzureResourceManager object at>
"""

Expand All @@ -39,11 +39,10 @@ class AzureResourceManager(object):
deployment = config.DEFAULT_DEPLOYMENT
template = None
parameters = {}

network = None
selected_serivce = "resource_groups"

def __init__(self, subscription=None, client_id=None, secret=None, tenant=None):
self.sshkey = SSHKey()
self.get_credential(subscription, client_id, secret, tenant)
self.template = Template()

Expand All @@ -56,12 +55,29 @@ def get_credential(self, subscription=None, client_id=None, secret=None, tenant=
self.cred = spc(client_id = cid, secret = sec, tenant = tid)
self.client = rmc(self.cred, sid)

def create(self, service_name=None, **kwargs):
if not service_name:
servie_name = self.selected_service

def delete(self, service_name=None, **kwargs):
if not service_name:
servie_name = self.selected_service

def list(self, service_name=None):
if not service_name:
servie_name = self.selected_service
func = getttr(self.client, service_name)
return func()

def get(self, service_name=None, **kwargs):
if not service_name:
servie_name = self.selected_service

def get_group_or_create(self):
new_name = utils.get_rand_name()
self.resource_group = new_name

def deploy(self, template=None, param=None):
self.sshkey.set_pubkey()
self.set_template(template)
self.set_parameters(param)
self.set_deployment_properties()
Expand Down Expand Up @@ -161,7 +177,7 @@ def view_info(self):
def _load_network_client(self, refresh=False):
if self.network:
return self.network
self.network = NMC(self.cred, self.subscription_id)
self.network = nmc(self.cred, self.subscription_id)
self.network.set_resource_group(self.resource_group)
# Tips
#
Expand All @@ -173,61 +189,4 @@ def _load_network_client(self, refresh=False):
# 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):

pvkey = None
pubkey = None
pubkey_path = None
pvkey_path = None

default_path = {
'pvkey': "~/.ssh/id_rsa",
'pubkey': "~/.ssh/id_rsa.pub"
}

def __init__(self, path=None):
self.set_pubkey(path)

def set_pubkey(self, path=None):
try:
path = os.path.expanduser(path or self.default_path['pubkey'])
with open(path, "r") as f:
self.pubkey = f.read()
f.close()
self.pubkey_path = path
return True
except Exception as e:
# debug / log
# print (e)
return False



5 changes: 3 additions & 2 deletions simpleazure/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@
DEFAULT_ROLE_SIZE = "Small" # ExtraSmall|Small|Medium|Large|ExtraLarge

# For ARM
DEFAULT_RESOURCE_GROUP = "saz-rg"
DEFAULT_DEPLOYMENT = "saz-deploy"
DEFAULT_RESOURCE_GROUP = "Default-SimpleAzure-" + \
(DEFAULT_LOCATION.replace(" ", "")
DEFAULT_DEPLOYMENT = "Default-SimpleAzure-Deployment"

def config_path():
return DEFAULT_AZURE_CONFIG_PATH
Expand Down
29 changes: 29 additions & 0 deletions simpleazure/sshkey.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import os

class SSHKey(object):

pvkey = None
pubkey = None
pubkey_path = None
pvkey_path = None

default_path = {
'pvkey': "~/.ssh/id_rsa",
'pubkey': "~/.ssh/id_rsa.pub"
}

def __init__(self, path=None):
self.set_pubkey(path)

def set_pubkey(self, path=None):
try:
path = os.path.expanduser(path or self.default_path['pubkey'])
with open(path, "r") as f:
self.pubkey = f.read()
f.close()
self.pubkey_path = path
return True
except Exception as e:
# debug / log
# print (e)
return False

0 comments on commit c15fb61

Please sign in to comment.