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

Implement zonId query parameter in GET method zones subscriptions

parent 7f7abe20
Loading
Loading
Loading
Loading
+3 −7
Original line number Diff line number Diff line
@@ -153,9 +153,9 @@ func ZonalTrafficSubGET(w http.ResponseWriter, r *http.Request) {
	zonalTrafficSubGet(w, r)
}

func ZonalTrafficSubListGET(w http.ResponseWriter, r *http.Request) {
	zonalTrafficSubListGet(w, r)
}
// func ZonalTrafficSubListGET(w http.ResponseWriter, r *http.Request) {
// 	zonalTrafficSubListGet(w, r)
// }

func ZoneSubListGET(w http.ResponseWriter, r *http.Request) {
	zoneSubListGET(w, r)
@@ -190,10 +190,6 @@ func ZoneStatusSubGET(w http.ResponseWriter, r *http.Request) {
	zoneStatusSubGet(w, r)
}

func ZoneStatusSubListGET(w http.ResponseWriter, r *http.Request) {
	zoneStatusSubListGet(w, r)
}

// func ZoneStatusSubPOST(w http.ResponseWriter, r *http.Request) {
// 	zoneStatusSubPost(w, r)
// }
+57 −85
Original line number Diff line number Diff line
@@ -5029,10 +5029,9 @@ func zonalTrafficSubDelete(w http.ResponseWriter, r *http.Request) {
}

func zoneSubListGET(w http.ResponseWriter, r *http.Request) {

	w.Header().Set("Content-Type", "application/json; charset=UTF-8")

	// Check if the 'subscription_type' and 'event' query parameters exist and get their values
	// Check if the 'subscription_type' and 'zoneId' query parameters exist and get their values
	queryParams := r.URL.Query()
	subscriptionType := queryParams.Get("subscription_type")
	zoneId := queryParams.Get("zoneId")
@@ -5046,24 +5045,20 @@ func zoneSubListGET(w http.ResponseWriter, r *http.Request) {
	switch subscriptionType {
	case "event":
		keyName := baseKey + typeZonalSubscription + "*"
		// If event parameter is provided, filter subscriptions by event
		if zoneId != "" {
			keyName += ":" + zoneId
		}
		err = rc.ForEachJSONEntry(keyName, populateZonalTrafficList, &subscriptions)
		err = rc.ForEachJSONEntry(keyName, func(key string, jsonInfo string, userData interface{}) error {
			return populateZonalTrafficList([]byte(jsonInfo), zoneId, &subscriptions)
		}, nil)
	case "status":
		keyName := baseKey + typeZoneStatusSubscription + "*"
		// If event parameter is provided, filter subscriptions by event
		if zoneId != "" {
			keyName += ":" + zoneId
		}
		err = rc.ForEachJSONEntry(keyName, populateZoneStatusList, &subscriptions)
		err = rc.ForEachJSONEntry(keyName, func(key string, jsonInfo string, userData interface{}) error {
			return populateZoneStatusList([]byte(jsonInfo), zoneId, &subscriptions)
		}, nil)
	default:
		// If no subscription_type is provided, return both responses
		var userSubListZoneEvent []Subscription
		keyNameEventZone := baseKey + typeZonalSubscription + "*"
		// If event parameter is provided, filter subscriptions by event
		errEvent := rc.ForEachJSONEntry(keyNameEventZone, populateZonalTrafficList, &userSubListZoneEvent)
		errEvent := rc.ForEachJSONEntry(keyNameEventZone, func(key string, jsonInfo string, userData interface{}) error {
			return populateZonalTrafficList([]byte(jsonInfo), zoneId, &userSubListZoneEvent)
		}, nil)
		if errEvent != nil {
			log.Error(errEvent.Error())
			errHandlerProblemDetails(w, errEvent.Error(), http.StatusInternalServerError)
@@ -5072,8 +5067,9 @@ func zoneSubListGET(w http.ResponseWriter, r *http.Request) {

		var userSubListStatus []Subscription
		keyNameEventZone = baseKey + typeZoneStatusSubscription + "*"
		// If event parameter is provided, filter subscriptions by event
		errStatus := rc.ForEachJSONEntry(keyNameEventZone, populateZoneStatusList, &userSubListStatus)
		errStatus := rc.ForEachJSONEntry(keyNameEventZone, func(key string, jsonInfo string, userData interface{}) error {
			return populateZoneStatusList([]byte(jsonInfo), zoneId, &userSubListStatus)
		}, nil)
		if errStatus != nil {
			log.Error(errStatus.Error())
			errHandlerProblemDetails(w, errStatus.Error(), http.StatusInternalServerError)
@@ -5142,32 +5138,32 @@ func zoneSubListGET(w http.ResponseWriter, r *http.Request) {
// 	w.WriteHeader(http.StatusOK)
// 	fmt.Fprint(w, string(jsonResponse))
// }
func zonalTrafficSubListGet(w http.ResponseWriter, r *http.Request) {
	w.Header().Set("Content-Type", "application/json; charset=UTF-8")
// func zonalTrafficSubListGet(w http.ResponseWriter, r *http.Request) {
// 	w.Header().Set("Content-Type", "application/json; charset=UTF-8")

	var response InlineNotificationSubscriptionList
	var zonalTrafficSubList NotificationSubscriptionList
	zonalTrafficSubList.ResourceURL = &LinkType{}
	zonalTrafficSubList.ResourceURL.Href = hostUrl.String() + basePath + "subscriptions/zonalTraffic"
	response.NotificationSubscriptionList = &zonalTrafficSubList
// 	var response InlineNotificationSubscriptionList
// 	var zonalTrafficSubList NotificationSubscriptionList
// 	zonalTrafficSubList.ResourceURL = &LinkType{}
// 	zonalTrafficSubList.ResourceURL.Href = hostUrl.String() + basePath + "subscriptions/zonalTraffic"
// 	response.NotificationSubscriptionList = &zonalTrafficSubList

	keyName := baseKey + typeZonalSubscription + "*"
	err := rc.ForEachJSONEntry(keyName, populateZonalTrafficList, &zonalTrafficSubList)
	if err != nil {
		log.Error(err.Error())
		errHandlerProblemDetails(w, err.Error(), http.StatusInternalServerError)
		return
	}
// 	keyName := baseKey + typeZonalSubscription + "*"
// 	err := rc.ForEachJSONEntry(keyName, populateZonalTrafficList, &zonalTrafficSubList)
// 	if err != nil {
// 		log.Error(err.Error())
// 		errHandlerProblemDetails(w, err.Error(), http.StatusInternalServerError)
// 		return
// 	}

	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))
}
// 	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 zonalTrafficSubGet(w http.ResponseWriter, r *http.Request) {
	w.Header().Set("Content-Type", "application/json; charset=UTF-8")
@@ -5662,20 +5658,21 @@ func zonalTrafficSubPut(w http.ResponseWriter, r *http.Request) {
	fmt.Fprint(w, string(jsonResponse))
}

func populateZonalTrafficList(key string, jsonInfo string, userData interface{}) error {
	subscriptions := userData.(*[]Subscription)
func populateZonalTrafficList(data []byte, zoneId string, subscriptions *[]Subscription) error {
	var zoneInfo ZoneLocationEventSubscription

	// Format response
	err := json.Unmarshal([]byte(jsonInfo), &zoneInfo)
	if err != nil {
	if err := json.Unmarshal(data, &zoneInfo); err != nil {
		return err
	}

	// Filter subscriptions by zoneId
	if zoneId != "" && zoneInfo.ZoneId != zoneId {
		return nil
	}

	href := ""
	if zoneInfo.Links != nil && zoneInfo.Links.Self != nil {
		href = zoneInfo.Links.Self.Href
	}
	// Create a Subscription instance
	sub := Subscription{
		Href:             href,
		SubscriptionType: zoneInfo.SubscriptionType,
@@ -5704,33 +5701,6 @@ func zoneStatusSubDelete(w http.ResponseWriter, r *http.Request) {
	w.WriteHeader(http.StatusNoContent)
}

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

	var response InlineNotificationSubscriptionList
	var zoneStatusSubList NotificationSubscriptionList
	zoneStatusSubList.ResourceURL = &LinkType{}
	zoneStatusSubList.ResourceURL.Href = hostUrl.String() + basePath + "subscriptions/zoneStatus"
	response.NotificationSubscriptionList = &zoneStatusSubList

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

	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 zoneStatusSubGet(w http.ResponseWriter, r *http.Request) {
	w.Header().Set("Content-Type", "application/json; charset=UTF-8")
	vars := mux.Vars(r)
@@ -5901,23 +5871,21 @@ func zoneStatusSubGet(w http.ResponseWriter, r *http.Request) {
// 	fmt.Fprint(w, string(jsonResponse))
// }

func populateZoneStatusList(key string, jsonInfo string, userData interface{}) error {
	subscriptions := userData.(*[]Subscription)
func populateZoneStatusList(data []byte, zoneId string, subscriptions *[]Subscription) error {
	var zoneInfo ZoneStatusSubscription

	// Format response
	err := json.Unmarshal([]byte(jsonInfo), &zoneInfo)
	if err != nil {
	if err := json.Unmarshal(data, &zoneInfo); err != nil {
		return err
	}

	// Extract the Href from the Links structure
	// Filter subscriptions by zoneId
	if zoneId != "" && zoneInfo.ZoneId != zoneId {
		return nil
	}

	href := ""
	if zoneInfo.Links != nil && zoneInfo.Links.Self != nil {
		href = zoneInfo.Links.Self.Href
	}

	// Create a Subscription instance
	sub := Subscription{
		Href:             href,
		SubscriptionType: zoneInfo.SubscriptionType,
@@ -6145,7 +6113,9 @@ func zoneStatusReInit() {
	var zoneList NotificationSubscriptionList

	keyName := baseKey + typeZoneStatusSubscription + "*"
	_ = rc.ForEachJSONEntry(keyName, populateZoneStatusList, &zoneList)
	_ = rc.ForEachJSONEntry(keyName, func(key string, jsonInfo string, userData interface{}) error {
		return populateZoneStatusList([]byte(jsonInfo), "", userData.(*[]Subscription))
	}, &zoneList.ZoneStatusSubscription)

	maxZoneStatusSubscriptionId := 0
	mutex.Lock()
@@ -6192,7 +6162,9 @@ func zonalTrafficReInit() {
	var zoneList NotificationSubscriptionList

	keyName := baseKey + typeZonalSubscription + "*"
	_ = rc.ForEachJSONEntry(keyName, populateZonalTrafficList, &zoneList)
	_ = rc.ForEachJSONEntry(keyName, func(key string, jsonInfo string, userData interface{}) error {
		return populateZonalTrafficList([]byte(jsonInfo), "", userData.(*[]Subscription))
	}, &zoneList.ZonalTrafficSubscription)

	maxZonalSubscriptionId := 0
	mutex.Lock()
+6 −13
Original line number Diff line number Diff line
@@ -309,12 +309,12 @@ var routes = Routes{
		ZonalTrafficSubGET,
	},

	Route{
		"ZonalTrafficSubListGET",
		strings.ToUpper("Get"),
		"/location/v3/subscriptions/zonalTraffic",
		ZonalTrafficSubListGET,
	},
	// Route{
	// 	"ZonalTrafficSubListGET",
	// 	strings.ToUpper("Get"),
	// 	"/location/v3/subscriptions/zonalTraffic",
	// 	ZonalTrafficSubListGET,
	// },

	Route{
		"ZoneSubListGET",
@@ -378,13 +378,6 @@ var routes = Routes{
		ZoneStatusSubGET,
	},

	Route{
		"ZoneStatusSubListGET",
		strings.ToUpper("Get"),
		"/location/v3/subscriptions/zoneStatus",
		ZoneStatusSubListGET,
	},

	// Route{
	// 	"ZoneStatusSubPOST",
	// 	strings.ToUpper("Post"),