NONRTRIC-1021: Add API-name to Kong-route
[nonrtric/plt/sme.git] / servicemanager / internal / publishserviceapi / typeupdate.go
index e8a1f9c..3b46a0f 100644 (file)
@@ -38,26 +38,30 @@ func (sd *ServiceAPIDescription) PrepareNewService() {
        sd.ApiId = &apiName
 }
 
-func (sd *ServiceAPIDescription) RegisterKong(kongDomain string,
-               kongProtocol string,
-               kongIPv4 common29122.Ipv4Addr,
-               kongDataPlanePort common29122.Port,
+func (sd *ServiceAPIDescription) RegisterKong(
+               kongDomain                       string,
+               kongProtocol             string,
+               kongControlPlaneIPv4 common29122.Ipv4Addr,
                kongControlPlanePort common29122.Port,
-               apfId string) (int, error) {
+               kongDataPlaneIPv4        common29122.Ipv4Addr,
+               kongDataPlanePort        common29122.Port,
+               apfId                            string) (int, error) {
 
        log.Trace("entering RegisterKong")
+       log.Debugf("RegisterKong kongDataPlaneIPv4 %s", kongDataPlaneIPv4)
+
        var (
                statusCode int
                err        error
        )
-       kongControlPlaneURL := fmt.Sprintf("%s://%s:%d", kongProtocol, kongIPv4, kongControlPlanePort)
+       kongControlPlaneURL := fmt.Sprintf("%s://%s:%d", kongProtocol, kongControlPlaneIPv4, kongControlPlanePort)
 
        statusCode, err = sd.createKongRoutes(kongControlPlaneURL, apfId)
        if (err != nil) || (statusCode != http.StatusCreated) {
                return statusCode, err
        }
 
-       sd.updateInterfaceDescription(kongIPv4, kongDataPlanePort, kongDomain)
+       sd.updateInterfaceDescription(kongDataPlaneIPv4, kongDataPlanePort, kongDomain)
 
        log.Trace("exiting from RegisterKong")
        return statusCode, nil
@@ -73,15 +77,16 @@ func (sd *ServiceAPIDescription) createKongRoutes(kongControlPlaneURL string, ap
        client := resty.New()
 
        profiles := *sd.AefProfiles
-       for _, profile := range profiles {
+       for i, profile := range profiles {
                log.Debugf("createKongRoutes, AefId %s", profile.AefId)
-               for _, version := range profile.Versions {
+               for j, version := range profile.Versions {
                        log.Debugf("createKongRoutes, apiVersion \"%s\"", version.ApiVersion)
-                       for _, resource := range *version.Resources {
-                               statusCode, err = sd.createKongRoute(kongControlPlaneURL, client, resource, apfId, profile.AefId, version.ApiVersion)
+                       for k, resource := range *version.Resources {
+                               statusCode, err = sd.createKongRoute(kongControlPlaneURL, client, &resource, apfId, profile.AefId, version.ApiVersion)
                                if (err != nil) || (statusCode != http.StatusCreated) {
                                        return statusCode, err
                                }
+                               (*profiles[i].Versions[j].Resources)[k] = resource
                        }
                }
        }
@@ -91,7 +96,7 @@ func (sd *ServiceAPIDescription) createKongRoutes(kongControlPlaneURL string, ap
 func (sd *ServiceAPIDescription) createKongRoute(
                kongControlPlaneURL string,
                client *resty.Client,
-               resource Resource,
+               resource *Resource,
                apfId string,
                aefId string,
                apiVersion string ) (int, error) {
@@ -110,9 +115,13 @@ func (sd *ServiceAPIDescription) createKongRoute(
        log.Debugf("createKongRoute, routeName %s", routeName)
        log.Debugf("createKongRoute, aefId %s", aefId)
 
-       uri := buildUriWithVersion(apiVersion, resource.Uri)
+       uri := buildUri(apiVersion, resource.Uri)
        log.Debugf("createKongRoute, uri %s", uri)
 
+       routeUri := buildUri(sd.ApiName, uri)
+       log.Debugf("createKongRoute, routeUri %s", routeUri)
+       resource.Uri = routeUri
+
        statusCode, err := sd.createKongService(kongControlPlaneURL, serviceName, uri, tags)
        if (err != nil) || (statusCode != http.StatusCreated) {
                return statusCode, err
@@ -123,7 +132,7 @@ func (sd *ServiceAPIDescription) createKongRoute(
        // Define the route information for Kong
        kongRouteInfo := map[string]interface{}{
                "name":       routeName,
-               "paths":      []string{uri},
+               "paths":      []string{routeUri},
                "methods":    resource.Operations,
                "tags":       tags,
                "strip_path": true,
@@ -155,15 +164,15 @@ func (sd *ServiceAPIDescription) createKongRoute(
        return resp.StatusCode(), nil
 }
 
-func buildUriWithVersion(apiVersion string, uri string) string {
-       if apiVersion != "" {
-               if apiVersion[0] != '/' {
-                       apiVersion = "/" + apiVersion
+func buildUri(prependUri string, uri string) string {
+       if prependUri != "" {
+               if prependUri[0] != '/' {
+                       prependUri = "/" + prependUri
                }
-               if apiVersion[len(apiVersion)-1] != '/' && uri[0] != '/' {
-                       apiVersion = apiVersion + "/"
+               if prependUri[len(prependUri)-1] != '/' && uri[0] != '/' {
+                       prependUri = prependUri + "/"
                }
-               uri = apiVersion + uri
+               uri = prependUri + uri
        }
        return uri
 }
@@ -272,10 +281,12 @@ func (sd *ServiceAPIDescription) findFirstAEFProfile() (common29122.Ipv4Addr, co
 }
 
 // Update our exposures to point to Kong by replacing in incoming interface description with Kong interface descriptions.
-func (sd *ServiceAPIDescription) updateInterfaceDescription(kongIPv4 common29122.Ipv4Addr, kongDataPlanePort common29122.Port, kongDomain string) {
+func (sd *ServiceAPIDescription) updateInterfaceDescription(kongDataPlaneIPv4 common29122.Ipv4Addr, kongDataPlanePort common29122.Port, kongDomain string) {
        log.Trace("updating InterfaceDescriptions")
+       log.Debugf("InterfaceDescriptions kongDataPlaneIPv4 %s", kongDataPlaneIPv4)
+
        interfaceDesc := InterfaceDescription{
-               Ipv4Addr: &kongIPv4,
+               Ipv4Addr: &kongDataPlaneIPv4,
                Port:     &kongDataPlanePort,
        }
        interfaceDescs := []InterfaceDescription{interfaceDesc}
@@ -288,14 +299,14 @@ func (sd *ServiceAPIDescription) updateInterfaceDescription(kongIPv4 common29122
        }
 }
 
-func (sd *ServiceAPIDescription) UnregisterKong(kongDomain string, kongProtocol string, kongIPv4 common29122.Ipv4Addr, kongDataPlanePort common29122.Port, kongControlPlanePort common29122.Port) (int, error) {
+func (sd *ServiceAPIDescription) UnregisterKong(kongDomain string, kongProtocol string, kongControlPlaneIPv4 common29122.Ipv4Addr, kongControlPlanePort common29122.Port) (int, error) {
        log.Trace("entering UnregisterKong")
 
        var (
                statusCode int
                err        error
        )
-       kongControlPlaneURL := fmt.Sprintf("%s://%s:%d", kongProtocol, kongIPv4, kongControlPlanePort)
+       kongControlPlaneURL := fmt.Sprintf("%s://%s:%d", kongProtocol, kongControlPlaneIPv4, kongControlPlanePort)
 
        statusCode, err = sd.deleteKongRoutes(kongControlPlaneURL)
        if (err != nil) || (statusCode != http.StatusNoContent) {