/
openapi_v2_backend_configuration.go
142 lines (125 loc) · 4.38 KB
/
openapi_v2_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
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
package openapi
import (
"errors"
"fmt"
"log"
"strings"
"github.com/dikhan/terraform-provider-openapi/v3/openapi/openapiutils"
"github.com/go-openapi/spec"
)
const extTfProviderMultiRegionFQDN = "x-terraform-provider-multiregion-fqdn"
const extTfProviderRegions = "x-terraform-provider-regions"
type specV2BackendConfiguration struct {
openAPIDocumentURL string
spec *spec.Swagger
}
func newOpenAPIBackendConfigurationV2(spec *spec.Swagger, openAPIDocumentURL string) (*specV2BackendConfiguration, error) {
if spec.Swagger != "2.0" {
return nil, fmt.Errorf("swagger version '%s' not supported, specV2BackendConfiguration only supports 2.0", spec.Swagger)
}
if openAPIDocumentURL == "" {
return nil, fmt.Errorf("missing mandatory parameter openAPIDocumentURL")
}
return &specV2BackendConfiguration{openAPIDocumentURL, spec}, nil
}
func (o specV2BackendConfiguration) getHost() (string, error) {
if o.spec.Host == "" {
log.Printf("[WARN] host field not specified in the swagger configuration, falling back to retrieving the host from where the OpenAPI document is served: '%s'", o.openAPIDocumentURL)
hostFromURL := openapiutils.GetHostFromURL(o.openAPIDocumentURL)
if hostFromURL == "" {
return "", fmt.Errorf("could not find valid host from URL provided: '%s'", o.openAPIDocumentURL)
}
return hostFromURL, nil
}
return o.spec.Host, nil
}
func (o specV2BackendConfiguration) getHostByRegion(region string) (string, error) {
if region == "" {
return "", fmt.Errorf("can't get host by region, missing region value")
}
isMultiRegion, host, allowedRegions, err := o.IsMultiRegion()
if err != nil {
return "", err
}
if !isMultiRegion {
return "", fmt.Errorf("missing '%s' extension or value provided not matching multiregion host format", extTfProviderMultiRegionFQDN)
}
if err := o.validateRegion(region, allowedRegions); err != nil {
return "", err
}
overrideHost, err := openapiutils.GetMultiRegionHost(host, region)
if err != nil {
return "", err
}
return overrideHost, nil
}
func (o specV2BackendConfiguration) validateRegion(region string, allowedRegions []string) error {
for _, r := range allowedRegions {
if r == region {
return nil
}
}
return fmt.Errorf("region %s not matching allowed ones %+v", region, allowedRegions)
}
func (o specV2BackendConfiguration) GetDefaultRegion(regions []string) (string, error) {
if regions == nil || len(regions) == 0 {
return "", fmt.Errorf("empty regions provided")
}
return regions[0], nil
}
func (o specV2BackendConfiguration) IsMultiRegion() (bool, string, []string, error) {
isHostMultiRegion, host, err := o.isHostMultiRegion()
if err != nil {
return false, "", nil, err
}
if isHostMultiRegion {
regions, err := o.getProviderRegions()
if err != nil {
return false, "", nil, err
}
return true, host, regions, nil
}
return false, "", nil, nil
}
func (o specV2BackendConfiguration) isHostMultiRegion() (bool, string, error) {
if host, exists := o.spec.Extensions.GetString(extTfProviderMultiRegionFQDN); exists {
isMultiRegion, _ := openapiutils.IsMultiRegionHost(host)
if !isMultiRegion {
return false, "", fmt.Errorf("'%s' extension value provided not matching multiregion host format", extTfProviderMultiRegionFQDN)
}
return true, host, nil
}
return false, "", nil
}
func (o specV2BackendConfiguration) getProviderRegions() ([]string, error) {
regionsExtensionValue, regionsExtensionExists := o.spec.Extensions.GetString(extTfProviderRegions)
if !regionsExtensionExists {
return nil, fmt.Errorf("mandatory multiregion '%s' extension missing", extTfProviderRegions)
}
if regionsExtensionValue == "" {
return nil, fmt.Errorf("mandatory multiregion '%s' extension empty value provided", extTfProviderRegions)
}
regions := strings.Split(strings.Replace(regionsExtensionValue, " ", "", -1), ",")
return regions, nil
}
func (o specV2BackendConfiguration) getBasePath() string {
return o.spec.BasePath
}
func (o specV2BackendConfiguration) getHTTPScheme() (string, error) {
var defaultScheme string
if len(o.spec.Schemes) == 0 {
return "", errors.New("no schemes specified - must use http or https")
}
for _, s := range o.spec.Schemes {
if s == "https" {
return s, nil
}
if s == "http" {
defaultScheme = s
}
}
if defaultScheme == "" {
return "", fmt.Errorf("specified schemes %s are not supported - must use http or https", o.spec.Schemes)
}
return defaultScheme, nil
}