@@ -54,7 +54,8 @@ const (
54
54
versionV1 Version = "v1"
55
55
versionBeta Version = "beta"
56
56
versionAlpha Version = "alpha"
57
- environmentStaging Environment = "staging"
57
+ EnvironmentStaging Environment = "staging"
58
+ EnvironmentProduction Environment = "production"
58
59
)
59
60
60
61
type CloudProvider struct {
@@ -79,7 +80,7 @@ type ConfigGlobal struct {
79
80
Zone string `gcfg:"zone"`
80
81
}
81
82
82
- func CreateCloudProvider (ctx context.Context , vendorVersion string , configPath string , computeEndpoint string , computeEnvironment Environment ) (* CloudProvider , error ) {
83
+ func CreateCloudProvider (ctx context.Context , vendorVersion string , configPath string , computeEndpoint url. URL , computeEnvironment Environment ) (* CloudProvider , error ) {
83
84
configFile , err := readConfig (configPath )
84
85
if err != nil {
85
86
return nil , err
@@ -173,7 +174,7 @@ func readConfig(configPath string) (*ConfigFile, error) {
173
174
return cfg , nil
174
175
}
175
176
176
- func createAlphaCloudService (ctx context.Context , vendorVersion string , tokenSource oauth2.TokenSource , computeEndpoint string , computeEnvironment Environment ) (* computealpha.Service , error ) {
177
+ func createAlphaCloudService (ctx context.Context , vendorVersion string , tokenSource oauth2.TokenSource , computeEndpoint url. URL , computeEnvironment Environment ) (* computealpha.Service , error ) {
177
178
computeOpts , err := getComputeVersion (ctx , tokenSource , computeEndpoint , computeEnvironment , versionAlpha )
178
179
if err != nil {
179
180
klog .Errorf ("Failed to get compute endpoint: %s" , err )
@@ -186,7 +187,7 @@ func createAlphaCloudService(ctx context.Context, vendorVersion string, tokenSou
186
187
return service , nil
187
188
}
188
189
189
- func createBetaCloudService (ctx context.Context , vendorVersion string , tokenSource oauth2.TokenSource , computeEndpoint string , computeEnvironment Environment ) (* computebeta.Service , error ) {
190
+ func createBetaCloudService (ctx context.Context , vendorVersion string , tokenSource oauth2.TokenSource , computeEndpoint url. URL , computeEnvironment Environment ) (* computebeta.Service , error ) {
190
191
computeOpts , err := getComputeVersion (ctx , tokenSource , computeEndpoint , computeEnvironment , versionBeta )
191
192
if err != nil {
192
193
klog .Errorf ("Failed to get compute endpoint: %s" , err )
@@ -199,7 +200,7 @@ func createBetaCloudService(ctx context.Context, vendorVersion string, tokenSour
199
200
return service , nil
200
201
}
201
202
202
- func createCloudService (ctx context.Context , vendorVersion string , tokenSource oauth2.TokenSource , computeEndpoint string , computeEnvironment Environment ) (* compute.Service , error ) {
203
+ func createCloudService (ctx context.Context , vendorVersion string , tokenSource oauth2.TokenSource , computeEndpoint url. URL , computeEnvironment Environment ) (* compute.Service , error ) {
203
204
computeOpts , err := getComputeVersion (ctx , tokenSource , computeEndpoint , computeEnvironment , versionV1 )
204
205
if err != nil {
205
206
klog .Errorf ("Failed to get compute endpoint: %s" , err )
@@ -212,32 +213,25 @@ func createCloudService(ctx context.Context, vendorVersion string, tokenSource o
212
213
return service , nil
213
214
}
214
215
215
- func getComputeVersion (ctx context.Context , tokenSource oauth2.TokenSource , computeEndpoint string , computeEnvironment Environment , computeVersion Version ) ([]option.ClientOption , error ) {
216
+ func getComputeVersion (ctx context.Context , tokenSource oauth2.TokenSource , computeEndpoint url. URL , computeEnvironment Environment , computeVersion Version ) ([]option.ClientOption , error ) {
216
217
client , err := newOauthClient (ctx , tokenSource )
217
218
if err != nil {
218
219
return nil , err
219
220
}
220
- computeEnvironmentSuffix := getPath (computeEnvironment , computeVersion )
221
221
computeOpts := []option.ClientOption {option .WithHTTPClient (client )}
222
222
223
- if computeEndpoint != "" {
224
- computeURL , err := url .ParseRequestURI (computeEndpoint )
225
- if err != nil {
226
- return nil , err
227
- }
228
- endpoint := computeURL .JoinPath (computeEnvironmentSuffix ).String ()
229
- _ , err = url .ParseRequestURI (endpoint )
230
- if err != nil {
231
- klog .Fatalf ("Error parsing compute endpoint %s" , endpoint )
232
- }
223
+ if computeEndpoint .String () != "" {
224
+ computeEnvironmentSuffix := constructComputeEndpointPath (computeEnvironment , computeVersion )
225
+ computeEndpoint .Path = computeEnvironmentSuffix
226
+ endpoint := computeEndpoint .String ()
233
227
computeOpts = append (computeOpts , option .WithEndpoint (endpoint ))
234
228
}
235
229
return computeOpts , nil
236
230
}
237
231
238
- func getPath (env Environment , version Version ) string {
232
+ func constructComputeEndpointPath (env Environment , version Version ) string {
239
233
prefix := ""
240
- if env == environmentStaging {
234
+ if env == EnvironmentStaging {
241
235
prefix = fmt .Sprintf ("%s_" , env )
242
236
}
243
237
return fmt .Sprintf ("compute/%s%s/" , prefix , version )
0 commit comments