Skip to content
This repository was archived by the owner on Jun 30, 2025. It is now read-only.

Commit 008d4ef

Browse files
committed
basic check mode support
this adds basic check mode support by skipping all API calls that could trigger changes. it does *NOT* update the service object that is returned, so while the changed flag will be correct, the service object will still reflect the state *before* the changes. Fixes: #1
1 parent 3971e09 commit 008d4ef

File tree

1 file changed

+23
-12
lines changed

1 file changed

+23
-12
lines changed

library/fastly_service.py

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1076,15 +1076,19 @@ def __init__(self, changed, actions, service):
10761076

10771077

10781078
class FastlyStateEnforcer(object):
1079-
def __init__(self, client):
1079+
def __init__(self, client, check_mode=False):
10801080
self.client = client
1081+
self.check_mode = check_mode
10811082

10821083
def apply_configuration(self, service_name, fastly_configuration, activate_new_version=True):
10831084
actions = []
10841085
service = self.client.get_service_by_name(service_name)
10851086

10861087
if service is None:
1087-
service = self.client.create_service(service_name)
1088+
if not self.check_mode:
1089+
service = self.client.create_service(service_name)
1090+
else:
1091+
service = FastlyService({'id': service_name, 'name': service_name, 'active_version': None, 'version': None})
10881092
actions.append("Created new service %s" % service_name)
10891093

10901094
if service.active_version is not None:
@@ -1097,20 +1101,24 @@ def apply_configuration(self, service_name, fastly_configuration, activate_new_v
10971101

10981102
if hasNoVersion or hasChanged:
10991103
if hasNoVersion:
1100-
version_number = self.create_new_version(service.id)
1104+
if not self.check_mode:
1105+
version_number = self.create_new_version(service.id)
11011106
actions.append("Created a new version because service has no active version")
11021107
elif hasChanged:
1103-
version_number = self.clone_version(service.id, current_version.number)
1108+
if not self.check_mode:
1109+
version_number = self.clone_version(service.id, current_version.number)
1110+
self.reset_version(service.id, version_number)
11041111
actions.append("Cloned an old version because service configuration was not up to date")
1105-
self.reset_version(service.id, version_number)
11061112

1107-
self.configure_version(service.id, fastly_configuration, version_number)
1113+
if not self.check_mode:
1114+
self.configure_version(service.id, fastly_configuration, version_number)
11081115

1109-
if activate_new_version:
1110-
self.client.activate_version(service.id, version_number)
1116+
if activate_new_version:
1117+
self.client.activate_version(service.id, version_number)
11111118

11121119
changed = len(actions) > 0
1113-
service = self.client.get_service(service.id)
1120+
if not self.check_mode:
1121+
service = self.client.get_service(service.id)
11141122
return FastlyStateEnforcerResult(actions=actions, changed=changed, service=service)
11151123

11161124
def create_new_version(self, service_id):
@@ -1230,7 +1238,10 @@ def delete_service(self, service_name):
12301238

12311239
actions = []
12321240

1233-
changed = self.client.delete_service(service_name)
1241+
if not self.check_mode:
1242+
changed = self.client.delete_service(service_name)
1243+
else:
1244+
changed = True
12341245

12351246
if changed:
12361247
actions.append('Deleted service %s' % service_name)
@@ -1261,7 +1272,7 @@ def __init__(self):
12611272
syslogs=dict(default=None, required=False, type='list'),
12621273
settings=dict(default=None, required=False, type='dict'),
12631274
),
1264-
supports_check_mode=False
1275+
supports_check_mode=True
12651276
)
12661277

12671278
def enforcer(self):
@@ -1271,7 +1282,7 @@ def enforcer(self):
12711282
fastly_api_key = os.environ['FASTLY_API_KEY']
12721283
else:
12731284
self.module.fail_json(msg="A Fastly API key is required for this module. Please set it and try again")
1274-
return FastlyStateEnforcer(FastlyClient(fastly_api_key))
1285+
return FastlyStateEnforcer(FastlyClient(fastly_api_key), self.module.check_mode)
12751286

12761287
def configuration(self):
12771288
try:

0 commit comments

Comments
 (0)