Debian Codegraph @

ansible_collections/kubernetes/core/plugins/module_utils/k8sdynamicclient.py

raw symbols SCIP
1 # Copyright [2017] [Red Hat, Inc.]
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15
16 from __future__ import absolute_import, division, print_function
17
18 __metaclass__ = type
19
20
21 from ansible_collections.kubernetes.core.plugins.module_utils.apply import k8s_apply
22 from ansible_collections.kubernetes.core.plugins.module_utils.exceptions import (
23 ApplyException,
24 )
25 from kubernetes.dynamic import DynamicClient
26
27
28 class K8SDynamicClient(DynamicClient):
29 def apply(self, resource, body=None, name=None, namespace=None, **kwargs):
30 body = super().serialize_body(body)
31 body["metadata"] = body.get("metadata", dict())
32 name = name or body["metadata"].get("name")
33 if not name:
34 raise ValueError(
35 "name is required to apply {0}.{1}".format(
36 resource.group_version, resource.kind
37 )
38 )
39 if resource.namespaced:
40 body["metadata"]["namespace"] = super().ensure_namespace(
41 resource, namespace, body
42 )
43 try:
44 return k8s_apply(resource, body, **kwargs)
45 except ApplyException as e:
46 raise ValueError(
47 "Could not apply strategic merge to %s/%s: %s"
48 % (body["kind"], body["metadata"]["name"], e)
49 )