|
12 | 12 | # See the License for the specific language governing permissions and
|
13 | 13 | # limitations under the License.
|
14 | 14 |
|
15 |
| -from typing import List, Union |
| 15 | +import logging |
| 16 | +from typing import List, Optional, Union |
16 | 17 |
|
17 | 18 | from google.api_core.exceptions import InvalidArgument
|
18 | 19 | from google.api_core.operation import Operation
|
|
38 | 39 | TimeTarget,
|
39 | 40 | SeekSubscriptionRequest,
|
40 | 41 | CreateSubscriptionRequest,
|
| 42 | + ExportConfig, |
41 | 43 | )
|
42 | 44 |
|
| 45 | +log = logging.getLogger(__name__) |
| 46 | + |
43 | 47 |
|
44 | 48 | class AdminClientImpl(AdminClientInterface):
|
45 | 49 | _underlying: AdminServiceClient
|
@@ -85,17 +89,51 @@ def list_topic_subscriptions(self, topic_path: TopicPath) -> List[SubscriptionPa
|
85 | 89 | def create_subscription(
|
86 | 90 | self,
|
87 | 91 | subscription: Subscription,
|
88 |
| - starting_offset: BacklogLocation = BacklogLocation.END, |
| 92 | + target: Union[BacklogLocation, PublishTime, EventTime] = BacklogLocation.END, |
| 93 | + starting_offset: Optional[BacklogLocation] = None, |
89 | 94 | ) -> Subscription:
|
| 95 | + if starting_offset: |
| 96 | + log.warning("starting_offset is deprecated. Use target instead.") |
| 97 | + target = starting_offset |
90 | 98 | path = SubscriptionPath.parse(subscription.name)
|
91 |
| - return self._underlying.create_subscription( |
| 99 | + requires_seek = isinstance(target, PublishTime) or isinstance(target, EventTime) |
| 100 | + requires_update = ( |
| 101 | + requires_seek |
| 102 | + and subscription.export_config |
| 103 | + and subscription.export_config.desired_state == ExportConfig.State.ACTIVE |
| 104 | + ) |
| 105 | + if requires_update: |
| 106 | + # Export subscriptions must be paused while seeking. The state is |
| 107 | + # later updated to active. |
| 108 | + subscription.export_config.desired_state = ExportConfig.State.PAUSED |
| 109 | + |
| 110 | + # Request 1 - Create the subscription. |
| 111 | + skip_backlog = False |
| 112 | + if isinstance(target, BacklogLocation): |
| 113 | + skip_backlog = target == BacklogLocation.END |
| 114 | + response = self._underlying.create_subscription( |
92 | 115 | request=CreateSubscriptionRequest(
|
93 | 116 | parent=str(path.to_location_path()),
|
94 | 117 | subscription=subscription,
|
95 | 118 | subscription_id=path.name,
|
96 |
| - skip_backlog=(starting_offset == BacklogLocation.END), |
| 119 | + skip_backlog=skip_backlog, |
97 | 120 | )
|
98 | 121 | )
|
| 122 | + # Request 2 (optional) - seek the subscription. |
| 123 | + if requires_seek: |
| 124 | + self.seek_subscription(subscription_path=path, target=target) |
| 125 | + # Request 3 (optional) - make the export subscription active. |
| 126 | + if requires_update: |
| 127 | + response = self.update_subscription( |
| 128 | + subscription=Subscription( |
| 129 | + name=response.name, |
| 130 | + export_config=ExportConfig( |
| 131 | + desired_state=ExportConfig.State.ACTIVE, |
| 132 | + ), |
| 133 | + ), |
| 134 | + update_mask=FieldMask(paths=["export_config.desired_state"]), |
| 135 | + ) |
| 136 | + return response |
99 | 137 |
|
100 | 138 | def get_subscription(self, subscription_path: SubscriptionPath) -> Subscription:
|
101 | 139 | return self._underlying.get_subscription(name=str(subscription_path))
|
|
0 commit comments