-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathpartner.go
199 lines (181 loc) · 6.57 KB
/
partner.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
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
package megaport
import (
"context"
"encoding/json"
"io"
"net/http"
"github.com/lithammer/fuzzysearch/fuzzy"
)
// PartnerService is an interface for interfacing with the Partner Port endpoints of the Megaport API.
type PartnerService interface {
// ListPartnerMegaports gets a list of all partner megaports in the Megaport Marketplace via the Megaport API.
ListPartnerMegaports(ctx context.Context) ([]*PartnerMegaport, error)
// FilterPartnerMegaportByProductName filters a list of partner megaports by product name in the Megaport API.
FilterPartnerMegaportByProductName(ctx context.Context, partners []*PartnerMegaport, productName string, exactMatch bool) ([]*PartnerMegaport, error)
// FilterPartnerMegaportByConnectType filters a list of partner megaports by connect type in the Megaport API.
FilterPartnerMegaportByConnectType(ctx context.Context, partners []*PartnerMegaport, connectType string, exactMatch bool) ([]*PartnerMegaport, error)
// FilterPartnerMegaportByCompanyName filters a list of partner megaports by company name in the Megaport API.
FilterPartnerMegaportByCompanyName(ctx context.Context, partners []*PartnerMegaport, companyName string, exactMatch bool) ([]*PartnerMegaport, error)
// FilterPartnerMegaportByLocationId filters a list of partner megaports by location ID in the Megaport API.
FilterPartnerMegaportByLocationId(ctx context.Context, partners []*PartnerMegaport, locationId int) ([]*PartnerMegaport, error)
// FilterPartnerMegaportByDiversityZone filters a list of partner megaports by diversity zone in the Megaport API.
FilterPartnerMegaportByDiversityZone(ctx context.Context, partners []*PartnerMegaport, diversityZone string, exactMatch bool) ([]*PartnerMegaport, error)
}
// NewPartnerService creates a new instance of the PartnerService.
func NewPartnerService(c *Client) *PartnerServiceOp {
return &PartnerServiceOp{
Client: c,
}
}
// PartnerServiceOp handles communication with Partner Port methods of the Megaport API.
type PartnerServiceOp struct {
Client *Client
}
// ListPartnerMegaports gets a list of all partner megaports in the Megaport Marketplace via the Megaport API.
func (svc *PartnerServiceOp) ListPartnerMegaports(ctx context.Context) ([]*PartnerMegaport, error) {
partnerMegaportUrl := "/v2/dropdowns/partner/megaports"
req, err := svc.Client.NewRequest(ctx, http.MethodGet, partnerMegaportUrl, nil)
if err != nil {
return nil, err
}
response, err := svc.Client.Do(ctx, req, nil)
if err != nil {
return nil, err
}
defer response.Body.Close()
body, fileErr := io.ReadAll(response.Body)
if fileErr != nil {
return nil, fileErr
}
partnerMegaportResponse := PartnerMegaportResponse{}
unmarshalErr := json.Unmarshal(body, &partnerMegaportResponse)
if unmarshalErr != nil {
return nil, unmarshalErr
}
return partnerMegaportResponse.Data, nil
}
// FilterPartnerMegaportByProductName filters a list of partner megaports by product name in the Megaport API.
func (svc *PartnerServiceOp) FilterPartnerMegaportByProductName(ctx context.Context, partners []*PartnerMegaport, productName string, exactMatch bool) ([]*PartnerMegaport, error) {
toReturn := []*PartnerMegaport{}
for _, partner := range partners {
match := false
if productName != "" {
if exactMatch { // Exact Match
if productName == partner.ProductName {
match = true
}
} else {
if fuzzy.Match(productName, partner.ProductName) {
match = true
}
}
} else {
match = true
}
if match && partner.VXCPermitted {
toReturn = append(toReturn, partner)
}
}
if len(toReturn) == 0 {
return nil, ErrNoPartnerPortsFound
}
return toReturn, nil
}
// FilterPartnerMegaportByConnectType filters a list of partner megaports by connect type in the Megaport API.
func (svc *PartnerServiceOp) FilterPartnerMegaportByConnectType(ctx context.Context, partners []*PartnerMegaport, connectType string, exactMatch bool) ([]*PartnerMegaport, error) {
toReturn := []*PartnerMegaport{}
for _, partner := range partners {
match := false
if connectType != "" {
if exactMatch { // Exact Match
if connectType == partner.ConnectType {
match = true
}
} else {
if fuzzy.Match(connectType, partner.ConnectType) {
match = true
}
}
} else {
match = true
}
if match && partner.VXCPermitted {
toReturn = append(toReturn, partner)
}
}
if len(toReturn) == 0 {
return nil, ErrNoPartnerPortsFound
}
return toReturn, nil
}
// FilterPartnerMegaportByCompanyName filters a list of partner megaports by company name in the Megaport API.
func (svc *PartnerServiceOp) FilterPartnerMegaportByCompanyName(ctx context.Context, partners []*PartnerMegaport, companyName string, exactMatch bool) ([]*PartnerMegaport, error) {
toReturn := []*PartnerMegaport{}
for _, partner := range partners {
match := false
if companyName != "" {
if exactMatch { // Exact Match
if companyName == partner.CompanyName {
match = true
}
} else {
if fuzzy.Match(companyName, partner.CompanyName) {
match = true
}
}
} else {
match = true
}
if match && partner.VXCPermitted {
toReturn = append(toReturn, partner)
}
}
if len(toReturn) == 0 {
return nil, ErrNoPartnerPortsFound
}
return toReturn, nil
}
// FilterPartnerMegaportByLocationId filters a list of partner megaports by location ID in the Megaport API.
func (svc *PartnerServiceOp) FilterPartnerMegaportByLocationId(ctx context.Context, partners []*PartnerMegaport, locationId int) ([]*PartnerMegaport, error) {
toReturn := []*PartnerMegaport{}
for _, partner := range partners {
if locationId >= 0 {
if locationId == partner.LocationId && partner.VXCPermitted {
toReturn = append(toReturn, partner)
}
} else {
toReturn = append(toReturn, partner)
}
}
if len(toReturn) == 0 {
return nil, ErrNoPartnerPortsFound
}
return toReturn, nil
}
// FilterPartnerMegaportByDiversityZone filters a list of partner megaports by diversity zone in the Megaport API.
func (svc *PartnerServiceOp) FilterPartnerMegaportByDiversityZone(ctx context.Context, partners []*PartnerMegaport, diversityZone string, exactMatch bool) ([]*PartnerMegaport, error) {
toReturn := []*PartnerMegaport{}
for _, partner := range partners {
match := false
if diversityZone != "" {
if exactMatch { // Exact Match
if diversityZone == partner.DiversityZone {
match = true
}
} else {
if fuzzy.Match(diversityZone, partner.DiversityZone) {
match = true
}
}
} else {
match = true
}
if match && partner.VXCPermitted {
toReturn = append(toReturn, partner)
}
}
if len(toReturn) == 0 {
return nil, ErrNoPartnerPortsFound
}
return toReturn, nil
}