Skip to content

Commit 3bb8100

Browse files
shawn-yang-googlecopybara-github
authored andcommitted
feat: Add encryption_spec support to Agent Engine genai sdk.
PiperOrigin-RevId: 797986621
1 parent b1d0b7c commit 3bb8100

File tree

3 files changed

+62
-2
lines changed

3 files changed

+62
-2
lines changed

tests/unit/vertexai/genai/test_agent_engines.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -501,6 +501,7 @@ def register_operations(self) -> Dict[str, List[str]]:
501501
"memory": "4Gi",
502502
}
503503
_TEST_AGENT_ENGINE_CONTAINER_CONCURRENCY = 4
504+
_TEST_AGENT_ENGINE_ENCRYPTION_SPEC = {"kms_key_name": "test-kms-key"}
504505
_TEST_AGENT_ENGINE_SPEC = _genai_types.ReasoningEngineSpecDict(
505506
agent_framework=_TEST_AGENT_ENGINE_FRAMEWORK,
506507
class_methods=[_TEST_AGENT_ENGINE_CLASS_METHOD_1],
@@ -803,6 +804,7 @@ def test_create_agent_engine_config_full(self, mock_prepare):
803804
max_instances=_TEST_AGENT_ENGINE_MAX_INSTANCES,
804805
resource_limits=_TEST_AGENT_ENGINE_RESOURCE_LIMITS,
805806
container_concurrency=_TEST_AGENT_ENGINE_CONTAINER_CONCURRENCY,
807+
encryption_spec=_TEST_AGENT_ENGINE_ENCRYPTION_SPEC,
806808
)
807809
assert config["display_name"] == _TEST_AGENT_ENGINE_DISPLAY_NAME
808810
assert config["description"] == _TEST_AGENT_ENGINE_DESCRIPTION
@@ -833,6 +835,7 @@ def test_create_agent_engine_config_full(self, mock_prepare):
833835
"resource_limits": _TEST_AGENT_ENGINE_RESOURCE_LIMITS,
834836
"container_concurrency": _TEST_AGENT_ENGINE_CONTAINER_CONCURRENCY,
835837
}
838+
assert config["encryption_spec"] == _TEST_AGENT_ENGINE_ENCRYPTION_SPEC
836839
assert config["spec"]["class_methods"] == [_TEST_AGENT_ENGINE_CLASS_METHOD_1]
837840

