class Chef::Provider::AzureLoadBalancer

Public Instance Methods

create_or_update_load_balancer() click to toggle source
# File lib/chef/provider/azure_load_balancer.rb, line 56
def create_or_update_load_balancer
  tags = new_resource.load_balancer_options[:tags]
  subinfo = network_management_client.subnets.get(new_resource.resource_group, new_resource.virtual_network, new_resource.subnet_name)

  backend_pool = []

  new_resource.virtual_machine.each do |ivm|
    count = backend_pool.count
    backend_pool[count] = Azure::ARM::Network::Models::BackendAddressPool.new
    backend_pool[count].name = ivm
  end

  frontend_ipconf = Azure::ARM::Network::Models::FrontendIPConfiguration.new.tap do |feip|
    new_resource.load_balancer_options[:frontend].each do |fendip|
      feip.name = fendip[:name]
      feip.subnet = subinfo
      feip.private_ipallocation_method = fendip[:private_ipallocation_method]
    end
  end

  probe = Azure::ARM::Network::Models::Probe.new.tap do |prb|
    new_resource.load_balancer_options[:probes].each do |prbes|
      prb.name = prbes[:name]
      prb.port = prbes[:port]
      prb.protocol = prbes[:protocol]
      if prbes[:protocol].casecmp('http')
        prb.request_path = prbes[:request_path]
      end
      prb.interval_in_seconds = prbes[:interval_in_seconds]
      prb.number_of_probes = prbes[:number_of_probes]
    end
  end

  lb = Azure::ARM::Network::Models::LoadBalancer.new.tap do |lbinfo|
    lbinfo.location = new_resource.location
    lbinfo.tags = tags
    lbinfo.frontend_ipconfigurations = [frontend_ipconf]
    lbinfo.backend_address_pools = backend_pool
    lbinfo.probes = [probe]
  end

  lb_update(lb) # inital build

  mylb = network_management_client.load_balancers.get(new_resource.resource_group, new_resource.name)

  new_resource.virtual_machine.each do |ivm|
    vmnic = network_management_client.network_interfaces.get(new_resource.resource_group, ivm)
    mylb.backend_address_pools.each do |pool|
      next unless pool.name == ivm
      vmnic.ip_configurations.first.load_balancer_backend_address_pools = [pool] # mylb.backend_address_pools
      network_management_client.network_interfaces.create_or_update(new_resource.resource_group, ivm, vmnic)
    end
  end

  frontend_ipconf_sub = MsRestAzure::SubResource.new.tap do |subresource|
    subresource.id = network_management_client.load_balancers.get(new_resource.resource_group, new_resource.name).frontend_ipconfigurations.first.id
  end

  backend_ipconf_sub = MsRestAzure::SubResource.new.tap do |subresource|
    subresource.id = mylb.backend_address_pools.first.id
  end

  probe_sub = MsRestAzure::SubResource.new.tap do |subresource|
    subresource.id = network_management_client.load_balancers.get(new_resource.resource_group, new_resource.name).probes.first.id
  end

  unless new_resource.load_balancer_options[:inboundnat].nil?
    inboudnat = Azure::ARM::Network::Models::InboundNatRule.new.tap do |inat|
      new_resource.load_balancer_options[:inboundnat].each do |ibnat|
        inat.name = ibnat[:name]
        inat.frontend_port = ibnat[:frontend_port]
        inat.backend_port = ibnat[:backend_port]
        inat.protocol = ibnat[:protocol]
        inat.enable_floating_ip = ibnat[:enable_floating_ip]
        inat.idle_timeout_in_minutes = ibnat[:idle_timeout_in_minutes]
        inat.frontend_ipconfiguration = frontend_ipconf_sub
      end
    end
  end

  lbrules = Azure::ARM::Network::Models::LoadBalancingRule.new.tap do |lbrs|
    new_resource.load_balancer_options[:lbr].each do |lbr|
      lbrs.name = lbr[:name]
      lbrs.backend_address_pool = backend_ipconf_sub
      lbrs.protocol = lbr[:protocol]
      lbrs.backend_port = lbr[:backend_port]
      lbrs.frontend_port = lbr[:frontend_port]
      lbrs.idle_timeout_in_minutes = lbr[:idle_timeout_in_minutes]
      lbrs.enable_floating_ip = lbr[:enable_floating_ip]
      lbrs.load_distribution = lbr[:load_distribution]
      lbrs.frontend_ipconfiguration = frontend_ipconf_sub
      lbrs.backend_address_pool = backend_ipconf_sub
      lbrs.probe = probe_sub
    end
  end

  lb = Azure::ARM::Network::Models::LoadBalancer.new.tap do |lbinfo|
    lbinfo.location = new_resource.location
    lbinfo.tags = tags
    lbinfo.frontend_ipconfigurations = [frontend_ipconf]
    lbinfo.backend_address_pools = backend_pool
    unless new_resource.load_balancer_options[:inboundnat].nil?
      lbinfo.inbound_nat_rules = [inboudnat]
    end
    lbinfo.probes = [probe]
    lbinfo.load_balancing_rules = [lbrules]
  end

  lb_update(lb) # update additions build
end
destroy_load_balancer() click to toggle source
# File lib/chef/provider/azure_load_balancer.rb, line 44
def destroy_load_balancer
  action_handler.report_progress 'Destroying Load Balancer...'
  try_azure_operation('destroying load balancer') do
    network_management_client.load_balancers.delete(new_resource.resource_group, new_resource.name)
  end
end
does_load_balancer_exist() click to toggle source
# File lib/chef/provider/azure_load_balancer.rb, line 38
def does_load_balancer_exist
  try_azure_operation('listing load balancers', true) do
    network_management_client.load_balancers.get(new_resource.resource_group, new_resource.name)
  end
end
lb_update(lb) click to toggle source
# File lib/chef/provider/azure_load_balancer.rb, line 52
def lb_update(lb)
  network_management_client.load_balancers.create_or_update(new_resource.resource_group, new_resource.name, lb)
end
whyrun_supported?() click to toggle source
# File lib/chef/provider/azure_load_balancer.rb, line 8
def whyrun_supported?
  true
end