github.com/google/cloudprober@v0.11.3/prober/serviceimpl.go (about) 1 // Copyright 2019 The Cloudprober Authors. 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 prober 16 17 import ( 18 "context" 19 20 pb "github.com/google/cloudprober/prober/proto" 21 "google.golang.org/grpc/codes" 22 "google.golang.org/grpc/status" 23 "google.golang.org/protobuf/proto" 24 ) 25 26 // AddProbe adds the given probe to cloudprober. 27 func (pr *Prober) AddProbe(ctx context.Context, req *pb.AddProbeRequest) (*pb.AddProbeResponse, error) { 28 p := req.GetProbeConfig() 29 30 if p == nil { 31 return &pb.AddProbeResponse{}, status.Errorf(codes.InvalidArgument, "probe config cannot be nil") 32 } 33 34 if err := pr.addProbe(p); err != nil { 35 return &pb.AddProbeResponse{}, err 36 } 37 38 // Send probe to the start probe channel to be started by a goroutine started 39 // at the prober start time. 40 pr.grpcStartProbeCh <- p.GetName() 41 42 return &pb.AddProbeResponse{}, nil 43 } 44 45 // RemoveProbe gRPC method cancels the given probe and removes its from the 46 // prober's internal database. 47 func (pr *Prober) RemoveProbe(ctx context.Context, req *pb.RemoveProbeRequest) (*pb.RemoveProbeResponse, error) { 48 pr.mu.Lock() 49 defer pr.mu.Unlock() 50 51 name := req.GetProbeName() 52 53 if name == "" { 54 return &pb.RemoveProbeResponse{}, status.Errorf(codes.InvalidArgument, "probe name cannot be empty") 55 } 56 57 if pr.Probes[name] == nil { 58 return &pb.RemoveProbeResponse{}, status.Errorf(codes.NotFound, "probe %s not found", name) 59 } 60 61 pr.probeCancelFunc[name]() 62 delete(pr.Probes, name) 63 64 return &pb.RemoveProbeResponse{}, nil 65 } 66 67 // ListProbes gRPC method returns the list of probes from the in-memory database. 68 func (pr *Prober) ListProbes(ctx context.Context, req *pb.ListProbesRequest) (*pb.ListProbesResponse, error) { 69 pr.mu.Lock() 70 defer pr.mu.Unlock() 71 72 resp := &pb.ListProbesResponse{} 73 74 for name, p := range pr.Probes { 75 resp.Probe = append(resp.Probe, &pb.Probe{ 76 Name: proto.String(name), 77 Config: p.ProbeDef, 78 }) 79 } 80 81 return resp, nil 82 }