838841
@mock.patch.object(_agent_engines_utils, "_prepare")
@@ -1288,6 +1291,7 @@ def test_create_agent_engine_with_env_vars_dict(
12881291
max_instances=None,
12891292
resource_limits=None,
12901293
container_concurrency=None,
1294+
encryption_spec=None,
12911295
)
12921296
request_mock.assert_called_with(
12931297
"post",

vertexai/_genai/agent_engines.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,17 @@ def _PscInterfaceConfig_to_vertex(
108108
return to_object
109109

110110

111+
def _EncryptionSpec_to_vertex(
112+
from_object: Union[dict[str, Any], object],
113+
parent_object: Optional[dict[str, Any]] = None,
114+
) -> dict[str, Any]:
115+
to_object: dict[str, Any] = {}
116+
if getv(from_object, ["kms_key_name"]) is not None:
117+
setv(to_object, ["kmsKeyName"], getv(from_object, ["kms_key_name"]))
118+
119+
return to_object
120+
121+
111122
def _CreateAgentEngineConfig_to_vertex(
112123
from_object: Union[dict[str, Any], object],
113124
parent_object: Optional[dict[str, Any]] = None,
@@ -145,6 +156,15 @@ def _CreateAgentEngineConfig_to_vertex(
145156
),
146157
)
147158

159+
if getv(from_object, ["encryption_spec"]) is not None:
160+
setv(
161+
parent_object,
162+
["encryptionSpec"],
163+
_EncryptionSpec_to_vertex(
164+
getv(from_object, ["encryption_spec"]), to_object
165+
),
166+
)
167+
148168
return to_object
149169

150170

@@ -332,6 +352,15 @@ def _UpdateAgentEngineConfig_to_vertex(
332352
),
333353
)
334354

355+
if getv(from_object, ["encryption_spec"]) is not None:
356+
setv(
357+
parent_object,
358+
["encryptionSpec"],
359+
_EncryptionSpec_to_vertex(
360+
getv(from_object, ["encryption_spec"]), to_object
361+
),
362+
)
363+
335364
if getv(from_object, ["update_mask"]) is not None:
336365
setv(
337366
parent_object,
@@ -1065,6 +1094,7 @@ def create(
10651094
max_instances=config.max_instances,
10661095
resource_limits=config.resource_limits,
10671096
container_concurrency=config.container_concurrency,
1097+
encryption_spec=config.encryption_spec,
10681098
)
10691099
operation = self._create(config=api_config)
10701100
# TODO: Use a more specific link.
@@ -1118,6 +1148,7 @@ def _create_config(
11181148
max_instances: Optional[int] = None,
11191149
resource_limits: Optional[dict[str, str]] = None,
11201150
container_concurrency: Optional[int] = None,
1151+
encryption_spec: Optional[types.EncryptionSpecDict] = None,
11211152
) -> types.UpdateAgentEngineConfigDict:
11221153
import sys
11231154

@@ -1137,7 +1168,11 @@ def _create_config(
11371168
update_masks.append("description")
11381169
config["description"] = description
11391170
if context_spec is not None:
1171+
update_masks.append("context_spec")
11401172
config["context_spec"] = context_spec
1173+
if encryption_spec is not None:
1174+
update_masks.append("encryption_spec")
1175+
config["encryption_spec"] = encryption_spec
11411176
if agent is not None:
11421177
project = self._api_client.project
11431178
if project is None:

vertexai/_genai/types.py

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3170,7 +3170,7 @@ class CustomJobSpecDict(TypedDict, total=False):
31703170

31713171

31723172
class EncryptionSpec(_common.BaseModel):
3173-
"""Represents a customer-managed encryption key spec that can be applied to a top-level resource."""
3173+
"""The encryption spec."""
31743174

31753175
kms_key_name: Optional[str] = Field(
31763176
default=None,
@@ -3179,7 +3179,7 @@ class EncryptionSpec(_common.BaseModel):
31793179

31803180

31813181
class EncryptionSpecDict(TypedDict, total=False):
3182-
"""Represents a customer-managed encryption key spec that can be applied to a top-level resource."""
3182+
"""The encryption spec."""
31833183

31843184
kms_key_name: Optional[str]
31853185
"""Required. The Cloud KMS resource identifier of the customer managed encryption key used to protect a resource. Has the form: `projects/my-project/locations/my-region/keyRings/my-kr/cryptoKeys/my-key`. The key needs to be in the same region as where the compute resource is created."""
@@ -4029,6 +4029,10 @@ class CreateAgentEngineConfig(_common.BaseModel):
40294029
Recommended value: 2 * cpu + 1. Defaults to 9.
40304030
""",
40314031
)
4032+
encryption_spec: Optional[EncryptionSpec] = Field(
4033+
default=None,
4034+
description="""The encryption spec to be used for the Agent Engine.""",
4035+
)
40324036

40334037

40344038
class CreateAgentEngineConfigDict(TypedDict, total=False):
@@ -4080,6 +4084,9 @@ class CreateAgentEngineConfigDict(TypedDict, total=False):
40804084
Recommended value: 2 * cpu + 1. Defaults to 9.
40814085
"""
40824086

4087+
encryption_spec: Optional[EncryptionSpecDict]
4088+
"""The encryption spec to be used for the Agent Engine."""
4089+
40834090

40844091
CreateAgentEngineConfigOrDict = Union[
40854092
CreateAgentEngineConfig, CreateAgentEngineConfigDict
@@ -4654,6 +4661,10 @@ class UpdateAgentEngineConfig(_common.BaseModel):
46544661
Recommended value: 2 * cpu + 1. Defaults to 9.
46554662
""",
46564663
)
4664+
encryption_spec: Optional[EncryptionSpec] = Field(
4665+
default=None,
4666+
description="""The encryption spec to be used for the Agent Engine.""",
4667+
)
46574668
update_mask: Optional[str] = Field(
46584669
default=None,
46594670
description="""The update mask to apply. For the `FieldMask` definition, see
@@ -4710,6 +4721,9 @@ class UpdateAgentEngineConfigDict(TypedDict, total=False):
47104721
Recommended value: 2 * cpu + 1. Defaults to 9.
47114722
"""
47124723

4724+
encryption_spec: Optional[EncryptionSpecDict]
4725+
"""The encryption spec to be used for the Agent Engine."""
4726+
47134727
update_mask: Optional[str]
47144728
"""The update mask to apply. For the `FieldMask` definition, see
47154729
https://protobuf.dev/reference/protobuf/google.protobuf/#field-mask."""
@@ -8981,6 +8995,10 @@ class AgentEngineConfig(_common.BaseModel):
89818995
Recommended value: 2 * cpu + 1. Defaults to 9.
89828996
""",
89838997
)
8998+
encryption_spec: Optional[EncryptionSpec] = Field(
8999+
default=None,
9000+
description="""The encryption spec to be used for the Agent Engine.""",
9001+
)
89849002

89859003

89869004
class AgentEngineConfigDict(TypedDict, total=False):
@@ -9053,5 +9071,8 @@ class AgentEngineConfigDict(TypedDict, total=False):
90539071
Recommended value: 2 * cpu + 1. Defaults to 9.
90549072
"""
90559073

9074+
encryption_spec: Optional[EncryptionSpecDict]
9075+
"""The encryption spec to be used for the Agent Engine."""
9076+
90569077

90579078
AgentEngineConfigOrDict = Union[AgentEngineConfig, AgentEngineConfigDict]

0 commit comments

Comments
 (0)