Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Shaffan committed Mar 22, 2016
0 parents commit 9e8a68c
Show file tree
Hide file tree
Showing 21 changed files with 549 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.idea
*.gem
3 changes: 3 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
source 'https://rubygems.org'
# Specify your gem's dependencies in fog-azure.gemspec
gemspec
23 changes: 23 additions & 0 deletions fog-azurerm.gemspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
lib = File.expand_path('../lib', __FILE__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
require 'fog/azurerm/version'

Gem::Specification.new do |spec|
spec.name = "fog-azurerm"
spec.version = Fog::AzureRM::VERSION
spec.authors = ["Shaffan Chaudhry"]
spec.summary = %q{Module for the 'fog' gem to support Azure Resource Manager cloud services.}
spec.description = %q{This library can be used as a module for `fog` or as standalone provider
to use the Azure Resource Manager cloud services in applications..}
spec.files = Dir["{lib}/**/*.rb", "bin/*", "LICENSE", "*.md"]
spec.require_paths = ["lib"]
spec.add_development_dependency 'rake', '~> 10.0'
spec.add_development_dependency 'shindo', '~> 0.3'
spec.add_dependency 'fog-core', '~> 1.27'
spec.add_dependency 'fog-json', '~> 1.0'
spec.add_dependency 'fog-xml', '~> 0.1'
spec.add_dependency 'azure_mgmt_compute', '~>0.2.1'
spec.add_dependency 'azure_mgmt_resources', '~>0.2.1'
spec.add_dependency 'azure_mgmt_storage', '~>0.2.1'
spec.add_dependency 'azure_mgmt_network', '~>0.2.1'
end
9 changes: 9 additions & 0 deletions lib/fog/azurerm.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
require 'fog/azurerm/version'
require 'fog/azurerm/core'
require 'fog/azurerm/dns'
require 'fog/azurerm/resources'

module Fog
module AzureRM
end
end
9 changes: 9 additions & 0 deletions lib/fog/azurerm/core.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
require "fog/core"

module Fog
module AzureRM
extend Fog::Provider
service(:resources, "Resources")
service(:dns, "DNS")
end
end
54 changes: 54 additions & 0 deletions lib/fog/azurerm/dns.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
require "fog/azurerm/core"

module Fog
module DNS
class AzureRM < Fog::Service
requires :tenant_id
requires :client_id
requires :client_secret
requires :subscription_id

request_path "fog/azurerm/requests/dns"
request :create_zone
request :delete_zone
request :list_zones

model_path "fog/azurerm/models/dns"
model :zone
collection :zones

class Mock
def initialize(options={})
begin
require "fog/azurerm/libraries/dns/zone"
require "fog/azurerm/libraries/dns/token"
rescue LoadError => e
retry if require("rubygems")
raise e.message
end
end
end

class Real
def initialize(options)
begin
require "fog/azurerm/libraries/dns/zone"
require "fog/azurerm/libraries/dns/token"
rescue LoadError => e
retry if require("rubygems")
raise e.message
end
@token = ::Fog::DNS::Libraries::Token.new(options[:tenant_id], options[:client_id], options[:client_secret])
token = @token.generate_token
@zone = ::Fog::DNS::Libraries::Zone.new(options[:subscription_id], token)
@resources = Fog::Resources::AzureRM.new(
:tenant_id => options[:tenant_id],
:client_id => options[:client_id],
:client_secret => options[:client_secret],
:subscription_id => options[:subscription_id]
)
end
end
end
end
end
4 changes: 4 additions & 0 deletions lib/fog/azurerm/libraries/constants.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Get Azure Token
AZURE_LOGIN_URL = 'https://login.windows.net'
AZURE_GRANT_TYPE = 'client_credentials'
AZURE_RESOURCE = 'https://management.azure.com'
42 changes: 42 additions & 0 deletions lib/fog/azurerm/libraries/dns/token.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
require 'rest_client'
require 'json'
require ::File.expand_path('../../constants', __FILE__)

module Fog
module DNS
module Libraries
class Token
def initialize(tenant_id, client_id, client_secret, http_proxy = nil)
@tenant_id = tenant_id
@client_id = client_id
@client_secret = client_secret
@http_proxy = http_proxy
end

def generate_token
login_url = "#{AZURE_LOGIN_URL}/#{@tenant_id}/oauth2/token"
unless @http_proxy.nil?
RestClient.proxy = @http_proxy
end

begin
token_response = RestClient.post(
login_url,
client_id: @client_id,
client_secret: @client_secret,
grant_type: AZURE_GRANT_TYPE,
resource: "#{AZURE_RESOURCE}/"
)
token_hash = JSON.parse(token_response)
token = 'Bearer ' + token_hash['access_token']
return token
rescue RestClient::Exception => e
body = JSON.parse(e.http_body)
msg = "Exception trying to retrieve the token: #{body['error']}, #{body['error_description']}"
fail msg
end
end
end
end
end
end
127 changes: 127 additions & 0 deletions lib/fog/azurerm/libraries/dns/zone.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
require 'rest_client'
require 'json'
require ::File.expand_path('../../constants', __FILE__)

module Fog
module DNS
module Libraries
class Zone
def initialize(subscription_id, token)
@subscription = subscription_id
@token = token
end

def check_for_zone(dns_resource_group, zone_name)
resource_url = "#{AZURE_RESOURCE}/subscriptions/#{@subscription}/resourceGroups/#{dns_resource_group}/providers/Microsoft.Network/dnsZones/#{zone_name}?api-version=2015-05-04-preview"
begin
dns_response = RestClient.get(
resource_url,
accept: 'application/json',
content_type: 'application/json',
authorization: @token
)
dns_hash = JSON.parse(dns_response)
if dns_hash.key?('id') && !dns_hash['id'].nil?
true
else
false
end
rescue RestClient::Exception => e
if e.http_code == 404
false
else
body = JSON.parse(e.http_body)
msg = "Exception checking if the zone exists: #{body['code']}, #{body['message']}"
fail msg
end
end
end

def create(dns_resource_group, zone_name)
if self.check_for_zone(dns_resource_group, zone_name)
puts "Zone #{zone_name} Exists, no need to create"
return
end

puts "Creating Zone #{zone_name} ..."
resource_url = "#{AZURE_RESOURCE}/subscriptions/#{@subscription}/resourceGroups/#{dns_resource_group}/providers/Microsoft.Network/dnsZones/#{zone_name}?api-version=2015-05-04-preview"
body = {
location: 'global',
tags: {},
properties: {} }
begin
dns_response = RestClient.put(
resource_url,
body.to_json,
accept: 'application/json',
content_type: 'application/json',
authorization: @token)
response_hash = JSON.parse(dns_response)
puts "Zone #{zone_name} created successfully."
response_hash
rescue RestClient::Exception => e
body = JSON.parse(e.http_body)
if body.key?('error')
body = body['error']
msg = "Exception creating zone: #{body['code']}, #{body['message']}"
else
msg = "Exception creating zone: #{body['code']}, #{body['message']}"
end

fail msg
end
end

def delete(dns_resource_group, zone_name)
unless self.check_for_zone(dns_resource_group, zone_name)
puts "Zone #{zone_name} does not exist, no need to delete"
return
end

puts "Deleting Zone #{zone_name} ..."
resource_url = "#{AZURE_RESOURCE}/subscriptions/#{@subscription}/resourceGroups/#{dns_resource_group}/providers/Microsoft.Network/dnsZones/#{zone_name}?api-version=2015-05-04-preview"
begin
dns_response = RestClient.delete(
resource_url,
accept: 'application/json',
content_type: 'application/json',
authorization: @token)
puts "Zone #{zone_name} deleted successfully."
true
rescue RestClient::Exception => e
body = JSON.parse(e.http_body)
if body.key?('error')
body = body['error']
msg = "Exception deleting zone: #{body['code']}, #{body['message']}"
else
msg = "Exception deleting zone: #{body['code']}, #{body['message']}"
end
fail msg
end
end

def list_zones(dns_resource_group)
resource_url = "#{AZURE_RESOURCE}/subscriptions/#{@subscription}/resourceGroups/#{dns_resource_group}/providers/Microsoft.Network/dnsZones?api-version=2015-05-04-preview"
begin
dns_response = RestClient.get(
resource_url,
accept: 'application/json',
content_type: 'application/json',
authorization: @token)
response_hash = JSON.parse(dns_response)
response_hash['value']
rescue RestClient::Exception => e
body = JSON.parse(e.http_body)
if body.key?('error')
body = body['error']
msg = "Exception fetching zones: #{body['code']}, #{body['message']}"
else
msg = "Exception fetching zones: #{body['code']}, #{body['message']}"
end
fail msg
end
end
end
end
end
end
21 changes: 21 additions & 0 deletions lib/fog/azurerm/models/dns/zone.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
module Fog
module DNS
class AzureRM
class Zone < Fog::Model
identity :name
attribute :id
attribute :resource_group

def save
requires :name
requires :resource_group
service.create_zone(resource_group, name)
end

def destroy
service.delete_zone(name, resource_group)
end
end
end
end
end
31 changes: 31 additions & 0 deletions lib/fog/azurerm/models/dns/zones.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
require "fog/core/collection"
require "fog/azurerm/models/dns/zone"

module Fog
module DNS
class AzureRM
class Zones < Fog::Collection
model Fog::DNS::AzureRM::Zone

def all()
zones = []
service.list_zones.each do |z|
hash = {}
z.each do |k, v|
hash[k] = v
end
zones << hash
end
load(zones)
end

def get(identity, resource_group)
all.find { |f| f.name == identity && f.resource_group == resource_group }
rescue Fog::Errors::NotFound
nil
end

end
end
end
end
31 changes: 31 additions & 0 deletions lib/fog/azurerm/models/resources/resource_group.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
module Fog
module Resources
class AzureRM
class ResourceGroup < Fog::Model
identity :name
attribute :id
attribute :location

def save
requires :name
requires :location
puts "Creating Resource Group: #{name}."
rg_properties = ::Azure::ARM::Resources::Models::ResourceGroup.new
rg_properties.location = location
promise = service.create_resource_group(name, rg_properties)
result = promise.value!
resource_group = result.body
puts "Resource Group #{resource_group.name} created successfully."
end

def destroy
puts "Deleting Resource Group: #{name}."
promise = service.delete_resource_group(name)
result = promise.value!
resource_group = result.body
puts "Resource Group #{name} deleted successfully."
end
end
end
end
end
Loading

0 comments on commit 9e8a68c

Please sign in to comment.