55"""
66
77import json
8+ import logging
89from typing import Any , Dict , List , Tuple
910
1011from absl import flags
12+ from perfkitbenchmarker import errors
1113from perfkitbenchmarker import regex_util
1214from perfkitbenchmarker import relational_db
1315from perfkitbenchmarker import relational_db_spec
1416from perfkitbenchmarker .providers .gcp import util
17+ import requests
1518
1619FLAGS = flags .FLAGS
1720
@@ -127,6 +130,8 @@ def _Create(self) -> None:
127130 'create' ,
128131 self .cluster_id ,
129132 '--password=%s' % self .spec .database_password ,
133+ '--network=%s' % self .client_vm .network .network_resource .name ,
134+ '--allocated-ip-range-name=google-service-range' ,
130135 ]
131136
132137 cmd = self ._GetAlloyDbCommand (cmd_string )
@@ -200,6 +205,49 @@ def _PostCreate(self) -> None:
200205 _ENABLE_COLUMNAR_RECOMMENDATION .value ,
201206 _ENABLE_AUTO_COLUMNARIZATION .value ,
202207 )
208+ self ._UpdateLabels (util .GetDefaultTags ())
209+
210+ def _DescribeCluster (self ) -> Dict [str , Any ]:
211+ cmd = util .GcloudCommand (
212+ self ,
213+ 'alloydb' ,
214+ 'clusters' ,
215+ 'describe' ,
216+ self .cluster_id ,
217+ )
218+ cmd .flags ['project' ] = self .project
219+ cmd .flags ['zone' ] = []
220+ cmd .flags ['region' ] = self .region
221+ stdout , _ , _ = cmd .Issue ()
222+ return json .loads (stdout )
223+
224+ def _GetLabels (self ) -> Dict [str , Any ]:
225+ """Gets labels from the current instance."""
226+ return self ._DescribeCluster ().get ('labels' , {})
227+
228+ def _UpdateLabels (self , labels : Dict [str , Any ]) -> None :
229+ """Updates the labels of the current instance."""
230+ header = {'Authorization' : f'Bearer { util .GetAccessToken ()} ' }
231+ url = (
232+ 'https://alloydb.googleapis.com/v1/'
233+ f'projects/{ self .project } /locations/{ self .region } /clusters/{ self .cluster_id } '
234+ )
235+ # Keep any existing labels
236+ tags = self ._GetLabels ()
237+ tags .update (labels )
238+ response = requests .patch (
239+ url ,
240+ headers = header ,
241+ params = {'updateMask' : 'labels' },
242+ json = {'labels' : tags },
243+ )
244+ logging .info (
245+ 'Update labels: status code %s, %s' , response .status_code , response .text
246+ )
247+ if response .status_code != 200 :
248+ raise errors .Resource .UpdateError (
249+ f'Unable to update AlloyDB cluster: { response .text } '
250+ )
203251
204252 def AddTableToColumnarEngine (self , table : str , database_name : str ) -> None :
205253 self .client_vm_query_tools .IssueSqlCommand (
@@ -281,7 +329,8 @@ def GetColumnarEngineRecommendation(
281329 )
282330
283331 def _GetAlloyDbCommand (
284- self , cmd_string : List [str ], timeout : int | None = None
332+ self ,
333+ cmd_string : List [str ],
285334 ) -> util .GcloudCommand :
286335 """Used to issue alloydb command."""
287336 cmd_string = [self , 'alpha' , 'alloydb' ] + cmd_string
@@ -291,18 +340,10 @@ def _GetAlloyDbCommand(
291340 cmd .flags ['region' ] = self .region
292341 return cmd
293342
294- def _IsReady (self , timeout : int = IS_READY_TIMEOUT ) -> bool :
295- """Return true if the underlying resource is ready.
296-
297- Alloydb Creation is synchronous, therefore is ready is not needed.
298-
299- Args:
300- timeout: how long to wait when checking if the DB is ready.
301-
302- Returns:
303- True if the resource was ready in time, False if the wait timed out.
304- """
305- return True
343+ def _IsReady (self ) -> bool :
344+ """Return true if the underlying resource is ready."""
345+ cluster = self ._DescribeCluster ()
346+ return cluster ['state' ] == 'READY'
306347
307348 def _ApplyDbFlags (self ):
308349 # Database flags is applied during creation.
0 commit comments