| 271 | |
| 272 | |
| 273 | class ConfigAPI(BaseAPI): |
| 274 | model = ConfigDB |
| 275 | schema = { |
| 276 | "title": "Config", |
| 277 | "description": "Pack config.", |
| 278 | "type": "object", |
| 279 | "properties": { |
| 280 | "id": { |
| 281 | "description": "The unique identifier for the config.", |
| 282 | "type": "string", |
| 283 | }, |
| 284 | "pack": { |
| 285 | "description": "The content pack this config belongs to.", |
| 286 | "type": "string", |
| 287 | }, |
| 288 | "values": { |
| 289 | "description": "Config values.", |
| 290 | "type": "object", |
| 291 | "default": {}, |
| 292 | }, |
| 293 | }, |
| 294 | "additionalProperties": False, |
| 295 | } |
| 296 | |
| 297 | def validate(self, validate_against_schema=False): |
| 298 | # Perform base API model validation against json schema |
| 299 | result = super(ConfigAPI, self).validate() |
| 300 | |
| 301 | # Perform config values validation against the config values schema |
| 302 | if validate_against_schema: |
| 303 | cleaned_values = self._validate_config_values_against_schema() |
| 304 | result.values = cleaned_values |
| 305 | |
| 306 | return result |
| 307 | |
| 308 | def _validate_config_values_against_schema(self): |
| 309 | try: |
| 310 | config_schema_db = ConfigSchema.get_by_pack(value=self.pack) |
| 311 | except StackStormDBObjectNotFoundError: |
| 312 | # Config schema is optional |
| 313 | return |
| 314 | |
| 315 | # Note: We are doing optional validation so for now, we do allow additional properties |
| 316 | instance = self.values or {} |
| 317 | schema = config_schema_db.attributes or {} |
| 318 | |
| 319 | configs_path = os.path.join(cfg.CONF.system.base_path, "configs/") |
| 320 | config_path = os.path.join(configs_path, "%s.yaml" % (self.pack)) |
| 321 | |
| 322 | cleaned = validate_config_against_schema( |
| 323 | config_schema=schema, |
| 324 | config_object=instance, |
| 325 | config_path=config_path, |
| 326 | pack_name=self.pack, |
| 327 | ) |
| 328 | |
| 329 | return cleaned |
| 330 |
no outgoing calls
no test coverage detected