Skip to content

Commit fad677a

Browse files
committed
Add MonitoredResource class and tests
1 parent 0bcca69 commit fad677a

2 files changed

Lines changed: 281 additions & 0 deletions

File tree

Lines changed: 184 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,184 @@
1+
/*
2+
* Copyright 2016 Google Inc. All Rights Reserved.
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.google.cloud;
18+
19+
import static com.google.common.base.Preconditions.checkNotNull;
20+
21+
import com.google.common.base.MoreObjects;
22+
import com.google.common.collect.ImmutableMap;
23+
24+
import java.io.Serializable;
25+
import java.util.HashMap;
26+
import java.util.Map;
27+
import java.util.Objects;
28+
29+
/**
30+
* Objects of this class represent a resource that can be used for monitoring, logging, billing, or
31+
* other purposes. Examples include virtual machine instances, databases, and storage devices such
32+
* as disks. The type field identifies a {@link MonitoredResourceDescriptor} object that describes
33+
* the resource's schema. Information in the labels field identifies the actual resource and its
34+
* attributes according to the schema.
35+
*
36+
* <p>For example, the monitored resource for Google Compute Engine VM instances has
37+
* {@code gce_instance} type and specifies values for the labels {@code instance_id} and
38+
* {@code zone} to identify particular VM instances.
39+
*/
40+
public final class MonitoredResource implements Serializable {
41+
42+
private static final long serialVersionUID = -4393604148752640581L;
43+
44+
private final String type;
45+
private final Map<String, String> labels;
46+
47+
/**
48+
* A builder for {@code MonitoredResource} objects.
49+
*/
50+
public static class Builder {
51+
52+
private String type;
53+
private Map<String, String> labels = new HashMap<>();
54+
55+
Builder(String type) {
56+
this.type = type;
57+
}
58+
59+
Builder(MonitoredResource monitoredResource) {
60+
this.type = monitoredResource.type;
61+
this.labels = new HashMap<>(monitoredResource.labels);
62+
}
63+
64+
/**
65+
* Sets the monitored resource type. This value must match the one of
66+
* {@link MonitoredResourceDescriptor#type()} of a {@code MonitoredResourceDescriptor} object.
67+
* For example, the type {@code cloudsql_database} represent databases in Google Cloud SQL.
68+
*/
69+
public Builder type(String type) {
70+
this.type = type;
71+
return this;
72+
}
73+
74+
/**
75+
* Sets the values for all the labels required by the corresponding monitored resource
76+
* descriptor (see {@link MonitoredResourceDescriptor#labels()}. For example, Google Compute
77+
* Engine VM instances use the labels {@code instance_id} and {@code zone}.
78+
*/
79+
public Builder labels(Map<String, String> labels) {
80+
this.labels = new HashMap<>(checkNotNull(labels));
81+
return this;
82+
}
83+
84+
/**
85+
* Adds a label to the labels of the monitored resource.
86+
*/
87+
public Builder addLabel(String key, String value) {
88+
this.labels.put(key, value);
89+
return this;
90+
}
91+
92+
/**
93+
* Clears all the labels of the monitored resource.
94+
*/
95+
public Builder clearLabels() {
96+
this.labels.clear();
97+
return this;
98+
}
99+
100+
public MonitoredResource build() {
101+
return new MonitoredResource(this);
102+
}
103+
}
104+
105+
MonitoredResource(Builder builder) {
106+
this.type = checkNotNull(builder.type);
107+
this.labels = ImmutableMap.copyOf(builder.labels);
108+
}
109+
110+
/**
111+
* Returns the monitored resource type. This value must match the one of
112+
* {@link MonitoredResourceDescriptor#type()} of a {@code MonitoredResourceDescriptor} object.
113+
* For example, the type {@code cloudsql_database} represent databases in Google Cloud SQL.
114+
*/
115+
public String type() {
116+
return type;
117+
}
118+
119+
/**
120+
* Returns the values for all the labels required by the corresponding monitored resource
121+
* descriptor (see {@link MonitoredResourceDescriptor#labels()}. For example, Google Compute
122+
* Engine VM instances use the labels {@code instance_id} and {@code zone}.
123+
*/
124+
public Map<String, String> labels() {
125+
return labels;
126+
}
127+
128+
@Override
129+
public int hashCode() {
130+
return Objects.hash(type, labels);
131+
}
132+
133+
@Override
134+
public boolean equals(Object obj) {
135+
if (obj == this) {
136+
return true;
137+
}
138+
if (!(obj instanceof MonitoredResource)) {
139+
return false;
140+
}
141+
MonitoredResource other = (MonitoredResource) obj;
142+
return Objects.equals(type, other.type) && Objects.equals(labels, other.labels);
143+
}
144+
145+
@Override
146+
public String toString() {
147+
return MoreObjects.toStringHelper(this)
148+
.add("type", type)
149+
.add("labels", labels)
150+
.toString();
151+
}
152+
153+
public com.google.api.MonitoredResource toPb() {
154+
return com.google.api.MonitoredResource.newBuilder()
155+
.setType(type)
156+
.putAllLabels(labels)
157+
.build();
158+
}
159+
160+
/**
161+
* Returns a builder for this {@code MonitoredResource} object.
162+
*/
163+
public Builder toBuilder() {
164+
return new Builder(this);
165+
}
166+
167+
/**
168+
* Returns a builder for {@code MonitoredResource} objects given the resource's type.
169+
*/
170+
public static Builder builder(String type) {
171+
return new Builder(type);
172+
}
173+
174+
/**
175+
* Creates a {@code MonitoredResource} object given the resource's type and labels.
176+
*/
177+
public static MonitoredResource of(String type, Map<String, String> labels) {
178+
return builder(type).labels(labels).build();
179+
}
180+
181+
public static MonitoredResource fromPb(com.google.api.MonitoredResource descriptorPb) {
182+
return new Builder(descriptorPb.getType()).labels(descriptorPb.getLabels()).build();
183+
}
184+
}
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
/*
2+
* Copyright 2016 Google Inc. All Rights Reserved.
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.google.cloud;
18+
19+
import static org.junit.Assert.assertEquals;
20+
21+
import com.google.common.collect.ImmutableMap;
22+
23+
import org.junit.Test;
24+
25+
import java.util.Map;
26+
27+
public class MonitoredResourceTest {
28+
29+
private static final String TYPE = "cloudsql_database";
30+
private static final Map<String, String> LABELS =
31+
ImmutableMap.of("dataset-id", "myDataset", "zone", "myZone");
32+
private static final MonitoredResource MONITORED_RESOURCE = MonitoredResource.builder(TYPE)
33+
.labels(LABELS)
34+
.build();
35+
36+
@Test
37+
public void testBuilder() {
38+
assertEquals(TYPE, MONITORED_RESOURCE.type());
39+
assertEquals(LABELS, MONITORED_RESOURCE.labels());
40+
MonitoredResource monitoredResource = MonitoredResource.builder(TYPE)
41+
.addLabel("dataset-id", "myDataset")
42+
.addLabel("zone", "myZone")
43+
.build();
44+
assertEquals(TYPE, monitoredResource.type());
45+
assertEquals(LABELS, monitoredResource.labels());
46+
compareMonitoredResource(MONITORED_RESOURCE, monitoredResource);
47+
monitoredResource = MonitoredResource.builder(TYPE)
48+
.type("global")
49+
.addLabel("dataset-id", "myDataset")
50+
.addLabel("zone", "myZone")
51+
.clearLabels()
52+
.build();
53+
assertEquals("global", monitoredResource.type());
54+
assertEquals(ImmutableMap.of(), monitoredResource.labels());
55+
}
56+
57+
@Test
58+
public void testToBuilder() {
59+
compareMonitoredResource(MONITORED_RESOURCE, MONITORED_RESOURCE.toBuilder().build());
60+
MonitoredResource monitoredResource = MONITORED_RESOURCE.toBuilder()
61+
.type("global")
62+
.clearLabels()
63+
.build();
64+
assertEquals("global", monitoredResource.type());
65+
assertEquals(ImmutableMap.of(), monitoredResource.labels());
66+
monitoredResource = monitoredResource.toBuilder()
67+
.type(TYPE)
68+
.labels(ImmutableMap.of("dataset-id", "myDataset"))
69+
.addLabel("zone", "myZone")
70+
.build();
71+
compareMonitoredResource(MONITORED_RESOURCE, monitoredResource);
72+
}
73+
74+
@Test
75+
public void testOf() {
76+
MonitoredResource monitoredResource = MonitoredResource.of(TYPE, LABELS);
77+
assertEquals(TYPE, monitoredResource.type());
78+
assertEquals(LABELS, monitoredResource.labels());
79+
compareMonitoredResource(MONITORED_RESOURCE, monitoredResource);
80+
}
81+
82+
@Test
83+
public void testToAndFromPb() {
84+
compareMonitoredResource(MONITORED_RESOURCE,
85+
MonitoredResource.fromPb(MONITORED_RESOURCE.toPb()));
86+
MonitoredResource monitoredResource =
87+
MonitoredResource.of(TYPE, ImmutableMap.<String, String>of());
88+
compareMonitoredResource(monitoredResource, MonitoredResource.fromPb(monitoredResource.toPb()));
89+
}
90+
91+
private void compareMonitoredResource(MonitoredResource expected, MonitoredResource value) {
92+
assertEquals(expected, value);
93+
assertEquals(expected.type(), value.type());
94+
assertEquals(expected.labels(), value.labels());
95+
assertEquals(expected.hashCode(), value.hashCode());
96+
}
97+
}

0 commit comments

Comments
 (0)