-
Notifications
You must be signed in to change notification settings - Fork 13
/
discovery.go
208 lines (173 loc) · 6.21 KB
/
discovery.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
200
201
202
203
204
205
206
207
208
package agent
import (
"github.com/Axway/agent-sdk/pkg/apic"
apiV1 "github.com/Axway/agent-sdk/pkg/apic/apiserver/models/api/v1"
"github.com/Axway/agent-sdk/pkg/apic/definitions"
"github.com/Axway/agent-sdk/pkg/util"
"github.com/Axway/agent-sdk/pkg/util/log"
)
// PublishAPIFunc definition for the PublishAPI func
type PublishAPIFunc func(serviceBody apic.ServiceBody) error
// getAPIByPrimaryKey - finds the api by the Primary Key from cache or API Server query
func getAPIByPrimaryKey(primaryKey string) *apiV1.ResourceInstance {
var api *apiV1.ResourceInstance
if agent.cacheManager != nil {
api = agent.cacheManager.GetAPIServiceWithPrimaryKey(primaryKey)
}
return api
}
// getAPIByID - finds the api by the ID from cache or API Server query
func getAPIByID(externalAPIID string) *apiV1.ResourceInstance {
var api *apiV1.ResourceInstance
if agent.cacheManager != nil {
api = agent.cacheManager.GetAPIServiceWithAPIID(externalAPIID)
}
return api
}
// getAPIByName - finds the api by the Name from cache or API Server query
func getAPIByName(apiName string) *apiV1.ResourceInstance {
var api *apiV1.ResourceInstance
if agent.cacheManager != nil {
api = agent.cacheManager.GetAPIServiceWithName(apiName)
}
return api
}
// IsAPIPublished - DEPRECATED Returns true if the API Service is already published
func IsAPIPublished(externalAPIID string) bool {
// DEPRECATED
log.DeprecationWarningReplace("IsAPIPublished", "IsAPIPublishedByID")
return IsAPIPublishedByID(externalAPIID)
}
// IsAPIPublishedByID - Returns true if the API Service is already published
func IsAPIPublishedByID(externalAPIID string) bool {
return getAPIByID(externalAPIID) != nil
}
// IsAPIPublishedByPrimaryKey - Returns true if the API Service is already published
func IsAPIPublishedByPrimaryKey(primaryKey string) bool {
return getAPIByPrimaryKey(primaryKey) != nil
}
// GetAttributeOnPublishedAPIByName - Returns the value on published proxy
func GetAttributeOnPublishedAPIByName(apiName string, attrName string) string {
api := getAPIByName(apiName)
return getAttributeFromResource(api, attrName)
}
// GetAttributeOnPublishedAPI - DEPRECATED Returns the value on published proxy
func GetAttributeOnPublishedAPI(externalAPIID string, attrName string) string {
// DEPRECATED
log.DeprecationWarningReplace("GetAttributeOnPublishedAPI", "GetAttributeOnPublishedAPIByID")
return GetAttributeOnPublishedAPIByID(externalAPIID, attrName)
}
func getAttributeFromResource(resource *apiV1.ResourceInstance, attrName string) string {
if resource == nil {
return ""
}
v, _ := util.GetAgentDetailsValue(resource, attrName)
return v
}
// GetAttributeOnPublishedAPIByID - Returns the value on published proxy
func GetAttributeOnPublishedAPIByID(externalAPIID string, attrName string) string {
api := getAPIByID(externalAPIID)
return getAttributeFromResource(api, attrName)
}
// GetAttributeOnPublishedAPIByPrimaryKey - Returns the value on published proxy
func GetAttributeOnPublishedAPIByPrimaryKey(primaryKey string, attrName string) string {
api := getAPIByPrimaryKey(primaryKey)
return getAttributeFromResource(api, attrName)
}
// GetOwnerOnPublishedAPIByName - Returns the owner spec of the published proxy
func GetOwnerOnPublishedAPIByName(apiName string) *apiV1.Owner {
api := getAPIByName(apiName)
if api == nil {
return nil
}
return api.Owner
}
// GetOwnerOnPublishedAPIByID - Returns the owner spec of the published proxy
func GetOwnerOnPublishedAPIByID(externalAPIID string) *apiV1.Owner {
api := getAPIByID(externalAPIID)
if api == nil {
return nil
}
return api.Owner
}
// GetOwnerOnPublishedAPIByPrimaryKey - Returns the owner spec of the published proxy
func GetOwnerOnPublishedAPIByPrimaryKey(primaryKey string) *apiV1.Owner {
api := getAPIByPrimaryKey(primaryKey)
if api == nil {
return nil
}
return api.Owner
}
func PublishingLock() {
agent.publishingLock.Lock()
}
func PublishingUnlock() {
agent.publishingLock.Unlock()
}
// PublishAPI - Publishes the API
func PublishAPI(serviceBody apic.ServiceBody) error {
if agent.apicClient != nil {
if agent.agentFeaturesCfg.MarketplaceProvisioningEnabled() {
var err error
_, err = publishAccessRequestDefinition(&serviceBody)
if err != nil {
return err
}
}
_, err := agent.apicClient.PublishService(&serviceBody)
if err != nil {
return err
}
log.Infof("Published API %v-%v in environment %v", serviceBody.APIName, serviceBody.Version, agent.cfg.GetEnvironmentName())
}
return nil
}
// RemovePublishedAPIAgentDetail -
func RemovePublishedAPIAgentDetail(externalAPIID, detailKey string) error {
apiSvc := agent.cacheManager.GetAPIServiceWithAPIID(externalAPIID)
details := util.GetAgentDetails(apiSvc)
if _, ok := details[detailKey]; !ok {
return nil
}
delete(details, detailKey)
util.SetAgentDetails(apiSvc, details)
err := agent.apicClient.CreateSubResource(apiSvc.ResourceMeta, map[string]interface{}{definitions.XAgentDetails: details})
if err != nil {
return err
}
err = agent.cacheManager.AddAPIService(apiSvc)
return err
}
func publishAccessRequestDefinition(serviceBody *apic.ServiceBody) (*apiV1.ResourceInstance, error) {
if serviceBody.GetAccessRequestDefinition() != nil {
newARD, err := createOrUpdateAccessRequestDefinition(serviceBody.GetAccessRequestDefinition())
if err == nil && newARD != nil {
serviceBody.SetAccessRequestDefinitionName(newARD.Name, true)
ard, err := newARD.AsInstance()
if err == nil {
agent.cacheManager.AddAccessRequestDefinition(ard)
}
return ard, err
}
return nil, err
}
return nil, nil
}
func getAPIValidator() APIValidator {
agent.apiValidatorLock.Lock()
defer agent.apiValidatorLock.Unlock()
return agent.apiValidator
}
func setAPIValidator(apiValidator APIValidator) {
agent.apiValidatorLock.Lock()
defer agent.apiValidatorLock.Unlock()
agent.apiValidator = apiValidator
}
// RegisterAPIValidator - Registers callback for validating the API on gateway
func RegisterAPIValidator(apiValidator APIValidator) {
setAPIValidator(apiValidator)
}
// RegisterDeleteServiceValidator - DEPRECATED Registers callback for validating if the service should be deleted
func RegisterDeleteServiceValidator(validator interface{}) {
log.Warnf("the RegisterDeleteServiceValidator is no longer used, please remove the call to it")
}