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

start/stop/restart azure webapp #44498

Merged
merged 8 commits into from
Aug 28, 2018
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
60 changes: 59 additions & 1 deletion lib/ansible/modules/cloud/azure/azure_rm_webapp.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,16 @@
- Purge any existing application settings. Replace web app application settings with app_settings.
type: bool

app_state:
description:
- Start/Stop/Restart the web app.
type: str
choices:
- started
- stopped
- restarted
default: started

state:
description:
- Assert the state of the Web App.
Expand Down Expand Up @@ -410,6 +420,11 @@ def __init__(self):
type='bool',
default=False
),
app_state=dict(
type='str',
choices=['started', 'stopped', 'restarted'],
default='started'
),
state=dict(
type='str',
default='present',
Expand Down Expand Up @@ -450,6 +465,7 @@ def __init__(self):
self.container_settings = None

self.purge_app_settings = False
self.app_state = 'started'

self.results = dict(
changed=False,
Expand Down Expand Up @@ -595,6 +611,7 @@ def exec_module(self, **kwargs):

to_be_updated = True
self.to_do = Actions.CreateOrUpdate
self.site.tags = self.tags

# service plan is required for creation
if not self.plan:
Expand Down Expand Up @@ -631,7 +648,7 @@ def exec_module(self, **kwargs):

self.log('Result: {0}'.format(old_response))

update_tags, old_response['tags'] = self.update_tags(old_response.get('tags', dict()))
update_tags, self.site.tags = self.update_tags(old_response.get('tags', None))

if update_tags:
to_be_updated = True
Expand Down Expand Up @@ -693,8 +710,26 @@ def exec_module(self, **kwargs):

if self.to_do == Actions.CreateOrUpdate:
response = self.create_update_webapp()

self.results['id'] = response['id']

webapp = None
if old_response:
webapp = old_response
if response:
webapp = response

if webapp:
if (webapp['state'] != 'Stopped' and self.app_state == 'stopped') or \
(webapp['state'] != 'Running' and self.app_state == 'started') or \
self.app_state == 'restarted':

self.results['changed'] = True
if self.check_mode:
return self.results

self.set_webapp_state(self.app_state)

return self.results

# compare existing web app with input, determine weather it's update operation
Expand Down Expand Up @@ -943,6 +978,29 @@ def get_webapp_configuration(self):

return False

def set_webapp_state(self, appstate):
'''
Start/stop/restart web app
:return: deserialized updating response
'''
try:
if appstate == 'started':
response = self.web_client.web_apps.start(resource_group_name=self.resource_group, name=self.name)
elif appstate == 'stopped':
response = self.web_client.web_apps.stop(resource_group_name=self.resource_group, name=self.name)
elif appstate == 'restarted':
response = self.web_client.web_apps.restart(resource_group_name=self.resource_group, name=self.name)
else:
self.fail("Invalid web app state {0}".format(appstate))

self.log("Response : {0}".format(response))

return response
except CloudError as ex:
request_id = ex.request_id if ex.request_id else ''
self.log("Failed to {0} web app {1} in resource group {2}, request_id {3} - {4}".format(
appstate, self.name, self.resource_group, request_id, str(ex)))


def main():
"""Main execution"""
Expand Down
27 changes: 26 additions & 1 deletion test/integration/targets/azure_rm_webapp/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,31 @@
plan: "{{ win_plan_name }}"
register: output

- name: stop the web app
azure_rm_webapp:
resource_group: "{{ resource_group }}"
name: "{{ win_app_name }}2"
plan: "{{ win_plan_name }}"
app_state: stopped
register: output

- name: assert output changed
assert:
that:
output.changed

# enable after webapp_facts merged
# - name: get the web app
# azure_rm_webapp_facts:
# resource_group: "{{ resource_group }}"
# name: "{{ win_app_name }}2"
# register: stopped

# - name: assert web app is stopped
# assert:
# that:
# - stopped.properties.state == "Stopped"

- name: Create a windows web app with existing app service plan, try to update some root level params
azure_rm_webapp:
resource_group: "{{ resource_group }}"
Expand Down Expand Up @@ -251,4 +276,4 @@

- name: Assert the web app was created
assert:
that: output.changed
that: output.changed