create_from_yaml function deals with files only

Adding the ability to deal with strings containing yaml seems to repel
to much. So we stay with create_from_yaml with a bad name.

This removes the need fro StringIO to wrap strings.
Also note:

```
with open('foo.txt') as f:
    y = yaml.safe_load_all(f)

for i in y:
    print(i)

\# raises ValueError: I/O operation on closed file.
```

Hence, we indent the whole method body into the open block.

with open('foo.txt') as f:
    y = yaml.safe_load_all(f)
    for i in y:
        print(i)
This commit is contained in:
Oz N Tiram 2019-06-20 22:48:35 +02:00
parent ed67d89f9b
commit 9e40421bcc

View File

@ -14,18 +14,12 @@
import re
import sys
from os import path
import yaml
from kubernetes import client
if sys.version_info.major < 3:
from StringIO import StringIO # noqa: F406
else:
from io import StringIO # noqa: F406
def create_from_yaml(
k8s_client,
@ -47,14 +41,6 @@ def create_from_yaml(
the yaml file already contains a namespace definition
this parameter has no effect.
Returns:
An k8s api object or list of apis objects created from YAML.
When a single object is generated, return type is dependent
on output_list.
Throws a FailToCreateError exception if creation of any object
fails with helpful messages from the server.
Available parameters for creating <kind>:
:param async_req bool
:param bool include_uninitialized: If true, partially initialized
@ -65,19 +51,24 @@ def create_from_yaml(
directive will result in an error response and no further
processing of the request.
Valid values are: - All: all dry run stages will be processed
Raises:
FailToCreateError which holds list of `client.rest.ApiException`
instances for each object that failed to create.
"""
with open(path.abspath(yaml_file)) as f:
content = f.read()
try:
yaml_file = StringIO(content)
except TypeError:
yaml_file = StringIO(content.decode('utf-8'))
yml_document_all = yaml.safe_load_all(f)
yml_document_all = yaml.safe_load_all(yaml_file)
failures = []
for yml_document in yml_document_all:
create_from_dict(k8s_client, yml_document, verbose,
**kwargs)
for yml_document in yml_document_all:
try:
create_from_dict(k8s_client, yml_document, verbose,
**kwargs)
except FailToCreateError as failure:
failures.extend(failure.api_exceptions)
if failures:
raise FailToCreateError(failures)
def create_from_dict(k8s_client, data, verbose=False, **kwargs):