update swagger config map
@@ -22,7 +22,6 @@ import (
"encoding/json"
"fmt"
- "strings"
"github.com/getkin/kin-openapi/openapi3"
)
@@ -125,13 +124,13 @@ func (r *ReconcileAPI) Reconcile(request reconcile.Request) (reconcile.Result, e
//Fetch swagger data from configmap
swaggerDataMap := apiConfigMap.Data
var swaggerData string
- var swaggerDataType string
+ var swaggerDataFile string
for key, value := range swaggerDataMap {
swaggerData = value
- swaggerTemp := strings.Split(key, ".")
- swaggerDataType = swaggerTemp[1]
+ swaggerDataFile = key
}
- fmt.Println("swagger data type : ", swaggerDataType)
+ fmt.Println("swagger data file : ", swaggerDataFile)
+
swagger, err := openapi3.NewSwaggerLoader().LoadSwaggerFromData([]byte(swaggerData))
if err != nil {
log.Error(err, "Swagger loading error ")
@@ -141,36 +140,47 @@ func (r *ReconcileAPI) Reconcile(request reconcile.Request) (reconcile.Result, e
data, ok := swagger.Extensions["x-mgw-production-endpoints"]
if ok {
prodEp := XMGWProductionEndpoints{}
- serviceEp := ServiceEndpoints{}
var endPoint string
datax, ok1 := data.(json.RawMessage)
fmt.Println(ok1)
if ok1 {
- err = json.Unmarshal(datax, &serviceEp)
-
+ err = json.Unmarshal(datax, &endPoint)
if err == nil {
- endPoint = "https://" + serviceEp.ServiceName
- checkt := []string{endPoint}
- prodEp.Urls = checkt
-
+ //check if service is available
currentService := &corev1.Service{}
err = r.client.Get(context.TODO(), types.NamespacedName{Namespace: "default",
- Name: serviceEp.ServiceName}, currentService)
+ Name: endPoint}, currentService)
if err != nil && errors.IsNotFound(err) {
log.Error(err, "Service CRD object is not found")
} else if err != nil {
log.Error(err, "Error in getting service")
} else {
+ endPoint = "https://" + endPoint
+ checkt := []string{endPoint}
+ prodEp.Urls = checkt
swagger.Extensions["x-mgw-production-endpoints"] = prodEp
}
-
}
}
}
final, err := swagger.MarshalJSON()
- fmt.Println(string(final))
+ newSwagger := string(final)
+ fmt.Println(newSwagger)
+
+ //update configmap with modified swagger
+
+ swaggerConfMap, err := createConfigMap(apiConfigMapRef, swaggerDataFile, newSwagger)
+ if err != nil {
+ log.Error(err, "Error in modified swagger configmap structure")
+ }
+
+ log.Info("Updating swagger configmap")
+ errConf := r.client.Update(context.TODO(), swaggerConfMap)
+ if errConf != nil {
+ log.Error(err, "Error in modified swagger configmap update")
+ }
// gets the data from analytics secret
analyticsData, err := getSecretData(r)
@@ -326,3 +336,17 @@ func createMGWSecret(r *ReconcileAPI, confData string) error {
}
}
+
+// createConfigMap creates a config file with the swagger
+func createConfigMap(apiConfigMapRef string, swaggerDataFile string, newSwagger string) (*corev1.ConfigMap, error) {
+
+ return &corev1.ConfigMap{
+ ObjectMeta: metav1.ObjectMeta{
+ Name: apiConfigMapRef,
+ Namespace: "default",
+ },
+ Data: map[string]string{
+ swaggerDataFile: newSwagger,
+ },
+ }, nil
+}
