-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add config to Identity Transaction Processor
Signed-off-by: Andrea Gunderson <[email protected]>
- Loading branch information
Showing
4 changed files
with
184 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
# | ||
# Sawtooth -- Identity Transaction Processor Configuration | ||
# | ||
|
||
# The url to connect to a running Validator | ||
# connect = "tcp://localhost:4004" |
Empty file.
129 changes: 129 additions & 0 deletions
129
families/identity/sawtooth_identity/processor/config/identity.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
# Copyright 2017 Intel Corporation | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# ------------------------------------------------------------------------------ | ||
|
||
import collections | ||
import logging | ||
import os | ||
|
||
import toml | ||
|
||
from sawtooth_sdk.processor.exceptions import LocalConfigurationError | ||
|
||
LOGGER = logging.getLogger(__name__) | ||
|
||
|
||
def load_default_identity_config(): | ||
""" | ||
Returns the default IdentityConfig | ||
""" | ||
return IdentityConfig( | ||
connect='tcp://localhost:4004', | ||
) | ||
|
||
|
||
def load_toml_identity_config(filename): | ||
"""Returns a IdentityConfig created by loading a TOML file from the | ||
filesystem. | ||
Args: | ||
filename (string): The name of the file to load the config from | ||
Returns: | ||
config (IdentityConfig): The IdentityConfig created from the stored | ||
toml file. | ||
Raises: | ||
LocalConfigurationError | ||
""" | ||
if not os.path.exists(filename): | ||
LOGGER.info( | ||
"Skipping transaction proccesor config loading from non-existent" | ||
" config file: %s", filename) | ||
return IdentityConfig() | ||
|
||
LOGGER.info("Loading transaction processor information from config: %s", | ||
filename) | ||
|
||
try: | ||
with open(filename) as fd: | ||
raw_config = fd.read() | ||
except IOError as e: | ||
raise LocalConfigurationError( | ||
"Unable to load transaction processor configuration file:" | ||
" {}".format(str(e))) | ||
|
||
toml_config = toml.loads(raw_config) | ||
invalid_keys = set(toml_config.keys()).difference( | ||
['connect']) | ||
if invalid_keys: | ||
raise LocalConfigurationError( | ||
"Invalid keys in transaction processor config: " | ||
"{}".format(", ".join(sorted(list(invalid_keys))))) | ||
|
||
config = IdentityConfig( | ||
connect=toml_config.get("connect", None) | ||
) | ||
|
||
return config | ||
|
||
|
||
def merge_identity_config(configs): | ||
""" | ||
Given a list of IdentityConfig object, merges them into a single | ||
IdentityConfig, giving priority in the order of the configs | ||
(first has highest priority). | ||
Args: | ||
config (list of IdentityConfigs): The list of identity configs that | ||
must be merged together | ||
Returns: | ||
config (IdentityConfig): One IdentityConfig that combines all of the | ||
passed in configs. | ||
""" | ||
connect = None | ||
|
||
for config in reversed(configs): | ||
if config.connect is not None: | ||
connect = config.connect | ||
|
||
return IdentityConfig( | ||
connect=connect | ||
) | ||
|
||
|
||
class IdentityConfig: | ||
def __init__(self, connect=None): | ||
self._connect = connect | ||
|
||
@property | ||
def connect(self): | ||
return self._connect | ||
|
||
def __repr__(self): | ||
# not including password for opentsdb | ||
return \ | ||
"{}(connect={})".format( | ||
self.__class__.__name__, | ||
repr(self._connect), | ||
) | ||
|
||
def to_dict(self): | ||
return collections.OrderedDict([ | ||
('connect', self._connect), | ||
]) | ||
|
||
def to_toml_string(self): | ||
return str(toml.dumps(self.to_dict())).strip().split('\n') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters