Skip to content

Instantly share code, notes, and snippets.

@jd jd/handson.py
Last active Apr 25, 2016

Embed
What would you like to do?
Hands-on Gnocchi + CK @ OpenStack Summit Newton
#!/usr/bin/env python
import uuid
import logging
import os
import random
import sys
from keystoneauth1.identity import v3
from keystoneauth1 import session
from keystoneclient.v3 import client as ks
from gnocchiclient import client
from gnocchiclient import noauth
from gnocchiclient import exceptions
LOG = logging.getLogger('')
LOG.setLevel(logging.DEBUG)
requests_log = logging.getLogger("requests")
requests_log.setLevel(logging.DEBUG)
console = logging.StreamHandler(sys.stderr)
LOG.addHandler(console)
auth = v3.Password(auth_url='http://localhost:5000/v3',
username='gnocchi',
password='password',
project_name='service',
user_domain_id='default',
project_domain_id='default')
# auth = noauth.GnocchiNoAuthPlugin(
# user_id=os.getenv("GNOCCHI_USER_ID"),
# project_id=os.getenv("GNOCCHI_PROJECT_ID"),
# roles="admin",
# endpoint=os.getenv("GNOCCHI_ENDPOINT"),
# )
session = session.Session(auth=auth)
ks_client = ks.Client(session=session)
for user in ks_client.users.get():
if user.name == "summit_user":
user_id = user.id
break
for project in ks_client.projects.get():
if project.name == "summit":
project_id = project.id
break
c = client.Client('1', session=session)
try:
c.archive_policy.create({
"name": "medium",
"definition":
[
# 1 minute over 1 hour
{"granularity": 60, "points": 60},
# 1 hour over 1 month
{"granularity": 3600, "points": 30 * 24},
# 1 day over a year
{"granularity": 3600 * 24, "points": 365},
]
})
except exceptions.ArchivePolicyAlreadyExists:
pass
images = [
"foo",
"bar",
]
flavors = [
"fooflavor",
"barflavor",
]
instances = [
c.resource.create("instance",
{
"id": str(uuid.uuid4()),
"user_id": user_id,
"project_id": project_id,
"started_at": "2016-04-%02dT12:%02d:00+00:00" % (i, random.randint(0, 29)),
"ended_at": "2016-04-%02dT12:%02d:00+00:00" % (i + 1, random.randint(30, 59)),
"flavor_id": random.choice(flavors),
"host": "compute1",
"image_ref": random.choice(images),
"display_name": "instance%d" % i,
"metrics":
{
"memory": {"archive_policy_name": "medium"},
"vcpus": {"archive_policy_name": "medium"},
"cpu": {"archive_policy_name": "medium"},
},
})
for i in range(1, 21)
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.