Skip to content

Commit

Permalink
Merge pull request #1108 from Tharsanan1/master
Browse files Browse the repository at this point in the history
API Creation in CP Triggered by DP Events
  • Loading branch information
CrowleyRajapakse authored Mar 29, 2024
2 parents d6e955e + f3897e4 commit 5f02e48
Show file tree
Hide file tree
Showing 11 changed files with 448 additions and 5 deletions.
1 change: 1 addition & 0 deletions apim-apk-agent/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ tasks.register('go_test', Exec) {
group 'go'
description 'Automates testing the packages named by the import paths.'
commandLine 'sh', '-c', "go test -race -coverprofile=coverage.out -covermode=atomic ./..."
environment "APK_HOME", "$rootDir"
}

tasks.named('go_revive_run').configure {
Expand Down
2 changes: 2 additions & 0 deletions apim-apk-agent/config/default_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ var defaultConfig = &Config{
ServiceURLDeprecated: UnassignedAsDeprecated,
Username: "admin",
Password: "$env{cp_admin_pwd}",
ClientID: "",
ClientSecret: "",
EnvironmentLabels: []string{"Default"},
RetryInterval: 5,
SkipSSLVerification: false,
Expand Down
2 changes: 2 additions & 0 deletions apim-apk-agent/config/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,8 @@ type controlPlane struct {
HTTPClient httpClient
RequestWorkerPool requestWorkerPool
InternalKeyIssuer string
ClientID string
ClientSecret string
}

// Dataplane struct contains the configurations related to the APK
Expand Down
2 changes: 1 addition & 1 deletion apim-apk-agent/internal/agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ func Run(conf *config.Config) {
defer wg.Done()
logger.LoggerAgent.Info("starting manager")
if err := mgr.Start(ctrl.SetupSignalHandler()); err != nil {
logger.LoggerAgent.Warn("problem running manager: %v", err)
logger.LoggerAgent.Warnf("problem running manager: %v", err)
}
}()

Expand Down
3 changes: 3 additions & 0 deletions apim-apk-agent/pkg/loggers/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ const (
pkgMsg = "github.com/wso2/product-apim-tooling/apim-apk-agent/pkg/messaging"
pkgHealth = "github.com/wso2/product-apim-tooling/apim-apk-agent/pkg/health"
pkgTLSUtils = "github.com/wso2/product-apim-tooling/apim-apk-agent/pkg/tlsutils"
pkgUtils = "github.com/wso2/product-apim-tooling/apim-apk-agent/pkg/utils"
pkgAdapter = "github.com/wso2/apk/adapter/pkg/adapter"
pkgSync = "github.com/wso2/product-apim-tooling/apim-apk-agent/pkg/synchronizer"
pkgSoapUtils = "github.com/wso2/apk/adapter/pkg/soaputils"
Expand All @@ -49,6 +50,7 @@ var (
LoggerMsg logging.Log
LoggerHealth logging.Log
LoggerTLSUtils logging.Log
LoggerUtils logging.Log
LoggerAdapter logging.Log
LoggerSync logging.Log
LoggerSoapUtils logging.Log
Expand All @@ -67,6 +69,7 @@ func UpdateLoggers() {
LoggerMsg = logging.InitPackageLogger(pkgMsg)
LoggerHealth = logging.InitPackageLogger(pkgHealth)
LoggerTLSUtils = logging.InitPackageLogger(pkgTLSUtils)
LoggerUtils = logging.InitPackageLogger(pkgUtils)
LoggerAdapter = logging.InitPackageLogger(pkgAdapter)
LoggerSync = logging.InitPackageLogger(pkgSync)
LoggerSoapUtils = logging.InitPackageLogger(pkgSoapUtils)
Expand Down
108 changes: 106 additions & 2 deletions apim-apk-agent/pkg/managementserver/rest_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,14 @@
package managementserver

import (
"bytes"
"fmt"
"net/http"

"github.com/gin-gonic/gin"
"github.com/wso2/product-apim-tooling/apim-apk-agent/config"
logger "github.com/wso2/product-apim-tooling/apim-apk-agent/pkg/loggers"
"github.com/wso2/product-apim-tooling/apim-apk-agent/pkg/utils"
"gopkg.in/yaml.v2"
"net/http"
)

func init() {
Expand All @@ -43,7 +46,108 @@ func StartInternalServer(port uint) {
applicationMappingList := GetAllApplicationMappings()
c.JSON(http.StatusOK, ApplicationMappingList{List: applicationMappingList})
})
r.POST("/apis", func(c *gin.Context) {
var event APICPEvent
if err := c.ShouldBindJSON(&event); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
if event.Event == DeleteEvent {
logger.LoggerMgtServer.Infof("Delete event received with APIUUID: %s", event.API.APIUUID)
// Delete the api
utils.DeleteAPI(event.API.APIUUID)
} else {
apiYaml := createAPIYaml(event)
definition := event.API.Definition
deploymentContent := createDeployementYaml()
zipFiles := []utils.ZipFile{{
Path: fmt.Sprintf("%s-%s/api.yaml", event.API.APIName, event.API.APIVersion),
Content: apiYaml,
}, {
Path: fmt.Sprintf("%s-%s/deployment_environments.yaml", event.API.APIName, event.API.APIVersion),
Content: deploymentContent,
}, {
Path: fmt.Sprintf("%s-%s/Definitions/swagger.yaml", event.API.APIName, event.API.APIVersion),
Content: definition,
}}
var buf bytes.Buffer
if err := utils.CreateZipFile(&buf, zipFiles); err != nil {
logger.LoggerMgtServer.Errorf("Error while creating apim zip file for api uuid: %s. Error: %+v", event.API.APIUUID, err)
}

id, err := utils.ImportAPI(fmt.Sprintf("admin-%s-%s.zip", event.API.APIName, event.API.APIVersion), &buf)
if err != nil {
logger.LoggerMgtServer.Errorf("Error while importing API. Sending error response to Adapter.")
c.JSON(http.StatusInternalServerError, err.Error())
return
}
c.JSON(http.StatusOK, map[string]string{"id": id})
}
})
gin.SetMode(gin.ReleaseMode)
publicKeyLocation, privateKeyLocation, _ := config.GetKeyLocations()
r.RunTLS(fmt.Sprintf(":%d", port), publicKeyLocation, privateKeyLocation)
}

func createAPIYaml(apiCPEvent APICPEvent) string {
data := map[string]interface{}{
"type": "api",
"version": "v4.3.0",
"data": map[string]interface{}{
"id": apiCPEvent.API.APIUUID,
"name": apiCPEvent.API.APIName,
"context": apiCPEvent.API.BasePath,
"version": apiCPEvent.API.APIVersion,
"organizationId": apiCPEvent.API.Organization,
"provider": "admin",
"lifeCycleStatus": "PUBLISHED",
"responseCachingEnabled": false,
"cacheTimeout": 300,
"hasThumbnail": false,
"isDefaultVersion": apiCPEvent.API.IsDefaultVersion,
"isRevision": false,
"revisionId": apiCPEvent.API.RevisionID,
"enableSchemaValidation": false,
"enableSubscriberVerification": false,
"type": "HTTP",
"endpointConfig": map[string]interface{}{
"endpoint_type": "http",
"sandbox_endpoints": map[string]interface{}{
"url": "http://local",
},
"production_endpoints": map[string]interface{}{
"url": "http://local",
},
},
"policies": []string{"Unlimited"},
"gatewayType": "wso2/apk",
"gatewayVendor": "wso2",
},
}

yamlBytes, _ := yaml.Marshal(data)
return string(yamlBytes)
}

func createDeployementYaml() string {
config, err := config.ReadConfigs()
envLabel := []string{"Default"}
if (err == nil) {
envLabel = config.ControlPlane.EnvironmentLabels
}
deploymentEnvData := []map[string]string{}
for _, label := range envLabel {
deploymentEnvData = append(deploymentEnvData, map[string]string{
"displayOnDevportal": "true",
"deploymentEnvironment": label,
})
}
data := map[string]interface{}{
"type": "deployment_environments",
"version": "v4.3.0",
"data": deploymentEnvData,
}

yamlBytes, _ := yaml.Marshal(data)
return string(yamlBytes)
}
38 changes: 38 additions & 0 deletions apim-apk-agent/pkg/managementserver/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,3 +104,41 @@ type ApplicationMapping struct {
type ApplicationMappingList struct {
List []ApplicationMapping `json:"list"`
}

// APICPEvent holds data of a specific API event from adapter
type APICPEvent struct {
Event EventType `json:"event"`
API API `json:"payload"`
}

// EventType is the type of api event. One of (CREATE, UPDATE, DELETE)
type EventType string

const (
// CreateEvent is create api event
CreateEvent EventType = "CREATE"
// DeleteEvent is delete api event
DeleteEvent EventType = "DELETE"
)

// API holds the api data from adapter api event
type API struct {
APIUUID string `json:"apiUUID"`
APIName string `json:"apiName"`
APIVersion string `json:"apiVersion"`
IsDefaultVersion bool `json:"isDefaultVersion"`
Definition string `json:"definition"`
APIType string `json:"apiType"`
BasePath string `json:"basePath"`
Organization string `json:"organization"`
SystemAPI bool `json:"systemAPI"`
APIProperties []Property `json:"apiProperties,omitempty"`
Environment string `json:"environment,omitempty"`
RevisionID string `json:"revisionID"`
}

// Property holds the api property
type Property struct {
Name string `json:"name,omitempty"`
Value string `json:"value,omitempty"`
}
2 changes: 1 addition & 1 deletion apim-apk-agent/pkg/transformer/transformer.go
Original file line number Diff line number Diff line change
Expand Up @@ -815,7 +815,7 @@ func createConfigMaps(certFiles map[string]string, k8sArtifact *K8sArtifacts) {
cm.Data[confKey] = confValue
certConfigMap := &cm

logger.LoggerTransformer.Debug("New ConfigMap Data: %v", *certConfigMap)
logger.LoggerTransformer.Debugf("New ConfigMap Data: %v", *certConfigMap)
k8sArtifact.ConfigMaps[certConfigMap.ObjectMeta.Name] = certConfigMap
}
}
Expand Down
2 changes: 1 addition & 1 deletion apim-apk-agent/pkg/transformer/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func readZipFile(file *zip.File) (*APIArtifact, error) {
apiArtifact.APIFileName = file.Name
zipReader, err := zip.NewReader(bytes.NewReader(content), int64(len(content)))
if err != nil {
logger.LoggerTransformer.Errorf("Error reading zip file: ", err)
logger.LoggerTransformer.Errorf("Error reading zip file: %+v", err)
return nil, err
}
for _, file := range zipReader.File {
Expand Down
Loading

0 comments on commit 5f02e48

Please sign in to comment.