Skip to content

Commit

Permalink
[cloud] ECS Service load balancer modification graceful fail (ansible…
Browse files Browse the repository at this point in the history
…#32876)

It is not possible to modify the load balancer configuration
for ECS Service.

As it is possible to detect this, it's nicer to fail gracefully
than return AWS's less meaningful failure message.

Fix PEP8 compliance
  • Loading branch information
willthames authored and ryansb committed Jan 22, 2018
1 parent b24d502 commit 142cacf
Showing 1 changed file with 10 additions and 26 deletions.
36 changes: 10 additions & 26 deletions lib/ansible/modules/cloud/amazon/ecs_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -334,32 +334,17 @@ def create_service(self, service_name, cluster_name, task_definition, load_balan
deploymentConfiguration=deployment_configuration,
placementConstraints=placement_constraints,
placementStrategy=placement_strategy)
return self.jsonize(response['service'])
return response['service']

def update_service(self, service_name, cluster_name, task_definition,
load_balancers, desired_count, client_token, role, deployment_configuration):
desired_count, deployment_configuration):
response = self.ecs.update_service(
cluster=cluster_name,
service=service_name,
taskDefinition=task_definition,
desiredCount=desired_count,
deploymentConfiguration=deployment_configuration)
return self.jsonize(response['service'])

def jsonize(self, service):
# some fields are datetime which is not JSON serializable
# make them strings
if 'deployments' in service:
for d in service['deployments']:
if 'createdAt' in d:
d['createdAt'] = str(d['createdAt'])
if 'updatedAt' in d:
d['updatedAt'] = str(d['updatedAt'])
if 'events' in service:
for e in service['events']:
if 'createdAt' in e:
e['createdAt'] = str(e['createdAt'])
return service
return response['service']

def delete_service(self, service, cluster=None):
return self.ecs.delete_service(cluster=cluster, service=service)
Expand Down Expand Up @@ -414,31 +399,30 @@ def main():
if existing and 'status' in existing and existing['status'] == "ACTIVE":
if service_mgr.is_matching_service(module.params, existing):
matching = True
results['service'] = service_mgr.jsonize(existing)
results['service'] = existing
else:
update = True

if not matching:
if not module.check_mode:
loadBalancers = module.params['load_balancers']
for loadBalancer in loadBalancers:
if 'containerPort' in loadBalancer:
loadBalancer['containerPort'] = int(loadBalancer['containerPort'])

role = module.params['role']
clientToken = module.params['client_token']
loadBalancers = module.params['load_balancers']

if update:
if (existing['loadBalancers'] or []) != loadBalancers:
module.fail_json(msg="It is not possible to update the load balancers of an existing service")
# update required
response = service_mgr.update_service(module.params['name'],
module.params['cluster'],
module.params['task_definition'],
loadBalancers,
module.params['desired_count'],
clientToken,
role,
deploymentConfiguration)
else:
for loadBalancer in loadBalancers:
if 'containerPort' in loadBalancer:
loadBalancer['containerPort'] = int(loadBalancer['containerPort'])
# doesn't exist. create it.
response = service_mgr.create_service(module.params['name'],
module.params['cluster'],
Expand Down

0 comments on commit 142cacf

Please sign in to comment.