Skip to content

Commit e114569

Browse files
authored
Add monitoring_get_resource tag/code sample to monitoring snippets. (GoogleCloudPlatform#1306)
* Add monitoring_get_resource tag/code sample to monitoring snippets. * Add monitoring_alert_delete_channel code, tag, and test.
1 parent 5c552d6 commit e114569

File tree

4 files changed

+160
-1
lines changed

4 files changed

+160
-1
lines changed

monitoring/cloud-client/src/main/java/com/example/monitoring/Snippets.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import com.google.cloud.monitoring.v3.MetricServiceClient.ListMetricDescriptorsPagedResponse;
2525
import com.google.cloud.monitoring.v3.MetricServiceClient.ListMonitoredResourceDescriptorsPagedResponse;
2626
import com.google.cloud.monitoring.v3.MetricServiceClient.ListTimeSeriesPagedResponse;
27+
import com.google.gson.Gson;
2728
import com.google.monitoring.v3.Aggregation;
2829
import com.google.monitoring.v3.CreateMetricDescriptorRequest;
2930
import com.google.monitoring.v3.CreateTimeSeriesRequest;
@@ -39,6 +40,7 @@
3940
import com.google.monitoring.v3.TypedValue;
4041
import com.google.protobuf.Duration;
4142
import com.google.protobuf.util.Timestamps;
43+
4244
import java.io.IOException;
4345
import java.util.ArrayList;
4446
import java.util.HashMap;
@@ -49,8 +51,8 @@
4951

5052

5153
public class Snippets {
52-
5354
private static final String CUSTOM_METRIC_DOMAIN = "custom.googleapis.com";
55+
private static final Gson gson = new Gson();
5456

5557
/**
5658
* Exercises the methods defined in this class.
@@ -392,6 +394,17 @@ void listMonitoredResources() throws IOException {
392394
// [END monitoring_list_resources]
393395
}
394396

397+
// [START monitoring_get_resource]
398+
void getMonitoredResource(String resourceId) throws IOException {
399+
String projectId = System.getProperty("projectId");
400+
MetricServiceClient client = MetricServiceClient.create();
401+
MonitoredResourceDescriptorName name =
402+
MonitoredResourceDescriptorName.of(projectId, resourceId);
403+
MonitoredResourceDescriptor response = client.getMonitoredResourceDescriptor(name);
404+
System.out.println("Retrieved Monitored Resource: " + gson.toJson(response));
405+
}
406+
// [END monitoring_get_resource]
407+
395408
/**
396409
* Gets full information for a monitored resource.
397410
*

monitoring/cloud-client/src/test/java/com/example/monitoring/SnippetsIT.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
import java.io.ByteArrayOutputStream;
2222
import java.io.PrintStream;
23+
2324
import org.junit.After;
2425
import org.junit.Before;
2526
import org.junit.Test;
@@ -70,6 +71,19 @@ public void testListMetricsDescriptor() throws Exception {
7071
assertThat(got).contains("metricDescriptors/bigquery.googleapis.com/query/count");
7172
}
7273

74+
@Test
75+
public void testGetMetricsDescriptor() throws Exception {
76+
// Act
77+
System.setProperty("projectId", SnippetsIT.getProjectId());
78+
Snippets snippets = new Snippets();
79+
80+
snippets.getMonitoredResource("api");
81+
82+
// Assert
83+
String got = bout.toString();
84+
assertThat(got).contains("Produced API");
85+
}
86+
7387
@Test
7488
public void testListTimeSeries() throws Exception {
7589
// Act
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* Copyright 2018 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example;
18+
19+
import com.google.cloud.monitoring.v3.NotificationChannelServiceClient;
20+
import com.google.monitoring.v3.NotificationChannelName;
21+
22+
import java.io.IOException;
23+
24+
public class DeleteNotificationChannel {
25+
/**
26+
* Demonstrates deleting a notification channel by name.
27+
* @param channelName Name of the notification channel to delete.
28+
*/
29+
// [START monitoring_alert_delete_channel]
30+
static void deleteNotificationChannel(String channelName) throws IOException {
31+
String projectId = System.getProperty("projectId");
32+
try (NotificationChannelServiceClient client = NotificationChannelServiceClient.create()) {
33+
NotificationChannelName name = NotificationChannelName.of(projectId, channelName);
34+
client.deleteNotificationChannel(channelName, false);
35+
System.out.println("Deleted notification channel " + channelName);
36+
}
37+
}
38+
// [END monitoring_alert_delete_channel]
39+
}
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/*
2+
* Copyright 2018 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example;
18+
19+
import static com.google.common.truth.Truth.assertThat;
20+
21+
import com.google.cloud.monitoring.v3.NotificationChannelServiceClient;
22+
import com.google.monitoring.v3.NotificationChannel;
23+
import com.google.monitoring.v3.ProjectName;
24+
25+
import java.io.ByteArrayOutputStream;
26+
import java.io.IOException;
27+
import java.io.PrintStream;
28+
29+
import org.junit.After;
30+
import org.junit.Before;
31+
import org.junit.BeforeClass;
32+
import org.junit.Test;
33+
import org.junit.runner.RunWith;
34+
import org.junit.runners.JUnit4;
35+
36+
/**
37+
* Tests for delete notification channel sample.
38+
*/
39+
@RunWith(JUnit4.class)
40+
@SuppressWarnings("checkstyle:abbreviationaswordinname")
41+
public class DeleteNotificationChannelIT {
42+
private ByteArrayOutputStream bout;
43+
private PrintStream out;
44+
private static final String LEGACY_PROJECT_ENV_NAME = "GCLOUD_PROJECT";
45+
private static final String PROJECT_ENV_NAME = "GOOGLE_CLOUD_PROJECT";
46+
private static String NOTIFICATION_CHANNEL_NAME = "channelname";
47+
private static NotificationChannel NOTIFICATION_CHANNEL;
48+
49+
private static String getProjectId() {
50+
String projectId = System.getProperty(PROJECT_ENV_NAME, System.getenv(PROJECT_ENV_NAME));
51+
if (projectId == null) {
52+
projectId = System.getProperty(LEGACY_PROJECT_ENV_NAME,
53+
System.getenv(LEGACY_PROJECT_ENV_NAME));
54+
}
55+
return projectId;
56+
}
57+
58+
@BeforeClass
59+
public static void setupClass() throws IOException {
60+
try (NotificationChannelServiceClient client = NotificationChannelServiceClient.create()) {
61+
String projectId = getProjectId();
62+
NOTIFICATION_CHANNEL = NotificationChannel.newBuilder()
63+
.setType("email")
64+
.putLabels("email_address", "[email protected]")
65+
.build();
66+
NotificationChannel channel = client.createNotificationChannel(ProjectName.of(projectId),
67+
NOTIFICATION_CHANNEL);
68+
NOTIFICATION_CHANNEL_NAME = channel.getName();
69+
}
70+
}
71+
72+
@Before
73+
public void setUp() {
74+
bout = new ByteArrayOutputStream();
75+
out = new PrintStream(bout);
76+
System.setOut(out);
77+
System.setProperty("projectId", DeleteNotificationChannelIT.getProjectId());
78+
}
79+
80+
@After
81+
public void tearDown() {
82+
System.setOut(null);
83+
}
84+
85+
@Test
86+
public void testDeleteNotificationChannel() throws Exception {
87+
// Act
88+
DeleteNotificationChannel.deleteNotificationChannel(NOTIFICATION_CHANNEL_NAME);
89+
// Assert
90+
String got = bout.toString();
91+
assertThat(got).contains(NOTIFICATION_CHANNEL_NAME);
92+
}
93+
}

0 commit comments

Comments
 (0)