-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Expand file tree
/
Copy pathservices.go
More file actions
208 lines (173 loc) · 7.67 KB
/
Copy pathservices.go
File metadata and controls
208 lines (173 loc) · 7.67 KB
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
package gcp
import (
"context"
"fmt"
"cloud.google.com/go/storage"
"google.golang.org/api/cloudresourcemanager/v3"
"google.golang.org/api/compute/v1"
"google.golang.org/api/dns/v1"
"google.golang.org/api/file/v1"
"google.golang.org/api/iam/v1"
"google.golang.org/api/option"
serviceusage "google.golang.org/api/serviceusage/v1beta1"
)
// ServiceNameGCP is the name of the GCP Service.
type ServiceNameGCP string
const (
// ServiceNameGCPCompute is the name used for the GCP Compute Service endpoint.
ServiceNameGCPCompute ServiceNameGCP = "compute"
// ServiceNameGCPContainer is the name used for the GCP Container Service endpoint.
ServiceNameGCPContainer ServiceNameGCP = "container"
// ServiceNameGCPCloudResource is the name used for the GCP Resource Manager Service endpoint.
ServiceNameGCPCloudResource ServiceNameGCP = "cloudresourcemanager"
// ServiceNameGCPDNS is the name used for the GCP DNS Service endpoint.
ServiceNameGCPDNS ServiceNameGCP = "dns"
// ServiceNameGCPFile is the name used for the GCP File Service endpoint.
ServiceNameGCPFile ServiceNameGCP = "file"
// ServiceNameGCPIAM is the name used for the GCP IAM Service endpoint.
ServiceNameGCPIAM ServiceNameGCP = "iam"
// ServiceNameGCPServiceUsage is the name used for the GCP Service Usage Service endpoint.
ServiceNameGCPServiceUsage ServiceNameGCP = "serviceusage"
// ServiceNameGCPStorage is the name used for the GCP Storage Service endpoint.
ServiceNameGCPStorage ServiceNameGCP = "storage"
)
// CreateServiceEndpoint creates a string endpoint for a service from the endpoint name.
func CreateServiceEndpoint(endpointName string, service ServiceNameGCP) string {
baseEndpoint := fmt.Sprintf("https://%s-%s.p.googleapis.com/", string(service), endpointName)
switch service {
case ServiceNameGCPCompute, ServiceNameGCPContainer, ServiceNameGCPStorage:
baseEndpoint = fmt.Sprintf("%s%s/v1/", baseEndpoint, string(service))
}
return baseEndpoint
}
// CreateEndpointOption creates an Endpoint Option for a service, overriding the base/default endpoint.
func CreateEndpointOption(endpointName string, service ServiceNameGCP) option.ClientOption {
endpoint := CreateServiceEndpoint(endpointName, service)
return option.WithEndpoint(endpoint)
}
// CredentialOptions returns the client options for authenticating with GCP,
// including universe domain support for Google Cloud Dedicated.
// When credential JSON is available, it is passed via WithCredentialsJSON so
// the client library can use self-signed JWTs for non-default universe
// domains (where the OAuth2 token endpoint is unavailable). Falls back to
// WithCredentials for metadata-based credentials that have no JSON.
func CredentialOptions(ssn *Session) ([]option.ClientOption, error) {
ud, err := ssn.Credentials.GetUniverseDomain()
if err != nil {
return nil, fmt.Errorf("failed to get universe domain: %w", err)
}
var opts []option.ClientOption
if len(ssn.Credentials.JSON) > 0 {
opts = append(opts, option.WithCredentialsJSON(ssn.Credentials.JSON)) //nolint:staticcheck // SA1019: will be replaced in https://github.com/openshift/installer/pull/10694
} else {
opts = append(opts, option.WithCredentials(ssn.Credentials))
}
opts = append(opts, option.WithUniverseDomain(ud))
return opts, nil
}
// getOptions creates the options for use during service creation.
func getOptions(ctx context.Context) ([]option.ClientOption, error) {
ssn, err := GetSession(ctx)
if err != nil {
return nil, fmt.Errorf("failed to get session: %w", err)
}
return CredentialOptions(ssn)
}
// GetComputeService creates the compute service. The service is created with credentials and any service
// endpoint overrides entered by the user in the installconfig.
func GetComputeService(ctx context.Context, options ...option.ClientOption) (*compute.Service, error) {
genOptions, err := getOptions(ctx)
if err != nil {
return nil, fmt.Errorf("failed to get compute service options: %w", err)
}
options = append(options, genOptions...)
svc, err := compute.NewService(ctx, options...)
if err != nil {
return nil, fmt.Errorf("failed to create compute service: %w", err)
}
return svc, nil
}
// GetDNSService creates the dns service. The service is created with credentials and any service
// endpoint overrides entered by the user in the installconfig.
func GetDNSService(ctx context.Context, options ...option.ClientOption) (*dns.Service, error) {
genOptions, err := getOptions(ctx)
if err != nil {
return nil, fmt.Errorf("failed to get dns service options: %w", err)
}
options = append(options, genOptions...)
svc, err := dns.NewService(ctx, options...)
if err != nil {
return nil, fmt.Errorf("failed to create dns service: %w", err)
}
return svc, nil
}
// GetCloudResourceService creates the cloud resource service. The service is created with credentials and any service
// endpoint overrides entered by the user in the installconfig.
func GetCloudResourceService(ctx context.Context, options ...option.ClientOption) (*cloudresourcemanager.Service, error) {
genOptions, err := getOptions(ctx)
if err != nil {
return nil, fmt.Errorf("failed to get cloud resource service options: %w", err)
}
options = append(options, genOptions...)
svc, err := cloudresourcemanager.NewService(ctx, options...)
if err != nil {
return nil, fmt.Errorf("failed to create cloud resource service: %w", err)
}
return svc, nil
}
// GetServiceUsageService creates the service usage service. The service is created with credentials and any service
// endpoint overrides entered by the user in the installconfig.
func GetServiceUsageService(ctx context.Context, options ...option.ClientOption) (*serviceusage.APIService, error) {
genOptions, err := getOptions(ctx)
if err != nil {
return nil, fmt.Errorf("failed to get service usage service options: %w", err)
}
options = append(options, genOptions...)
svc, err := serviceusage.NewService(ctx, options...)
if err != nil {
return nil, fmt.Errorf("failed to create service usage service: %w", err)
}
return svc, nil
}
// GetIAMService creates the iam service. The service is created with credentials and any service
// endpoint overrides entered by the user in the installconfig.
func GetIAMService(ctx context.Context, options ...option.ClientOption) (*iam.Service, error) {
genOptions, err := getOptions(ctx)
if err != nil {
return nil, fmt.Errorf("failed to get IAM service options: %w", err)
}
options = append(options, genOptions...)
svc, err := iam.NewService(ctx, options...)
if err != nil {
return nil, fmt.Errorf("failed to create IAM service: %w", err)
}
return svc, nil
}
// GetStorageService creates the storage service. The service is created with credentials and any service
// endpoint overrides entered by the user in the installconfig.
func GetStorageService(ctx context.Context, options ...option.ClientOption) (*storage.Client, error) {
genOptions, err := getOptions(ctx)
if err != nil {
return nil, fmt.Errorf("failed to get storage service options: %w", err)
}
options = append(options, genOptions...)
svc, err := storage.NewClient(ctx, options...)
if err != nil {
return nil, fmt.Errorf("failed to create storage service: %w", err)
}
return svc, nil
}
// GetFileService creates the file service. The service is created with credentials and any service
// endpoint overrides entered by the user in the installconfig.
func GetFileService(ctx context.Context, options ...option.ClientOption) (*file.Service, error) {
genOptions, err := getOptions(ctx)
if err != nil {
return nil, fmt.Errorf("failed to get file service options: %w", err)
}
options = append(options, genOptions...)
svc, err := file.NewService(ctx, options...)
if err != nil {
return nil, fmt.Errorf("failed to create file service: %w", err)
}
return svc, nil
}