Skip to content

Commit

Permalink
added azure_virtual_network resource and provider
Browse files Browse the repository at this point in the history
  • Loading branch information
John Smyth committed Sep 16, 2015
1 parent 86f3d3a commit 172fb0a
Show file tree
Hide file tree
Showing 5 changed files with 162 additions and 2 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
*.sw[opn]
Gemfile.lock
*.gem
/nodes
/docs/examples
Expand Down
139 changes: 139 additions & 0 deletions lib/chef/provider/azure_virtual_network.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
require 'chef/provisioning/azurerm/azure_provider'

class Chef
class Provider
class AzureVirtualNetwork < Chef::Provisioning::AzureRM::AzureProvider
provides :azure_virtual_network

def whyrun_supported?
true
end

action :create do

virtual_network_exists = does_virtual_network_exist

if virtual_network_exists
converge_by("update virtual network #{new_resource.name}") do
#update_virtual_network
create_virtual_network # are create and update different (and should they be??)
end
else
# Create the storage account complete with tags and properties
converge_by("create virtual network #{new_resource.name}") do
create_virtual_network
# now update the resource with properties that are not settable in the create operation (e.g. create domain)
#update_virtual_network
end
end
end

action :destroy do
converge_by("destroy virtual network: #{new_resource.name}") do
if does_virtual_network_exist
destroy_virtual_network
else
action_handler.report_progress "virtual network #{new_resource.name} was not found."
end
end
end

def does_virtual_network_exist
virtual_network_list = network_management_client.virtual_networks.list(new_resource.resource_group).value!
virtual_network_list.body.value.each do |virtual_network|
return true if virtual_network.name == new_resource.name
end
false
end

def destroy_virtual_network
action_handler.report_progress 'Destroying Virtual Network...'
begin
result = network_management_client.virtual_networks.delete(new_resource.resource_group, new_resource.name).value!
Chef::Log.debug(result)
rescue MsRestAzure::AzureOperationError => operation_error
error = operation_error.body['error']
Chef::Log.error "ERROR destroying Virtual Network: #{error}"
raise operation_error
end

virtual_network
end

def create_virtual_network
virtual_network = Azure::ARM::Network::Models::VirtualNetwork.new

virtual_network.tags = new_resource.tags
virtual_network.location = new_resource.location

virtual_network.properties = create_virtual_network_properties(
new_resource.address_prefixes, new_resource.subnets, new_resource.dhcp_servers )

action_handler.report_progress 'Creating Virtual Network...'
begin
result = network_management_client.virtual_networks.create_or_update(new_resource.resource_group, new_resource.name, virtual_network).value!
Chef::Log.debug(result)
rescue MsRestAzure::AzureOperationError => operation_error
error = operation_error.body['error']
Chef::Log.error "ERROR updating Virtual Network: #{error}"
raise operation_error
end
end

#def update_virtual_network
# update_virtual_network_tags
#end

#def update_virtual_network_tags
# virtual_network = Azure::ARM::Network::Models::VirtualNetwork.new
# virtual_network.tags = new_resource.tags
# virtual_network.properties = Azure::ARM::Network::Models::VirtualNetworkPropertiesFormat.new
# action_handler.report_progress 'updating Tags'
# begin
# result = network_management_client.virtual_networks.create_or_update(new_resource.resource_group, new_resource.name, virtual_network).value!
# Chef::Log.debug(result)
# rescue MsRestAzure::AzureOperationError => operation_error
# error = operation_error.body['error']
# Chef::Log.error "ERROR updating Virtual Network tags: #{error}"
# raise operation_error
# end
#end

def create_virtual_network_properties( address_prefixes, subnets, dhcp_servers )
props = Azure::ARM::Network::Models::VirtualNetworkPropertiesFormat.new

props.address_space = Azure::ARM::Network::Models::AddressSpace.new
props.address_space.address_prefixes = address_prefixes

if (dhcp_servers)
props.dhcp_options = Azure::ARM::Network::Models::DhcpOptions.new
props.dhcp_options = dhcp_severs
end

props.subnets=[]
subnets.each do |subnet|
props.subnets.push(create_subnet(subnet[:name], subnet[:address_prefix]))
end

props
end


def create_subnet(subnet_name, subnet_address)
subnet = Azure::ARM::Network::Models::Subnet.new
subnet.name = subnet_name
subnet.properties = Azure::ARM::Network::Models::SubnetPropertiesFormat.new
subnet.properties.address_prefix = subnet_address

subnet
end


end #class AzureVirtualNetwork
end #class Provider
end #class Chef





2 changes: 1 addition & 1 deletion lib/chef/provisioning/azurerm.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
Chef::Log.info("chef-provisioning-azurerm #{Chef::Provisioning::AzureRM::VERSION}")
Chef::Log.info("chef-provisioning #{Chef::Provisioning::VERSION}")

resources = %w(resource_group resource_template storage_account)
resources = %w(resource_group resource_template storage_account virtual_network)
resources.each do |r|
require "chef/resource/azure_#{r}"
require "chef/provider/azure_#{r}"
Expand Down
2 changes: 1 addition & 1 deletion lib/chef/provisioning/azurerm/azure_provider.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def compute_management_client

def network_management_client
credentials = Credentials.new.azure_credentials_for_subscription(new_resource.subscription_id)
client = Azure::ARM::Network::NetworkManagementClient.new(credentials)
client = Azure::ARM::Network::NetworkResourceProviderClient.new(credentials)
client.subscription_id = new_resource.subscription_id
client
end
Expand Down
19 changes: 19 additions & 0 deletions lib/chef/resource/azure_virtual_network.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
require 'chef/provisioning/azurerm/azure_resource'


class Chef
class Resource
class AzureVirtualNetwork < Chef::Provisioning::AzureRM::AzureResource
resource_name :azure_virtual_network
actions :create, :destroy, :nothing
default_action :create
attribute :name, kind_of: String, name_attribute: true, regex: /^[\w]{3,24}$/i
attribute :resource_group, kind_of: String
attribute :location, kind_of: String, default: 'westus'
attribute :tags, kind_of: Hash
attribute :address_prefixes, kind_of: Array
attribute :subnets, kind_of: Array
attribute :dhcp_servers, kind_of: Array
end
end
end

0 comments on commit 172fb0a

Please sign in to comment.