Skip to content

Commit

Permalink
[google compute] add pricing data update script
Browse files Browse the repository at this point in the history
Also used the script to update  `pricing.json` with current prices on a
per-region basis.
  • Loading branch information
mbrukman committed Feb 17, 2015
1 parent e41001e commit 9ad9cb8
Show file tree
Hide file tree
Showing 2 changed files with 140 additions and 1 deletion.
86 changes: 86 additions & 0 deletions contrib/update_google_prices.py
@@ -0,0 +1,86 @@
#!/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.
"""
Loads Google Cloud Platform prices and updates the `pricing.json` data file.
"""

import json
import os
import sys
import time
import urllib2

BASE_PATH = os.path.dirname(os.path.abspath(__file__))
PRICING_FILE_PATH = os.path.join(BASE_PATH, '../libcloud/data/pricing.json')
PRICING_FILE_PATH = os.path.abspath(PRICING_FILE_PATH)

GOOGLE_CLOUD_PRICES = 'https://cloudpricingcalculator.appspot.com/static/data/pricelist.json'


def main(argv):
# Read the current pricing data.
libcloud_data = {}
with open(PRICING_FILE_PATH, 'r') as libcloud_in:
libcloud_data = json.loads(libcloud_in.read())

# Download the current Google Cloud Platform pricing.
req = urllib2.Request(GOOGLE_CLOUD_PRICES, '')
google_ext_prices = json.loads(urllib2.urlopen(req).read())
if not 'gcp_price_list' in google_ext_prices:
sys.stderr.write('Google Cloud pricing data missing "gcp_price_list" node\n')
sys.exit(1)

# Clear the Google entry in the "compute" section before update.
libcloud_google_data = libcloud_data['compute']['google'] = {}

# This is a map from regions used in the pricing JSON file to the regions as
# reflected in the Google Cloud Platform documentation and APIs.
pricing_to_region = {
'us': 'us',
'eu': 'europe',
'apac': 'asia'
}

# Initialize Google Cloud Platform regions.
for _, region in pricing_to_region.iteritems():
libcloud_google_data[region] = {}

# Update Google Compute Engine pricing.
gcp_price_list = google_ext_prices['gcp_price_list']
gce_vm_prefix = 'CP-COMPUTEENGINE-VMIMAGE-'
for name, prices in gcp_price_list.iteritems():
if not name.startswith(gce_vm_prefix):
continue
short_name = name[len(gce_vm_prefix):]
machine_type = short_name.lower()
for key, price in prices.iteritems():
if key in pricing_to_region:
region = pricing_to_region[key]
libcloud_google_data[region][machine_type] = price

# Update last-modified timestamp.
libcloud_data['updated'] = int(time.time())

# Write updated price list.
with open(PRICING_FILE_PATH, 'w') as libcloud_out:
json_str = json.dumps(libcloud_data, indent=4, separators=(',', ': '),
sort_keys=True)
libcloud_out.write(json_str)


if __name__ == '__main__':
sys.exit(main(sys.argv))
55 changes: 54 additions & 1 deletion libcloud/data/pricing.json
Expand Up @@ -341,6 +341,59 @@
"512MB": 0.095,
"8GB": 1.52
},
"google": {
"asia": {
"f1-micro": 0.013,
"g1-small": 0.0347,
"n1-highcpu-16": 0.688,
"n1-highcpu-2": 0.086,
"n1-highcpu-4": 0.172,
"n1-highcpu-8": 0.344,
"n1-highmem-16": 1.296,
"n1-highmem-2": 0.162,
"n1-highmem-4": 0.324,
"n1-highmem-8": 0.648,
"n1-standard-1": 0.069,
"n1-standard-16": 1.104,
"n1-standard-2": 0.138,
"n1-standard-4": 0.276,
"n1-standard-8": 0.552
},
"europe": {
"f1-micro": 0.013,
"g1-small": 0.0347,
"n1-highcpu-16": 0.688,
"n1-highcpu-2": 0.086,
"n1-highcpu-4": 0.172,
"n1-highcpu-8": 0.344,
"n1-highmem-16": 1.296,
"n1-highmem-2": 0.162,
"n1-highmem-4": 0.324,
"n1-highmem-8": 0.648,
"n1-standard-1": 0.069,
"n1-standard-16": 1.104,
"n1-standard-2": 0.138,
"n1-standard-4": 0.276,
"n1-standard-8": 0.552
},
"us": {
"f1-micro": 0.012,
"g1-small": 0.032,
"n1-highcpu-16": 0.64,
"n1-highcpu-2": 0.08,
"n1-highcpu-4": 0.16,
"n1-highcpu-8": 0.32,
"n1-highmem-16": 1.184,
"n1-highmem-2": 0.148,
"n1-highmem-4": 0.296,
"n1-highmem-8": 0.592,
"n1-standard-1": 0.063,
"n1-standard-16": 1.008,
"n1-standard-2": 0.126,
"n1-standard-4": 0.252,
"n1-standard-8": 0.504
}
},
"nephoscale": {
"1": 0.6,
"11": 0.35,
Expand Down Expand Up @@ -587,5 +640,5 @@
}
},
"storage": {},
"updated": 1410030750
"updated": 1424204847
}

0 comments on commit 9ad9cb8

Please sign in to comment.