Skip to content

Commit

Permalink
azure quickstart templates
Browse files Browse the repository at this point in the history
  • Loading branch information
lee212 committed Oct 7, 2016
1 parent 8ab60c7 commit a0c9ad0
Show file tree
Hide file tree
Showing 2 changed files with 138 additions and 4 deletions.
97 changes: 97 additions & 0 deletions simpleazure/azure_quickstart_templates.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
# -*- coding: utf-8 -*-

"""
simpleazure.azure-quickstart-templates
This module supports listing all azure-quickstart-templates from github
:copyright:
:license:
"""

from github import Github
import json

class AzureQuickStartTemplates(object):
"""Constructs a :class:`AzureQuickStartTemplates <AzureQuickStartTemplates>`.
Returns :class:`AzureQuickStartTemplates <AzureQuickStartTemplates>` instance.
Description:
Azure provides a list of ARM templates written by community developers and
this class reads the list and downloads json data to run the template directly.
"""

def __init__(self):
self.git = Github()
self.git_repo = "azure-quickstart-templates"
self.git_owner = "Azure"

def get_list(self):
"""Returns a list of items in a repository except files"""

self.git.set_var("repo", self.git_repo)
self.git.set_var("owner", self.git_owner)
#res = [ item if item['type'] == "dir" for item in self.git.get_list() ]
res = {}
items = self.git.get_list()
for item in items:
if item['type'] != "dir":
continue
if item['name'] == ".github":
continue
try:
# required files
azuredeploy = self.get_azuredeploy(item['path'])
parameters = self.get_parameters(item['path'])
meta = self.get_metadata(item['path'])
except:
# If fails, skip to next
continue
etc = self.get_all(item['path'])
nested = self.get_nested(item['path'])
scripts = self.get_scripts(item['path'])
res[item['name']] = {
"azuredeploy": azuredeploy,
"parameters": parameters,
"metadata": meta,
"nested": nested,
"scripts": scripts,
"etc": etc
}
meta = azuredeploy = parameters = nested = scripts = etc = ""
return res

def get_metadata(self, path):
"""Returns cotents of the metadata.json file from a path"""
return self._get_json_contents(path + "/metadata.json")

def get_azuredeploy(self, path):
"""Returns cotents of the azuredeploy.json file from a path"""
return self._get_json_contents(path + "/azuredeploy.json")

def get_parameters(self, path):
"""Returns cotents of the azuredeploy.parameters.json file from a path"""
return self._get_json_contents(path + "/azuredeploy.parameters.json")

def get_nested(self, path):
"""Returns nested templates from a path"""
self.git.set_var("path", path + "/nested")
return self.git.get_list() or ""

def get_scripts(self, path):
"""Returns scripts from a path"""
self.git.set_var("path", path + "/scripts")
return self.git.get_list() or ""

def get_all(self, path):
"""Returns all items from a path"""
self.git.set_var("path", path)
return self.git.get_list()

def _get_json_contents(self, path):
self.git.set_var("path", path)
data = json.loads(self.git.get_file())
return data

45 changes: 41 additions & 4 deletions simpleazure/github.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ class Github(object):
"""Constructs a :class:`Github <Github>`.
Returns :class:`Github <Github>` instance.
Disclaimer: This class does not cover every API calls from github.
'Repositories' and 'Search' are mainly used to support listing and
searching contents related to Azure Resource Manager Templates.
Usage::
"""
Expand All @@ -29,11 +33,13 @@ class Github(object):

# Define api_path and exec_str when you add a new api call
api_path = {
"get_contents": "/repos/:owner/:repo/contents/:path"
"get_file": "/repos/:owner/:repo/contents/:path",
"get_list": "/repos/:owner/:repo/contents/"
}

exec_results = {
"get_contents": "self.results['content'].decode('base64')",
"get_file": "self.results['content'].decode('base64')",
"get_list": "self.results",
"search_code": "self.results",
}

Expand Down Expand Up @@ -70,11 +76,16 @@ def search_code(self, word):
def set_var(self, key, value):
exec("self." + key + "='" + value + "'")

def get_contents(self):
def unset_var(self, key):
exec("self." + key + "=''")

def get_file(self):
"""https://developer.github.com/v3/repos/contents/#get-contents
GET /repos/:owner/:repo/contents/:path
This is for a file
"""
self.action = inspect.stack()[0][3]
api_path = self._get_api_path(self.action)
Expand All @@ -83,10 +94,31 @@ def get_contents(self):
if self.run_api():
return self.get_results()

def get_list(self):
"""https://developer.github.com/v3/repos/contents/#get-contents
GET /repos/:owner/:repo/contents/:path
This is for a directory
"""
return self._exec()

def _exec(self):
self.action = inspect.stack()[1][3]
api_path = self._get_api_path(self.action)
request_url = self._fill_in_path(api_path)
self.request_url = request_url
if self.run_api():
return self.get_results()

def get_results(self):
ret = None
exec_str = self.exec_results[self.action]
exec("ret = " + exec_str)
try:
exec("ret = " + exec_str)
except:
ret = self.results
return ret

def _get_api_path(self, action_name):
Expand Down Expand Up @@ -115,6 +147,11 @@ def run_api(self):
return True
return False

def run_direct(self, url):
self.request_url = url
if self.run_api():
return self.get_results()

def reset(self):
self.request_url = ""
self.repo = ""
Expand Down

0 comments on commit a0c9ad0

Please sign in to comment.