<commit_before>package api

import (
	"fmt"
	"github.com/Aptomi/aptomi/pkg/slinga/object/codec"
	"github.com/Aptomi/aptomi/pkg/slinga/server/controller"
	"github.com/julienschmidt/httprouter"
	"io/ioutil"
	"net/http"
)

type PolicyAPI struct {
	ctl   controller.PolicyController
	codec codec.MarshalUnmarshaler
}

func (h *PolicyAPI) handleGetPolicy(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
	rev, key, ns := p.ByName("rev"), p.ByName("key"), p.ByName("ns")

	fmt.Printf("[handleGetPolicy] rev: %s, key: %s, ns: %s\n", rev, key, ns)

	if len(rev) == 0 {
		// todo(slukjanov): fail better
		panic("Revision should be specified (0 for current)")
	}

	if (len(key) == 0) && (len(ns) == 0) {
		// get full policy from specific revision
	}

	if (len(key) > 0) && (len(ns) > 0) {
		// todo(slukjanov): unreachable, better failing
		panic("Only one of key or namespace could specified")
	}

	if len(key) != 0 {
		// get by key from specific revision
	}

	if len(ns) != 0 {
		// get all by ns from specific revision
	}
}

func (h *PolicyAPI) handlePolicyUpdate(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
	body, err := ioutil.ReadAll(r.Body)
	if err != nil {
		panic(fmt.Sprintf("Error while reading bytes from request Body: %s", err))
	}

	// todo remove bad logging
	fmt.Println(string(body))

	objects, err := h.codec.UnmarshalOneOrMany(body)
	if err != nil {
		panic(fmt.Sprintf("Error unmarshaling policy update request: %s", err))
	}
	policy, err := h.ctl.UpdatePolicy(objects)
	if err != nil {
		panic(fmt.Sprintf("Error while updating policy: %s", err))
	}

	// todo remove bad logging
	fmt.Println(policy)

	// temp send back received data (to impl some table output on client side)
	// todo send full updated policy
	_, err = fmt.Fprint(w, string(body))
	if err != nil {
		panic(fmt.Sprintf("Error while writing response bytes: %s", err))
	}
}

func Serve(router *httprouter.Router, ctl controller.PolicyController, cod codec.MarshalUnmarshaler) {
	h := PolicyAPI{ctl, cod}

	router.GET("/api/v1/revision/:rev/policy", h.handleGetPolicy)               // get full policy from specific revision
	router.GET("/api/v1/revision/:rev/policy/key/:key", h.handleGetPolicy)      // get by key from specific revision
	router.GET("/api/v1/revision/:rev/policy/namespace/:ns", h.handleGetPolicy) // get policy for namespace from specific revision

	router.POST("/api/v1/revision", h.handlePolicyUpdate)
}
<commit_msg>Fix policy API according to decision that policy lives separately from revision<commit_after>package api

import (
	"fmt"
	"github.com/Aptomi/aptomi/pkg/slinga/object/codec"
	"github.com/Aptomi/aptomi/pkg/slinga/server/controller"
	"github.com/julienschmidt/httprouter"
	"io/ioutil"
	"net/http"
)

type PolicyAPI struct {
	ctl   controller.PolicyController
	codec codec.MarshalUnmarshaler
}

func (a *PolicyAPI) handleGetPolicy(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
	rev, ns := p.ByName("rev"), p.ByName("ns")

	if len(rev) == 0 {
		rev = "0" // latest revision
	}

	fmt.Printf("[handleGetPolicy] rev: %s, ns: %s\n", rev, ns)

	if len(ns) != 0 {
		// get all by ns from specific revision
	}
}

func (a *PolicyAPI) handlePolicyUpdate(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
	body, err := ioutil.ReadAll(r.Body)
	if err != nil {
		panic(fmt.Sprintf("Error while reading bytes from request Body: %s", err))
	}

	// todo remove bad logging
	fmt.Println(string(body))

	objects, err := a.codec.UnmarshalOneOrMany(body)
	if err != nil {
		panic(fmt.Sprintf("Error unmarshaling policy update request: %s", err))
	}
	policy, err := a.ctl.UpdatePolicy(objects)
	if err != nil {
		panic(fmt.Sprintf("Error while updating policy: %s", err))
	}

	// todo remove bad logging
	fmt.Println(policy)

	// temp send back received data (to impl some table output on client side)
	// todo send full updated policy
	_, err = fmt.Fprint(w, string(body))
	if err != nil {
		panic(fmt.Sprintf("Error while writing response bytes: %s", err))
	}
}

func Serve(router *httprouter.Router, ctl controller.PolicyController, cod codec.MarshalUnmarshaler) {
	h := PolicyAPI{ctl, cod}

	router.GET("/api/v1/policy", h.handleGetPolicy)
	router.GET("/api/v1/policy/:rev", h.handleGetPolicy)
	router.GET("/api/v1/policy/:rev/namespace/:ns", h.handleGetPolicy)

	router.POST("/api/v1/policy", h.handlePolicyUpdate)
}

{'fraction_non_alphanumeric': 0.1460237659963437, 'fraction_numerical': 0.004341864716636197, 'mean_word_length': 4.113317757009346, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4645963', 'n_tokens_mistral': 1557, 'n_tokens_neox': 1519, 'n_words': 400}