def test_cloud_id_aws_based_on_region_and_cloud_name(self): """When cloud_name is aws, return proper cloud-id based on region.""" self.assertEqual( "aws-china", canonical_cloud_id(cloud_name="aws", region="cn-north-1", platform="platform"), ) self.assertEqual( "aws", canonical_cloud_id(cloud_name="aws", region="us-east-1", platform="platform"), ) self.assertEqual( "aws-gov", canonical_cloud_id(cloud_name="aws", region="us-gov-1", platform="platform"), ) self.assertEqual( # Overrideen non-aws cloud_name is returned "!aws", canonical_cloud_id(cloud_name="!aws", region="us-gov-1", platform="platform"), )
def test_cloud_id_azure_based_on_region_and_cloud_name(self): """Report cloud-id when cloud_name is azure and region is in china.""" self.assertEqual( 'azure-china', canonical_cloud_id(cloud_name='azure', region='chinaeast', platform='platform')) self.assertEqual( 'azure', canonical_cloud_id(cloud_name='azure', region='!chinaeast', platform='platform'))
def test_cloud_id_azure_based_on_region_and_cloud_name(self): """Report cloud-id when cloud_name is azure and region is in china.""" self.assertEqual( "azure-china", canonical_cloud_id(cloud_name="azure", region="chinaeast", platform="platform"), ) self.assertEqual( "azure", canonical_cloud_id(cloud_name="azure", region="!chinaeast", platform="platform"), )
def test_cloud_id_returns_platform_on_unknown_cloud_name(self): """When region is set but cloud_name is unknown return cloud_name.""" self.assertEqual( 'platform', canonical_cloud_id(cloud_name=METADATA_UNKNOWN, region='region', platform='platform'))
def test_cloud_id_returns_platform_on_none(self): """When region and cloud_name are unknown, return platform.""" self.assertEqual( 'platform', canonical_cloud_id(cloud_name=None, region=None, platform='platform'))
def test_cloud_id_returns_platform_on_unknowns(self): """When region and cloud_name are unknown, return platform.""" self.assertEqual( 'platform', canonical_cloud_id(cloud_name=METADATA_UNKNOWN, region=METADATA_UNKNOWN, platform='platform'))
def handle_args(name, args): """Handle calls to 'cloud-id' cli. Print the canonical cloud-id on which the instance is running. @return: 0 on success, 1 otherwise. """ try: instance_data = json.load(open(args.instance_data)) except IOError: return error( "File not found '%s'. Provide a path to instance data json file" ' using --instance-data' % args.instance_data) except ValueError as e: return error( "File '%s' is not valid json. %s" % (args.instance_data, e)) v1 = instance_data.get('v1', {}) cloud_id = canonical_cloud_id( v1.get('cloud_name', METADATA_UNKNOWN), v1.get('region', METADATA_UNKNOWN), v1.get('platform', METADATA_UNKNOWN)) if args.json: v1['cloud_id'] = cloud_id response = json.dumps( # Pretty, sorted json v1, indent=1, sort_keys=True, separators=(',', ': ')) elif args.long: response = '%s\t%s' % (cloud_id, v1.get('region', METADATA_UNKNOWN)) else: response = cloud_id sys.stdout.write('%s\n' % response) return 0
def handle_args(name, args): """Handle calls to 'cloud-id' cli. Print the canonical cloud-id on which the instance is running. @return: 0 on success, 1 otherwise. """ try: instance_data = json.load(open(args.instance_data)) except IOError: return error( "File not found '%s'. Provide a path to instance data json file" " using --instance-data" % args.instance_data) except ValueError as e: return error("File '%s' is not valid json. %s" % (args.instance_data, e)) v1 = instance_data.get("v1", {}) cloud_id = canonical_cloud_id( v1.get("cloud_name", METADATA_UNKNOWN), v1.get("region", METADATA_UNKNOWN), v1.get("platform", METADATA_UNKNOWN), ) if args.json: v1["cloud_id"] = cloud_id response = json.dumps( # Pretty, sorted json v1, indent=1, sort_keys=True, separators=(",", ": ")) elif args.long: response = "%s\t%s" % (cloud_id, v1.get("region", METADATA_UNKNOWN)) else: response = cloud_id sys.stdout.write("%s\n" % response) return 0
def test_cloud_id_returns_cloud_name_on_unknown_region(self): """When region is unknown, return cloud_name.""" for region in (None, METADATA_UNKNOWN): self.assertEqual( 'cloudname', canonical_cloud_id(cloud_name='cloudname', region=region, platform='platform'))
def test_cloud_id_aws_based_on_region_and_cloud_name(self): """When cloud_name is aws, return proper cloud-id based on region.""" self.assertEqual( 'aws-china', canonical_cloud_id(cloud_name='aws', region='cn-north-1', platform='platform')) self.assertEqual( 'aws', canonical_cloud_id(cloud_name='aws', region='us-east-1', platform='platform')) self.assertEqual( 'aws-gov', canonical_cloud_id(cloud_name='aws', region='us-gov-1', platform='platform')) self.assertEqual( # Overrideen non-aws cloud_name is returned '!aws', canonical_cloud_id(cloud_name='!aws', region='us-gov-1', platform='platform'))
def handle_args(name, args): """Handle calls to 'cloud-id' cli. Print the canonical cloud-id on which the instance is running. @return: 0 on success, 1 on error, 2 on disabled, 3 on cloud-init not run. """ status, _status_details, _time = get_status_details() if status == UXAppStatus.DISABLED: sys.stdout.write("{0}\n".format(status.value)) return 2 elif status == UXAppStatus.NOT_RUN: sys.stdout.write("{0}\n".format(status.value)) return 3 try: instance_data = json.load(open(args.instance_data)) except IOError: return error( "File not found '%s'. Provide a path to instance data json file" " using --instance-data" % args.instance_data) except ValueError as e: return error("File '%s' is not valid json. %s" % (args.instance_data, e)) v1 = instance_data.get("v1", {}) cloud_id = canonical_cloud_id( v1.get("cloud_name", METADATA_UNKNOWN), v1.get("region", METADATA_UNKNOWN), v1.get("platform", METADATA_UNKNOWN), ) if args.json: v1["cloud_id"] = cloud_id response = json.dumps( # Pretty, sorted json v1, indent=1, sort_keys=True, separators=(",", ": ")) elif args.long: response = "%s\t%s" % (cloud_id, v1.get("region", METADATA_UNKNOWN)) else: response = cloud_id sys.stdout.write("%s\n" % response) return 0