Skip to content

Commit

Permalink
APIGOV-28186 add async spec builder (#810)
Browse files Browse the repository at this point in the history
  • Loading branch information
alrosca authored Jul 18, 2024
1 parent 2bc329e commit 6c29895
Show file tree
Hide file tree
Showing 6 changed files with 829 additions and 0 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ require (
github.com/spf13/viper v1.12.0
github.com/stretchr/testify v1.9.0
github.com/subosito/gotenv v1.4.0
github.com/swaggest/go-asyncapi v0.8.0
github.com/tidwall/gjson v1.14.0
github.com/tomnomnom/linkheader v0.0.0-20180905144013-02ca5825eb80
golang.org/x/net v0.24.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,8 @@ github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsT
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/subosito/gotenv v1.4.0 h1:yAzM1+SmVcz5R4tXGsNMu1jUl2aOJXoiWUCEwwnGrvs=
github.com/subosito/gotenv v1.4.0/go.mod h1:mZd6rFysKEcUhUHXJk0C/08wAgyDBFuwEYL7vWWGaGo=
github.com/swaggest/go-asyncapi v0.8.0 h1:aze7YL3o/4fkxx9ZL8ubKdyYCqFJy+9+JHpwLyF10H4=
github.com/swaggest/go-asyncapi v0.8.0/go.mod h1:s1urrZuYPcNPJrRUU/kF1eOnIBU02O4JfXCDS09wONI=
github.com/tidwall/gjson v1.14.0 h1:6aeJ0bzojgWLa82gDQHcx3S0Lr/O51I9bJ5nv6JFx5w=
github.com/tidwall/gjson v1.14.0/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
github.com/tidwall/match v1.1.1 h1:+Ho715JplO36QYgwN9PGYNhgZvoUSc9X2c80KVTi+GA=
Expand Down
82 changes: 82 additions & 0 deletions pkg/apic/asyncapi.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package apic

import (
"fmt"
"net/url"
"strconv"
"strings"

management "github.com/Axway/agent-sdk/pkg/apic/apiserver/models/management/v1alpha1"
asyncSpec "github.com/swaggest/go-asyncapi/spec-2.4.0"
)

const (
protocol = "protocol"
)

// custom parse URL to allow SASL_* schemes(url.Parse throws error)
func parseURL(strURL string) (scheme, host string, port int64, err error) {
urlElements := strings.Split(strURL, "://")
remaining := strURL
if len(urlElements) > 1 {
scheme = urlElements[0]
remaining = urlElements[1]
}

strURL = fmt.Sprintf("tmp://%s", remaining)
u, e := url.Parse(strURL)
if e != nil {
err = e
return
}

host = u.Hostname()
port, _ = strconv.ParseInt(u.Port(), 10, 32)
return
}

type asyncApi struct {
spec *asyncSpec.AsyncAPI
raw []byte
}

func (a *asyncApi) GetResourceType() string {
return AsyncAPI
}

func (a *asyncApi) GetID() string {
return a.spec.ID
}

func (a *asyncApi) GetTitle() string {
return a.spec.Info.Title
}

func (a *asyncApi) GetVersion() string {
return a.spec.Info.Version
}

func (a *asyncApi) GetEndpoints() ([]management.ApiServiceInstanceSpecEndpoint, error) {
endpoints := make([]management.ApiServiceInstanceSpecEndpoint, 0)
for _, server := range a.spec.Servers {
scheme, host, port, err := parseURL(server.Server.URL)
if err != nil {
return nil, err
}
endpoints = append(endpoints, management.ApiServiceInstanceSpecEndpoint{
Host: host,
Protocol: scheme,
Port: int32(port),
Routing: management.ApiServiceInstanceSpecRouting{
Details: map[string]interface{}{
protocol: server.Server.Protocol,
},
},
})
}
return endpoints, nil
}

func (a *asyncApi) GetSpecBytes() []byte {
return a.raw
}
Loading

0 comments on commit 6c29895

Please sign in to comment.