5
5
from pydantic import BaseModel
6
6
7
7
from models .core import (
8
+ GroupVersionKind ,
9
+ NsCrGvkName ,
8
10
Transaction ,
9
11
TransactionContent ,
10
12
TransactionCr ,
@@ -68,6 +70,11 @@ def add_to_transaction_replace(self, resource: BaseModel) -> None:
68
70
69
71
self .add_to_transaction (resource , _REPLACE )
70
72
73
+ def add_to_transaction_delete (self , resource : BaseModel ) -> None :
74
+ """Add resource to the delete list of a transaction"""
75
+
76
+ self .add_to_transaction (resource , _DELETE )
77
+
71
78
def add_to_transaction (self , resource : BaseModel , type : TxType ) -> None :
72
79
"""Add resource to transaction"""
73
80
@@ -78,15 +85,34 @@ def add_to_transaction(self, resource: BaseModel, type: TxType) -> None:
78
85
)
79
86
)
80
87
88
+ if (
89
+ content .apiVersion is None
90
+ or content .kind is None
91
+ or content .metadata is None
92
+ ):
93
+ raise ValueError (
94
+ f"Resource { content .apiVersion } { content .kind } is not a valid resource"
95
+ )
96
+
81
97
logger .info (
82
98
f"Adding '{ content .kind } ' resource from '{ content .apiVersion } ' to the '{ type } ' transaction list"
83
99
)
84
100
85
101
tx_type_mapping = {
86
102
"create" : TransactionType (create = TransactionValue (value = content )),
87
- # "delete": TransactionType(delete=content),
103
+ "delete" : TransactionType (
104
+ delete = NsCrGvkName (
105
+ gvk = GroupVersionKind (
106
+ group = content .apiVersion .split ("/" )[0 ],
107
+ version = content .apiVersion .split ("/" )[1 ],
108
+ kind = content .kind ,
109
+ ),
110
+ name = content .metadata .name ,
111
+ namespace = content .metadata .namespace ,
112
+ )
113
+ ),
88
114
"modify" : TransactionType (modify = TransactionValue (value = content )),
89
- "replace" : TransactionType (modify = TransactionValue (value = content )),
115
+ "replace" : TransactionType (replace = TransactionValue (value = content )),
90
116
}
91
117
92
118
tx_cr = TransactionCr (type = tx_type_mapping [type ])
@@ -114,6 +140,8 @@ def commit_transaction(self) -> Any:
114
140
exclude_unset = True , exclude_none = True , exclude_defaults = True
115
141
)
116
142
143
+ logger .info (f"Committing transaction: { content } " )
144
+
117
145
response = self .post (
118
146
url = self .transaction_endpoint ,
119
147
content = content ,
0 commit comments