Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[google compute] add pricing data update script
Also used the script to update `pricing.json` with current prices on a per-region basis.
- Loading branch information
Showing
2 changed files
with
140 additions
and
1 deletion.
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,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)) |
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