forked from G-Core/gcorelabscloud-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
endpoint_search.go
75 lines (64 loc) · 2.24 KB
/
endpoint_search.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
package gcorecloud
import (
"strconv"
"strings"
)
// EndpointOpts specifies search criteria used by queries against an
// GCore service. The options must contain enough information to
// unambiguously identify one, and only one, endpoint within the catalog.
//
// Usually, these are passed to service client factory functions in a provider
// package, like "gcore.NewClusterTemplateV1()".
type EndpointOpts struct {
// Type [required] is the service type for the client (e.g., "cluster",
// "nodegroup", "clustertemplates"). Generally, this will be supplied by the service client
// function, but a user-given value will be honored if provided.
Type string
// Name [optional] is the service name for the client (e.g., "k8s") as it
// appears in the service catalog. Services can have the same Type but a
// different Name, which is why both Type and Name are sometimes needed.
Name string
// Region [required] is the geographic region in which the endpoint resides,
// generally specifying which datacenter should house your resources.
// Required only for services that span multiple regions.
Region int
// Project [required] is GCloud project
Project int
// version
Version string
}
/*
EndpointLocator is an internal function to be used by provider implementations.
It provides an implementation that locates a single endpoint from a service
catalog for a specific ProviderClient based on user-provided EndpointOpts. The
provider then uses it to discover related ServiceClients.
*/
type EndpointLocator func(EndpointOpts) (string, error)
// ApplyDefaults is an internal method to be used by provider implementations.
//
// It sets EndpointOpts fields if not already set, including a default type.
func (eo *EndpointOpts) ApplyDefaults(t string) {
if eo.Type == "" {
eo.Type = t
}
}
func intIntoPathPath(value int) string {
if value != 0 {
return strconv.Itoa(value)
}
return ""
}
// DefaultEndpointLocator - function to prepare API endpoint
func DefaultEndpointLocator(endpoint string) EndpointLocator {
return func(eo EndpointOpts) (string, error) {
params := []string{
StripLastSlashURL(endpoint),
eo.Version,
eo.Name,
intIntoPathPath(eo.Project),
intIntoPathPath(eo.Region),
eo.Type,
}
return strings.Join(params, "/"), nil
}
}