/
openapi_stub_backend_configuration.go
83 lines (72 loc) · 1.96 KB
/
openapi_stub_backend_configuration.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
package openapi
import (
"fmt"
"github.com/dikhan/terraform-provider-openapi/v3/openapi/openapiutils"
)
type specStubBackendConfiguration struct {
host string
basePath string
httpScheme string
regions []string
err error
hostErr error
defaultRegionErr error
hostByRegionErr error
getHTTPSchemeBehavior func() (string, error)
}
func newStubBackendConfiguration(host, basePath string, httpScheme string) *specStubBackendConfiguration {
return &specStubBackendConfiguration{
host: host,
basePath: basePath,
httpScheme: httpScheme,
}
}
func newStubBackendMultiRegionConfiguration(host string, regions []string) *specStubBackendConfiguration {
isMultiRegion, _ := openapiutils.IsMultiRegionHost(host)
if !isMultiRegion {
return nil
}
return &specStubBackendConfiguration{
host: host,
regions: regions,
}
}
func (s *specStubBackendConfiguration) getHost() (string, error) {
if s.hostErr != nil {
return "", s.hostErr
}
return s.host, nil
}
func (s *specStubBackendConfiguration) getBasePath() string {
return s.basePath
}
func (s *specStubBackendConfiguration) getHTTPScheme() (string, error) {
if s.getHTTPSchemeBehavior != nil {
return s.getHTTPSchemeBehavior()
}
return s.httpScheme, nil
}
func (s *specStubBackendConfiguration) getHostByRegion(region string) (string, error) {
if s.hostByRegionErr != nil {
return "", s.hostByRegionErr
}
return fmt.Sprintf(s.host, region), nil
}
func (s *specStubBackendConfiguration) GetDefaultRegion(regions []string) (string, error) {
if s.defaultRegionErr != nil {
return "", s.defaultRegionErr
}
if len(regions) == 0 {
return "", fmt.Errorf("empty regions provided")
}
return s.regions[0], nil
}
func (s *specStubBackendConfiguration) IsMultiRegion() (bool, string, []string, error) {
if s.err != nil {
return false, "", nil, s.err
}
if len(s.regions) > 0 {
return true, s.host, s.regions, nil
}
return false, "", nil, nil
}