Commit a841a92d authored by Ikram Haq's avatar Ikram Haq
Browse files

Add zoneId parameter to get the specific zone information

parent dde89b2f
Loading
Loading
Loading
Loading
+92 −77
Original line number Diff line number Diff line
@@ -1832,77 +1832,48 @@ func apByIdGet(w http.ResponseWriter, r *http.Request) {
	fmt.Fprint(w, string(jsonResponse))
}

func zonesGet(w http.ResponseWriter, r *http.Request) {
	w.Header().Set("Content-Type", "application/json; charset=UTF-8")

	// Check if zoneId query parameter is provided
	zoneID := r.URL.Query().Get("zoneId")
	if zoneID != "" {
		// If zoneId is provided, retrieve information for that specific zone
		var zoneInfo ZoneInfo

		// Retrieve zone information from the DB
		jsonZoneInfo, _ := rc.JSONGetEntry(baseKey+typeZone+":"+zoneID, ".")
		if jsonZoneInfo == "" {
			w.WriteHeader(http.StatusNotFound)
			return
		}

		// Unmarshal the retrieved JSON into zoneInfo struct
		err := json.Unmarshal([]byte(jsonZoneInfo), &zoneInfo)
		if err != nil {
			log.Error(err.Error())
			errHandlerProblemDetails(w, err.Error(), http.StatusInternalServerError)
			return
		}

		// Marshal the zoneInfo into JSON response
		jsonResponse, err := json.Marshal(InlineZoneInfo{ZoneInfo: &zoneInfo})
		if err != nil {
			log.Error(err.Error())
			errHandlerProblemDetails(w, err.Error(), http.StatusInternalServerError)
			return
		}

		w.WriteHeader(http.StatusOK)
		fmt.Fprint(w, string(jsonResponse))
	} else {
		// If zoneId is not provided, retrieve information for all zones
		var response InlineZoneList
		var zoneList ZoneList
		zoneList.ResourceURL = hostUrl.String() + basePath + "queries/zones"
		response.ZoneList = &zoneList
// func zonesGet(w http.ResponseWriter, r *http.Request) {
// 	w.Header().Set("Content-Type", "application/json; charset=UTF-8")

		// Retrieve zone list information from the DB
		keyName := baseKey + typeZone + ":*"
		err := rc.ForEachJSONEntry(keyName, populateZoneList, &zoneList)
		if err != nil {
			log.Error(err.Error())
			errHandlerProblemDetails(w, err.Error(), http.StatusInternalServerError)
			return
		}
// 	// Check if zoneId query parameter is provided
// 	zoneID := r.URL.Query().Get("zoneId")
// 	if zoneID != "" {
// 		// If zoneId is provided, retrieve information for that specific zone
// 		var zoneInfo ZoneInfo

		// Marshal the zoneList into JSON response
		jsonResponse, err := json.Marshal(response)
		if err != nil {
			log.Error(err.Error())
			errHandlerProblemDetails(w, err.Error(), http.StatusInternalServerError)
			return
		}
// 		// Retrieve zone information from the DB
// 		jsonZoneInfo, _ := rc.JSONGetEntry(baseKey+typeZone+":"+zoneID, ".")
// 		if jsonZoneInfo == "" {
// 			w.WriteHeader(http.StatusNotFound)
// 			return
// 		}

		w.WriteHeader(http.StatusOK)
		fmt.Fprint(w, string(jsonResponse))
	}
}
// 		// Unmarshal the retrieved JSON into zoneInfo struct
// 		err := json.Unmarshal([]byte(jsonZoneInfo), &zoneInfo)
// 		if err != nil {
// 			log.Error(err.Error())
// 			errHandlerProblemDetails(w, err.Error(), http.StatusInternalServerError)
// 			return
// 		}

// func zonesGet(w http.ResponseWriter, r *http.Request) {
// 	w.Header().Set("Content-Type", "application/json; charset=UTF-8")
// 		// Marshal the zoneInfo into JSON response
// 		jsonResponse, err := json.Marshal(InlineZoneInfo{ZoneInfo: &zoneInfo})
// 		if err != nil {
// 			log.Error(err.Error())
// 			errHandlerProblemDetails(w, err.Error(), http.StatusInternalServerError)
// 			return
// 		}

// 		w.WriteHeader(http.StatusOK)
// 		fmt.Fprint(w, string(jsonResponse))
// 	} else {
// 		// If zoneId is not provided, retrieve information for all zones
// 		var response InlineZoneList
// 		var zoneList ZoneList
// 		zoneList.ResourceURL = hostUrl.String() + basePath + "queries/zones"
// 		response.ZoneList = &zoneList

// 		// Retrieve zone list information from the DB
// 		keyName := baseKey + typeZone + ":*"
// 		err := rc.ForEachJSONEntry(keyName, populateZoneList, &zoneList)
// 		if err != nil {
@@ -1911,15 +1882,59 @@ func zonesGet(w http.ResponseWriter, r *http.Request) {
// 			return
// 		}

// 		// Marshal the zoneList into JSON response
// 		jsonResponse, err := json.Marshal(response)
// 		if err != nil {
// 			log.Error(err.Error())
// 			errHandlerProblemDetails(w, err.Error(), http.StatusInternalServerError)
// 			return
// 		}

// 		w.WriteHeader(http.StatusOK)
// 		fmt.Fprint(w, string(jsonResponse))
// 	}
// }

func zonesGet(w http.ResponseWriter, r *http.Request) {
	w.Header().Set("Content-Type", "application/json; charset=UTF-8")

	// Parse query parameters
	zoneID := r.URL.Query().Get("zoneId")

	var response InlineZoneList
	var zoneList ZoneList
	zoneList.ResourceURL = hostUrl.String() + basePath + "queries/zones"
	response.ZoneList = &zoneList

	keyName := baseKey + typeZone + ":*"
	err := rc.ForEachJSONEntry(keyName, populateZoneList, &zoneList)
	if err != nil {
		log.Error(err.Error())
		errHandlerProblemDetails(w, err.Error(), http.StatusInternalServerError)
		return
	}

	// Filter zone list if zoneId parameter is provided
	if zoneID != "" {
		filteredZoneList := ZoneList{}
		for _, zone := range zoneList.Zone {
			if zone.ZoneId == zoneID {
				filteredZoneList.Zone = append(filteredZoneList.Zone, zone)
				break // Assuming zone IDs are unique, stop after finding the match
			}
		}
		response.ZoneList = &filteredZoneList
	}

	jsonResponse, err := json.Marshal(response)
	if err != nil {
		log.Error(err.Error())
		errHandlerProblemDetails(w, err.Error(), http.StatusInternalServerError)
		return
	}
	w.WriteHeader(http.StatusOK)
	fmt.Fprint(w, string(jsonResponse))
}

func zonesByIdGet(w http.ResponseWriter, r *http.Request) {
	w.Header().Set("Content-Type", "application/json; charset=UTF-8")