/
publish.go
96 lines (87 loc) · 2.64 KB
/
publish.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
package discovery
import (
coreAgent "github.com/Axway/agent-sdk/pkg/agent"
"github.com/Axway/agent-sdk/pkg/apic"
"github.com/Axway/agent-sdk/pkg/util"
"github.com/Axway/agents-mulesoft/pkg/config"
"github.com/sirupsen/logrus"
)
// publisher implements the Repeater interface. Waits for for items on a channel and publishes them to central
type publisher struct {
apiChan chan *ServiceDetail
stopPublish chan bool
publishAPI coreAgent.PublishAPIFunc
}
func (p *publisher) Stop() {
p.stopPublish <- true
}
func (p *publisher) OnConfigChange(_ *config.MulesoftConfig) {
// noop
}
// Loop publishes apis to central.
func (p *publisher) Loop() {
for {
select {
case serviceDetail := <-p.apiChan:
p.publish(serviceDetail)
case <-p.stopPublish:
logrus.Debug("stopping publish listener")
return
}
}
}
// publish Publishes the API to Amplify Central.
func (p *publisher) publish(serviceDetail *ServiceDetail) {
log := logrus.WithFields(logrus.Fields{
"name": serviceDetail.APIName,
"id": serviceDetail.ID,
"stage": serviceDetail.Stage,
"version": serviceDetail.Version,
})
log.Infof("Publishing to Amplify Central")
serviceBody, err := BuildServiceBody(serviceDetail)
if err != nil {
log.WithError(err).Error("error building service body")
return
}
err = p.publishAPI(serviceBody)
if err != nil {
log.WithError(err).Error("error publishing to Amplify Central")
return
}
log.Infof("Published API to Amplify Central")
}
// BuildServiceBody - creates the service definition
func BuildServiceBody(service *ServiceDetail) (apic.ServiceBody, error) {
tags := map[string]interface{}{}
if service.Tags != nil {
for _, tag := range service.Tags {
tags[tag] = true
}
}
return apic.NewServiceBodyBuilder().
SetAPIName(service.APIName).
SetAPISpec(service.APISpec).
SetAPIUpdateSeverity(service.APIUpdateSeverity).
SetAuthPolicy(service.AuthPolicy).
SetDescription(service.Description).
SetDocumentation(service.Documentation).
SetID(service.ID).
SetImage(service.Image).
SetImageContentType(service.ImageContentType).
SetResourceType(service.ResourceType).
SetServiceAgentDetails(util.MapStringStringToMapStringInterface(service.AgentDetails)).
SetServiceAttribute(service.ServiceAttributes).
SetStage(service.Stage).
SetState(service.State).
SetStatus(service.Status).
SetSubscriptionName(service.SubscriptionName).
SetTags(tags).
SetTitle(service.Title).
SetURL(service.URL).
SetVersion(service.Version).
SetAccessRequestDefinitionName(service.AccessRequestDefinition, false).
SetCredentialRequestDefinitions(service.CRDs).
SetSourceDataplaneType(apic.Mulesoft, false).
Build()
}