Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

appservice: support to get external ip address used for DNS A records #2627

Merged
merged 3 commits into from
Mar 27, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,11 @@
short-summary: List all hostname bindings.
"""

helps['appservice web config hostname get-external-ip'] = """
type: command
short-summary: get the ip address to configure your DNS settings for A records
"""

helps['appservice web config backup list'] = """
type: command
short-summary: List all backups of a web app.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ def transform_web_list_output(webs):
cli_command(__name__, 'appservice web config hostname add', 'azure.cli.command_modules.appservice.custom#add_hostname')
cli_command(__name__, 'appservice web config hostname list', 'azure.cli.command_modules.appservice.custom#list_hostnames')
cli_command(__name__, 'appservice web config hostname delete', 'azure.cli.command_modules.appservice.custom#delete_hostname')
cli_command(__name__, 'appservice web config hostname get-external-ip', 'azure.cli.command_modules.appservice.custom#get_external_ip')
cli_command(__name__, 'appservice web config container update', 'azure.cli.command_modules.appservice.custom#update_container_settings')
cli_command(__name__, 'appservice web config container delete', 'azure.cli.command_modules.appservice.custom#delete_container_settings')
cli_command(__name__, 'appservice web config container show', 'azure.cli.command_modules.appservice.custom#show_container_settings', exception_handler=empty_on_404)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,30 @@ def list_hostnames(resource_group_name, webapp_name, slot=None):
slot)


def get_external_ip(resource_group_name, webapp_name):
# logics here are ported from portal
client = web_client_factory()
webapp = client.web_apps.get(resource_group_name, webapp_name)
if webapp.hosting_environment_profile:
address = client.app_service_environments.list_vips(
resource_group_name, webapp.hosting_environment_profile.name)
if address.internal_ip_address:
ip_address = address.internal_ip_address
else:
vip = next((s for s in webapp.host_name_ssl_states
if s.ssl_state == SslState.ip_based_enabled), None)
ip_address = (vip and vip.virtual_ip) or address.service_ip_address
else:
ip_address = _resolve_hostname_through_dns(webapp.default_host_name)

return {'ip': ip_address}


def _resolve_hostname_through_dns(hostname):
import socket
return socket.gethostbyname(hostname)


def create_webapp_slot(resource_group_name, webapp, slot, configuration_source=None):
client = web_client_factory()
site = client.web_apps.get(resource_group_name, webapp)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,20 @@

from msrestazure.azure_exceptions import CloudError
from azure.mgmt.web.models import (SourceControl, HostNameBinding, Site, SiteConfig,
HostNameSslState, SslState, Certificate)
HostNameSslState, SslState, Certificate,
AddressResponse, HostingEnvironmentProfile)
from azure.mgmt.web import WebSiteManagementClient
from azure.cli.core.adal_authentication import AdalAuthentication
from azure.cli.core._util import CLIError
from azure.cli.command_modules.appservice.custom import (set_deployment_user,
update_git_token, add_hostname,
update_site_configs,
get_external_ip,
view_in_browser,
sync_site_repo,
list_publish_profiles,
_match_host_names_from_cert,
bind_ssl_cert)
bind_ssl_cert,
list_publish_profiles)

# pylint: disable=line-too-long

Expand Down Expand Up @@ -83,6 +85,67 @@ def test_set_domain_name(self, client_factory_mock):
# assert
self.assertEqual(result.name, domain)

@mock.patch('azure.cli.command_modules.appservice.custom.web_client_factory', autospec=True)
def test_get_external_ip_from_ase(self, client_factory_mock):
client = mock.Mock()
client_factory_mock.return_value = client
# set up the web inside a ASE, with an ip based ssl binding
host_env = HostingEnvironmentProfile('id11')
host_env.name = 'ase1'
host_env.resource_group = 'myRg'

host_ssl_state = HostNameSslState(ssl_state=SslState.ip_based_enabled, virtual_ip='1.2.3.4')
client.web_apps.get.return_value = Site('antarctica', hosting_environment_profile=host_env,
host_name_ssl_states=[host_ssl_state])
client.app_service_environments.list_vips.return_value = AddressResponse()

# action
result = get_external_ip('myRg', 'myWeb')

# assert, we return the virtual ip from the ip based ssl binding
self.assertEqual('1.2.3.4', result['ip'])

# tweak to have no ip based ssl binding, but it is in an internal load balancer
host_ssl_state2 = HostNameSslState(ssl_state=SslState.sni_enabled)
client.web_apps.get.return_value = Site('antarctica', hosting_environment_profile=host_env,
host_name_ssl_states=[host_ssl_state2])
client.app_service_environments.list_vips.return_value = AddressResponse(internal_ip_address='4.3.2.1')

# action
result = get_external_ip('myRg', 'myWeb')

# assert, we take the ILB address
self.assertEqual('4.3.2.1', result['ip'])

# tweak to have no ip based ssl binding, and not in internal load balancer
host_ssl_state2 = HostNameSslState(ssl_state=SslState.sni_enabled)
client.web_apps.get.return_value = Site('antarctica', hosting_environment_profile=host_env,
host_name_ssl_states=[host_ssl_state2])
client.app_service_environments.list_vips.return_value = AddressResponse(service_ip_address='1.1.1.1')

# action
result = get_external_ip('myRg', 'myWeb')

# assert, we take service ip
self.assertEqual('1.1.1.1', result['ip'])

@mock.patch('azure.cli.command_modules.appservice.custom.web_client_factory', autospec=True)
@mock.patch('azure.cli.command_modules.appservice.custom._resolve_hostname_through_dns', autospec=True)
def test_get_external_ip_from_dns(self, resolve_hostname_mock, client_factory_mock):
client = mock.Mock()
client_factory_mock.return_value = client

# set up the web inside a ASE, with an ip based ssl binding
site = Site('antarctica')
site.default_host_name = 'myweb.com'
client.web_apps.get.return_value = site

# action
get_external_ip('myRg', 'myWeb')

# assert, we return the virtual ip from the ip based ssl binding
resolve_hostname_mock.assert_called_with('myweb.com')

@mock.patch('azure.cli.command_modules.appservice.custom._generic_site_operation', autospec=True)
def test_update_site_config(self, site_op_mock):
site_config = SiteConfig('antarctica')
Expand Down