def _load_file(self, handle): """Decode the JSON file. Broken out for testing.""" try: return jsonutils.load(handle) except ValueError as e: LOG.exception(_("Could not decode scheduler options: '%s'"), e) return {}
def get_instance_network_qos(instance_type, instance_uuid): qos_json_file = FLAGS.network_qos_config qos_info = None private_qos = 0 public_qos = 0 # Firstly, get default qos configure from qos_json_file. if os.path.exists(qos_json_file): with open(qos_json_file, 'r') as f: qos_info = jsonutils.load(f) if qos_info: qos_key = qos_info['policy']['key'] qos_inst = qos_info['shaping'] inst_type = instance_type if qos_key == 'ecu': key = (inst_type["vcpus"] * int(inst_type["extra_specs"]["ecus_per_vcpu:"])) else: key = inst_type["vcpus"] key = str(key) if qos_inst['private'].get(key): private_qos = qos_inst['private'][key]['rate'] else: private_qos = qos_inst['private']['default']['rate'] if qos_inst['public'].get(key): public_qos = qos_inst['public'][key]['rate'] else: public_qos = qos_inst['public']['default']['rate'] # If instance_uuid is None, just take the default qos configure. if instance_uuid is None: return dict(private_qos=private_qos, public_qos=public_qos) # If qos is set manully, use the manul configure instead. system_meta = db.instance_system_metadata_get(context.get_admin_context(), instance_uuid) network_qos = system_meta.get('network-qos') if network_qos: network_qos_info = jsonutils.loads(network_qos) for info in network_qos_info: if info['type'] == 'private': private_qos = int(info['rate']) else: public_qos = int(info['rate']) return dict(private_qos=private_qos, public_qos=public_qos)
def __init__(self, *args, **kwargs): super(JsonFileVendorData, self).__init__(*args, **kwargs) data = {} fpath = CONF.vendordata_jsonfile_path logprefix = "%s[%s]: " % (file_opt.name, fpath) if fpath: try: with open(fpath, "r") as fp: data = jsonutils.load(fp) except IOError as e: if e.errno == errno.ENOENT: LOG.warn(logprefix + _("file does not exist")) else: LOG.warn(logprefix + _("Unexpected IOError when reading")) raise e except ValueError: LOG.warn(logprefix + _("failed to load json")) raise self._data = data
def setUp(self): """Copy live policy.json file and convert all actions to allow users of the specified role only """ super(RoleBasedPolicyFixture, self).setUp() policy = jsonutils.load(open(CONF.policy_file)) # Convert all actions to require specified role for action, rule in policy.iteritems(): policy[action] = 'role:%s' % self.role self.policy_dir = self.useFixture(fixtures.TempDir()) self.policy_file_name = os.path.join(self.policy_dir.path, 'policy.json') with open(self.policy_file_name, 'w') as policy_file: jsonutils.dump(policy, policy_file) CONF.set_override('policy_file', self.policy_file_name) nova.policy.reset() nova.policy.init() self.addCleanup(nova.policy.reset)