Skip to content

Commit

Permalink
adding graphql API creation
Browse files Browse the repository at this point in the history
  • Loading branch information
CrowleyRajapakse committed Feb 19, 2024
1 parent 94a941e commit 8ca0894
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 22 deletions.
3 changes: 2 additions & 1 deletion apim-apk-agent/internal/utils/apis_fetcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,12 +109,13 @@ func FetchAPIsOnEvent(conf *config.Config, apiUUID *string, k8sClient client.Cli
return nil, err
}
apkConf, apiUUID, revisionID, apkErr := transformer.GenerateAPKConf(artifact.APIJson, artifact.ClientCerts)
logger.LoggerSync.Infof("APK COnf: %s", apkConf)
if apkErr != nil {
logger.LoggerSync.Errorf("Error while generating APK-Conf: %v", apkErr)
return nil, err
}
k8ResourceEndpoint := conf.DataPlane.K8ResourceEndpoint
crResponse, err := transformer.GenerateCRs(apkConf, artifact.Swagger, k8ResourceEndpoint)
crResponse, err := transformer.GenerateCRs(apkConf, artifact.Schema, k8ResourceEndpoint)
if err != nil {
logger.LoggerSync.Errorf("Error occured in receiving the updated CRDs: %v", err)
return nil, err
Expand Down
2 changes: 1 addition & 1 deletion apim-apk-agent/pkg/transformer/api_model.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ type APIArtifact struct {
APIJson string `json:"apiJson"`
APIFileName string `json:"apiFileName"`
EnvConfig string `json:"envConfig"`
Swagger string `json:"swagger"`
Schema string `json:"schema"`
DeploymentDescriptor string `json:"deploymentDescriptor"`
ClientCerts string `json:"clientCert"`
RevisionID uint32 `json:"revisionId"`
Expand Down
41 changes: 24 additions & 17 deletions apim-apk-agent/pkg/transformer/transformer.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,29 +67,36 @@ func GenerateAPKConf(APIJson string, clientCerts string) (string, string, uint32
apk.Name = apiYamlData.Name
apk.Context = apiYamlData.Context
apk.Version = apiYamlData.Version
logger.LoggerTransformer.Infof("Type: %s", apiYamlData.Type)
apk.Type = getAPIType(apiYamlData.Type)
logger.LoggerTransformer.Infof("API Type: %s", apk.Type)
apk.DefaultVersion = apiYamlData.DefaultVersion
apk.DefinitionPath = "/definition"
apk.SubscriptionValidation = true
apkOperations := make([]Operation, len(apiYamlData.Operations))
logger.LoggerTransformer.Infof("apiYamlData.Operations: %v", apiYamlData.Operations)

for i, operation := range apiYamlData.Operations {

reqPolicyCount := len(operation.OperationPolicies.Request)
resPolicyCount := len(operation.OperationPolicies.Response)
reqInterceptor, resInterceptor := getReqAndResInterceptors(reqPolicyCount, resPolicyCount)

op := &Operation{
Target: operation.Target,
Verb: operation.Verb,
Scopes: operation.Scopes,
Secured: true,
OperationPolicies: &OperationPolicies{
Request: *reqInterceptor,
Response: *resInterceptor,
},
logger.LoggerTransformer.Infof("Operation: %v", operation)

if operation.Verb != "SUBSCRIPTION" {

reqPolicyCount := len(operation.OperationPolicies.Request)
resPolicyCount := len(operation.OperationPolicies.Response)
reqInterceptor, resInterceptor := getReqAndResInterceptors(reqPolicyCount, resPolicyCount)

op := &Operation{
Target: operation.Target,
Verb: operation.Verb,
Scopes: operation.Scopes,
Secured: true,
OperationPolicies: &OperationPolicies{
Request: *reqInterceptor,
Response: *resInterceptor,
},
}
apkOperations[i] = *op
}
apkOperations[i] = *op
}

apk.Operations = &apkOperations
Expand Down Expand Up @@ -150,8 +157,8 @@ func getAPIType(protocolType string) string {
switch protocolType {
case "HTTP", "HTTPS":
apiType = "REST"
case "GraphQL":
apiType = "GraphQL"
case "GRAPHQL":
apiType = "GRAPHQL"
}
return apiType
}
Expand Down
18 changes: 15 additions & 3 deletions apim-apk-agent/pkg/transformer/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,21 @@ func readZipFile(file *zip.File) (*APIArtifact, error) {
return nil, err
}
for _, file := range zipReader.File {
logger.LoggerTransformer.Info("Reading " + file.Name)
if strings.Contains(file.Name, "swagger.json") {
openAPIContent, err := ReadContent(file)
if err != nil {
return nil, err
}
apiArtifact.Swagger = string(openAPIContent)
apiArtifact.Schema = string(openAPIContent)
}

if strings.Contains(file.Name, "schema.graphql") {
graphqlContent, err := ReadContent(file)
if err != nil {
return nil, err
}
apiArtifact.Schema = string(graphqlContent)
}

if strings.Contains(file.Name, "api.json") {
Expand Down Expand Up @@ -118,8 +127,11 @@ func readAPIZipFile(file *zip.File, apiArtifact *APIArtifact) error {
}

if strings.Contains(file.Name, "swagger.json") {
apiArtifact.Swagger = string(content)
apiArtifact.Schema = string(content)
}

if strings.Contains(file.Name, "schema.graphql") {
apiArtifact.Schema = string(content)
}

if strings.Contains(file.Name, "api.json") {
Expand Down Expand Up @@ -215,7 +227,7 @@ func DecodeAPIArtifacts(payload []byte) ([]APIArtifact, error) {
apiArtifacts = append(apiArtifacts, apiArtifact)
}
apiArtifact.APIJson = ""
apiArtifact.Swagger = ""
apiArtifact.Schema = ""
apiArtifact.RevisionID = 0
apiArtifact.APIFileName = ""
}
Expand Down

0 comments on commit 8ca0894

Please sign in to comment.