forked from yext/cloudprober
/
gcp.go
124 lines (112 loc) · 3.85 KB
/
gcp.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
// Copyright 2017-2018 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
/*
Package gcp implements a GCP (Google Compute Platform) resources provider for
ResourceDiscovery server.
It currently supports following GCP resources:
GCE Instances (gce_instance)
GCP provider is configured through a protobuf based config file
(proto/config.proto). Example config:
{
project_id: 'test-project-1'
project_id: 'test-project-2'
gce_instances {}
}
*/
package gcp
import (
"errors"
"fmt"
"strings"
"cloud.google.com/go/compute/metadata"
"github.com/google/cloudprober/logger"
pb "github.com/google/cloudprober/targets/rds/proto"
configpb "github.com/google/cloudprober/targets/rds/server/gcp/proto"
)
// Provider implements a GCP provider for a ResourceDiscovery server.
type Provider struct {
gceInstances map[string]*gceInstancesLister
rtcVariables map[string]*rtcVariablesLister
}
// ListResources returns the list of resources from the cache.
func (p *Provider) ListResources(req *pb.ListResourcesRequest) (*pb.ListResourcesResponse, error) {
tok := strings.SplitN(req.GetResourcePath(), "/", 2)
if len(tok) != 2 {
return nil, fmt.Errorf("%s is not a valid GCP resource path", req.GetResourcePath())
}
resType := tok[0]
project := tok[1]
switch resType {
case "gce_instances":
gil := p.gceInstances[project]
if gil == nil {
return nil, fmt.Errorf("gcp: GCE instances lister for the project %s not found", project)
}
resources, err := gil.listResources(req.GetFilter(), req.GetIpConfig())
return &pb.ListResourcesResponse{Resources: resources}, err
case "rtc_variables":
rvl := p.rtcVariables[project]
if rvl == nil {
return nil, fmt.Errorf("gcp: RTC variables lister for the project %s not found", project)
}
resources, err := rvl.listResources(req.GetFilter())
return &pb.ListResourcesResponse{Resources: resources}, err
default:
return nil, fmt.Errorf("gcp: unsupported resource type: %s", resType)
}
}
// New creates a GCP provider for RDS server, based on the provided config.
func New(c *configpb.ProviderConfig, l *logger.Logger) (*Provider, error) {
projects := c.GetProject()
if len(projects) == 0 {
if !metadata.OnGCE() {
return nil, errors.New("rds.gcp.New(): project is required for GCP resources when not running on GCE")
}
proj, err := metadata.ProjectID()
if err != nil {
return nil, fmt.Errorf("rds.gcp.New(): error getting local project ID: %v", err)
}
projects = append(projects, proj)
}
p := &Provider{
gceInstances: make(map[string]*gceInstancesLister),
rtcVariables: make(map[string]*rtcVariablesLister),
}
// Enable GCE instances lister if configured.
if c.GetGceInstances() != nil {
for _, project := range projects {
gil, err := newGCEInstancesLister(project, c.GetApiVersion(), c.GetGceInstances(), l)
if err != nil {
return nil, err
}
p.gceInstances[project] = gil
}
}
// Enable regional forwarding lister if configured.
// TODO(manugarg): implement this.
if c.GetRegionalForwardingRules() != nil {
return nil, errors.New("regional forwarding rules are not supported yet")
}
// Enable RTC variables lister if configured.
if c.GetRtcVariables() != nil {
for _, project := range projects {
rvl, err := newRTCVariablesLister(project, c.GetApiVersion(), c.GetRtcVariables(), l)
if err != nil {
return nil, err
}
p.rtcVariables[project] = rvl
}
}
return p, nil
}