azure_rm_webapp: fix updating app settings (#49181)

This commit is contained in:
Yunge Zhu 2019-01-04 16:32:36 +08:00 committed by GitHub
parent 06f0cde97d
commit 83da2de9a0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 40 additions and 27 deletions

View file

@ -388,7 +388,7 @@ def webapp_to_dict(webapp):
class Actions: class Actions:
NoAction, CreateOrUpdate, UpdateAppSettings, Delete = range(4) CreateOrUpdate, UpdateAppSettings, Delete = range(3)
class AzureRMWebApps(AzureRMModuleBase): class AzureRMWebApps(AzureRMModuleBase):
@ -504,7 +504,7 @@ class AzureRMWebApps(AzureRMModuleBase):
id=None, id=None,
) )
self.state = None self.state = None
self.to_do = Actions.NoAction self.to_do = []
self.frameworks = None self.frameworks = None
@ -642,7 +642,7 @@ class AzureRMWebApps(AzureRMModuleBase):
self.log("Web App instance doesn't exist") self.log("Web App instance doesn't exist")
to_be_updated = True to_be_updated = True
self.to_do = Actions.CreateOrUpdate self.to_do.append(Actions.CreateOrUpdate)
self.site.tags = self.tags self.site.tags = self.tags
# service plan is required for creation # service plan is required for creation
@ -688,19 +688,19 @@ class AzureRMWebApps(AzureRMModuleBase):
# check if root level property changed # check if root level property changed
if self.is_updatable_property_changed(old_response): if self.is_updatable_property_changed(old_response):
to_be_updated = True to_be_updated = True
self.to_do = Actions.CreateOrUpdate self.to_do.append(Actions.CreateOrUpdate)
# check if site_config changed # check if site_config changed
old_config = self.get_webapp_configuration() old_config = self.get_webapp_configuration()
if self.is_site_config_changed(old_config): if self.is_site_config_changed(old_config):
to_be_updated = True to_be_updated = True
self.to_do = Actions.CreateOrUpdate self.to_do.append(Actions.CreateOrUpdate)
# check if linux_fx_version changed # check if linux_fx_version changed
if old_config.linux_fx_version != self.site_config.get('linux_fx_version', ''): if old_config.linux_fx_version != self.site_config.get('linux_fx_version', ''):
to_be_updated = True to_be_updated = True
self.to_do = Actions.CreateOrUpdate self.to_do.append(Actions.CreateOrUpdate)
self.app_settings_strDic = self.list_app_settings() self.app_settings_strDic = self.list_app_settings()
@ -708,11 +708,12 @@ class AzureRMWebApps(AzureRMModuleBase):
if self.purge_app_settings: if self.purge_app_settings:
to_be_updated = True to_be_updated = True
self.app_settings_strDic.properties = dict() self.app_settings_strDic.properties = dict()
self.to_do.append(Actions.UpdateAppSettings)
# check if app settings changed # check if app settings changed
if self.purge_app_settings or self.is_app_settings_changed(): if self.purge_app_settings or self.is_app_settings_changed():
to_be_updated = True to_be_updated = True
self.to_do = Actions.CreateOrUpdate self.to_do.append(Actions.UpdateAppSettings)
if self.app_settings: if self.app_settings:
for key in self.app_settings.keys(): for key in self.app_settings.keys():
@ -740,11 +741,15 @@ class AzureRMWebApps(AzureRMModuleBase):
if self.check_mode: if self.check_mode:
return self.results return self.results
if self.to_do == Actions.CreateOrUpdate: if Actions.CreateOrUpdate in self.to_do:
response = self.create_update_webapp() response = self.create_update_webapp()
self.results['id'] = response['id'] self.results['id'] = response['id']
if Actions.UpdateAppSettings in self.to_do:
update_response = self.update_app_settings()
self.results['id'] = update_response.id
webapp = None webapp = None
if old_response: if old_response:
webapp = old_response webapp = old_response
@ -785,14 +790,12 @@ class AzureRMWebApps(AzureRMModuleBase):
# comparing existing app setting with input, determine whether it's changed # comparing existing app setting with input, determine whether it's changed
def is_app_settings_changed(self): def is_app_settings_changed(self):
if self.app_settings: if self.app_settings:
if len(self.app_settings_strDic.properties) != len(self.app_settings): if self.app_settings_strDic.properties:
return True
elif self.app_settings_strDic.properties and len(self.app_settings_strDic.properties) > 0:
for key in self.app_settings.keys(): for key in self.app_settings.keys():
if not self.app_settings_strDic.properties.get(key) \ if self.app_settings[key] != self.app_settings_strDic.properties.get(key, None):
or self.app_settings[key] != self.app_settings_strDic.properties[key]:
return True return True
else:
return True
return False return False
# comparing deployment source with input, determine wheather it's changed # comparing deployment source with input, determine wheather it's changed
@ -943,10 +946,8 @@ class AzureRMWebApps(AzureRMModuleBase):
return response return response
except CloudError as ex: except CloudError as ex:
self.log("Failed to list application settings for web app {0} in resource group {1}".format( self.fail("Failed to list application settings for web app {0} in resource group {1}: {2}".format(
self.name, self.resource_group)) self.name, self.resource_group, str(ex)))
return False
def update_app_settings(self): def update_app_settings(self):
''' '''
@ -960,12 +961,10 @@ class AzureRMWebApps(AzureRMModuleBase):
resource_group_name=self.resource_group, name=self.name, app_settings=self.app_settings_strDic) resource_group_name=self.resource_group, name=self.name, app_settings=self.app_settings_strDic)
self.log("Response : {0}".format(response)) self.log("Response : {0}".format(response))
return response.as_dict() return response
except CloudError as ex: except CloudError as ex:
self.log("Failed to update application settings for web app {0} in resource group {1}".format( self.fail("Failed to update application settings for web app {0} in resource group {1}: {2}".format(
self.name, self.resource_group)) self.name, self.resource_group, str(ex)))
return False
def create_or_update_source_control(self): def create_or_update_source_control(self):
''' '''

View file

@ -101,24 +101,38 @@
- output.webapps[0].app_settings | length == 1 - output.webapps[0].app_settings | length == 1
- output.webapps[0].frameworks | length > 1 # there's default frameworks eg net_framework - output.webapps[0].frameworks | length > 1 # there's default frameworks eg net_framework
- name: Update app settings - name: Update app settings and framework
azure_rm_webapp: azure_rm_webapp:
resource_group: "{{ resource_group }}" resource_group: "{{ resource_group }}"
name: "{{ win_app_name }}4" name: "{{ win_app_name }}4"
plan: "{{ win_plan_name }}" plan: "{{ win_plan_name }}"
frameworks: frameworks:
- name: "java" - name: "java"
version: "1.8" version: "1.7"
settings: settings:
java_container: "Tomcat" java_container: "Tomcat"
java_container_version: "8.0" java_container_version: "8.5"
app_settings: app_settings:
testkey2: "testvalue2" testkey2: "testvalue2"
register: output register: output
- name: Assert the web app was updated - name: Assert the web app was updated
assert: assert:
that: output.changed that:
- output.changed
- name: get web app with name
azure_rm_webapp_facts:
resource_group: "{{ resource_group }}"
name: "{{ win_app_name }}4"
register: output
- name: Assert updating
assert:
that:
- output.webapps[0].app_settings | length == 2
- output.webapps[0].app_settings['testkey'] == 'testvalue'
- output.webapps[0].app_settings['testkey2'] == 'testvalue2'
- name: get web app with return publishing profile - name: get web app with return publishing profile
azure_rm_webapp_facts: azure_rm_webapp_facts: