Ejemplo n.º 1
0
def auth_token(request):
    try:
        status=request.session["status"]
        token_data=request.session["token_data"] 
        if status==True:
            return HttpResponseRedirect('/admin/')
        else:
            if request.method == 'POST':
                username=request.POST['user']
                password=request.POST['pass']
                domain=request.POST['domain']
                project=request.POST['project']
                auth_uri = 'http://160.85.4.64:5000'
                status, token_data = keystone_api.get_token_v3(auth_uri,True,username=username, password=password, domain=domain,project=project)
                request.session["status"] = status
                request.session["token_data"] = token_data
                request.session['username']=username
                request.session['password']=password
                request.session['domain']=domain
                request.session['project']=project
                return HttpResponseRedirect('/admin/')
            else:
                messages.warning(request, "Unsuccessful authentication!")
                return render(request,'auth_token.html')
    except KeyError:
        if request.method == 'POST':
            username=request.POST['user']
            password=request.POST['pass']
            domain=request.POST['domain']
            project=request.POST['project']
            auth_uri = 'http://160.85.4.64:5000'
            status, token_data = keystone_api.get_token_v3(auth_uri,True,username=username, password=password, domain=domain,project=project)
            request.session["status"] = status
            request.session["token_data"] = token_data
            request.session['username']=username
            request.session['password']=password
            request.session['domain']=domain
            request.session['project']=project
            if 'next' in request.GET:
                return redirect(request.GET['next'])
            else:
                if settings.GLOBAL_SETTINGS['PRO_DEBUG']==True:
                    return HttpResponseRedirect('/token_data/')
                else:
                    return HttpResponseRedirect('/admin/')
                    
        return render(request,'auth_token.html')
Ejemplo n.º 2
0
def auth_token(request):
    try:
        status=request.session["status"]
        token_data=request.session["token_data"] 
        if status==True:
            return HttpResponseRedirect('/admin/')
        else:
            if request.method == 'POST':
                username=request.POST['user']
                password=request.POST['pass']
                domain=request.POST['domain']
                project=request.POST['project']
                auth_uri = config["AUTH_URI"]
                status, token_data = keystone_api.get_token_v3(auth_uri,True,username=username, password=password, domain=domain,project=project)
                request.session["status"] = status
                request.session["token_data"] = token_data
                request.session['username']=username
                request.session['password']=password
                request.session['domain']=domain
                request.session['project']=project
                return HttpResponseRedirect('/admin/')
            else:
                messages.warning(request, "Unsuccessful authentication!")
                return render(request,'auth_token.html')
    except KeyError:
        if request.method == 'POST':
            username=request.POST['user']
            password=request.POST['pass']
            domain=request.POST['domain']
            project=request.POST['project']
            auth_uri = config["AUTH_URI"]
            status, token_data = keystone_api.get_token_v3(auth_uri,True,username=username, password=password, domain=domain,project=project)
            request.session["status"] = status
            request.session["token_data"] = token_data
            request.session['username']=username
            request.session['password']=password
            request.session['domain']=domain
            request.session['project']=project
            if 'next' in request.GET:
                return redirect(request.GET['next'])
            else:
                if settings.GLOBAL_SETTINGS['PRO_DEBUG']==True:
                    return HttpResponseRedirect('/token_data/')
                else:
                    return HttpResponseRedirect('/admin/')
                    
        return render(request,'auth_token.html')
Ejemplo n.º 3
0
def main(argv):

    print "Hello there. This is a simple periodic counter."
    logger.info('--------------- \n')
    logger.info('Starting periodic ')
    auth_uri = 'http://160.85.4.64:5000' #internal test-setup, replace it with your own value
    #auth_uri = 'http://160.85.231.233:5000' #internal test-setup, replace it with your own value
                    
    status, token_data = keystone_api.get_token_v3(auth_uri,False)
    if status:
        print 'The authentication was successful.'
        print '--------------------------------------------------------------------------------------------------------'
        print 'The authentication token is: ', token_data["token_id"]
        pom=token_data["token_id"]
        user=token_data["user-id"]
        logger.info('Successful authentication for user %s', user)
    else:
        print "Authentication was not successful."
        logger.warn('Authentication not successful')
    if status:
        status, meter_list = ceilometer_api.get_meter_list(pom, token_data["metering"])
        if status:
            logger.info('Printing meters')
            print "The list of available meters are printed next."
            print '--------------------------------------------------------------------------------------------------------------------------'
            print '%1s %16s %2s %10s %2s %10s %2s %70s %1s' % ('|','meter-name', '|', 'meter-type', '|', 'meter-unit', '|', 'meter-id', '|')
            print '--------------------------------------------------------------------------------------------------------------------------'

            for i in range(len(meter_list)):
                print '%1s %16s %2s %10s %2s %10s %2s %70s %1s' % ('|', meter_list[i]["meter-name"], '|', meter_list[i]["meter-type"], '|', meter_list[i]["meter-unit"], '|', meter_list[i]["meter-id"].strip(), '|')         
                logger.info('Meter list %s', meter_list[i]["meter-name"])
            status,meters_used,meters_ids,input,price=pricing(token_data["metering"],meter_list,pom,None)
            logger.info('Defining pricing function. %s', input)
            global conn    
            conn = sqlite3.connect(path2+'/meters.db',check_same_thread=False)
            func=[None]*9
            for i in range(len(input)):
                func[i]=input[i]
            conn.execute("INSERT INTO PRICING_FUNC (USER_ID,PARAM1,SIGN1,PARAM2,SIGN2,PARAM3,SIGN3,PARAM4,SIGN4,PARAM5) \
                       VALUES ( '"+ str(user) +"', '"+ str(func[0]) +"','" +str(func[1]) +"','" +str(func[2]) +"','" + str(func[3]) +" ','"+ str(func[4]) +"' ,' "+ str(func[5])+"' ,' "+ str(func[6]) +"' ,' "+ str(func[7])+"' ,' "+str(func[8])+" ')")
            conn.commit()
            logger.info('Insert pricing function in database')
            cursor = conn.execute("SELECT max(ID)  from PRICING_FUNC")
            id_price = cursor.fetchone()[0]
            if status:
                time=raw_input("Enter the desired time interval in seconds. ")
                if time=="":
                    time=10                
                periodic_counts = [None]*len(meters_used)
                for i in range(len(meters_used)):
                    periodic_counts[i]=[]
                periodic_counter(meters_used,token_data["metering"],pom,periodic_counts,0,time,meters_ids,input,meter_list,id_price) 
                logger.info('Starting counter. Time interval is %s ', time)
                conn.close()
               

    return True
Ejemplo n.º 4
0
class ApiTest(unittest.TestCase):
    auth_uri = 'http://160.85.4.10:5000' 
    compute="http://160.85.4.10:8774/v2/323936522894416b903d3528fa971537"
    metering="http://160.85.4.10:8777"
    status, token_data = keystone_api.get_token_v3(auth_uri)
    for key, value in token_data.iteritems():
        if key=="token-id":
            token_id=value
          
    def test_api(self):
        for key, value in self.token_data.iteritems():
            if key==self.compute:
                self.assertEqual(self.compute, value) 
            if key==self.metering:
                self.assertEqual(self.metering,value)
Ejemplo n.º 5
0
def auth_token_user(request):
    try:
        status=request.session["status"]
        token_data=request.session["token_data"] 
        return HttpResponseRedirect('/user/')
    except KeyError:
        if request.method == 'POST':
            username=request.POST['user']
            password=request.POST['pass']
            domain=request.POST['domain']
            project=request.POST['project']
            auth_uri = config["AUTH_URI"]
            status, token_data = keystone_api.get_token_v3(auth_uri,True,username=username, password=password, domain=domain,project=project)
            request.session["status"] = status
            request.session["token_data"] = token_data
            if 'next' in request.GET:
                return redirect(request.GET['next'])
            else:
                return HttpResponseRedirect('/user/')
                    
        return render(request,'auth_token_user.html')
Ejemplo n.º 6
0
def auth_token_user(request):
    try:
        status=request.session["status"]
        token_data=request.session["token_data"] 
        return HttpResponseRedirect('/user/')
    except KeyError:
        if request.method == 'POST':
            username=request.POST['user']
            password=request.POST['pass']
            domain=request.POST['domain']
            project=request.POST['project']
            auth_uri = 'http://160.85.4.64:5000'
            status, token_data = keystone_api.get_token_v3(auth_uri,True,username=username, password=password, domain=domain,project=project)
            request.session["status"] = status
            request.session["token_data"] = token_data
            if 'next' in request.GET:
                return redirect(request.GET['next'])
            else:
                return HttpResponseRedirect('/user/')
                    
        return render(request,'auth_token_user.html')
Ejemplo n.º 7
0
def main(argv):
    print "Hello There. This is a simple test pricing function."
    auth_uri = 'http://160.85.4.64:5000' #internal test-setup, replace it with your own value
    status, token_data = keystone_api.get_token_v3(auth_uri)
    if status:
        print 'The authentication was successful.'
        print '--------------------------------------------------------------------------------------------------------'
        print 'The authentication token is: ', token_data["token-id"]
        pom=token_data["token-id"]
        logger.info('Authentication was successful')
    else:
        print "Authentication was not successful."
        logger.info('Authentication was not successful')
    if status:
        status, meter_list = ceilometer_api.get_meter_list(pom, token_data["metering"])
        if status:
            print "The list of available meters are printed next."
            print '--------------------------------------------------------------------------------------------------------------------------'
            print '%1s %16s %2s %10s %2s %10s %2s %70s %1s' % ('|','meter-name', '|', 'meter-type', '|', 'meter-unit', '|', 'meter-id', '|')
            print '--------------------------------------------------------------------------------------------------------------------------'
            for i in range(len(meter_list)):
                print '%1s %16s %2s %10s %2s %10s %2s %70s %1s' % ('|', meter_list[i]["meter-name"], '|', meter_list[i]["meter-type"], '|', meter_list[i]["meter-unit"], '|', meter_list[i]["meter-id"].strip(), '|')
            print '--------------------------------------------------------------------------------------------------------------------------'
 
            #user defined price function; usage of white space for parsing the data
            price=0
            price_def=raw_input("Define the pricing function. Use only the meters from above and numbers as arguments. Use the following signs: '+' for sum, '-' for substraction, '*' for multiplying, '/' for division or '%' for percentage. Use whitespace in between. ")
            price_def=price_def.split(" ")
            if len(price_def)>9:
                print "You can use only 5 parameters"
                logger.warn('Pricing function not properly defined')
                price_def=raw_input("Define the pricing function. Use only the meters from above and numbers as arguments. Use the following signs: '+' for sum, '-' for substraction, '*' for multiplying, '/' for division or '%' for percentage. Use whitespace in between. ")            
            
            meters_used=[None]
            
            for i in range(len(price_def)):
                j=0
                while j<len(meter_list):
                    if price_def[i]==meter_list[j]["meter-name"]:
                        meters_used.append(price_def[i])
                        print "Enter query arguments."
                        status,sample_list=ceilometer_api.get_meter_samples(price_def[i],token_data["metering"],pom,True,meter_list)
                        logger.info('Getting meter samples')
                        if sample_list==[]:
                            price_def[i]=str(0)

                        #replace the meter name with the metered data
                        for n,m in enumerate(price_def):
                            if m==price_def[i]:
                                for k in range(len(sample_list)):
                                    price_def[n]=str(sample_list[k]["counter-volume"])                                 
                        break
                    else:
                        j=j+1
                       
            
            status_ret=True 
            
            for i in range(len(price_def)):
                if i==0:   
                     if is_number(price_def[i]):    
                         price=price+float(price_def[i]) 
                                  
                     else:
                         status_ret=False  
                if i%2!=0:
                    if price_def[i] in ["+","-","*","/","%"]:
   
                            #check if every other parameter is a number and the parameters in between math signs
                            if is_number(price_def[i+1]):
                                 x=float(price_def[i+1])
                                
                            else:
                                status_ret=False
                                break
                            
                            if price_def[i]=="+":
                                price=price+x
                            if price_def[i]=="-": 
                                price=price-x
                            if price_def[i]=="*":
                                price=price*x
                            if price_def[i]=="/":
                                if x!=0:
                                    price=price/x
                                else:
                                    print "Division by zero."
                                    logger.warn('Division by zero')
                                    status_ret=False
                            if price_def[i]=="%":
                                price=price*x/100.0
                                print price
                    else:
                        status_ret=False
                else:
                    continue
            if status_ret==True:
                print "The price value is: " + str(price)
                logger.info('Calculated price is: %s',price)
            else:
                print "Error. Poorly defined pricing function."
                logger.warn('Pricing function not properly defined')
        return status_ret,meters_used
Ejemplo n.º 8
0
def main(argv):
    print "Hello There. This is a simple test application making a test API call to OpenStack"
    auth_uri = 'http://160.85.4.64:5000'  #internal test-setup, replace it with your own value
    status, token_data = keystone_api.get_token_v3(auth_uri, False)
    if status:
        print 'The authentication was successful, below are the data we got:'
        print '--------------------------------------------------------------------------------------------------------'
        print '%1s %32s %2s %64s %1s' % ('|', 'key', '|', 'value', '|')
        print '--------------------------------------------------------------------------------------------------------'
        for key, value in token_data.iteritems():
            if key not in {'token_id'}:
                print '%1s %32s %2s %64s %1s' % ('|', key, '|', value, '|')
        print '--------------------------------------------------------------------------------------------------------'
        print 'The authentication token is: ', token_data["token_id"]
        pom = token_data["token_id"]

    else:
        print "Authentication was not successful."
    if status:
        status, tenant_list = keystone_api.get_list_tenants(pom, auth_uri)
        status, user_list = keystone_api.get_users_per_tenant(
            pom, auth_uri, '4e9c4e1b93124cdba2a930e98eb26ede')
        for i in range(len(user_list)):
            print '%20s %20s' % (user_list[i]["user_id"],
                                 user_list[i]["user_name"])

        #status, server_list = compute_api.get_server_list(token_data["token-id"], token_data["computev3"])
        #if status:
        #   print "The list of servers are printed next."
        #   print server_list
        status, meter_list = ceilometer_api.get_meter_list(
            pom, token_data["metering"])
        total_price = periodic.daily_count("2014-02-26", 1,
                                           "323936522894416b903d3528fa971537")
        if status:
            print "The list of available meters are printed next."
            print '--------------------------------------------------------------------------------------------------------------------------'
            print '%1s %16s %2s %10s %2s %10s %2s %40s %1s %70s' % (
                '|', 'meter-name', '|', 'meter-type', '|', 'meter-unit', '|',
                'meter-id', '|', 'resource-id')
            print '--------------------------------------------------------------------------------------------------------------------------'
            for i in range(len(meter_list)):
                print '%1s %16s %2s %10s %2s %10s %2s %40s %1s %70s' % (
                    '|', meter_list[i]["meter-name"], '|',
                    meter_list[i]["meter-type"], '|',
                    meter_list[i]["meter-unit"], '|',
                    meter_list[i]["meter-id"].strip(), '|',
                    meter_list[i]["resource-id"])
            print '--------------------------------------------------------------------------------------------------------------------------'

        meter_name = raw_input("Enter meter name: ")

        st, stat_list = ceilometer_api.meter_statistics(
            meter_name, token_data["metering"], pom, meter_list, False)
        if st:
            print '--------------------------------------------------------------------------------------------------------------------------'
            print "The statistics for your meters is printed next."
            print '--------------------------------------------------------------------------------------------------------------------------'

            for i in range(len(stat_list)):
                print "Average: " + str(stat_list[i]["average"])
                print "Count: " + str(stat_list[i]["count"])
                print "Duration: " + str(stat_list[i]["duration"])
                print "Duration end: " + str(stat_list[i]["duration-end"])
                print "Duration start: " + str(stat_list[i]["duration-start"])
                print "Max: " + str(stat_list[i]["max"])
                print "Min: " + str(stat_list[i]["min"])
                print "Period: " + str(stat_list[i]["period"])
                print "Period end: " + str(stat_list[i]["period-end"])
                print "Period start: " + str(stat_list[i]["period-start"])
                print "Sum: " + str(stat_list[i]["sum"])
                print "Unit: " + str(stat_list[i]["unit"])
                print "Group by: " + str(stat_list[i]["group-by"])
                print '--------------------------------------------------------------------------------------------------------------------------'
            print '--------------------------------------------------------------------------------------------------------------------------'

        print "Query initialization for meter samples function."
        status, sample_list = ceilometer_api.get_meter_samples(
            meter_name, token_data["metering"], pom, True, meter_list, False,
            "")
        if status:
            print '--------------------------------------------------------------------------------------------------------------------------'
            print "The samples for your meter are printed next."
            print '--------------------------------------------------------------------------------------------------------------------------'

            for i in range(len(sample_list)):
                print "Counter name: " + str(sample_list[i]["counter-name"])
                print "Counter unit: " + str(sample_list[i]["counter-unit"])
                print "Counter volume: " + str(
                    sample_list[i]["counter-volume"])
                print "Counter type: " + str(sample_list[i]["counter-type"])
                print "Message id: " + str(sample_list[i]["message-id"])
                print "Project id: " + str(sample_list[i]["project-id"])
                print "Resource id: " + str(sample_list[i]["resource-id"])
                print "Resource metadata: "
                print sample_list[i]["resource-metadata"]
                print "Source: " + str(sample_list[i]["source"])
                print "Timestamp: " + str(sample_list[i]["timestamp"])
                print "User ID: " + str(sample_list[i]["user-id"])
                print '--------------------------------------------------------------------------------------------------------------------------'
            print '--------------------------------------------------------------------------------------------------------------------------'

        print "Query initialization for get resources function."
        status, resources_list = ceilometer_api.get_resources(
            token_data["metering"], pom, True)
        if status:
            print '--------------------------------------------------------------------------------------------------------------------------'
            print "The resources for your meter are printed next."
            print '--------------------------------------------------------------------------------------------------------------------------'

            for i in range(len(resources_list)):
                print "Resource id: " + str(resources_list[i]["resource-id"])
                print "Links: "
                for j in range(len(resources_list[i]["links"])):
                    print "Href: " + str(resources_list[i]["links"][j]["href"])
                    print "Rel: " + str(resources_list[i]["links"][j]["rel"])
                print "Project id: " + str(resources_list[i]["project-id"])

                print "Resource metadata: "
                print resources_list[i]["metadata"]
                #print "Source: " + str(resources_list[i]["source"])

                print "User ID: " + str(resources_list[i]["user-id"])
                print '--------------------------------------------------------------------------------------------------------------------------'

        print "Query initialization for get resources by id function."
        resource_id = raw_input("Enter resource id: ")
        status, resources_list = ceilometer_api.get_resources_by_id(
            token_data["metering"], pom, resource_id)

        if status:
            print '--------------------------------------------------------------------------------------------------------------------------'
            print "The resources for your meter are printed next."
            print '--------------------------------------------------------------------------------------------------------------------------'

            for i in range(len(resources_list)):
                print "Resource id: " + str(resources_list[i]["resource-id"])
                print "Links: "
                for j in range(len(resources_list[i]["links"])):
                    print "Href: " + str(resources_list[i]["links"][j]["href"])
                    print "Rel: " + str(resources_list[i]["links"][j]["rel"])
                print "Project id: " + str(resources_list[i]["project-id"])

                print "Resource metadata: "
                print resources_list[i]["metadata"]
                #print "Source: " + str(resources_list[i]["source"])
                #print "First sample timestamp: " + str(resources_list[i]["first-sample-timestamp"])
                #print "Last sample timestamp: " + str(resources_list[i]["last-sample-timestamp"])

                print "User ID: " + str(resources_list[i]["user-id"])
                print '--------------------------------------------------------------------------------------------------------------------------'

    return True
