protected override void ProcessRecord() { if (MetricName != null && MetricName.Length > 0) { foreach (string metricName in MetricName) { string formattedMetricName = PrefixProjectToMetricName(metricName, Project); try { ProjectsResource.MetricsResource.GetRequest getRequest = Service.Projects.Metrics.Get(formattedMetricName); WriteObject(getRequest.Execute()); } catch (GoogleApiException ex) when(ex.HttpStatusCode == HttpStatusCode.NotFound) { WriteResourceMissingError( exceptionMessage: $"Metric '{metricName}' does not exist in project '{Project}'.", errorId: "MetricNotFound", targetObject: metricName); } } } else { ProjectsResource.MetricsResource.ListRequest listRequest = Service.Projects.Metrics.List($"projects/{Project}"); do { ListLogMetricsResponse response = listRequest.Execute(); if (response.Metrics != null) { WriteObject(response.Metrics, true); } listRequest.PageToken = response.NextPageToken; }while (!Stopping && listRequest.PageToken != null); } }
protected override LoggingBaseServiceRequest <LogMetric> GetRequest(LogMetric logMetric) { string formattedMetricName = PrefixProjectToMetricName(MetricName, Project); // If user does not supply filter or description for update request, we have to use the existing metric's filter. if (string.IsNullOrWhiteSpace(logMetric.Filter) || string.IsNullOrWhiteSpace(logMetric.Description)) { try { ProjectsResource.MetricsResource.GetRequest getRequest = Service.Projects.Metrics.Get(formattedMetricName); LogMetric existingMetric = getRequest.Execute(); if (string.IsNullOrWhiteSpace(logMetric.Filter)) { logMetric.Filter = existingMetric.Filter; } if (string.IsNullOrWhiteSpace(logMetric.Description)) { logMetric.Description = existingMetric.Description; } } catch (GoogleApiException ex) when(ex.HttpStatusCode == HttpStatusCode.NotFound) { // We don't need to throw for description since it's optional. if (string.IsNullOrWhiteSpace(logMetric.Filter)) { throw new PSArgumentNullException( "Cannot construct filter for the metric." + "Please use either -LogName, -Severity, -ResourceType, -Before, -After or -Filter parameters."); } } } return(Service.Projects.Metrics.Update(logMetric, PrefixProjectToMetricName(MetricName, Project))); }