def ProcessConfigOverrideFile(config_override_file): """Reads a JSON/YAML config_override_file and returns collection of config override object.""" try: overrides = json.loads(config_override_file[0]) except ValueError as e: try: overrides = yaml.load(config_override_file[0]) except yaml.YAMLParseError as e: raise InvalidSpecFileError( 'Error parsing config_override file: [{}]'.format(e)) messages = utils.GetMessages() message_class = messages.GameServerConfigOverride try: overrides_message = [encoding.DictToMessage(o, message_class) for o in overrides] except AttributeError: raise InvalidSchemaError( 'Invalid schema: unexpected game server config override(s) format.') except _messages.ValidationError as e: # Unfortunately apitools doesn't provide a way to get the path to the # invalid field here. raise InvalidSchemaError('Invalid schema: [{}]'.format(e)) unrecognized_field_paths = _GetUnrecognizedFieldPaths(overrides_message) if unrecognized_field_paths: error_msg_lines = ['Invalid schema, the following fields are unrecognized:'] error_msg_lines += unrecognized_field_paths raise InvalidSchemaError('\n'.join(error_msg_lines)) return overrides_message
def ProcessScalingConfigsFile(scaling_configs_file): """Reads a JSON/YAML scaling_configs_file and returns collectiong of scaling configs object.""" try: scaling_configs = json.loads(scaling_configs_file[0]) except ValueError as e: try: scaling_configs = yaml.load(scaling_configs_file[0]) except yaml.YAMLParseError as e: raise InvalidSpecFileError( 'Error parsing scaling_configs file: [{}]'.format(e)) messages = utils.GetMessages() message_class = messages.ScalingConfig try: selector = messages.LabelSelector() scaling_configs_message = [] for sc in scaling_configs: esc = encoding.DictToMessage(sc, message_class) if not esc.selectors: # Add default selector if not set esc.selectors = [selector] scaling_configs_message.append(esc) except AttributeError: raise InvalidSchemaError( 'Invalid schema: expected proper scaling configs') except _messages.ValidationError as e: # Unfortunately apitools doesn't provide a way to get the path to the # invalid field here. raise InvalidSchemaError('Invalid schema: [{}]'.format(e)) return scaling_configs_message
def ProcessFleetConfigsFile(fleet_configs_file): """Reads a JSON/YAML fleet_configs_file and returns collectiong of fleet configs object.""" try: fleet_configs = json.loads(fleet_configs_file[0]) except ValueError as e: try: fleet_configs = yaml.load(fleet_configs_file[0]) except yaml.YAMLParseError as e: raise InvalidSpecFileError( 'Error parsing fleet_configs file: [{}]'.format(e)) messages = utils.GetMessages() message_class = messages.FleetConfig try: fleet_configs_message = [encoding.DictToMessage(fc, message_class) for fc in fleet_configs] except AttributeError: raise InvalidSchemaError( 'Invalid schema: expected proper fleet configs') except _messages.ValidationError as e: # Unfortunately apitools doesn't provide a way to get the path to the # invalid field here. raise InvalidSchemaError('Invalid schema: [{}]'.format(e)) unrecognized_field_paths = _GetUnrecognizedFieldPaths(fleet_configs_message) if unrecognized_field_paths: error_msg_lines = ['Invalid schema, the following fields are unrecognized:'] error_msg_lines += unrecognized_field_paths raise InvalidSchemaError('\n'.join(error_msg_lines)) return fleet_configs_message
def ClearDefaultConfig(ref, args, request): del ref if args.clear_default_config: if not request.gameServerDeploymentRollout: messages = utils.GetMessages(utils.GetApiVersionFromArgs(args)) gsd = messages.GameServerDeploymentRollout() request.gameServerDeploymentRollout = gsd request.gameServerDeploymentRollout.defaultGameServerConfig = '' return request
def ClearConfigOverrides(ref, args, request): del ref if args.clear_config_overrides: if not request.gameServerDeploymentRollout: messages = utils.GetMessages(utils.GetApiVersionFromArgs(args)) gsd = messages.GameServerDeploymentRollout() request.gameServerDeploymentRollout = gsd request.gameServerDeploymentRollout.gameServerConfigOverrides = [] return request
def ProcessConfigsFiles(ref, args, request): """Reads the config into GameServerConfig proto and updates the request.""" del ref if args.config_overrides_file: if not request.gameServerDeploymentRollout: messages = utils.GetMessages(utils.GetApiVersionFromArgs(args)) gsd = messages.GameServerDeploymentRollout() request.gameServerDeploymentRollout = gsd request.gameServerDeploymentRollout.gameServerConfigOverrides = utils.ProcessConfigOverrideFile( args.config_overrides_file, utils.GetApiVersionFromArgs(args)) return request
def ProcessConfigsFiles(ref, args, request): """Reads the fleet/scaling configs into FleetConfig/ScalingConfig protos and updates the request.""" del ref if args.fleet_configs_file: if not request.gameServerConfig: messages = utils.GetMessages(utils.GetApiVersionFromArgs(args)) gsc = messages.GameServerConfig() request.gameServerConfig = gsc request.gameServerConfig.fleetConfigs = utils.ProcessFleetConfigsFile( args.fleet_configs_file, utils.GetApiVersionFromArgs(args)) if args.scaling_configs_file: if not request.gameServerConfig: messages = utils.GetMessages(utils.GetApiVersionFromArgs(args)) gsc = messages.GameServerConfig() request.gameServerConfig = gsc request.gameServerConfig.scalingConfigs = utils.ProcessScalingConfigsFile( args.scaling_configs_file, utils.GetApiVersionFromArgs(args)) return request
def ProcessScalingConfigsFile(scaling_configs_file): """Reads a JSON/YAML scaling_configs_file and returns collectiong of scaling configs object.""" try: scaling_configs = json.loads(scaling_configs_file[0]) except ValueError as e: try: scaling_configs = yaml.load(scaling_configs_file[0]) except yaml.YAMLParseError as e: raise InvalidSpecFileError( 'Error parsing scaling_configs file: [{}]'.format(e)) messages = utils.GetMessages() message_class = messages.ScalingConfig selector = messages.LabelSelector() scaling_configs_message = [] try: for sc in scaling_configs: esc = encoding.DictToMessage(sc, message_class) if not esc.selectors: # Add default selector if not set esc.selectors = [selector] # Convert yaml to json spec = yaml.load(esc.fleetAutoscalerSpec) spec_as_json_str = json.dumps(spec) esc.fleetAutoscalerSpec = spec_as_json_str scaling_configs_message.append(esc) except AttributeError: raise InvalidSchemaError( 'Invalid schema: expected proper scaling configs') except _messages.ValidationError as e: # The most likely reason this is reaised is the file that is submitted is # following new format (json/yaml without string blob) and we will parse # with the new format for sc in scaling_configs: s = messages.ScalingConfig() if 'selectors' in sc: s.selectors = sc['selectors'] else: # Add default selector if not set s.selectors = [selector] if 'name' in sc: s.name = sc['name'] if 'schedules' in sc: s.schedules = sc['schedules'] if 'fleetAutoscalerSpec' not in sc: raise InvalidSchemaError( 'Invalid schema: expected proper scaling configs') spec_as_json_str = json.dumps(sc['fleetAutoscalerSpec']) s.fleetAutoscalerSpec = spec_as_json_str scaling_configs_message.append(s) return scaling_configs_message
def ProcessFleetConfigsFile(fleet_configs_file): """Reads a JSON/YAML fleet_configs_file and returns collectiong of fleet configs object.""" try: fleet_configs = json.loads(fleet_configs_file[0]) except ValueError as e: try: fleet_configs = yaml.load(fleet_configs_file[0]) except yaml.YAMLParseError as e: raise InvalidSpecFileError( 'Error parsing fleet_configs file: [{}]'.format(e)) messages = utils.GetMessages() message_class = messages.FleetConfig fleet_configs_message = [] try: for fc in fleet_configs: f = encoding.DictToMessage(fc, message_class) spec = yaml.load(f.fleetSpec) spec_as_json_str = json.dumps(spec) f.fleetSpec = spec_as_json_str fleet_configs_message.append(f) except AttributeError: raise InvalidSchemaError( 'Invalid schema: expected proper fleet configs') except _messages.ValidationError as e: # The most likely reason this is reaised is the file that is submitted is # following new format (json/yaml without string blob) and we will parse # with the new format for fc in fleet_configs: f = messages.FleetConfig() if 'name' in fc: f.name = fc['name'] if 'fleetSpec' not in fc: raise InvalidSchemaError( 'Invalid schema: expected proper fleet configs') spec_as_json_str = json.dumps(fc['fleetSpec']) f.fleetSpec = spec_as_json_str fleet_configs_message.append(f) unrecognized_field_paths = _GetUnrecognizedFieldPaths( fleet_configs_message) if unrecognized_field_paths: error_msg_lines = [ 'Invalid schema, the following fields are unrecognized:' ] error_msg_lines += unrecognized_field_paths raise InvalidSchemaError('\n'.join(error_msg_lines)) return fleet_configs_message