Skip to content

Instantly share code, notes, and snippets.

@PerStirpes
Last active August 11, 2020 00:48
Show Gist options
  • Save PerStirpes/46e0391376e0fa9259ad3fc53facad0a to your computer and use it in GitHub Desktop.
Save PerStirpes/46e0391376e0fa9259ad3fc53facad0a to your computer and use it in GitHub Desktop.
Get Audit Log Feed
from future import print_function
import time
import launchdarkly_api
import csv
import datetime
Configure API key authorization: Token
configuration = launchdarkly_api.Configuration()
configuration.api_key['Authorization'] = 'api'
# Uncomment below to setup prefix (e.g. Bearer) for API key, if needed
configuration.api_key_prefix['Authorization'] = 'Bearer'
# create an instance of the API classes
project_api_instance = launchdarkly_api.ProjectsApi(launchdarkly_api.ApiClient(configuration))
flag_api_instance = launchdarkly_api.FeatureFlagsApi(launchdarkly_api.ApiClient(configuration))
# Returns a list of all projects in the account.
project_api_response = project_api_instance.get_projects()
projects = project_api_response.items
# open new CSV
with open('launchdarkly.csv', 'w') as csvfile:
# set header names
fieldnames = ['project_name', 'environment_name', 'flag_name', 'flag_created_at', 'flag_enabled', 'flag_deleted', 'feature_id', 'feature_comment']
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
writer.writeheader()
# for each project...
for project in projects:
# ...and each environment in those projects...
for environment in project.environments:
# Returns a list of all flag configurations in this environment
flag_api_response = flag_api_instance.get_feature_flags(project.key, env=environment.key)
flags = flag_api_response.items
# capture data
project_name = project.name
environment_name = environment.name
# for each flag in environment
for flag in flags:
# capture more data
flag_name = flag.name
base_datetime = datetime.datetime( 1970, 1, 1 )
delta = datetime.timedelta( 0, 0, 0, flag.creation_date )
flag_created_at = base_datetime + delta
flag_enabled = flag.environments[environment.key].on
flag_deleted = False
# if flag has custom attributes, capture it
feature_id = flag.tags
if "feature_comment" in flag.custom_properties:
feature_comment = flag.custom_properties.feature_comment
else:
feature_comment = "no comment"
# write row
writer.writerow({
'project_name': project_name,
'environment_name': environment_name,
'flag_name': flag_name,
'flag_created_at': flag_created_at,
'flag_enabled': flag_enabled,
'flag_deleted': flag_deleted,
'feature_id': feature_id,
'feature_comment': feature_comment
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment