diff --git a/openstackclient/common/utils.py b/openstackclient/common/utils.py index d7702a3f0f..bc9ed26412 100644 --- a/openstackclient/common/utils.py +++ b/openstackclient/common/utils.py @@ -249,3 +249,13 @@ def get_password(stdin): raise exceptions.CommandError("Error reading password.") raise exceptions.CommandError("There was a request to be prompted for a" " password and a terminal was not detected.") + + +def read_blob_file_contents(blob_file): + try: + with open(blob_file) as file: + blob = file.read().strip() + return blob + except IOError: + msg = "Error occurred trying to read from file %s" + raise exceptions.CommandError(msg % blob_file) diff --git a/openstackclient/identity/v3/policy.py b/openstackclient/identity/v3/policy.py index a760d8cdd8..641ff9c83c 100644 --- a/openstackclient/identity/v3/policy.py +++ b/openstackclient/identity/v3/policy.py @@ -48,7 +48,7 @@ class CreatePolicy(show.ShowOne): def take_action(self, parsed_args): self.log.debug('take_action(%s)' % parsed_args) - blob = _read_blob_file_contents(parsed_args.blob_file) + blob = utils.read_blob_file_contents(parsed_args.blob_file) identity_client = self.app.client_manager.identity policy = identity_client.policies.create( @@ -138,7 +138,7 @@ class SetPolicy(command.Command): blob = None if parsed_args.blob_file: - blob = _read_blob_file_contents(parsed_args.blob_file) + blob = utils.read_blob_file_contents(parsed_args.blob_file) kwargs = {} if blob: @@ -174,9 +174,3 @@ class ShowPolicy(show.ShowOne): parsed_args.policy) return zip(*sorted(six.iteritems(policy._info))) - - -def _read_blob_file_contents(blob_file): - with open(blob_file) as file: - blob = file.read().strip() - return blob