Ejemplo n.º 9
0
def main(argv):
    print "Hello There. This is a simple test application making a test API call to OpenStack"
    auth_uri = 'http://160.85.4.64:5000' #internal test-setup, replace it with your own value
    status, token_data = keystone_api.get_token_v3(auth_uri,False)
    if status:
        print 'The authentication was successful, below are the data we got:'
        print '--------------------------------------------------------------------------------------------------------'
        print '%1s %32s %2s %64s %1s' % ('|', 'key', '|', 'value', '|')
        print '--------------------------------------------------------------------------------------------------------'
        for key, value in token_data.iteritems():
            if key not in {'token_id'}:
                print '%1s %32s %2s %64s %1s' % ('|', key, '|', value, '|')
        print '--------------------------------------------------------------------------------------------------------'
        print 'The authentication token is: ', token_data["token_id"]
        pom=token_data["token_id"]

    else:
        print "Authentication was not successful."
    if status:
        status,tenant_list=keystone_api.get_list_tenants(pom,auth_uri)
        status,user_list=keystone_api.get_users_per_tenant(pom,auth_uri,'4e9c4e1b93124cdba2a930e98eb26ede')
        for i in range(len(user_list)):
            print '%20s %20s' %(user_list[i]["user_id"], user_list[i]["user_name"])
            
        #status, server_list = compute_api.get_server_list(token_data["token-id"], token_data["computev3"])
        #if status:
         #   print "The list of servers are printed next."
         #   print server_list
        status, meter_list = ceilometer_api.get_meter_list(pom, token_data["metering"])
        total_price=periodic.daily_count("2014-02-26",1, "323936522894416b903d3528fa971537")
        if status:
            print "The list of available meters are printed next."
            print '--------------------------------------------------------------------------------------------------------------------------'
            print '%1s %16s %2s %10s %2s %10s %2s %40s %1s %70s' % ('|','meter-name', '|', 'meter-type', '|', 'meter-unit', '|', 'meter-id', '|','resource-id')
            print '--------------------------------------------------------------------------------------------------------------------------'
            for i in range(len(meter_list)):
                print '%1s %16s %2s %10s %2s %10s %2s %40s %1s %70s' % ('|', meter_list[i]["meter-name"], '|', meter_list[i]["meter-type"], '|', meter_list[i]["meter-unit"], '|', meter_list[i]["meter-id"].strip(), '|',meter_list[i]["resource-id"])
            print '--------------------------------------------------------------------------------------------------------------------------'
 
        meter_name=raw_input("Enter meter name: ")

        st,stat_list=ceilometer_api.meter_statistics(meter_name, token_data["metering"],pom,meter_list,False)
        if st:
            print '--------------------------------------------------------------------------------------------------------------------------'
            print "The statistics for your meters is printed next."
            print '--------------------------------------------------------------------------------------------------------------------------'
            
            for i in range(len(stat_list)):
                print "Average: " + str(stat_list[i]["average"]) 
                print "Count: " + str(stat_list[i]["count"])
                print "Duration: "+ str(stat_list[i]["duration"]) 
                print "Duration end: " + str(stat_list[i]["duration-end"]) 
                print "Duration start: "+ str(stat_list[i]["duration-start"]) 
                print "Max: " + str(stat_list[i]["max"])
                print "Min: " + str(stat_list[i]["min"]) 
                print "Period: " + str(stat_list[i]["period"]) 
                print "Period end: " + str(stat_list[i]["period-end"]) 
                print "Period start: " + str(stat_list[i]["period-start"]) 
                print "Sum: " + str(stat_list[i]["sum"]) 
                print "Unit: " + str(stat_list[i]["unit"]) 
                print "Group by: " + str(stat_list[i]["group-by"]) 
                print '--------------------------------------------------------------------------------------------------------------------------'         
            print '--------------------------------------------------------------------------------------------------------------------------'
        
        print "Query initialization for meter samples function."
        status,sample_list=ceilometer_api.get_meter_samples(meter_name,token_data["metering"],pom,True,meter_list,False,"")
        if status:
            print '--------------------------------------------------------------------------------------------------------------------------'
            print "The samples for your meter are printed next."
            print '--------------------------------------------------------------------------------------------------------------------------'
            
            for i in range(len(sample_list)):
                print "Counter name: " + str(sample_list[i]["counter-name"]) 
                print "Counter unit: " + str(sample_list[i]["counter-unit"])
                print "Counter volume: "+ str(sample_list[i]["counter-volume"]) 
                print "Counter type: " + str(sample_list[i]["counter-type"]) 
                print "Message id: "+ str(sample_list[i]["message-id"]) 
                print "Project id: " + str(sample_list[i]["project-id"])
                print "Resource id: " + str(sample_list[i]["resource-id"]) 
                print "Resource metadata: " 
                print sample_list[i]["resource-metadata"]
                print "Source: " + str(sample_list[i]["source"]) 
                print "Timestamp: " + str(sample_list[i]["timestamp"]) 
                print "User ID: " + str(sample_list[i]["user-id"]) 
                print '--------------------------------------------------------------------------------------------------------------------------'         
            print '--------------------------------------------------------------------------------------------------------------------------'
        
        print "Query initialization for get resources function."
        status,resources_list=ceilometer_api.get_resources(token_data["metering"], pom,True)
        if status:
            print '--------------------------------------------------------------------------------------------------------------------------'
            print "The resources for your meter are printed next."
            print '--------------------------------------------------------------------------------------------------------------------------'
            
            for i in range(len(resources_list)):
                print "Resource id: " + str(resources_list[i]["resource-id"])
                print "Links: " 
                for j in range(len(resources_list[i]["links"])):
                    print "Href: " + str(resources_list[i]["links"][j]["href"])
                    print "Rel: " + str(resources_list[i]["links"][j]["rel"])
                print "Project id: " + str(resources_list[i]["project-id"])
                 
                print "Resource metadata: " 
                print resources_list[i]["metadata"]
                #print "Source: " + str(resources_list[i]["source"]) 

                print "User ID: " + str(resources_list[i]["user-id"]) 
                print '--------------------------------------------------------------------------------------------------------------------------'         
        
        print "Query initialization for get resources by id function."    
        resource_id=raw_input("Enter resource id: ")
        status,resources_list=ceilometer_api.get_resources_by_id(token_data["metering"], pom,resource_id)

        if status:
            print '--------------------------------------------------------------------------------------------------------------------------'
            print "The resources for your meter are printed next."
            print '--------------------------------------------------------------------------------------------------------------------------'
            
            for i in range(len(resources_list)):
                print "Resource id: " + str(resources_list[i]["resource-id"])
                print "Links: " 
                for j in range(len(resources_list[i]["links"])):
                    print "Href: " + str(resources_list[i]["links"][j]["href"])
                    print "Rel: " + str(resources_list[i]["links"][j]["rel"])
                print "Project id: " + str(resources_list[i]["project-id"])
                 
                print "Resource metadata: " 
                print resources_list[i]["metadata"]
                #print "Source: " + str(resources_list[i]["source"]) 
                #print "First sample timestamp: " + str(resources_list[i]["first-sample-timestamp"]) 
                #print "Last sample timestamp: " + str(resources_list[i]["last-sample-timestamp"]) 

                print "User ID: " + str(resources_list[i]["user-id"]) 
                print '--------------------------------------------------------------------------------------------------------------------------'         
        
        
    return True
Ejemplo n.º 10
0
def main(argv):

    print "Hello there. This is a simple periodic counter."
    logger.info('--------------- \n')
    logger.info('Starting periodic ')
    auth_uri = 'http://160.85.4.64:5000'  #internal test-setup, replace it with your own value
    #auth_uri = 'http://160.85.231.233:5000' #internal test-setup, replace it with your own value

    status, token_data = keystone_api.get_token_v3(auth_uri, False)
    if status:
        print 'The authentication was successful.'
        print '--------------------------------------------------------------------------------------------------------'
        print 'The authentication token is: ', token_data["token_id"]
        pom = token_data["token_id"]
        user = token_data["user-id"]
        logger.info('Successful authentication for user %s', user)
    else:
        print "Authentication was not successful."
        logger.warn('Authentication not successful')
    if status:
        status, meter_list = ceilometer_api.get_meter_list(
            pom, token_data["metering"])
        if status:
            logger.info('Printing meters')
            print "The list of available meters are printed next."
            print '--------------------------------------------------------------------------------------------------------------------------'
            print '%1s %16s %2s %10s %2s %10s %2s %70s %1s' % (
                '|', 'meter-name', '|', 'meter-type', '|', 'meter-unit', '|',
                'meter-id', '|')
            print '--------------------------------------------------------------------------------------------------------------------------'

            for i in range(len(meter_list)):
                print '%1s %16s %2s %10s %2s %10s %2s %70s %1s' % (
                    '|', meter_list[i]["meter-name"], '|',
                    meter_list[i]["meter-type"], '|',
                    meter_list[i]["meter-unit"], '|',
                    meter_list[i]["meter-id"].strip(), '|')
                logger.info('Meter list %s', meter_list[i]["meter-name"])
            status, meters_used, meters_ids, input, price = pricing(
                token_data["metering"], meter_list, pom, None)
            logger.info('Defining pricing function. %s', input)
            global conn
            conn = sqlite3.connect(path2 + '/meters.db',
                                   check_same_thread=False)
            func = [None] * 9
            for i in range(len(input)):
                func[i] = input[i]
            conn.execute(
                "INSERT INTO PRICING_FUNC (USER_ID,PARAM1,SIGN1,PARAM2,SIGN2,PARAM3,SIGN3,PARAM4,SIGN4,PARAM5) \
                       VALUES ( '" + str(user) + "', '" + str(func[0]) +
                "','" + str(func[1]) + "','" + str(func[2]) + "','" +
                str(func[3]) + " ','" + str(func[4]) + "' ,' " + str(func[5]) +
                "' ,' " + str(func[6]) + "' ,' " + str(func[7]) + "' ,' " +
                str(func[8]) + " ')")
            conn.commit()
            logger.info('Insert pricing function in database')
            cursor = conn.execute("SELECT max(ID)  from PRICING_FUNC")
            id_price = cursor.fetchone()[0]
            if status:
                time = raw_input(
                    "Enter the desired time interval in seconds. ")
                if time == "":
                    time = 10
                periodic_counts = [None] * len(meters_used)
                for i in range(len(meters_used)):
                    periodic_counts[i] = []
                periodic_counter(meters_used, token_data["metering"], pom,
                                 periodic_counts, 0, time, meters_ids, input,
                                 meter_list, id_price)
                logger.info('Starting counter. Time interval is %s ', time)
                conn.close()

    return True