Exemple #1
0
func TestIsKeyNotFound(t *testing.T) {
	e := etcd.NewClient([]string{"http://127.0.0.1:4001"})
	newContext(t)
	defer contextCleanup(t)

	_, err := e.Get("lochness/some-randon-non-existent-key", false, false)

	if !lochness.IsKeyNotFound(err) {
		t.Fatalf("was expecting a KeyNotFound error, got: %#v\n", err)
	}

	err = errors.New("lochness/some-random-non-key-not-found-error")
	if lochness.IsKeyNotFound(err) {
		t.Fatal("got unexpected positive KeyNotFound error for err:", err)
	}
}
Exemple #2
0
func contextCleanup(t *testing.T) {
	e := etcd.NewClient([]string{"http://127.0.0.1:4001"})
	if !e.SyncCluster() {
		t.Fatal("cannot sync cluster. make sure etcd is running at http://127.0.0.1:4001")
	}

	_, err := e.Delete("lochness", true)
	if !lochness.IsKeyNotFound(err) {
		h.Ok(t, err)
	}
}
Exemple #3
0
// UpdateVLANGroupMembership updates a VLAN's group membership
func UpdateVLANGroupMembership(w http.ResponseWriter, r *http.Request) {
	hr := HTTPResponse{w}
	vlan, ok := getVLANHelper(hr, r)
	if !ok {
		return
	}
	var newGroups []string
	if err := json.NewDecoder(r.Body).Decode(&newGroups); err != nil {
		hr.JSONMsg(http.StatusBadRequest, err.Error())
		return
	}

	// Determine group modifications necessary
	// -1 : Removed
	//  0 : No change
	//  1 : Added
	ctx := GetContext(r)
	groupModifications := make(map[string]int)
	for _, oldGroup := range vlan.VLANGroups() {
		groupModifications[oldGroup] = -1
	}
	for _, newGroup := range newGroups {
		groupModifications[newGroup]++
	}
	// Make group modifications
	for groupID, action := range groupModifications {
		vlanGroup, err := ctx.VLANGroup(groupID)
		if err != nil {
			if lochness.IsKeyNotFound(err) {
				hr.JSONMsg(http.StatusBadRequest, "group not found")
			} else {
				hr.JSONMsg(http.StatusInternalServerError, err.Error())
			}
			return
		}
		switch action {
		case -1:
			if err := vlanGroup.RemoveVLAN(vlan); err != nil {
				hr.JSONError(http.StatusInternalServerError, err)
				return
			}
		case 0:
			break
		case 1:
			if err := vlanGroup.AddVLAN(vlan); err != nil {
				hr.JSONError(http.StatusInternalServerError, err)
				return
			}
		}
	}

	hr.JSON(http.StatusOK, vlan.VLANGroups())
}
Exemple #4
0
// ListVLANs gets a list of all VLANs
func ListVLANs(w http.ResponseWriter, r *http.Request) {
	hr := HTTPResponse{w}
	ctx := GetContext(r)
	vlans := make(lochness.VLANs, 0)
	err := ctx.ForEachVLAN(func(vlan *lochness.VLAN) error {
		vlans = append(vlans, vlan)
		return nil
	})
	if err != nil && !lochness.IsKeyNotFound(err) {
		hr.JSONError(http.StatusInternalServerError, err)
		return
	}
	hr.JSON(http.StatusOK, vlans)
}
Exemple #5
0
func ipxeHandler(w http.ResponseWriter, r *http.Request) {
	s := context.Get(r, "_server_").(*server)

	hv, code, msg := getHV(s, mux.Vars(r)["ip"])
	if code != http.StatusOK {
		http.Error(w, msg, code)
		return
	}

	version := hv.Config["version"]
	if version == "" {
		var err error
		version, err = s.ctx.GetConfig("defaultVersion")
		if err != nil && !lochness.IsKeyNotFound(err) {
			// XXX: should be fatal?
			log.WithFields(log.Fields{
				"error": err,
				"func":  "lochness.GetConfig",
			}).Error("failed to get a version")
		}
		if version == "" {
			version = s.defaultVersion
		}
	}

	options := map[string]string{
		"uuid": hv.ID,
	}
	data := map[string]string{
		"BaseURL": s.baseURL,
		"Options": mapToOptions(options) + " " + s.addOpts,
		"Version": version,
	}

	if err := s.t.Execute(w, data); err != nil {
		// we should not get here
		http.Error(w, err.Error(), http.StatusInternalServerError)
	}
}