crossplane-function-sdk-python 0.11.0__py3-none-any.whl → 0.12.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -15,4 +15,4 @@
15
15
  """The version of function-sdk-python."""
16
16
 
17
17
  # This is set at build time, using "hatch version"
18
- __version__ = "0.11.0"
18
+ __version__ = "0.12.0"
@@ -16,6 +16,7 @@
16
16
 
17
17
  import dataclasses
18
18
  import datetime
19
+ import hashlib
19
20
 
20
21
  import pydantic
21
22
  from google.protobuf import json_format
@@ -59,6 +60,25 @@ def update(r: fnv1.Resource, source: dict | structpb.Struct | pydantic.BaseModel
59
60
  raise TypeError(msg)
60
61
 
61
62
 
63
+ def update_status(
64
+ r: fnv1.Resource,
65
+ status: dict | pydantic.BaseModel,
66
+ ) -> None:
67
+ """Update a resource's status.
68
+
69
+ Args:
70
+ r: A composite or composed resource to update.
71
+ status: The status to set, as a dictionary or Pydantic model.
72
+
73
+ Sets ``r.resource.status`` from the supplied status. When the status
74
+ is a Pydantic model, fields set to their default value are excluded,
75
+ matching the behavior of :func:`update`.
76
+ """
77
+ if isinstance(status, pydantic.BaseModel):
78
+ status = status.model_dump(exclude_defaults=True, warnings=False)
79
+ update(r, {"status": status})
80
+
81
+
62
82
  def dict_to_struct(d: dict) -> structpb.Struct:
63
83
  """Create a Struct well-known type from the supplied dict.
64
84
 
@@ -99,11 +119,17 @@ class Condition:
99
119
  last_transition_time: datetime.time | None = None
100
120
 
101
121
 
102
- def get_condition(resource: structpb.Struct, typ: str) -> Condition:
122
+ def get_condition(
123
+ resource: structpb.Struct | fnv1.Resource | None,
124
+ typ: str,
125
+ ) -> Condition:
103
126
  """Get the supplied status condition of the supplied resource.
104
127
 
105
128
  Args:
106
- resource: A Crossplane resource.
129
+ resource: A Crossplane resource. Can be a protobuf Struct (the raw
130
+ resource), an fnv1.Resource wrapper, or None. When an
131
+ fnv1.Resource is supplied, the Struct is extracted automatically.
132
+ When None is supplied, an unknown condition is returned.
107
133
  typ: The type of status condition to get (e.g. Ready).
108
134
 
109
135
  Returns:
@@ -111,9 +137,23 @@ def get_condition(resource: structpb.Struct, typ: str) -> Condition:
111
137
 
112
138
  A status condition is always returned. If the status condition isn't present
113
139
  in the supplied resource, a condition with status "Unknown" is returned.
140
+
141
+ Accepting fnv1.Resource and None makes it safe to pass the result of a
142
+ protobuf map ``.get()`` call directly. This avoids auto-vivification, which
143
+ silently inserts a default entry when using bracket access on a missing
144
+ key::
145
+
146
+ # Safe — .get() returns None without mutating the map.
147
+ c = get_condition(req.observed.resources.get("bucket"), "Ready")
148
+
149
+ # Unsafe — bracket access auto-vivifies an empty Resource.
150
+ c = get_condition(req.observed.resources["bucket"].resource, "Ready")
114
151
  """
115
152
  unknown = Condition(typ=typ, status="Unknown")
116
153
 
154
+ if isinstance(resource, fnv1.Resource):
155
+ resource = resource.resource
156
+
117
157
  if not resource or "status" not in resource:
118
158
  return unknown
119
159
 
@@ -140,3 +180,37 @@ def get_condition(resource: structpb.Struct, typ: str) -> Condition:
140
180
  return condition
141
181
 
142
182
  return unknown
183
+
184
+
185
+ _DNS_LABEL_MAX = 63
186
+ _HASH_LEN = 5
187
+
188
+
189
+ def child_name(*parts: str, sep: str = "-") -> str:
190
+ """Build a deterministic, DNS-label-safe name for a child resource.
191
+
192
+ Args:
193
+ *parts: Name components to join (e.g. parent name, suffix).
194
+ sep: Separator between parts. Defaults to "-".
195
+
196
+ Returns:
197
+ A name that is at most 63 characters long.
198
+
199
+ Composition functions often derive child resource names from a parent
200
+ name and a discriminator. The resulting name must be a valid DNS label
201
+ (at most 63 characters). This function joins the parts, appends a
202
+ deterministic 5-character hash suffix for uniqueness, and truncates
203
+ the prefix to fit within the limit.
204
+
205
+ The hash suffix is always appended, even for short names, so that
206
+ names are visually consistent regardless of length::
207
+
208
+ child_name("my-xr", "bucket") # "my-xr-bucket-a1b2c"
209
+ child_name("my-very-long-xr-name",
210
+ "with-a-very-long-suffix") # truncated to 63 chars
211
+ """
212
+ full = sep.join(parts)
213
+ h = hashlib.sha256(full.encode()).hexdigest()[:_HASH_LEN]
214
+ max_prefix = _DNS_LABEL_MAX - _HASH_LEN - 1
215
+ prefix = full[:max_prefix].rstrip(sep)
216
+ return f"{prefix}{sep}{h}"
@@ -81,6 +81,44 @@ def fatal(rsp: fnv1.RunFunctionResponse, message: str) -> None:
81
81
  )
82
82
 
83
83
 
84
+ _STATUS_MAP = {
85
+ "True": fnv1.STATUS_CONDITION_TRUE,
86
+ "False": fnv1.STATUS_CONDITION_FALSE,
87
+ "Unknown": fnv1.STATUS_CONDITION_UNKNOWN,
88
+ }
89
+
90
+
91
+ def set_conditions(
92
+ rsp: fnv1.RunFunctionResponse,
93
+ *conditions: resource.Condition,
94
+ ) -> None:
95
+ """Set one or more conditions on the composite resource (XR).
96
+
97
+ Args:
98
+ rsp: The RunFunctionResponse to update.
99
+ *conditions: The conditions to set.
100
+
101
+ Each condition is appended to ``rsp.conditions``. Crossplane uses the
102
+ conditions returned by a function to set custom status conditions on
103
+ the composite resource.
104
+
105
+ The ``last_transition_time`` field of each condition is ignored.
106
+ Crossplane sets the transition time itself.
107
+
108
+ Do not set the ``Ready`` condition type. Crossplane manages it based
109
+ on resource readiness.
110
+ """
111
+ for condition in conditions:
112
+ c = fnv1.Condition(
113
+ type=condition.typ,
114
+ status=_STATUS_MAP.get(condition.status, fnv1.STATUS_CONDITION_UNKNOWN),
115
+ reason=condition.reason or "",
116
+ )
117
+ if condition.message:
118
+ c.message = condition.message
119
+ rsp.conditions.append(c)
120
+
121
+
84
122
  def set_output(rsp: fnv1.RunFunctionResponse, output: dict | structpb.Struct) -> None:
85
123
  """Set the output field in a RunFunctionResponse for operation functions.
86
124
 
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: crossplane-function-sdk-python
3
- Version: 0.11.0
3
+ Version: 0.12.0
4
4
  Summary: The Python SDK for Crossplane composition functions
5
5
  Project-URL: Documentation, https://github.com/crossplane/function-sdk-python#readme
6
6
  Project-URL: Issues, https://github.com/crossplane/function-sdk-python/issues
@@ -14,8 +14,8 @@ Classifier: Programming Language :: Python :: 3.11
14
14
  Classifier: Typing :: Typed
15
15
  Requires-Python: >=3.11
16
16
  Requires-Dist: grpcio-reflection==1.*
17
- Requires-Dist: grpcio==1.76.0
18
- Requires-Dist: protobuf==6.33.5
17
+ Requires-Dist: grpcio==1.80.0
18
+ Requires-Dist: protobuf==7.35.0
19
19
  Requires-Dist: pydantic==2.*
20
20
  Requires-Dist: structlog==25.*
21
21
  Description-Content-Type: text/markdown
@@ -1,9 +1,9 @@
1
- crossplane/function/__version__.py,sha256=ahkaD-KLm3Leg7GtmZ8KV2SldwFdjs1valYJmCVClgA,705
1
+ crossplane/function/__version__.py,sha256=gtv076U2x6TC6KinwdT_-8jd1AkxueHUDZc5tRyideE,705
2
2
  crossplane/function/logging.py,sha256=Zawqq36FBzjkVg4bJ3VGK12-so7N8yAMoSPrhdwpBSA,2503
3
3
  crossplane/function/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
4
4
  crossplane/function/request.py,sha256=ncgGTyAvBOdZ8M7KPy_H9pL08_cYzKJ3D9q_rnSjPXo,8199
5
- crossplane/function/resource.py,sha256=C9TFpDLhAEOFgKEpQ1C8ycfAllCruCWafLuU3tVr3oU,4950
6
- crossplane/function/response.py,sha256=y4Jj8v2L_WCo1M0hJk68PgJyy8xMhvufB8Mlm6zAOFo,5804
5
+ crossplane/function/resource.py,sha256=9tNOsxOzT2mpIbw-tUXfLxH8zs2mGkrkU2JRkFSUN_E,7697
6
+ crossplane/function/response.py,sha256=7LR5CFaMOpBU2Fv-TMFe_CFx0mgZXX9WDm7vV8FtwtY,6965
7
7
  crossplane/function/runtime.py,sha256=2S-AheR2o0pJxJRhDZDoWpJxCleIv_aHTnzRGgrZv_8,5415
8
8
  crossplane/function/proto/v1/run_function.proto,sha256=GbJ0wVUEjfpMsn2DsnhiPvtmL09z3oyMbWwf6viQtiw,16676
9
9
  crossplane/function/proto/v1/run_function_pb2.py,sha256=eeQajHePjvswGCEH2FRxyr8P-K0Go55U5L4rAa32JA8,14440
@@ -13,7 +13,7 @@ crossplane/function/proto/v1beta1/run_function.proto,sha256=EVYIEQCiPu1eOdGJQ3tu
13
13
  crossplane/function/proto/v1beta1/run_function_pb2.py,sha256=G4TTK_cYTOQlRN5byADcEFPjTILYYWj3b4m19iiPO4g,14664
14
14
  crossplane/function/proto/v1beta1/run_function_pb2.pyi,sha256=MvjcY39d1xJ4ZZmRibhehCKF1mHVGb6qfpoCQO8XYqE,14374
15
15
  crossplane/function/proto/v1beta1/run_function_pb2_grpc.py,sha256=Hj5KCSTyT_vIpTXWwc5MwiKxwxaOhjbfY5Iedidx9AQ,4033
16
- crossplane_function_sdk_python-0.11.0.dist-info/METADATA,sha256=WoSPppEIvEubwgLykNP9FfIRripNYhzSeTdH6k8pog0,3244
17
- crossplane_function_sdk_python-0.11.0.dist-info/WHEEL,sha256=WLgqFyCfm_KASv4WHyYy0P3pM_m7J5L9k2skdKLirC8,87
18
- crossplane_function_sdk_python-0.11.0.dist-info/licenses/LICENSE,sha256=xx0jnfkXJvxRnG63LTGOxlggYnIysveWIZ6H3PNdCrQ,11357
19
- crossplane_function_sdk_python-0.11.0.dist-info/RECORD,,
16
+ crossplane_function_sdk_python-0.12.0.dist-info/METADATA,sha256=2l-XG9JHoD_zywEhVUkZQ97iLT05wO4qFEpdeB8Gs3Y,3244
17
+ crossplane_function_sdk_python-0.12.0.dist-info/WHEEL,sha256=QccIxa26bgl1E6uMy58deGWi-0aeIkkangHcxk2kWfw,87
18
+ crossplane_function_sdk_python-0.12.0.dist-info/licenses/LICENSE,sha256=xx0jnfkXJvxRnG63LTGOxlggYnIysveWIZ6H3PNdCrQ,11357
19
+ crossplane_function_sdk_python-0.12.0.dist-info/RECORD,,
@@ -1,4 +1,4 @@
1
1
  Wheel-Version: 1.0
2
- Generator: hatchling 1.28.0
2
+ Generator: hatchling 1.29.0
3
3
  Root-Is-Purelib: true
4
4
  Tag: py3-none-any