def _create_stackdriver_group_if_missing(project, job_name, region): dashboard_url = None # first see if dashboard is already created for job; otherwise it will # create a new one of the same name but a new ID try: dashboard_url = sd_utils.get_stackdriver_group_url( project, job_name, region ) except Exception as e: msg = ( "Error encountered while trying to determine whether a " "Stackdriver dashboard already exists for this job: {}. " "Skipping...".format(e) ) logging.warning(msg) return if dashboard_url: msg = ( "Reusing existing dashboard previously created for " "'{}': {}.".format(job_name, dashboard_url) ) logging.info(msg) else: # nothing already exists; we'll create one now dashboard_url = sd_utils.create_stackdriver_group( project, job_name, region ) return dashboard_url
def _verify_stackdriver_dashboard(self): logging.info("Verifying Stackdriver Dashboard...") dashboard_url = None try: dashboard_url = sd_utils.get_stackdriver_group_url( self.project, self.job_name, self.region) # Raising a general exception (caught in verify_job) since we're unsure # of how to proceed except Exception as e: msg = ("Error encountered while trying to determine whether a " "Stackdriver dashboard already exists for this job: . " "{}".format(e)) logging.error(msg) raise e if dashboard_url: logging.info( "Stackdriver Dashboard exists at {}".format(dashboard_url)) return True elif self.create_resources: logging.info("Creating Stackdriver Dashboard...") # nothing already exists; we'll create one now dashboard_url = sd_utils.create_stackdriver_group( self.project, self.job_name, self.region) if dashboard_url: # could still be none logging.info("Stackdriver Dashboard created at {}".format( dashboard_url)) return True # reason why dashboard couldn't be created will already be logged logging.error("Could not create Stackdriver Dashboard.") else: logging.info("No Stackdriver Dashboard exists.") return False
def test_create_stackdriver_group_errors(sd_client, caplog): sd_client.create_group.side_effect = Exception("fuuuu") ret_url = sd_utils.create_stackdriver_group( "test-gcp-project", "test-job-name-0", "test-region" ) assert not ret_url assert 1 == len(caplog.records)
def test_create_stackdriver_group(sd_client, groups, caplog): sd_client.create_group.return_value = groups[0] ret_url = sd_utils.create_stackdriver_group( "test-gcp-project", "test-job-name-0", "test-region" ) exp_url = ( "https://app.google.stackdriver.com/groups/1234567890/" "test-job-name-0-test-region-klio-dashboard?" "project=test-gcp-project" ) assert exp_url == ret_url group_arg = { "display_name": "test-job-name-0-test-region-klio-dashboard", "filter": "resource.metadata.name=starts_with(test-job-name-0)", } sd_client.create_group.assert_called_once_with( request={"name": "projects/test-gcp-project", "group": group_arg} ) assert 1 == len(caplog.records)