github.com/Axway/agent-sdk@v1.1.101/pkg/apic/asyncapi.go (about)

     1  package apic
     2  
     3  import (
     4  	"fmt"
     5  	"net/url"
     6  	"strconv"
     7  	"strings"
     8  
     9  	management "github.com/Axway/agent-sdk/pkg/apic/apiserver/models/management/v1alpha1"
    10  	asyncSpec "github.com/swaggest/go-asyncapi/spec-2.4.0"
    11  )
    12  
    13  const (
    14  	protocol = "protocol"
    15  )
    16  
    17  // custom parse URL to allow SASL_* schemes(url.Parse throws error)
    18  func parseURL(strURL string) (scheme, host string, port int64, err error) {
    19  	urlElements := strings.Split(strURL, "://")
    20  	remaining := strURL
    21  	if len(urlElements) > 1 {
    22  		scheme = urlElements[0]
    23  		remaining = urlElements[1]
    24  	}
    25  
    26  	strURL = fmt.Sprintf("tmp://%s", remaining)
    27  	u, e := url.Parse(strURL)
    28  	if e != nil {
    29  		err = e
    30  		return
    31  	}
    32  
    33  	host = u.Hostname()
    34  	port, _ = strconv.ParseInt(u.Port(), 10, 32)
    35  	return
    36  }
    37  
    38  type asyncApi struct {
    39  	spec *asyncSpec.AsyncAPI
    40  	raw  []byte
    41  }
    42  
    43  func (a *asyncApi) GetResourceType() string {
    44  	return AsyncAPI
    45  }
    46  
    47  func (a *asyncApi) GetID() string {
    48  	return a.spec.ID
    49  }
    50  
    51  func (a *asyncApi) GetTitle() string {
    52  	return a.spec.Info.Title
    53  }
    54  
    55  func (a *asyncApi) GetVersion() string {
    56  	return a.spec.Info.Version
    57  }
    58  
    59  func (a *asyncApi) GetEndpoints() ([]management.ApiServiceInstanceSpecEndpoint, error) {
    60  	endpoints := make([]management.ApiServiceInstanceSpecEndpoint, 0)
    61  	for _, server := range a.spec.Servers {
    62  		scheme, host, port, err := parseURL(server.Server.URL)
    63  		if err != nil {
    64  			return nil, err
    65  		}
    66  		endpoints = append(endpoints, management.ApiServiceInstanceSpecEndpoint{
    67  			Host:     host,
    68  			Protocol: scheme,
    69  			Port:     int32(port),
    70  			Routing: management.ApiServiceInstanceSpecRouting{
    71  				Details: map[string]interface{}{
    72  					protocol: server.Server.Protocol,
    73  				},
    74  			},
    75  		})
    76  	}
    77  	return endpoints, nil
    78  }
    79  
    80  func (a *asyncApi) GetSpecBytes() []byte {
    81  	return a.raw
    82  }