Skip to content

Instantly share code, notes, and snippets.

@jd
Last active July 21, 2017 16:12
Show Gist options
  • Save jd/a9a0e9ba1649fa56c53ab8206d55f35b to your computer and use it in GitHub Desktop.
Save jd/a9a0e9ba1649fa56c53ab8206d55f35b to your computer and use it in GitHub Desktop.
Deleting Gnocchi metric with Keystone auth
#!/usr/bin/env python
import os
from concurrent.futures import thread
from gnocchiclient import client
from keystoneauth1 import identity
from keystoneauth1 import session
auth = identity.Password(auth_url=os.getenv("OS_AUTH_URL"),
username=os.getenv("OS_USERNAME"),
project_name=os.getenv("OS_PROJECT_NAME"),
password=os.getenv("OS_PASSWORD"))
# import gnocchiclient.auth
# auth = gnocchiclient.auth.GnocchiBasicPlugin(
# "admin", os.getenv("PIFPAF_GNOCCHI_HTTP_URL"))
g = client.Client(1, session.Session(auth=auth))
def delete_metric(g, metric_id):
try:
g.metric.delete(metric_id)
except Exception as e:
print("Unable to delete metric '%s': %s" % (metric_id, e))
else:
print("Deleted metric '%s'" % metric_id)
last_metric_id = None
with thread.ThreadPoolExecutor(max_workers=10) as p:
while True:
metrics = list(g.metric.list(limit=1000, marker=last_metric_id,
sorts=["id:desc"]))
if not metrics:
break
for metric in metrics[:]:
if metric['archive_policy']['name'] == 'low':
metrics.remove(metric)
p.submit(delete_metric, g, metric['id'])
if metrics:
last_metric_id = metrics[-1]['id']
else:
last_metric_id = None
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment