Skip to content

Commit 3d082a1

Browse files
Merge pull request #4152 from justinsb/mockgcp_monitoring_notification
mockgcp: fill in monitoring notificationchannel
2 parents f62802b + 169a3cb commit 3d082a1

File tree

7 files changed

+309
-12
lines changed

7 files changed

+309
-12
lines changed

mockgcp/mock_http_roundtrip.go

+1-3
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ import (
9090
"github.com/GoogleCloudPlatform/k8s-config-connector/mockgcp/mockmanagedkafka"
9191
"github.com/GoogleCloudPlatform/k8s-config-connector/mockgcp/mockmetastore"
9292
"github.com/GoogleCloudPlatform/k8s-config-connector/mockgcp/mockmodelarmor"
93-
"github.com/GoogleCloudPlatform/k8s-config-connector/mockgcp/mockmonitoring"
93+
_ "github.com/GoogleCloudPlatform/k8s-config-connector/mockgcp/mockmonitoring"
9494
"github.com/GoogleCloudPlatform/k8s-config-connector/mockgcp/mocknetapp"
9595
"github.com/GoogleCloudPlatform/k8s-config-connector/mockgcp/mocknetworkconnectivity"
9696
"github.com/GoogleCloudPlatform/k8s-config-connector/mockgcp/mocknetworkmanagement"
@@ -224,12 +224,10 @@ func NewMockRoundTripper(ctx context.Context, k8sClient client.Client, storage s
224224
services = append(services, mockclouddeploy.New(env, storage))
225225
services = append(services, mocksecretmanager.New(env, storage))
226226
services = append(services, mockspanner.New(env, storage))
227-
services = append(services, mockmonitoring.New(env, storage))
228227
services = append(services, mockpubsublite.New(env, storage))
229228
services = append(services, mocknetworkconnectivity.New(env, storage))
230229
services = append(services, mocknotebooks.New(env, storage))
231230
services = append(services, mockprivilegedaccessmanager.New(env, storage))
232-
233231
services = append(services, mockredis.New(env, storage))
234232
services = append(services, mocksecuresourcemanager.New(env, storage))
235233
services = append(services, mockservicenetworking.New(env, storage))

mockgcp/mockmonitoring/normalize.go

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// Copyright 2025 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package mockmonitoring
16+
17+
import (
18+
"github.com/GoogleCloudPlatform/k8s-config-connector/mockgcp/mockgcpregistry"
19+
)
20+
21+
var _ mockgcpregistry.SupportsNormalization = &MockService{}
22+
23+
const (
24+
emailPlaceholder = "[email protected]"
25+
timePlaceholder = "2024-04-01T12:34:56.123456Z"
26+
)
27+
28+
func (s *MockService) ConfigureVisitor(url string, replacements mockgcpregistry.NormalizingVisitor) {
29+
replacements.ReplacePath(".creationRecord.mutateTime", timePlaceholder)
30+
replacements.ReplacePath(".notificationChannels[].creationRecord.mutateTime", timePlaceholder)
31+
32+
replacements.ReplacePath(".mutationRecord.mutateTime", timePlaceholder)
33+
replacements.ReplacePath(".notificationChannels[].mutationRecord.mutateTime", timePlaceholder)
34+
35+
replacements.ReplacePath(".mutationRecords[].mutateTime", timePlaceholder)
36+
replacements.ReplacePath(".notificationChannels[].mutationRecords[].mutateTime", timePlaceholder)
37+
38+
replacements.ReplacePath(".creationRecord.mutatedBy", emailPlaceholder)
39+
replacements.ReplacePath(".mutationRecord.mutatedBy", emailPlaceholder)
40+
replacements.ReplacePath(".mutationRecords[].mutatedBy", emailPlaceholder)
41+
42+
}
43+
44+
func (s *MockService) Previsit(event mockgcpregistry.Event, replacements mockgcpregistry.NormalizingVisitor) {
45+
}

mockgcp/mockmonitoring/notificationchannel.go

+32
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15+
// +tool:mockgcp-service
16+
// http.host: monitoring.googleapis.com
17+
// proto.service: google.monitoring.v3.NotificationChannelService
18+
1519
package mockmonitoring
1620

1721
import (
@@ -24,9 +28,11 @@ import (
2428
"google.golang.org/grpc/status"
2529
"google.golang.org/protobuf/proto"
2630
"google.golang.org/protobuf/types/known/timestamppb"
31+
"google.golang.org/protobuf/types/known/wrapperspb"
2732

2833
"github.com/GoogleCloudPlatform/k8s-config-connector/mockgcp/common/projects"
2934
pb "github.com/GoogleCloudPlatform/k8s-config-connector/mockgcp/generated/mockgcp/monitoring/v3"
35+
"github.com/GoogleCloudPlatform/k8s-config-connector/mockgcp/pkg/storage"
3036
"github.com/golang/protobuf/ptypes/empty"
3137
)
3238

@@ -51,6 +57,31 @@ func (s *NotificationChannelService) GetNotificationChannel(ctx context.Context,
5157
return obj, nil
5258
}
5359

60+
func (s *NotificationChannelService) ListNotificationChannels(ctx context.Context, req *pb.ListNotificationChannelsRequest) (*pb.ListNotificationChannelsResponse, error) {
61+
name, err := s.parseNotificationChannelName(req.GetName() + "/notificationChannels/" + "placeholder")
62+
if err != nil {
63+
return nil, err
64+
}
65+
66+
findPrefix := strings.TrimSuffix(name.String(), "placeholder")
67+
68+
var notificationChannels []*pb.NotificationChannel
69+
70+
findKind := (&pb.NotificationChannel{}).ProtoReflect().Descriptor()
71+
if err := s.storage.List(ctx, findKind, storage.ListOptions{Prefix: findPrefix}, func(obj proto.Message) error {
72+
notificationChannel := obj.(*pb.NotificationChannel)
73+
notificationChannels = append(notificationChannels, notificationChannel)
74+
return nil
75+
}); err != nil {
76+
return nil, err
77+
}
78+
79+
return &pb.ListNotificationChannelsResponse{
80+
NotificationChannels: notificationChannels,
81+
TotalSize: int32(len(notificationChannels)),
82+
}, nil
83+
}
84+
5485
func (s *NotificationChannelService) CreateNotificationChannel(ctx context.Context, req *pb.CreateNotificationChannelRequest) (*pb.NotificationChannel, error) {
5586
now := time.Now()
5687

@@ -72,6 +103,7 @@ func (s *NotificationChannelService) CreateNotificationChannel(ctx context.Conte
72103
MutateTime: timestamppb.New(now),
73104
})
74105
obj.Name = fqn
106+
obj.Enabled = wrapperspb.Bool(true)
75107

76108
if err := s.storage.Create(ctx, fqn, obj); err != nil {
77109
return nil, err

mockgcp/mockmonitoring/service.go

+6-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"github.com/GoogleCloudPlatform/k8s-config-connector/mockgcp/common"
2222
"github.com/GoogleCloudPlatform/k8s-config-connector/mockgcp/common/httpmux"
2323
"github.com/GoogleCloudPlatform/k8s-config-connector/mockgcp/common/operations"
24+
"github.com/GoogleCloudPlatform/k8s-config-connector/mockgcp/mockgcpregistry"
2425
"github.com/GoogleCloudPlatform/k8s-config-connector/mockgcp/pkg/storage"
2526
"google.golang.org/grpc"
2627

@@ -29,6 +30,10 @@ import (
2930
monitoringpb "github.com/GoogleCloudPlatform/k8s-config-connector/mockgcp/generated/mockgcp/monitoring/v3"
3031
)
3132

33+
func init() {
34+
mockgcpregistry.Register(New)
35+
}
36+
3237
// MockService represents a mocked apikeys service.
3338
type MockService struct {
3439
*common.MockEnvironment
@@ -37,7 +42,7 @@ type MockService struct {
3742
}
3843

3944
// New creates a MockService.
40-
func New(env *common.MockEnvironment, storage storage.Storage) *MockService {
45+
func New(env *common.MockEnvironment, storage storage.Storage) mockgcpregistry.MockService {
4146
s := &MockService{
4247
MockEnvironment: env,
4348
storage: storage,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,221 @@
1+
POST https://monitoring.googleapis.com/v3/projects/${projectId}/notificationChannels?alt=json
2+
Accept: application/json
3+
Authorization: (removed)
4+
Connection: keep-alive
5+
Content-Type: application/json
6+
7+
{
8+
"description": "Test Channel Description",
9+
"displayName": "Test Channel ${uniqueId}",
10+
"labels": {
11+
"email_address": "test-${uniqueId}@example.com"
12+
},
13+
"type": "email"
14+
}
15+
16+
200 OK
17+
Content-Type: application/json; charset=UTF-8
18+
Server: ESF
19+
Vary: Origin
20+
Vary: X-Origin
21+
Vary: Referer
22+
X-Content-Type-Options: nosniff
23+
X-Frame-Options: SAMEORIGIN
24+
X-Xss-Protection: 0
25+
26+
{
27+
"creationRecord": {
28+
"mutateTime": "2024-04-01T12:34:56.123456Z"
29+
},
30+
"description": "Test Channel Description",
31+
"displayName": "Test Channel ${uniqueId}",
32+
"enabled": true,
33+
"labels": {
34+
"email_address": "test-${uniqueId}@example.com"
35+
},
36+
"mutationRecords": [
37+
{
38+
"mutateTime": "2024-04-01T12:34:56.123456Z"
39+
}
40+
],
41+
"name": "projects/${projectId}/notificationChannels/${notificationChannelID}",
42+
"type": "email"
43+
}
44+
45+
---
46+
47+
GET https://monitoring.googleapis.com/v3/projects/${projectId}/notificationChannels?alt=json
48+
Accept: application/json
49+
Authorization: (removed)
50+
Connection: keep-alive
51+
52+
200 OK
53+
Content-Type: application/json; charset=UTF-8
54+
Server: ESF
55+
Vary: Origin
56+
Vary: X-Origin
57+
Vary: Referer
58+
X-Content-Type-Options: nosniff
59+
X-Frame-Options: SAMEORIGIN
60+
X-Xss-Protection: 0
61+
62+
{
63+
"notificationChannels": [
64+
{
65+
"creationRecord": {
66+
"mutateTime": "2024-04-01T12:34:56.123456Z"
67+
},
68+
"description": "Test Channel Description",
69+
"displayName": "Test Channel ${uniqueId}",
70+
"enabled": true,
71+
"labels": {
72+
"email_address": "test-${uniqueId}@example.com"
73+
},
74+
"mutationRecords": [
75+
{
76+
"mutateTime": "2024-04-01T12:34:56.123456Z"
77+
}
78+
],
79+
"name": "projects/${projectId}/notificationChannels/${notificationChannelID}",
80+
"type": "email"
81+
}
82+
],
83+
"totalSize": 1
84+
}
85+
86+
---
87+
88+
GET https://monitoring.googleapis.com/v3/projects/${projectId}/notificationChannels?alt=json&filter=labels.email_address%3D%22test-${uniqueId}%40example.com%22
89+
Accept: application/json
90+
Authorization: (removed)
91+
Connection: keep-alive
92+
93+
200 OK
94+
Content-Type: application/json; charset=UTF-8
95+
Server: ESF
96+
Vary: Origin
97+
Vary: X-Origin
98+
Vary: Referer
99+
X-Content-Type-Options: nosniff
100+
X-Frame-Options: SAMEORIGIN
101+
X-Xss-Protection: 0
102+
103+
{
104+
"notificationChannels": [
105+
{
106+
"creationRecord": {
107+
"mutateTime": "2024-04-01T12:34:56.123456Z"
108+
},
109+
"description": "Test Channel Description",
110+
"displayName": "Test Channel ${uniqueId}",
111+
"enabled": true,
112+
"labels": {
113+
"email_address": "test-${uniqueId}@example.com"
114+
},
115+
"mutationRecords": [
116+
{
117+
"mutateTime": "2024-04-01T12:34:56.123456Z"
118+
}
119+
],
120+
"name": "projects/${projectId}/notificationChannels/${notificationChannelID}",
121+
"type": "email"
122+
}
123+
],
124+
"totalSize": 1
125+
}
126+
127+
---
128+
129+
GET https://monitoring.googleapis.com/v3/projects/${projectId}/notificationChannels/${notificationChannelID}?alt=json
130+
Accept: application/json
131+
Authorization: (removed)
132+
Connection: keep-alive
133+
134+
200 OK
135+
Content-Type: application/json; charset=UTF-8
136+
Server: ESF
137+
Vary: Origin
138+
Vary: X-Origin
139+
Vary: Referer
140+
X-Content-Type-Options: nosniff
141+
X-Frame-Options: SAMEORIGIN
142+
X-Xss-Protection: 0
143+
144+
{
145+
"creationRecord": {
146+
"mutateTime": "2024-04-01T12:34:56.123456Z"
147+
},
148+
"description": "Test Channel Description",
149+
"displayName": "Test Channel ${uniqueId}",
150+
"enabled": true,
151+
"labels": {
152+
"email_address": "test-${uniqueId}@example.com"
153+
},
154+
"mutationRecords": [
155+
{
156+
"mutateTime": "2024-04-01T12:34:56.123456Z"
157+
}
158+
],
159+
"name": "projects/${projectId}/notificationChannels/${notificationChannelID}",
160+
"type": "email"
161+
}
162+
163+
---
164+
165+
GET https://monitoring.googleapis.com/v3/projects/${projectId}/notificationChannels?alt=json&filter=labels.email_address%3D%22test-${uniqueId}%40example.com%22
166+
Accept: application/json
167+
Authorization: (removed)
168+
Connection: keep-alive
169+
170+
200 OK
171+
Content-Type: application/json; charset=UTF-8
172+
Server: ESF
173+
Vary: Origin
174+
Vary: X-Origin
175+
Vary: Referer
176+
X-Content-Type-Options: nosniff
177+
X-Frame-Options: SAMEORIGIN
178+
X-Xss-Protection: 0
179+
180+
{
181+
"notificationChannels": [
182+
{
183+
"creationRecord": {
184+
"mutateTime": "2024-04-01T12:34:56.123456Z"
185+
},
186+
"description": "Test Channel Description",
187+
"displayName": "Test Channel ${uniqueId}",
188+
"enabled": true,
189+
"labels": {
190+
"email_address": "test-${uniqueId}@example.com"
191+
},
192+
"mutationRecords": [
193+
{
194+
"mutateTime": "2024-04-01T12:34:56.123456Z"
195+
}
196+
],
197+
"name": "projects/${projectId}/notificationChannels/${notificationChannelID}",
198+
"type": "email"
199+
}
200+
],
201+
"totalSize": 1
202+
}
203+
204+
---
205+
206+
DELETE https://monitoring.googleapis.com/v3/projects/${projectId}/notificationChannels/${notificationChannelID}?alt=json&force=False
207+
Accept: application/json
208+
Authorization: (removed)
209+
Connection: keep-alive
210+
211+
200 OK
212+
Content-Type: application/json; charset=UTF-8
213+
Server: ESF
214+
Vary: Origin
215+
Vary: X-Origin
216+
Vary: Referer
217+
X-Content-Type-Options: nosniff
218+
X-Frame-Options: SAMEORIGIN
219+
X-Xss-Protection: 0
220+
221+
{}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
- exec: gcloud beta monitoring channels create --type=email --channel-labels=email_address=test-${uniqueId}@example.com --display-name="Test Channel ${uniqueId}" --description="Test Channel Description" --project=${projectId}
2+
- exec: gcloud beta monitoring channels list --project=${projectId}
3+
- exec: gcloud beta monitoring channels describe $(gcloud beta monitoring channels list --filter='labels.email_address="test-${uniqueId}@example.com"' --format='value(name)') --project=${projectId}
4+
- exec: gcloud beta monitoring channels delete $(gcloud beta monitoring channels list --filter='labels.email_address="test-${uniqueId}@example.com"' --format='value(name)') --project=${projectId} --quiet

0 commit comments

Comments
 (0)