You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@libcloud.apache.org by to...@apache.org on 2013/08/01 14:41:14 UTC
[3/8] git commit: Add cli utility for updating pricing file.
Add cli utility for updating pricing file.
Project: http://git-wip-us.apache.org/repos/asf/libcloud/repo
Commit: http://git-wip-us.apache.org/repos/asf/libcloud/commit/5660b33d
Tree: http://git-wip-us.apache.org/repos/asf/libcloud/tree/5660b33d
Diff: http://git-wip-us.apache.org/repos/asf/libcloud/diff/5660b33d
Branch: refs/heads/trunk
Commit: 5660b33dec344f7a1cd5abc23a0da57a9eeb65c9
Parents: 769c5e0
Author: Tomaz Muraus <to...@tomaz.me>
Authored: Mon Jul 29 21:28:50 2013 +0200
Committer: Tomaz Muraus <to...@apache.org>
Committed: Thu Aug 1 14:30:03 2013 +0200
----------------------------------------------------------------------
bin/libcloud | 30 ++++++++++++++
libcloud/cli/__init__.py | 0
libcloud/cli/pricing.py | 84 +++++++++++++++++++++++++++++++++++++++
libcloud/utils/connection.py | 31 +++++++++++++++
4 files changed, 145 insertions(+)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/libcloud/blob/5660b33d/bin/libcloud
----------------------------------------------------------------------
diff --git a/bin/libcloud b/bin/libcloud
new file mode 100755
index 0000000..270922e
--- /dev/null
+++ b/bin/libcloud
@@ -0,0 +1,30 @@
+#!/usr/bin/env python
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements. See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You 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.
+
+from __future__ import with_statement
+
+import argparse
+
+from libcloud.cli.pricing import add_subparser, update_pricing
+
+parser = argparse.ArgumentParser(prog='libcloud', usage='%(prog)s')
+subparsers = parser.add_subparsers(dest='subparser_name')
+add_subparser(subparsers=subparsers)
+
+args = parser.parse_args()
+
+if args.subparser_name == 'update-pricing':
+ update_pricing(file_url=args.file_url, file_path=args.file_path)
http://git-wip-us.apache.org/repos/asf/libcloud/blob/5660b33d/libcloud/cli/__init__.py
----------------------------------------------------------------------
diff --git a/libcloud/cli/__init__.py b/libcloud/cli/__init__.py
new file mode 100644
index 0000000..e69de29
http://git-wip-us.apache.org/repos/asf/libcloud/blob/5660b33d/libcloud/cli/pricing.py
----------------------------------------------------------------------
diff --git a/libcloud/cli/pricing.py b/libcloud/cli/pricing.py
new file mode 100644
index 0000000..584d292
--- /dev/null
+++ b/libcloud/cli/pricing.py
@@ -0,0 +1,84 @@
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements. See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You 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 os
+import sys
+
+try:
+ import simplejson as json
+except ImportError:
+ import json
+
+
+from libcloud.pricing import CUSTOM_PRICING_FILE_PATH
+from libcloud.utils.connection import get_response_object
+
+__all__ = [
+ 'add_subparser',
+ 'update_pricing'
+]
+
+# Default URL to the pricing file
+DEFAULT_FILE_URL = 'https://git-wip-us.apache.org/repos/asf?p=libcloud.git;a=blob_plain;f=libcloud/data/pricing.json'
+
+
+def add_subparser(subparsers):
+ parser = subparsers.add_parser('update-pricing',
+ help='Update Libcloud pricing file')
+ parser.add_argument('--file-path', dest='file_path', action='store',
+ default=CUSTOM_PRICING_FILE_PATH,
+ help='Path where the file will be saved')
+ parser.add_argument('--file-url', dest='file_url', action='store',
+ default=DEFAULT_FILE_URL,
+ help='URL to the pricing file')
+ return parser
+
+
+def update_pricing(file_url, file_path):
+ dir_name = os.path.dirname(file_path)
+
+ if not os.path.exists(dir_name):
+ # Verify a valid path is provided
+ sys.stderr.write('Can\'t write to %s, directory %s, doesn\'t exist\n' %
+ (file_path, dir_name))
+ sys.exit(2)
+
+ if os.path.exists(file_path) and os.path.isdir(file_path):
+ sys.stderr.write('Can\'t write to %s file path because it\'s a'
+ ' directory\n' %
+ (file_path))
+ sys.exit(2)
+
+ response = get_response_object(file_url)
+ body = response.body
+
+ # Verify pricing file is valid
+ try:
+ data = json.loads(body)
+ except json.decoder.JSONDecodeError:
+ sys.stderr.write('Provided URL doesn\'t contain valid pricing'
+ ' data\n')
+ sys.exit(3)
+
+ if not data.get('updated', None):
+ sys.stderr.write('Provided URL doesn\'t contain valid pricing'
+ ' data\n')
+ sys.exit(3)
+
+ # No need to stream it since file is small
+ with open(file_path, 'w') as file_handle:
+ file_handle.write(response.body)
+
+ print('Pricing file saved to %s' % (file_path))
http://git-wip-us.apache.org/repos/asf/libcloud/blob/5660b33d/libcloud/utils/connection.py
----------------------------------------------------------------------
diff --git a/libcloud/utils/connection.py b/libcloud/utils/connection.py
new file mode 100644
index 0000000..bd2b50d
--- /dev/null
+++ b/libcloud/utils/connection.py
@@ -0,0 +1,31 @@
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements. See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You 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.
+
+from libcloud.utils.py3 import urlparse, parse_qs
+from libcloud.common.base import Connection
+
+__all__ = [
+ 'get_response_object'
+]
+
+
+def get_response_object(url):
+ parsed_url = urlparse.urlparse(url)
+ parsed_qs = parse_qs(parsed_url.query)
+ secure = parsed_url.scheme == 'https'
+
+ con = Connection(secure=secure, host=parsed_url.netloc)
+ response = con.request(method='GET', action=parsed_url.path, params=parsed_qs)
+ return response