Sie sind auf Seite 1von 3

#

#
#
#
#
#
#
#
#
#

Examine the following function descriptions and fill in the function


definition for get_pool_by_ip, get_pools_by_cidr, and get_pools_by_content
Allowed Assumptions:
- No two pools have the same IP address
- Internal consistency (all uids map to valid references)
Notes:
- All uids are non-zero positive integers
- IP addresses are strings in dotted decimal form, e.g.: '192.168.1.10'

import socket,struct
class Example(object):
def address_in_network(self,ip, net):
ip_addr = struct.unpack('<L', socket.inet_aton(ip))[0]
net, bits = net.split('/')
net_addr = struct.unpack('<L', socket.inet_aton(net))[0]
netmask = ((1L << int(bits)) - 1)
return ip_addr & netmask == net_addr & netmask
def get_all_pools(self):
# List all pools.
# Returns a list of pool uids, or [] if no pools
# [
# <pool uid 1>,
# <pool uid 2>,
# ...
# ]
pass
def get_pool(self, uid):
# Get the contents of a specific pool by uid.
# Returns a dict of pool attributes:
# {
# 'active': <integer enumerating state>,
# 'datasource_id': <datasource uid>,
# 'managed': <boolean indicating managed status>,
# 'name': <string describing the pool>,
# 'uid': <pool uid>
# }
pass
def get_datasource(self, uid):
# Get the contents of a specific datasource by uid.
# Returns a dict of datasource attributes:
# {
# 'search_order': <integer>,
# 'type': 'DS',
# 'uid': <datasource uid>,
# 'name': <string describing the datasource>,
# 'properties': <property list (see below)>
# }
# Where property list is an *unordered list* of key-value pairs (not
# a dictionary) as follows:
# [
# ['master_address_1', <ip address as string>],

# ['password', <password as string>],


# ['username', <username as string>]
# ]
pass
def get_pool_by_ip(self, ip):
# Get the uid of the pool which has the given IP address
# Returns the uid, or None if no such pool exists
subnet_addr = ip.split('.')[0] + ".0.0.0"
#assuming a mask of 24
pools_by_cidr = self.get_pools_by_cidr(subnet_addr, 24)
if ip in pools_by_cidr:
return pools_by_cidr[ip]
return None

def get_pools_by_cidr(self, ip, mask):


# Get pools in the specified CIDR block (ip/mask), where mask is an int
# Returns a dict of {<ip>: <uid>, ...}, {} if no pools match
#
# For example, passing ('10.0.0.0', 24) might return {'10.0.0.3': 9999}
pool_dict={}
ipwithMask = ip + "/" + str(mask)
pool_id_list = self.get_all_pools()
for pool_id in pool_id_list:
pool = self.get_pool(pool_id)
if 'datasource_id' not in pool:
continue
dat_id = pool['datasource_id']
data_source = self.get_datasource(dat_id)
ip_address = ''
for value_list in data_source['properties'] :
if (value_list[0] == 'master_address_1') and (value_list[1] != '
') :
ip_address = value_list[1]
break
if ip_address == '':
continue
if self.address_in_network( ip_address, ipwithMask ):
pool_dict[ip_address] = pool_id
return pool_dict;

def get_pools_by_content(self, desc={}):


# Get pools which match all of the fields described in dictionary desc
# Allowed fields include any of the attributes of a pool (no need to
# support datasource attributes).
# Returns a list of uids, or [] if no pools match

#
# For example, passing {'managed': True, 'active': 3} might
# return [10, 1234, 5555, 678], where each pool with that uid is
# both managed and inactive. Passing {} would return all pool uuids.
uid_list=[]
if not desc:
return uid_list
pools = self.get_all_pools()
for pool_id in pools:
pool = self.get_pool(pool_id)
if len(pool.viewitems() & desc.viewitems()) == len(desc.items()):
uid_list.append(pool['uid'])
return uid_list

Das könnte Ihnen auch gefallen