<commit_before>package main

/*
  This is a little program that writes 100m keys to a level db to check the size.
  Then deletes a bunch of keys to see how that is reflected.
*/

import (
	"fmt"
	"github.com/jmhodges/levigo"
	"math/rand"
	"os"
	"time"
)

func main() {
	seriesNames := []string{"events",
		"some_host_2342.stats.cpu.idle", "actions", "host_another_2.stats.cpu.idle",
		"some_long_name.with-other_stuff.in.it.and_whatnot"}
	pointsWritten := 0
	dbDir := "/tmp/chronos_db_test"
	os.RemoveAll(dbDir)
	pointsToWritePerSeries := 3000000
	pointsToDeletePerSeries := pointsToWritePerSeries / 2

	opts := levigo.NewOptions()
	hundregMegabytes := 100 * 1048576
	opts.SetCache(levigo.NewLRUCache(hundregMegabytes))
	opts.SetCreateIfMissing(true)
	opts.SetBlockSize(262144)
	os.MkdirAll(dbDir, 0744)
	db, err := levigo.Open(dbDir, opts)
	if err != nil {
		panic(err)
	}
	writeOptions := levigo.NewWriteOptions()
	defer writeOptions.Close()

	// this initializes the ends of the keyspace so seeks don't mess with us.
	db.Put(writeOptions, []byte("a"), []byte(""))
	db.Put(writeOptions, []byte("z"), []byte(""))

	start := time.Now()
	for _, seriesName := range seriesNames {
		fmt.Println("Writing Series: ", seriesName)
		for i := 0; i < pointsToWritePerSeries; i++ {
			key := fmt.Sprintf("c~%s~1381456156%012d~%d", seriesName, i, rand.Int())
			db.Put(writeOptions, []byte(key), []byte(fmt.Sprintf("%f", rand.Float64())))
			pointsWritten += 1
			if pointsWritten%500000 == 0 {
				fmt.Println("COUNT: ", pointsWritten)
			}
		}
	}
	diff := time.Now().Sub(start)
	fmt.Printf("DONE. %d points written in %s\n", pointsWritten, diff.String())
	fmt.Printf("hit enter after checking size of dir and ready to continue...")
	var s string
	fmt.Scanf("%s", &s)
	deleteCount := 0
	start = time.Now()
	for _, seriesName := range seriesNames {
		fmt.Println("Deleting: ", seriesName)
		count := 0
		key := fmt.Sprintf("c~%s~", seriesName)
		ro := levigo.NewReadOptions()
		it := db.NewIterator(ro)
		it.Seek([]byte(key))
		var lastKey []byte
		for it = it; it.Valid() && count < pointsToDeletePerSeries; it.Next() {
			lastKey = it.Key()
			db.Delete(writeOptions, it.Key())
			count += 1
			deleteCount += 1
		}
		it.Close()
		ro.Close()
		rangeToCompact := &levigo.Range{[]byte(key), lastKey}
		db.CompactRange(*rangeToCompact)
	}
	diff = time.Now().Sub(start)
	fmt.Printf("DONE. %d points deleted in %s\n", deleteCount, diff.String())
}
<commit_msg>Update check to test writing values during compaction<commit_after>package main

/*
  This is a little program that writes 100m keys to a level db to check the size.
  Then deletes a bunch of keys to see how that is reflected.
*/

import (
	"fmt"
	"github.com/jmhodges/levigo"
	"math/rand"
	"os"
	"os/exec"
	"time"
)

func main() {
	seriesNames := []string{"events",
		"some_host_2342.stats.cpu.idle", "actions", "host_another_2.stats.cpu.idle",
		"some_long_name.with-other_stuff.in.it.and_whatnot"}
	pointsWritten := 0

	dbDir := "/tmp/chronos_db_test"
	os.RemoveAll(dbDir)
	pointsToWritePerSeries := 2000000
	pointsToDeletePerSeries := pointsToWritePerSeries / 2

	opts := levigo.NewOptions()
	hundregMegabytes := 100 * 1048576
	opts.SetCache(levigo.NewLRUCache(hundregMegabytes))
	opts.SetCreateIfMissing(true)
	opts.SetBlockSize(262144)
	os.MkdirAll(dbDir, 0744)
	db, err := levigo.Open(dbDir, opts)
	if err != nil {
		panic(err)
	}
	writeOptions := levigo.NewWriteOptions()
	defer writeOptions.Close()

	// this initializes the ends of the keyspace so seeks don't mess with us.
	db.Put(writeOptions, []byte("a"), []byte(""))
	db.Put(writeOptions, []byte("z"), []byte(""))

	start := time.Now()
	for _, seriesName := range seriesNames {
		fmt.Println("Writing Series: ", seriesName)
		for i := 0; i < pointsToWritePerSeries; i++ {
			key := fmt.Sprintf("c~%s~1381456156%012d~%d", seriesName, i, rand.Int())
			db.Put(writeOptions, []byte(key), []byte(fmt.Sprintf("%f", rand.Float64())))
			pointsWritten += 1
			if pointsWritten%500000 == 0 {
				fmt.Println("COUNT: ", pointsWritten)
			}
		}
		out, _ := exec.Command("du", "-h", dbDir).Output()
		fmt.Println("SIZE: ", string(out))
	}
	diff := time.Now().Sub(start)
	fmt.Printf("DONE. %d points written in %s\n", pointsWritten, diff.String())
	out, _ := exec.Command("du", "-h", dbDir).Output()
	fmt.Println("SIZE: ", string(out))
	deleteCount := 0
	start = time.Now()
	ranges := make([]*levigo.Range, 0)
	for _, seriesName := range seriesNames {
		fmt.Println("Deleting: ", seriesName)
		count := 0
		key := fmt.Sprintf("c~%s~", seriesName)
		ro := levigo.NewReadOptions()
		it := db.NewIterator(ro)
		it.Seek([]byte(key))
		var lastKey []byte
		for it = it; it.Valid() && count < pointsToDeletePerSeries; it.Next() {
			lastKey = it.Key()
			db.Delete(writeOptions, it.Key())
			count += 1
			deleteCount += 1
		}
		it.Close()
		ro.Close()
		rangeToCompact := &levigo.Range{[]byte(key), lastKey}
		ranges = append(ranges, rangeToCompact)
		out, _ := exec.Command("du", "-h", dbDir).Output()
		fmt.Println("SIZE: ", string(out))
	}
	fmt.Printf("DONE. %d points deleted in %s\n", deleteCount, diff.String())

	fmt.Println("Starting compaction in background...")
	compactionDone := make(chan int)
	for _, r := range ranges {
		go func(r *levigo.Range) {
			db.CompactRange(*r)
			compactionDone <- 1
		}(r)
	}

	start = time.Now()
	fmt.Println("Writing new points...")
	pointsWritten = 0
	for _, seriesName := range seriesNames {
		fmt.Println("Writing Series: ", seriesName)
		for i := pointsToWritePerSeries; i < pointsToWritePerSeries*2; i++ {
			key := fmt.Sprintf("c~%s~1381456156%012d~%d", seriesName, i, rand.Int())
			db.Put(writeOptions, []byte(key), []byte(fmt.Sprintf("%f", rand.Float64())))
			pointsWritten += 1
			if pointsWritten%500000 == 0 {
				fmt.Println("COUNT: ", pointsWritten)
			}
		}
		out, _ := exec.Command("du", "-h", dbDir).Output()
		fmt.Println("SIZE: ", string(out))
	}

	fmt.Println("waiting for compaction to finish...")
	for i := 0; i < len(ranges); i++ {
		<-compactionDone
	}
	diff = time.Now().Sub(start)
	out, _ = exec.Command("du", "-h", dbDir).Output()
	fmt.Printf("DONE. %d points written during compaction in %s\n", deleteCount, diff.String())
	fmt.Println("SIZE: ", string(out))
}
<|endoftext|>


<commit_before>package dependency

import (
	"fmt"
	"io/ioutil"
	"net"
	"reflect"
	"testing"

	consulapi "github.com/hashicorp/consul/api"
	"github.com/hashicorp/consul/testutil"
	vaultapi "github.com/hashicorp/vault/api"
	"github.com/hashicorp/vault/http"
	"github.com/hashicorp/vault/logical"
	"github.com/hashicorp/vault/vault"
)

func TestDeepCopyAndSortTags(t *testing.T) {
	tags := []string{"hello", "world", "these", "are", "tags"}
	expected := []string{"are", "hello", "tags", "these", "world"}

	result := deepCopyAndSortTags(tags)
	if !reflect.DeepEqual(result, expected) {
		t.Errorf("expected %#v to be %#v", result, expected)
	}
}

// testConsulServer is a helper for creating a Consul server and returning the
// appropriate configuration to connect to it.
func testConsulServer(t *testing.T) (*ClientSet, *testutil.TestServer) {
	consul := testutil.NewTestServerConfig(t, func(c *testutil.TestServerConfig) {
		c.Stdout = ioutil.Discard
		c.Stderr = ioutil.Discard
	})

	config := consulapi.DefaultConfig()
	config.Address = consul.HTTPAddr
	client, err := consulapi.NewClient(config)
	if err != nil {
		defer consul.Stop()
		t.Fatal(err)
	}

	clients := NewClientSet()
	if err := clients.Add(client); err != nil {
		defer consul.Stop()
		t.Fatal(err)
	}

	return clients, consul
}

type vaultServer struct {
	Token string

	core *vault.Core
	ln   net.Listener
}

func (s *vaultServer) Stop() {
	s.ln.Close()
}

func (s *vaultServer) CreateSecret(path string, data map[string]interface{}) error {
	req := &logical.Request{
		Operation:   logical.WriteOperation,
		Path:        fmt.Sprintf("secret/%s", path),
		Data:        data,
		ClientToken: s.Token,
	}
	_, err := s.core.HandleRequest(req)
	return err
}

// testVaultServer is a helper for creating a Vault server and returning the
// appropriate client to connect to it.
func testVaultServer(t *testing.T) (*ClientSet, *vaultServer) {
	core, _, token := vault.TestCoreUnsealed(t)
	ln, addr := http.TestServer(t, core)

	config := vaultapi.DefaultConfig()
	config.Address = addr
	client, err := vaultapi.NewClient(config)
	if err != nil {
		defer ln.Close()
		t.Fatal(err)
	}

	client.SetToken(token)

	clients := NewClientSet()
	if err := clients.Add(client); err != nil {
		defer ln.Close()
		t.Fatal(err)
	}

	return clients, &vaultServer{Token: token, core: core, ln: ln}
}
<commit_msg>Run consul servers in parallel<commit_after>package dependency

import (
	"fmt"
	"io/ioutil"
	"net"
	"reflect"
	"testing"

	consulapi "github.com/hashicorp/consul/api"
	"github.com/hashicorp/consul/testutil"
	vaultapi "github.com/hashicorp/vault/api"
	"github.com/hashicorp/vault/http"
	"github.com/hashicorp/vault/logical"
	"github.com/hashicorp/vault/vault"
)

func TestDeepCopyAndSortTags(t *testing.T) {
	tags := []string{"hello", "world", "these", "are", "tags"}
	expected := []string{"are", "hello", "tags", "these", "world"}

	result := deepCopyAndSortTags(tags)
	if !reflect.DeepEqual(result, expected) {
		t.Errorf("expected %#v to be %#v", result, expected)
	}
}

// testConsulServer is a helper for creating a Consul server and returning the
// appropriate configuration to connect to it.
func testConsulServer(t *testing.T) (*ClientSet, *testutil.TestServer) {
	t.Parallel()

	consul := testutil.NewTestServerConfig(t, func(c *testutil.TestServerConfig) {
		c.Stdout = ioutil.Discard
		c.Stderr = ioutil.Discard
	})

	config := consulapi.DefaultConfig()
	config.Address = consul.HTTPAddr
	client, err := consulapi.NewClient(config)
	if err != nil {
		defer consul.Stop()
		t.Fatal(err)
	}

	clients := NewClientSet()
	if err := clients.Add(client); err != nil {
		defer consul.Stop()
		t.Fatal(err)
	}

	return clients, consul
}

type vaultServer struct {
	Token string

	core *vault.Core
	ln   net.Listener
}

func (s *vaultServer) Stop() {
	s.ln.Close()
}

func (s *vaultServer) CreateSecret(path string, data map[string]interface{}) error {
	req := &logical.Request{
		Operation:   logical.WriteOperation,
		Path:        fmt.Sprintf("secret/%s", path),
		Data:        data,
		ClientToken: s.Token,
	}
	_, err := s.core.HandleRequest(req)
	return err
}

// testVaultServer is a helper for creating a Vault server and returning the
// appropriate client to connect to it.
func testVaultServer(t *testing.T) (*ClientSet, *vaultServer) {
	core, _, token := vault.TestCoreUnsealed(t)
	ln, addr := http.TestServer(t, core)

	config := vaultapi.DefaultConfig()
	config.Address = addr
	client, err := vaultapi.NewClient(config)
	if err != nil {
		defer ln.Close()
		t.Fatal(err)
	}

	client.SetToken(token)

	clients := NewClientSet()
	if err := clients.Add(client); err != nil {
		defer ln.Close()
		t.Fatal(err)
	}

	return clients, &vaultServer{Token: token, core: core, ln: ln}
}
<|endoftext|>


<commit_before>package network

import (
	"common"
	"encoding/json"
	"log"
	"net"
)

type NetworkMultiplexer struct {
	in            chan common.NetworkMessage
	out           chan idHandler
	hosts         map[string][]common.NetworkMessageHandler
	createdServer bool
}

type idHandler struct {
	handler common.NetworkMessageHandler
	SwarmId string
}

func NewNetworkMultiplexer() common.NetworkMultiplexer {
	m := new(NetworkMultiplexer)
	m.in = make(chan common.NetworkMessage)
	m.out = make(chan idHandler)
	m.hosts = make(map[string][]common.NetworkMessageHandler)
	m.createdServer = false
	go m.listen()
	return m
}

func (m *NetworkMultiplexer) listen() {
	for {
		select {
		case c := <-m.out:
			m.hosts[c.SwarmId] = append(m.hosts[c.SwarmId], c.handler)
		case o := <-m.in:
			log.Println("MULTI: Transaction ", o, "to be sent to", len(m.hosts[o.SwarmId]))
			for _, s := range m.hosts[o.SwarmId] {
				go s.HandleNetworkMessage(o)
			}
		}
	}
}

func (m *NetworkMultiplexer) AddListener(SwarmId string, c common.NetworkMessageHandler) {
	m.out <- idHandler{c, SwarmId}
}

func (m *NetworkMultiplexer) SendNetworkMessage(o common.NetworkMessage) {
	m.in <- o
}

func (m *NetworkMultiplexer) Listen(addr string) {

	ln, err := net.Listen("tcp", addr)
	if err != nil {
		log.Println("MULTI: ERROR COULD NOT CREATE SERVER WITH ADDRESS:", addr)
		log.Fatal("MULTI ERROR MESSAGE:", err)
	}

	log.Println("MULTI: SUCCESSFULLY CREATED ADDRESS: addr")
	log.Println("MULTI: LISTENING FOR CONNECTORS")

	for {
		conn, err := ln.Accept()
		if err != nil {
			// Or it might be conn.LocalAddr().String(), unsure which to use
			log.Println("MULTI: ERROR CONNECTION REFUSED FOR ADDRESS:", conn.RemoteAddr().String())
			log.Println("MULTI ERROR MESSAGE:", err)
			continue
		}
		log.Println("MULTI: CONNECTED TO ADDRESS: ", conn.RemoteAddr().String())
		log.Println("MULTI: SENDING MESSAGE TO ADDRESS: ", conn.RemoteAddr().String())

		msg := "TESTING CONNECTION WITH " + addr
		b, err := json.Marshal(msg)
		if err != nil {
			log.Println("MULTI: ERROR CANNOT ENCODE TEST MESSAGE TO:", conn.RemoteAddr().String())
			log.Println("MULTI ERROR MESSAGE:", err)
			continue
		}
		_, err = conn.Write(b)
		if err != nil {
			log.Println("MULTI: ERROR COULD NOT SEND MESSAGE TO: ", conn.RemoteAddr().String())
			log.Println("MULTI ERROR MESSAGE:", err)
			continue
		}

		log.Println("MULTI: WAITING FOR RESPONSE FROM ADDRESS: ", conn.RemoteAddr().String())
		_, err = conn.Read(b)
		if err != nil {
			log.Println("MULTI: ERROR NO MESSAGE RECEIVED FROM: ", conn.RemoteAddr().String())
			log.Println("MULTI ERROR MESSAGE:", err)
			continue
		}
		err = json.Unmarshal(b, msg)
		if err != nil {
			log.Println("MULTI: ERROR CANNOT DECODE MESSAGE FROM: ", conn.RemoteAddr().String())
			log.Println("MULTI ERROR MESSAGE:", err)
			continue
		}
		log.Println("MULTI: MESSAGE RECEIVED FROM:", conn.RemoteAddr().String(), "READING OUT MESSAGE")
		log.Println(msg)

		defer conn.Close()
	}
	defer ln.Close()

}

func (m *NetworkMultiplexer) Connect(addr string) {
	conn, err := net.Dial("tcp", addr)

	if err != nil {
		log.Println("MULTI: ERROR CANNOT CONNECT TO SPECIFIED ADDRESS")
		log.Fatal("MULTI ERROR MESSAGE:", err)
	}

	log.Println("MULTI: CONNECTED TO ADDRESS:", addr)

	var (
		b   []byte
		msg string
	)

	_, err = conn.Read(b)
	if err != nil {
		panic(err)
	}

	err = json.Unmarshal(b, msg)
	if err != nil {
		panic(err)
	}
	log.Println(msg)

	msg = "MESSAGE FROM " + addr + " WAS RECEIVED"
	b, err = json.Marshal(msg)
	if err != nil {
		panic(err)
	}
	_, err = conn.Write(b)
	if err != nil {
		panic(err)
	}

	defer conn.Close()
}
<commit_msg>Much more convenient syntax in multiplexer.go<commit_after>package network

import (
	"common"
	"encoding/json"
	"log"
	"net"
)

type NetworkMultiplexer struct {
	in            chan common.NetworkMessage
	out           chan idHandler
	hosts         map[string][]common.NetworkMessageHandler
	createdServer bool
}

type idHandler struct {
	handler common.NetworkMessageHandler
	SwarmId string
}

func NewNetworkMultiplexer() common.NetworkMultiplexer {
	m := new(NetworkMultiplexer)
	m.in = make(chan common.NetworkMessage)
	m.out = make(chan idHandler)
	m.hosts = make(map[string][]common.NetworkMessageHandler)
	m.createdServer = false
	go m.listen()
	return m
}

func (m *NetworkMultiplexer) listen() {
	for {
		select {
		case c := <-m.out:
			m.hosts[c.SwarmId] = append(m.hosts[c.SwarmId], c.handler)
		case o := <-m.in:
			log.Println("MULTI: Transaction ", o, "to be sent to", len(m.hosts[o.SwarmId]))
			for _, s := range m.hosts[o.SwarmId] {
				go s.HandleNetworkMessage(o)
			}
		}
	}
}

func (m *NetworkMultiplexer) AddListener(SwarmId string, c common.NetworkMessageHandler) {
	m.out <- idHandler{c, SwarmId}
}

func (m *NetworkMultiplexer) SendNetworkMessage(o common.NetworkMessage) {
	m.in <- o
}

func (m *NetworkMultiplexer) Listen(addr string) {

	ln, err := net.Listen("tcp", addr)
	if err != nil {
		log.Println("MULTI: ERROR COULD NOT CREATE SERVER WITH ADDRESS:", addr)
		log.Fatal("MULTI ERROR MESSAGE:", err)
	}

	log.Println("MULTI: SUCCESSFULLY CREATED ADDRESS: addr")
	log.Println("MULTI: LISTENING FOR CONNECTORS")

	for {
		conn, err := ln.Accept()
		if err != nil {
			// Or it might be conn.LocalAddr().String(), unsure which to use
			log.Println("MULTI: ERROR CONNECTION REFUSED FOR ADDRESS:", conn.RemoteAddr().String())
			log.Println("MULTI ERROR MESSAGE:", err)
			continue
		}
		log.Println("MULTI: CONNECTED TO ADDRESS: ", conn.RemoteAddr().String())
		log.Println("MULTI: SENDING MESSAGE TO ADDRESS: ", conn.RemoteAddr().String())

		msg := "TESTING CONNECTION WITH " + addr
		en := json.NewEncoder(conn)
		en.Encode(msg)

		de := json.NewDecoder(conn)
		de.Decode(msg)

		log.Println("MULTI: MESSAGE RECEIVED FROM:", conn.RemoteAddr().String())
		log.Println(msg)

		defer conn.Close()
	}

	defer ln.Close()

}

func (m *NetworkMultiplexer) Connect(addr string) {
	conn, err := net.Dial("tcp", addr)

	if err != nil {
		log.Println("MULTI: ERROR CANNOT CONNECT TO SPECIFIED ADDRESS")
		log.Fatal("MULTI ERROR MESSAGE:", err)
	}

	log.Println("MULTI: CONNECTED TO ADDRESS:", addr)

	var msg string

	de := json.NewDecoder(conn)
	de.Decode(msg)

	log.Println("MULTI: MESSAGE RECEIVED FROM:", addr)
	log.Println(msg)

	msg = "MESSAGE CONFIRMED AS RECIEVED"

	en := json.NewEncoder(conn)
	en.Encode(msg)

	defer conn.Close()
}
<|endoftext|>


<commit_before>package terraform

import (
	"testing"
)

func TestStateAdd(t *testing.T) {
	cases := map[string]struct {
		Err      bool
		From, To string
		Value    interface{}
		One, Two *State
	}{
		"ModuleState => Module Addr (new)": {
			false,
			"",
			"module.foo",
			&ModuleState{
				Path: rootModulePath,
				Resources: map[string]*ResourceState{
					"test_instance.foo": &ResourceState{
						Type: "test_instance",
						Primary: &InstanceState{
							ID: "foo",
						},
					},

					"test_instance.bar": &ResourceState{
						Type: "test_instance",
						Primary: &InstanceState{
							ID: "foo",
						},
					},
				},
			},

			&State{},
			&State{
				Modules: []*ModuleState{
					&ModuleState{
						Path: []string{"root", "foo"},
						Resources: map[string]*ResourceState{
							"test_instance.foo": &ResourceState{
								Type: "test_instance",
								Primary: &InstanceState{
									ID: "foo",
								},
							},

							"test_instance.bar": &ResourceState{
								Type: "test_instance",
								Primary: &InstanceState{
									ID: "foo",
								},
							},
						},
					},
				},
			},
		},

		"ModuleState w/ outputs and deps => Module Addr (new)": {
			false,
			"",
			"module.foo",
			&ModuleState{
				Path: rootModulePath,
				Outputs: map[string]interface{}{
					"foo": "bar",
				},
				Dependencies: []string{"foo"},
				Resources: map[string]*ResourceState{
					"test_instance.foo": &ResourceState{
						Type: "test_instance",
						Primary: &InstanceState{
							ID: "foo",
						},
					},

					"test_instance.bar": &ResourceState{
						Type: "test_instance",
						Primary: &InstanceState{
							ID: "foo",
						},
					},
				},
			},

			&State{},
			&State{
				Modules: []*ModuleState{
					&ModuleState{
						Path: []string{"root", "foo"},
						Outputs: map[string]interface{}{
							"foo": "bar",
						},
						Dependencies: []string{"foo"},
						Resources: map[string]*ResourceState{
							"test_instance.foo": &ResourceState{
								Type: "test_instance",
								Primary: &InstanceState{
									ID: "foo",
								},
							},

							"test_instance.bar": &ResourceState{
								Type: "test_instance",
								Primary: &InstanceState{
									ID: "foo",
								},
							},
						},
					},
				},
			},
		},

		"ModuleState => Module Addr (existing)": {
			true,
			"",
			"module.foo",
			&ModuleState{},
			&State{
				Modules: []*ModuleState{
					&ModuleState{
						Path: []string{"root", "foo"},
						Resources: map[string]*ResourceState{
							"test_instance.baz": &ResourceState{
								Type: "test_instance",
								Primary: &InstanceState{
									ID: "foo",
								},
							},
						},
					},
				},
			},
			nil,
		},

		"ResourceState => Resource Addr (new)": {
			false,
			"aws_instance.bar",
			"aws_instance.foo",
			&ResourceState{
				Type: "test_instance",
				Primary: &InstanceState{
					ID: "foo",
				},
			},

			&State{},
			&State{
				Modules: []*ModuleState{
					&ModuleState{
						Path: []string{"root"},
						Resources: map[string]*ResourceState{
							"aws_instance.foo": &ResourceState{
								Type: "test_instance",
								Primary: &InstanceState{
									ID: "foo",
								},
							},
						},
					},
				},
			},
		},

		"ResourceState w/ deps, provider => Resource Addr (new)": {
			false,
			"aws_instance.bar",
			"aws_instance.foo",
			&ResourceState{
				Type:         "test_instance",
				Provider:     "foo",
				Dependencies: []string{"bar"},
				Primary: &InstanceState{
					ID: "foo",
				},
			},

			&State{},
			&State{
				Modules: []*ModuleState{
					&ModuleState{
						Path: []string{"root"},
						Resources: map[string]*ResourceState{
							"aws_instance.foo": &ResourceState{
								Type:         "test_instance",
								Provider:     "foo",
								Dependencies: []string{"bar"},
								Primary: &InstanceState{
									ID: "foo",
								},
							},
						},
					},
				},
			},
		},

		"ResourceState w/ tainted => Resource Addr (new)": {
			false,
			"aws_instance.bar",
			"aws_instance.foo",
			&ResourceState{
				Type: "test_instance",
				Tainted: []*InstanceState{
					&InstanceState{
						ID: "foo",
					},
				},
			},

			&State{},
			&State{
				Modules: []*ModuleState{
					&ModuleState{
						Path: []string{"root"},
						Resources: map[string]*ResourceState{
							"aws_instance.foo": &ResourceState{
								Type:    "test_instance",
								Primary: &InstanceState{},
								Tainted: []*InstanceState{
									&InstanceState{
										ID: "foo",
									},
								},
							},
						},
					},
				},
			},
		},

		"ResourceState => Resource Addr (existing)": {
			true,
			"aws_instance.bar",
			"aws_instance.foo",
			&ResourceState{
				Type: "test_instance",
				Primary: &InstanceState{
					ID: "foo",
				},
			},

			&State{
				Modules: []*ModuleState{
					&ModuleState{
						Path: []string{"root"},
						Resources: map[string]*ResourceState{
							"aws_instance.foo": &ResourceState{
								Type: "test_instance",
								Primary: &InstanceState{
									ID: "foo",
								},
							},
						},
					},
				},
			},
			nil,
		},

		"ResourceState => Module (new)": {
			false,
			"aws_instance.bar",
			"module.foo",
			&ResourceState{
				Type: "test_instance",
				Primary: &InstanceState{
					ID: "foo",
				},
			},

			&State{},
			&State{
				Modules: []*ModuleState{
					&ModuleState{
						Path: []string{"root", "foo"},
						Resources: map[string]*ResourceState{
							"aws_instance.bar": &ResourceState{
								Type: "test_instance",
								Primary: &InstanceState{
									ID: "foo",
								},
							},
						},
					},
				},
			},
		},

		"InstanceState => Resource (new)": {
			false,
			"aws_instance.bar.primary",
			"aws_instance.baz",
			&InstanceState{
				ID: "foo",
			},

			&State{},
			&State{
				Modules: []*ModuleState{
					&ModuleState{
						Path: []string{"root"},
						Resources: map[string]*ResourceState{
							"aws_instance.baz": &ResourceState{
								Type: "aws_instance",
								Primary: &InstanceState{
									ID: "foo",
								},
							},
						},
					},
				},
			},
		},

		"InstanceState => Module (new)": {
			false,
			"aws_instance.bar.primary",
			"module.foo",
			&InstanceState{
				ID: "foo",
			},

			&State{},
			&State{
				Modules: []*ModuleState{
					&ModuleState{
						Path: []string{"root", "foo"},
						Resources: map[string]*ResourceState{
							"aws_instance.bar": &ResourceState{
								Type: "aws_instance",
								Primary: &InstanceState{
									ID: "foo",
								},
							},
						},
					},
				},
			},
		},
	}

	for k, tc := range cases {
		// Make sure they're both initialized as normal
		tc.One.init()
		if tc.Two != nil {
			tc.Two.init()
		}

		// Add the value
		err := tc.One.Add(tc.From, tc.To, tc.Value)
		if (err != nil) != tc.Err {
			t.Fatalf("bad: %s\n\n%s", k, err)
		}
		if tc.Err {
			continue
		}

		// Prune them both to be sure
		tc.One.prune()
		tc.Two.prune()

		// Verify equality
		if !tc.One.Equal(tc.Two) {
			t.Fatalf("Bad: %s\n\n%#v\n\n%#v", k, tc.One, tc.Two)
			//t.Fatalf("Bad: %s\n\n%s\n\n%s", k, tc.One.String(), tc.Two.String())
		}
	}
}
<commit_msg>terraform: test moving a module to be nested<commit_after>package terraform

import (
	"testing"
)

func TestStateAdd(t *testing.T) {
	cases := map[string]struct {
		Err      bool
		From, To string
		Value    interface{}
		One, Two *State
	}{
		"ModuleState => Module Addr (new)": {
			false,
			"",
			"module.foo",
			&ModuleState{
				Path: rootModulePath,
				Resources: map[string]*ResourceState{
					"test_instance.foo": &ResourceState{
						Type: "test_instance",
						Primary: &InstanceState{
							ID: "foo",
						},
					},

					"test_instance.bar": &ResourceState{
						Type: "test_instance",
						Primary: &InstanceState{
							ID: "foo",
						},
					},
				},
			},

			&State{},
			&State{
				Modules: []*ModuleState{
					&ModuleState{
						Path: []string{"root", "foo"},
						Resources: map[string]*ResourceState{
							"test_instance.foo": &ResourceState{
								Type: "test_instance",
								Primary: &InstanceState{
									ID: "foo",
								},
							},

							"test_instance.bar": &ResourceState{
								Type: "test_instance",
								Primary: &InstanceState{
									ID: "foo",
								},
							},
						},
					},
				},
			},
		},

		"ModuleState => Nested Module Addr (new)": {
			false,
			"",
			"module.foo.module.bar",
			&ModuleState{
				Path: rootModulePath,
				Resources: map[string]*ResourceState{
					"test_instance.foo": &ResourceState{
						Type: "test_instance",
						Primary: &InstanceState{
							ID: "foo",
						},
					},

					"test_instance.bar": &ResourceState{
						Type: "test_instance",
						Primary: &InstanceState{
							ID: "foo",
						},
					},
				},
			},

			&State{},
			&State{
				Modules: []*ModuleState{
					&ModuleState{
						Path: []string{"root", "foo", "bar"},
						Resources: map[string]*ResourceState{
							"test_instance.foo": &ResourceState{
								Type: "test_instance",
								Primary: &InstanceState{
									ID: "foo",
								},
							},

							"test_instance.bar": &ResourceState{
								Type: "test_instance",
								Primary: &InstanceState{
									ID: "foo",
								},
							},
						},
					},
				},
			},
		},

		"ModuleState w/ outputs and deps => Module Addr (new)": {
			false,
			"",
			"module.foo",
			&ModuleState{
				Path: rootModulePath,
				Outputs: map[string]interface{}{
					"foo": "bar",
				},
				Dependencies: []string{"foo"},
				Resources: map[string]*ResourceState{
					"test_instance.foo": &ResourceState{
						Type: "test_instance",
						Primary: &InstanceState{
							ID: "foo",
						},
					},

					"test_instance.bar": &ResourceState{
						Type: "test_instance",
						Primary: &InstanceState{
							ID: "foo",
						},
					},
				},
			},

			&State{},
			&State{
				Modules: []*ModuleState{
					&ModuleState{
						Path: []string{"root", "foo"},
						Outputs: map[string]interface{}{
							"foo": "bar",
						},
						Dependencies: []string{"foo"},
						Resources: map[string]*ResourceState{
							"test_instance.foo": &ResourceState{
								Type: "test_instance",
								Primary: &InstanceState{
									ID: "foo",
								},
							},

							"test_instance.bar": &ResourceState{
								Type: "test_instance",
								Primary: &InstanceState{
									ID: "foo",
								},
							},
						},
					},
				},
			},
		},

		"ModuleState => Module Addr (existing)": {
			true,
			"",
			"module.foo",
			&ModuleState{},
			&State{
				Modules: []*ModuleState{
					&ModuleState{
						Path: []string{"root", "foo"},
						Resources: map[string]*ResourceState{
							"test_instance.baz": &ResourceState{
								Type: "test_instance",
								Primary: &InstanceState{
									ID: "foo",
								},
							},
						},
					},
				},
			},
			nil,
		},

		"ResourceState => Resource Addr (new)": {
			false,
			"aws_instance.bar",
			"aws_instance.foo",
			&ResourceState{
				Type: "test_instance",
				Primary: &InstanceState{
					ID: "foo",
				},
			},

			&State{},
			&State{
				Modules: []*ModuleState{
					&ModuleState{
						Path: []string{"root"},
						Resources: map[string]*ResourceState{
							"aws_instance.foo": &ResourceState{
								Type: "test_instance",
								Primary: &InstanceState{
									ID: "foo",
								},
							},
						},
					},
				},
			},
		},

		"ResourceState w/ deps, provider => Resource Addr (new)": {
			false,
			"aws_instance.bar",
			"aws_instance.foo",
			&ResourceState{
				Type:         "test_instance",
				Provider:     "foo",
				Dependencies: []string{"bar"},
				Primary: &InstanceState{
					ID: "foo",
				},
			},

			&State{},
			&State{
				Modules: []*ModuleState{
					&ModuleState{
						Path: []string{"root"},
						Resources: map[string]*ResourceState{
							"aws_instance.foo": &ResourceState{
								Type:         "test_instance",
								Provider:     "foo",
								Dependencies: []string{"bar"},
								Primary: &InstanceState{
									ID: "foo",
								},
							},
						},
					},
				},
			},
		},

		"ResourceState w/ tainted => Resource Addr (new)": {
			false,
			"aws_instance.bar",
			"aws_instance.foo",
			&ResourceState{
				Type: "test_instance",
				Tainted: []*InstanceState{
					&InstanceState{
						ID: "foo",
					},
				},
			},

			&State{},
			&State{
				Modules: []*ModuleState{
					&ModuleState{
						Path: []string{"root"},
						Resources: map[string]*ResourceState{
							"aws_instance.foo": &ResourceState{
								Type:    "test_instance",
								Primary: &InstanceState{},
								Tainted: []*InstanceState{
									&InstanceState{
										ID: "foo",
									},
								},
							},
						},
					},
				},
			},
		},

		"ResourceState => Resource Addr (existing)": {
			true,
			"aws_instance.bar",
			"aws_instance.foo",
			&ResourceState{
				Type: "test_instance",
				Primary: &InstanceState{
					ID: "foo",
				},
			},

			&State{
				Modules: []*ModuleState{
					&ModuleState{
						Path: []string{"root"},
						Resources: map[string]*ResourceState{
							"aws_instance.foo": &ResourceState{
								Type: "test_instance",
								Primary: &InstanceState{
									ID: "foo",
								},
							},
						},
					},
				},
			},
			nil,
		},

		"ResourceState => Module (new)": {
			false,
			"aws_instance.bar",
			"module.foo",
			&ResourceState{
				Type: "test_instance",
				Primary: &InstanceState{
					ID: "foo",
				},
			},

			&State{},
			&State{
				Modules: []*ModuleState{
					&ModuleState{
						Path: []string{"root", "foo"},
						Resources: map[string]*ResourceState{
							"aws_instance.bar": &ResourceState{
								Type: "test_instance",
								Primary: &InstanceState{
									ID: "foo",
								},
							},
						},
					},
				},
			},
		},

		"InstanceState => Resource (new)": {
			false,
			"aws_instance.bar.primary",
			"aws_instance.baz",
			&InstanceState{
				ID: "foo",
			},

			&State{},
			&State{
				Modules: []*ModuleState{
					&ModuleState{
						Path: []string{"root"},
						Resources: map[string]*ResourceState{
							"aws_instance.baz": &ResourceState{
								Type: "aws_instance",
								Primary: &InstanceState{
									ID: "foo",
								},
							},
						},
					},
				},
			},
		},

		"InstanceState => Module (new)": {
			false,
			"aws_instance.bar.primary",
			"module.foo",
			&InstanceState{
				ID: "foo",
			},

			&State{},
			&State{
				Modules: []*ModuleState{
					&ModuleState{
						Path: []string{"root", "foo"},
						Resources: map[string]*ResourceState{
							"aws_instance.bar": &ResourceState{
								Type: "aws_instance",
								Primary: &InstanceState{
									ID: "foo",
								},
							},
						},
					},
				},
			},
		},
	}

	for k, tc := range cases {
		// Make sure they're both initialized as normal
		tc.One.init()
		if tc.Two != nil {
			tc.Two.init()
		}

		// Add the value
		err := tc.One.Add(tc.From, tc.To, tc.Value)
		if (err != nil) != tc.Err {
			t.Fatalf("bad: %s\n\n%s", k, err)
		}
		if tc.Err {
			continue
		}

		// Prune them both to be sure
		tc.One.prune()
		tc.Two.prune()

		// Verify equality
		if !tc.One.Equal(tc.Two) {
			t.Fatalf("Bad: %s\n\n%#v\n\n%#v", k, tc.One, tc.Two)
			//t.Fatalf("Bad: %s\n\n%s\n\n%s", k, tc.One.String(), tc.Two.String())
		}
	}
}
<|endoftext|>


<commit_before>// Copyright 2019 The Hugo Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// Package page contains the core interfaces and types for the Page resource,
// a core component in Hugo.
package page

import (
	"html/template"

	"github.com/gohugoio/hugo/identity"

	"github.com/bep/gitmap"
	"github.com/gohugoio/hugo/config"
	"github.com/gohugoio/hugo/tpl"

	"github.com/gohugoio/hugo/common/maps"
	"github.com/gohugoio/hugo/compare"
	"github.com/gohugoio/hugo/hugofs/files"

	"github.com/gohugoio/hugo/navigation"
	"github.com/gohugoio/hugo/related"
	"github.com/gohugoio/hugo/resources/resource"
	"github.com/gohugoio/hugo/source"
)

// Clear clears any global package state.
func Clear() error {
	spc.clear()
	return nil
}

// AlternativeOutputFormatsProvider provides alternative output formats for a
// Page.
type AlternativeOutputFormatsProvider interface {
	// AlternativeOutputFormats gives the alternative output formats for the
	// current output.
	// Note that we use the term "alternative" and not "alternate" here, as it
	// does not necessarily replace the other format, it is an alternative representation.
	AlternativeOutputFormats() OutputFormats
}

// AuthorProvider provides author information.
type AuthorProvider interface {
	// Deprecated.
	Author() Author
	// Deprecated.
	Authors() AuthorList
}

// ChildCareProvider provides accessors to child resources.
type ChildCareProvider interface {
	Pages() Pages

	// RegularPages returns a list of pages of kind 'Page'.
	// In Hugo 0.57 we changed the Pages method so it returns all page
	// kinds, even sections. If you want the old behaviour, you can
	// use RegularPages.
	RegularPages() Pages

	// RegularPagesRecursive returns all regular pages below the current
	// section.
	RegularPagesRecursive() Pages

	Resources() resource.Resources
}

// ContentProvider provides the content related values for a Page.
type ContentProvider interface {
	Content() (any, error)
	Plain() string
	PlainWords() []string
	Summary() template.HTML
	Truncated() bool
	FuzzyWordCount() int
	WordCount() int
	ReadingTime() int
	Len() int
}

// FileProvider provides the source file.
type FileProvider interface {
	File() source.File
}

// GetPageProvider provides the GetPage method.
type GetPageProvider interface {
	// GetPage looks up a page for the given ref.
	//    {{ with .GetPage "blog" }}{{ .Title }}{{ end }}
	//
	// This will return nil when no page could be found, and will return
	// an error if the ref is ambiguous.
	GetPage(ref string) (Page, error)

	// GetPageWithTemplateInfo is for internal use only.
	GetPageWithTemplateInfo(info tpl.Info, ref string) (Page, error)
}

// GitInfoProvider provides Git info.
type GitInfoProvider interface {
	GitInfo() *gitmap.GitInfo
	CodeOwners() []string
}

// InSectionPositioner provides section navigation.
type InSectionPositioner interface {
	NextInSection() Page
	PrevInSection() Page
}

// InternalDependencies is considered an internal interface.
type InternalDependencies interface {
	// GetRelatedDocsHandler is for internal use only.
	GetRelatedDocsHandler() *RelatedDocsHandler
}

// OutputFormatsProvider provides the OutputFormats of a Page.
type OutputFormatsProvider interface {
	OutputFormats() OutputFormats
}

// Page is the core interface in Hugo.
type Page interface {
	ContentProvider
	TableOfContentsProvider
	PageWithoutContent
}

// PageMetaProvider provides page metadata, typically provided via front matter.
type PageMetaProvider interface {
	// The 4 page dates
	resource.Dated

	// Aliases forms the base for redirects generation.
	Aliases() []string

	// BundleType returns the bundle type: "leaf", "branch" or an empty string if it is none.
	// See https://gohugo.io/content-management/page-bundles/
	BundleType() files.ContentClass

	// A configured description.
	Description() string

	// Whether this is a draft. Will only be true if run with the --buildDrafts (-D) flag.
	Draft() bool

	// IsHome returns whether this is the home page.
	IsHome() bool

	// Configured keywords.
	Keywords() []string

	// The Page Kind. One of page, home, section, taxonomy, term.
	Kind() string

	// The configured layout to use to render this page. Typically set in front matter.
	Layout() string

	// The title used for links.
	LinkTitle() string

	// IsNode returns whether this is an item of one of the list types in Hugo,
	// i.e. not a regular content
	IsNode() bool

	// IsPage returns whether this is a regular content
	IsPage() bool

	// Param looks for a param in Page and then in Site config.
	Param(key any) (any, error)

	// Path gets the relative path, including file name and extension if relevant,
	// to the source of this Page. It will be relative to any content root.
	Path() string

	// This is just a temporary bridge method. Use Path in templates.
	Pathc() string

	// The slug, typically defined in front matter.
	Slug() string

	// This page's language code. Will be the same as the site's.
	Lang() string

	// IsSection returns whether this is a section
	IsSection() bool

	// Section returns the first path element below the content root.
	Section() string

	// Returns a slice of sections (directories if it's a file) to this
	// Page.
	SectionsEntries() []string

	// SectionsPath is SectionsEntries joined with a /.
	SectionsPath() string

	// Sitemap returns the sitemap configuration for this page.
	Sitemap() config.Sitemap

	// Type is a discriminator used to select layouts etc. It is typically set
	// in front matter, but will fall back to the root section.
	Type() string

	// The configured weight, used as the first sort value in the default
	// page sort if non-zero.
	Weight() int
}

// PageRenderProvider provides a way for a Page to render content.
type PageRenderProvider interface {
	Render(layout ...string) (template.HTML, error)
	RenderString(args ...any) (template.HTML, error)
}

// PageWithoutContent is the Page without any of the content methods.
type PageWithoutContent interface {
	RawContentProvider
	resource.Resource
	PageMetaProvider
	resource.LanguageProvider

	// For pages backed by a file.
	FileProvider

	GitInfoProvider

	// Output formats
	OutputFormatsProvider
	AlternativeOutputFormatsProvider

	// Tree navigation
	ChildCareProvider
	TreeProvider

	// Horizontal navigation
	InSectionPositioner
	PageRenderProvider
	PaginatorProvider
	Positioner
	navigation.PageMenusProvider

	// TODO(bep)
	AuthorProvider

	// Page lookups/refs
	GetPageProvider
	RefProvider

	resource.TranslationKeyProvider
	TranslationsProvider

	SitesProvider

	// Helper methods
	ShortcodeInfoProvider
	compare.Eqer

	// Scratch returns a Scratch that can be used to store temporary state.
	// Note that this Scratch gets reset on server rebuilds. See Store() for a variant that survives.
	maps.Scratcher

	// Store returns a Scratch that can be used to store temporary state.
	// In contrast to Scratch(), this Scratch is not reset on server rebuilds.
	Store() *maps.Scratch

	RelatedKeywordsProvider

	// GetTerms gets the terms of a given taxonomy,
	// e.g. GetTerms("categories")
	GetTerms(taxonomy string) Pages

	// Used in change/dependency tracking.
	identity.Provider

	DeprecatedWarningPageMethods
}

// Positioner provides next/prev navigation.
type Positioner interface {
	Next() Page
	Prev() Page

	// Deprecated: Use Prev. Will be removed in Hugo 0.57
	PrevPage() Page

	// Deprecated: Use Next. Will be removed in Hugo 0.57
	NextPage() Page
}

// RawContentProvider provides the raw, unprocessed content of the page.
type RawContentProvider interface {
	RawContent() string
}

// RefProvider provides the methods needed to create reflinks to pages.
type RefProvider interface {
	Ref(argsm map[string]any) (string, error)
	RefFrom(argsm map[string]any, source any) (string, error)
	RelRef(argsm map[string]any) (string, error)
	RelRefFrom(argsm map[string]any, source any) (string, error)
}

// RelatedKeywordsProvider allows a Page to be indexed.
type RelatedKeywordsProvider interface {
	// Make it indexable as a related.Document
	RelatedKeywords(cfg related.IndexConfig) ([]related.Keyword, error)
}

// ShortcodeInfoProvider provides info about the shortcodes in a Page.
type ShortcodeInfoProvider interface {
	// HasShortcode return whether the page has a shortcode with the given name.
	// This method is mainly motivated with the Hugo Docs site's need for a list
	// of pages with the `todo` shortcode in it.
	HasShortcode(name string) bool
}

// SitesProvider provide accessors to get sites.
type SitesProvider interface {
	Site() Site
	Sites() Sites
}

// TableOfContentsProvider provides the table of contents for a Page.
type TableOfContentsProvider interface {
	TableOfContents() template.HTML
}

// TranslationsProvider provides access to any translations.
type TranslationsProvider interface {

	// IsTranslated returns whether this content file is translated to
	// other language(s).
	IsTranslated() bool

	// AllTranslations returns all translations, including the current Page.
	AllTranslations() Pages

	// Translations returns the translations excluding the current Page.
	Translations() Pages
}

// TreeProvider provides section tree navigation.
type TreeProvider interface {

	// IsAncestor returns whether the current page is an ancestor of the given
	// Note that this method is not relevant for taxonomy lists and taxonomy terms pages.
	IsAncestor(other any) (bool, error)

	// CurrentSection returns the page's current section or the page itself if home or a section.
	// Note that this will return nil for pages that is not regular, home or section pages.
	CurrentSection() Page

	// IsDescendant returns whether the current page is a descendant of the given
	// Note that this method is not relevant for taxonomy lists and taxonomy terms pages.
	IsDescendant(other any) (bool, error)

	// FirstSection returns the section on level 1 below home, e.g. "/docs".
	// For the home page, this will return itself.
	FirstSection() Page

	// InSection returns whether the given page is in the current section.
	// Note that this will always return false for pages that are
	// not either regular, home or section pages.
	InSection(other any) (bool, error)

	// Parent returns a section's parent section or a page's section.
	// To get a section's subsections, see Page's Sections method.
	Parent() Page

	// Sections returns this section's subsections, if any.
	// Note that for non-sections, this method will always return an empty list.
	Sections() Pages

	// Page returns a reference to the Page itself, kept here mostly
	// for legacy reasons.
	Page() Page
}

// DeprecatedWarningPageMethods lists deprecated Page methods that will trigger
// a WARNING if invoked.
// This was added in Hugo 0.55.
type DeprecatedWarningPageMethods any // This was emptied in Hugo 0.93.0.

// Move here to trigger ERROR instead of WARNING.
// TODO(bep) create wrappers and put into the Page once it has some methods.
type DeprecatedErrorPageMethods any
<commit_msg>resources/page: Mark some more interface methods as internal<commit_after>// Copyright 2019 The Hugo Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// Package page contains the core interfaces and types for the Page resource,
// a core component in Hugo.
package page

import (
	"html/template"

	"github.com/gohugoio/hugo/identity"

	"github.com/bep/gitmap"
	"github.com/gohugoio/hugo/config"
	"github.com/gohugoio/hugo/tpl"

	"github.com/gohugoio/hugo/common/maps"
	"github.com/gohugoio/hugo/compare"
	"github.com/gohugoio/hugo/hugofs/files"

	"github.com/gohugoio/hugo/navigation"
	"github.com/gohugoio/hugo/related"
	"github.com/gohugoio/hugo/resources/resource"
	"github.com/gohugoio/hugo/source"
)

// Clear clears any global package state.
func Clear() error {
	spc.clear()
	return nil
}

// AlternativeOutputFormatsProvider provides alternative output formats for a
// Page.
type AlternativeOutputFormatsProvider interface {
	// AlternativeOutputFormats gives the alternative output formats for the
	// current output.
	// Note that we use the term "alternative" and not "alternate" here, as it
	// does not necessarily replace the other format, it is an alternative representation.
	AlternativeOutputFormats() OutputFormats
}

// AuthorProvider provides author information.
type AuthorProvider interface {
	// Deprecated.
	Author() Author
	// Deprecated.
	Authors() AuthorList
}

// ChildCareProvider provides accessors to child resources.
type ChildCareProvider interface {
	Pages() Pages

	// RegularPages returns a list of pages of kind 'Page'.
	// In Hugo 0.57 we changed the Pages method so it returns all page
	// kinds, even sections. If you want the old behaviour, you can
	// use RegularPages.
	RegularPages() Pages

	// RegularPagesRecursive returns all regular pages below the current
	// section.
	RegularPagesRecursive() Pages

	Resources() resource.Resources
}

// ContentProvider provides the content related values for a Page.
type ContentProvider interface {
	Content() (any, error)
	Plain() string
	PlainWords() []string
	Summary() template.HTML
	Truncated() bool
	FuzzyWordCount() int
	WordCount() int
	ReadingTime() int
	Len() int
}

// FileProvider provides the source file.
type FileProvider interface {
	File() source.File
}

// GetPageProvider provides the GetPage method.
type GetPageProvider interface {
	// GetPage looks up a page for the given ref.
	//    {{ with .GetPage "blog" }}{{ .Title }}{{ end }}
	//
	// This will return nil when no page could be found, and will return
	// an error if the ref is ambiguous.
	GetPage(ref string) (Page, error)

	// GetPageWithTemplateInfo is for internal use only.
	GetPageWithTemplateInfo(info tpl.Info, ref string) (Page, error)
}

// GitInfoProvider provides Git info.
type GitInfoProvider interface {
	GitInfo() *gitmap.GitInfo
	CodeOwners() []string
}

// InSectionPositioner provides section navigation.
type InSectionPositioner interface {
	NextInSection() Page
	PrevInSection() Page
}

// InternalDependencies is considered an internal interface.
type InternalDependencies interface {
	// GetRelatedDocsHandler is for internal use only.
	GetRelatedDocsHandler() *RelatedDocsHandler
}

// OutputFormatsProvider provides the OutputFormats of a Page.
type OutputFormatsProvider interface {
	OutputFormats() OutputFormats
}

// Page is the core interface in Hugo.
type Page interface {
	ContentProvider
	TableOfContentsProvider
	PageWithoutContent
}

// PageMetaProvider provides page metadata, typically provided via front matter.
type PageMetaProvider interface {
	// The 4 page dates
	resource.Dated

	// Aliases forms the base for redirects generation.
	Aliases() []string

	// BundleType returns the bundle type: "leaf", "branch" or an empty string if it is none.
	// See https://gohugo.io/content-management/page-bundles/
	BundleType() files.ContentClass

	// A configured description.
	Description() string

	// Whether this is a draft. Will only be true if run with the --buildDrafts (-D) flag.
	Draft() bool

	// IsHome returns whether this is the home page.
	IsHome() bool

	// Configured keywords.
	Keywords() []string

	// The Page Kind. One of page, home, section, taxonomy, term.
	Kind() string

	// The configured layout to use to render this page. Typically set in front matter.
	Layout() string

	// The title used for links.
	LinkTitle() string

	// IsNode returns whether this is an item of one of the list types in Hugo,
	// i.e. not a regular content
	IsNode() bool

	// IsPage returns whether this is a regular content
	IsPage() bool

	// Param looks for a param in Page and then in Site config.
	Param(key any) (any, error)

	// Path gets the relative path, including file name and extension if relevant,
	// to the source of this Page. It will be relative to any content root.
	Path() string

	// This is just a temporary bridge method. Use Path in templates.
	// Pathc is for internal usage only.
	Pathc() string

	// The slug, typically defined in front matter.
	Slug() string

	// This page's language code. Will be the same as the site's.
	Lang() string

	// IsSection returns whether this is a section
	IsSection() bool

	// Section returns the first path element below the content root.
	Section() string

	// Returns a slice of sections (directories if it's a file) to this
	// Page.
	SectionsEntries() []string

	// SectionsPath is SectionsEntries joined with a /.
	SectionsPath() string

	// Sitemap returns the sitemap configuration for this page.
	Sitemap() config.Sitemap

	// Type is a discriminator used to select layouts etc. It is typically set
	// in front matter, but will fall back to the root section.
	Type() string

	// The configured weight, used as the first sort value in the default
	// page sort if non-zero.
	Weight() int
}

// PageRenderProvider provides a way for a Page to render content.
type PageRenderProvider interface {
	Render(layout ...string) (template.HTML, error)
	RenderString(args ...any) (template.HTML, error)
}

// PageWithoutContent is the Page without any of the content methods.
type PageWithoutContent interface {
	RawContentProvider
	resource.Resource
	PageMetaProvider
	resource.LanguageProvider

	// For pages backed by a file.
	FileProvider

	GitInfoProvider

	// Output formats
	OutputFormatsProvider
	AlternativeOutputFormatsProvider

	// Tree navigation
	ChildCareProvider
	TreeProvider

	// Horizontal navigation
	InSectionPositioner
	PageRenderProvider
	PaginatorProvider
	Positioner
	navigation.PageMenusProvider

	// TODO(bep)
	AuthorProvider

	// Page lookups/refs
	GetPageProvider
	RefProvider

	resource.TranslationKeyProvider
	TranslationsProvider

	SitesProvider

	// Helper methods
	ShortcodeInfoProvider
	compare.Eqer

	// Scratch returns a Scratch that can be used to store temporary state.
	// Note that this Scratch gets reset on server rebuilds. See Store() for a variant that survives.
	maps.Scratcher

	// Store returns a Scratch that can be used to store temporary state.
	// In contrast to Scratch(), this Scratch is not reset on server rebuilds.
	Store() *maps.Scratch

	RelatedKeywordsProvider

	// GetTerms gets the terms of a given taxonomy,
	// e.g. GetTerms("categories")
	GetTerms(taxonomy string) Pages

	// Used in change/dependency tracking.
	identity.Provider

	DeprecatedWarningPageMethods
}

// Positioner provides next/prev navigation.
type Positioner interface {
	Next() Page
	Prev() Page

	// Deprecated: Use Prev. Will be removed in Hugo 0.57
	PrevPage() Page

	// Deprecated: Use Next. Will be removed in Hugo 0.57
	NextPage() Page
}

// RawContentProvider provides the raw, unprocessed content of the page.
type RawContentProvider interface {
	RawContent() string
}

// RefProvider provides the methods needed to create reflinks to pages.
type RefProvider interface {
	Ref(argsm map[string]any) (string, error)

	// RefFrom is for internal use only.
	RefFrom(argsm map[string]any, source any) (string, error)

	RelRef(argsm map[string]any) (string, error)

	// RefFrom is for internal use only.
	RelRefFrom(argsm map[string]any, source any) (string, error)
}

// RelatedKeywordsProvider allows a Page to be indexed.
type RelatedKeywordsProvider interface {
	// Make it indexable as a related.Document
	// RelatedKeywords is meant for internal usage only.
	RelatedKeywords(cfg related.IndexConfig) ([]related.Keyword, error)
}

// ShortcodeInfoProvider provides info about the shortcodes in a Page.
type ShortcodeInfoProvider interface {
	// HasShortcode return whether the page has a shortcode with the given name.
	// This method is mainly motivated with the Hugo Docs site's need for a list
	// of pages with the `todo` shortcode in it.
	HasShortcode(name string) bool
}

// SitesProvider provide accessors to get sites.
type SitesProvider interface {
	Site() Site
	Sites() Sites
}

// TableOfContentsProvider provides the table of contents for a Page.
type TableOfContentsProvider interface {
	TableOfContents() template.HTML
}

// TranslationsProvider provides access to any translations.
type TranslationsProvider interface {

	// IsTranslated returns whether this content file is translated to
	// other language(s).
	IsTranslated() bool

	// AllTranslations returns all translations, including the current Page.
	AllTranslations() Pages

	// Translations returns the translations excluding the current Page.
	Translations() Pages
}

// TreeProvider provides section tree navigation.
type TreeProvider interface {

	// IsAncestor returns whether the current page is an ancestor of the given
	// Note that this method is not relevant for taxonomy lists and taxonomy terms pages.
	IsAncestor(other any) (bool, error)

	// CurrentSection returns the page's current section or the page itself if home or a section.
	// Note that this will return nil for pages that is not regular, home or section pages.
	CurrentSection() Page

	// IsDescendant returns whether the current page is a descendant of the given
	// Note that this method is not relevant for taxonomy lists and taxonomy terms pages.
	IsDescendant(other any) (bool, error)

	// FirstSection returns the section on level 1 below home, e.g. "/docs".
	// For the home page, this will return itself.
	FirstSection() Page

	// InSection returns whether the given page is in the current section.
	// Note that this will always return false for pages that are
	// not either regular, home or section pages.
	InSection(other any) (bool, error)

	// Parent returns a section's parent section or a page's section.
	// To get a section's subsections, see Page's Sections method.
	Parent() Page

	// Sections returns this section's subsections, if any.
	// Note that for non-sections, this method will always return an empty list.
	Sections() Pages

	// Page returns a reference to the Page itself, kept here mostly
	// for legacy reasons.
	Page() Page
}

// DeprecatedWarningPageMethods lists deprecated Page methods that will trigger
// a WARNING if invoked.
// This was added in Hugo 0.55.
type DeprecatedWarningPageMethods any // This was emptied in Hugo 0.93.0.

// Move here to trigger ERROR instead of WARNING.
// TODO(bep) create wrappers and put into the Page once it has some methods.
type DeprecatedErrorPageMethods any
<|endoftext|>


<commit_before>/**
 * Copyright 2017 IBM Corp.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package resources

import "github.com/jinzhu/gorm"

const (
	SpectrumScale    string = "spectrum-scale"
	SpectrumScaleNFS string = "spectrum-scale-nfs"
	SoftlayerNFS     string = "softlayer-nfs"
	SCBE             string = "scbe"
)

type UbiquityServerConfig struct {
	Port                int
	LogPath             string
	ConfigPath          string
	SpectrumScaleConfig SpectrumScaleConfig
	ScbeConfig          ScbeConfig
	BrokerConfig        BrokerConfig
	DefaultBackend      string
	LogLevel            string
}

// TODO we should consider to move dedicated backend structs to the backend resource file instead of this one.
type SpectrumScaleConfig struct {
	DefaultFilesystemName string
	NfsServerAddr         string
	SshConfig             SshConfig
	RestConfig            RestConfig
	ForceDelete           bool
}

type CredentialInfo struct {
	UserName string `json:"username"`
	Password string `json:"password"`
	Group    string `json:"group"`
}

type ConnectionInfo struct {
	CredentialInfo CredentialInfo
	Port           int
	ManagementIP   string
	SkipVerifySSL  bool
}

type ScbeConfig struct {
	ConfigPath           string // TODO consider to remove later
	ConnectionInfo       ConnectionInfo
	DefaultService       string // SCBE storage service to be used by default if not mentioned by plugin
	DefaultVolumeSize    string // The default volume size in case not specified by user
	UbiquityInstanceName string // Prefix for the volume name in the storage side (max length 15 char)

	DefaultFilesystemType string // The default filesystem type to create on new provisioned volume during attachment to the host
}

const UbiquityInstanceNameMaxSize = 15
const DefaultForScbeConfigParamDefaultVolumeSize = "1"    // if customer don't mention size, then the default is 1gb
const DefaultForScbeConfigParamDefaultFilesystem = "ext4" // if customer don't mention fstype, then the default is ext4
const PathToMountUbiquityBlockDevices = "/ubiquity/%s"    // %s is the WWN of the volume # TODO this should be moved to docker plugin side
const OptionNameForVolumeFsType = "fstype"                // the option name of the fstype and also the key in the volumeConfig

type SshConfig struct {
	User string
	Host string
	Port string
}

type RestConfig struct {
	Endpoint string
	User     string
	Password string
	Hostname string
}

type SpectrumNfsRemoteConfig struct {
	ClientConfig string
}

type BrokerConfig struct {
	ConfigPath string
	Port       int //for CF Service broker
}

type UbiquityPluginConfig struct {
	DockerPlugin            UbiquityDockerPluginConfig
	LogPath                 string
	UbiquityServer          UbiquityServerConnectionInfo
	SpectrumNfsRemoteConfig SpectrumNfsRemoteConfig
	ScbeRemoteConfig        ScbeRemoteConfig
	Backends                []string
	LogLevel                string
}

type UbiquityDockerPluginConfig struct {
	//Address          string
	Port             int
	PluginsDirectory string
}

type UbiquityServerConnectionInfo struct {
	Address string
	Port    int
}

type ScbeRemoteConfig struct {
	SkipRescanISCSI bool
}

//go:generate counterfeiter -o ../fakes/fake_storage_client.go . StorageClient

type StorageClient interface {
	Activate(activateRequest ActivateRequest) error
	CreateVolume(createVolumeRequest CreateVolumeRequest) error
	RemoveVolume(removeVolumeRequest RemoveVolumeRequest) error
	ListVolumes(listVolumeRequest ListVolumesRequest) ([]Volume, error)
	GetVolume(getVolumeRequest GetVolumeRequest) (Volume, error)
	GetVolumeConfig(getVolumeConfigRequest GetVolumeConfigRequest) (map[string]interface{}, error)
	Attach(attachRequest AttachRequest) (string, error)
	Detach(detachRequest DetachRequest) error
}

//go:generate counterfeiter -o ../fakes/fake_mounter.go . Mounter

type Mounter interface {
	Mount(mountRequest MountRequest) (string, error)
	Unmount(unmountRequest UnmountRequest) error
	ActionAfterDetach(request AfterDetachRequest) error
}

type ActivateRequest struct {
	Backends []string
	Opts     map[string]string
}

type CreateVolumeRequest struct {
	CredentialInfo CredentialInfo
	Name    string
	Backend string
	Opts    map[string]interface{}
}

type RemoveVolumeRequest struct {
	CredentialInfo CredentialInfo
	Name string
}

type ListVolumesRequest struct {
	CredentialInfo CredentialInfo
	//TODO add filter
	Backends []string
}

type AttachRequest struct {
	CredentialInfo CredentialInfo
	Name string
	Host string
}

type DetachRequest struct {
	CredentialInfo CredentialInfo
	Name string
	Host string
}
type GetVolumeRequest struct {
	CredentialInfo CredentialInfo
	Name string
}
type GetVolumeConfigRequest struct {
	CredentialInfo CredentialInfo
	Name string
}
type ActivateResponse struct {
	Implements []string
	Err        string
}

type GenericResponse struct {
	Err string
}

type MountRequest struct {
	Mountpoint   string
	VolumeConfig map[string]interface{}
}
type UnmountRequest struct {
	VolumeConfig map[string]interface{}
}
type AfterDetachRequest struct {
	VolumeConfig map[string]interface{}
}
type AttachResponse struct {
	Mountpoint string
	Err        string
}

type MountResponse struct {
	Mountpoint string
	Err        string
}

type GetResponse struct {
	Volume Volume
	Err    string
}

type DockerGetResponse struct {
	Volume map[string]interface{}
	Err    string
}

type Volume struct {
	gorm.Model
	Name       string
	Backend    string
	Mountpoint string
}

type GetConfigResponse struct {
	VolumeConfig map[string]interface{}
	Err          string
}

type ListResponse struct {
	Volumes []Volume
	Err     string
}

type FlexVolumeResponse struct {
	Status  string `json:"status"`
	Message string `json:"message"`
	Device  string `json:"device"`
}

type FlexVolumeMountRequest struct {
	MountPath   string                 `json:"mountPath"`
	MountDevice string                 `json:"name"`
	Opts        map[string]interface{} `json:"opts"`
}

type FlexVolumeUnmountRequest struct {
	MountPath string `json:"mountPath"`
}

type FlexVolumeAttachRequest struct {
	Name string            `json:"name"`
	Host string            `json:"host"`
	Opts map[string]string `json:"opts"`
}

type FlexVolumeDetachRequest struct {
	Name string `json:"name"`
	Host string `json:"host"`
}
<commit_msg> #99: add credentials to all requests<commit_after>/**
 * Copyright 2017 IBM Corp.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package resources

import "github.com/jinzhu/gorm"

const (
	SpectrumScale    string = "spectrum-scale"
	SpectrumScaleNFS string = "spectrum-scale-nfs"
	SoftlayerNFS     string = "softlayer-nfs"
	SCBE             string = "scbe"
)

type UbiquityServerConfig struct {
	Port                int
	LogPath             string
	ConfigPath          string
	SpectrumScaleConfig SpectrumScaleConfig
	ScbeConfig          ScbeConfig
	BrokerConfig        BrokerConfig
	DefaultBackend      string
	LogLevel            string
}

// TODO we should consider to move dedicated backend structs to the backend resource file instead of this one.
type SpectrumScaleConfig struct {
	DefaultFilesystemName string
	NfsServerAddr         string
	SshConfig             SshConfig
	RestConfig            RestConfig
	ForceDelete           bool
}

type CredentialInfo struct {
	UserName string `json:"username"`
	Password string `json:"password"`
	Group    string `json:"group"`
}

type ConnectionInfo struct {
	CredentialInfo CredentialInfo
	Port           int
	ManagementIP   string
	SkipVerifySSL  bool
}

type ScbeConfig struct {
	ConfigPath           string // TODO consider to remove later
	ConnectionInfo       ConnectionInfo
	DefaultService       string // SCBE storage service to be used by default if not mentioned by plugin
	DefaultVolumeSize    string // The default volume size in case not specified by user
	UbiquityInstanceName string // Prefix for the volume name in the storage side (max length 15 char)

	DefaultFilesystemType string // The default filesystem type to create on new provisioned volume during attachment to the host
}

const UbiquityInstanceNameMaxSize = 15
const DefaultForScbeConfigParamDefaultVolumeSize = "1"    // if customer don't mention size, then the default is 1gb
const DefaultForScbeConfigParamDefaultFilesystem = "ext4" // if customer don't mention fstype, then the default is ext4
const PathToMountUbiquityBlockDevices = "/ubiquity/%s"    // %s is the WWN of the volume # TODO this should be moved to docker plugin side
const OptionNameForVolumeFsType = "fstype"                // the option name of the fstype and also the key in the volumeConfig

type SshConfig struct {
	User string
	Host string
	Port string
}

type RestConfig struct {
	Endpoint string
	User     string
	Password string
	Hostname string
}

type SpectrumNfsRemoteConfig struct {
	ClientConfig string
}

type BrokerConfig struct {
	ConfigPath string
	Port       int //for CF Service broker
}

type UbiquityPluginConfig struct {
	DockerPlugin            UbiquityDockerPluginConfig
	LogPath                 string
	UbiquityServer          UbiquityServerConnectionInfo
	SpectrumNfsRemoteConfig SpectrumNfsRemoteConfig
	ScbeRemoteConfig        ScbeRemoteConfig
	Backends                []string
	LogLevel                string
	CredentialInfo          CredentialInfo
}

type UbiquityDockerPluginConfig struct {
	//Address          string
	Port             int
	PluginsDirectory string
}

type UbiquityServerConnectionInfo struct {
	Address string
	Port    int
}

type ScbeRemoteConfig struct {
	SkipRescanISCSI bool
}

//go:generate counterfeiter -o ../fakes/fake_storage_client.go . StorageClient

type StorageClient interface {
	Activate(activateRequest ActivateRequest) error
	CreateVolume(createVolumeRequest CreateVolumeRequest) error
	RemoveVolume(removeVolumeRequest RemoveVolumeRequest) error
	ListVolumes(listVolumeRequest ListVolumesRequest) ([]Volume, error)
	GetVolume(getVolumeRequest GetVolumeRequest) (Volume, error)
	GetVolumeConfig(getVolumeConfigRequest GetVolumeConfigRequest) (map[string]interface{}, error)
	Attach(attachRequest AttachRequest) (string, error)
	Detach(detachRequest DetachRequest) error
}

//go:generate counterfeiter -o ../fakes/fake_mounter.go . Mounter

type Mounter interface {
	Mount(mountRequest MountRequest) (string, error)
	Unmount(unmountRequest UnmountRequest) error
	ActionAfterDetach(request AfterDetachRequest) error
}

type ActivateRequest struct {
	Backends []string
	Opts     map[string]string
}

type CreateVolumeRequest struct {
	CredentialInfo CredentialInfo
	Name    string
	Backend string
	Opts    map[string]interface{}
}

type RemoveVolumeRequest struct {
	CredentialInfo CredentialInfo
	Name string
}

type ListVolumesRequest struct {
	CredentialInfo CredentialInfo
	//TODO add filter
	Backends []string
}

type AttachRequest struct {
	CredentialInfo CredentialInfo
	Name string
	Host string
}

type DetachRequest struct {
	CredentialInfo CredentialInfo
	Name string
	Host string
}
type GetVolumeRequest struct {
	CredentialInfo CredentialInfo
	Name string
}
type GetVolumeConfigRequest struct {
	CredentialInfo CredentialInfo
	Name string
}
type ActivateResponse struct {
	Implements []string
	Err        string
}

type GenericResponse struct {
	Err string
}

type MountRequest struct {
	Mountpoint   string
	VolumeConfig map[string]interface{}
}
type UnmountRequest struct {
	VolumeConfig map[string]interface{}
}
type AfterDetachRequest struct {
	VolumeConfig map[string]interface{}
}
type AttachResponse struct {
	Mountpoint string
	Err        string
}

type MountResponse struct {
	Mountpoint string
	Err        string
}

type GetResponse struct {
	Volume Volume
	Err    string
}

type DockerGetResponse struct {
	Volume map[string]interface{}
	Err    string
}

type Volume struct {
	gorm.Model
	Name       string
	Backend    string
	Mountpoint string
}

type GetConfigResponse struct {
	VolumeConfig map[string]interface{}
	Err          string
}

type ListResponse struct {
	Volumes []Volume
	Err     string
}

type FlexVolumeResponse struct {
	Status  string `json:"status"`
	Message string `json:"message"`
	Device  string `json:"device"`
}

type FlexVolumeMountRequest struct {
	MountPath   string                 `json:"mountPath"`
	MountDevice string                 `json:"name"`
	Opts        map[string]interface{} `json:"opts"`
}

type FlexVolumeUnmountRequest struct {
	MountPath string `json:"mountPath"`
}

type FlexVolumeAttachRequest struct {
	Name string            `json:"name"`
	Host string            `json:"host"`
	Opts map[string]string `json:"opts"`
}

type FlexVolumeDetachRequest struct {
	Name string `json:"name"`
	Host string `json:"host"`
}
<|endoftext|>


<commit_before>package abc

type Doer interface {
	do()
}
<commit_msg>normjson: add Empty to abc<commit_after>package abc

type Doer interface {
	do()
}

type Empty interface{}
<|endoftext|>


<commit_before>// Copyright 2013 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.

package common

import (
	"github.com/juju/names"
)

// AuthFunc returns whether the given entity is available to some operation.
type AuthFunc func(tag string) bool

// GetAuthFunc returns an AuthFunc.
type GetAuthFunc func() (AuthFunc, error)

// Authorizer represents a value that can be asked for authorization
// information on its associated authenticated entity. It is
// implemented by an API server to allow an API implementation to ask
// questions about the client that is currently connected.
type Authorizer interface {
	// AuthMachineAgent returns whether the authenticated entity is a
	// machine agent.
	AuthMachineAgent() bool

	// AuthUnitAgent returns whether the authenticated entity is a
	// unit agent.
	AuthUnitAgent() bool

	// AuthOwner returns whether the authenticated entity is the same
	// as the given entity.
	// TODO(dfc) this should take a tag not a string
	AuthOwner(tag string) bool

	// AuthEnvironManager returns whether the authenticated entity is
	// a machine running the environment manager job.
	AuthEnvironManager() bool

	// AuthClient returns whether the authenticated entity
	// is a client user.
	AuthClient() bool

	// GetAuthTag returns the tag of the authenticated entity.
	GetAuthTag() names.Tag
}

// AuthEither returns an AuthFunc generator that returns an AuthFunc
// that accepts any tag authorized by either of its arguments.
func AuthEither(a, b GetAuthFunc) GetAuthFunc {
	return func() (AuthFunc, error) {
		f1, err := a()
		if err != nil {
			return nil, err
		}
		f2, err := b()
		if err != nil {
			return nil, err
		}
		return func(tag string) bool {
			return f1(tag) || f2(tag)
		}, nil
	}
}

// AuthAlways returns an authentication function that always returns true
func AuthAlways() GetAuthFunc {
	return func() (AuthFunc, error) {
		return func(tag string) bool {
			return true
		}, nil
	}
}

// AuthNever returns an authentication function that never returns true
func AuthNever() GetAuthFunc {
	return func() (AuthFunc, error) {
		return func(tag string) bool {
			return false
		}, nil
	}
}
<commit_msg>sentences end with a full stop.<commit_after>// Copyright 2013 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.

package common

import (
	"github.com/juju/names"
)

// AuthFunc returns whether the given entity is available to some operation.
type AuthFunc func(tag string) bool

// GetAuthFunc returns an AuthFunc.
type GetAuthFunc func() (AuthFunc, error)

// Authorizer represents a value that can be asked for authorization
// information on its associated authenticated entity. It is
// implemented by an API server to allow an API implementation to ask
// questions about the client that is currently connected.
type Authorizer interface {
	// AuthMachineAgent returns whether the authenticated entity is a
	// machine agent.
	AuthMachineAgent() bool

	// AuthUnitAgent returns whether the authenticated entity is a
	// unit agent.
	AuthUnitAgent() bool

	// AuthOwner returns whether the authenticated entity is the same
	// as the given entity.
	// TODO(dfc) this should take a tag not a string
	AuthOwner(tag string) bool

	// AuthEnvironManager returns whether the authenticated entity is
	// a machine running the environment manager job.
	AuthEnvironManager() bool

	// AuthClient returns whether the authenticated entity
	// is a client user.
	AuthClient() bool

	// GetAuthTag returns the tag of the authenticated entity.
	GetAuthTag() names.Tag
}

// AuthEither returns an AuthFunc generator that returns an AuthFunc
// that accepts any tag authorized by either of its arguments.
func AuthEither(a, b GetAuthFunc) GetAuthFunc {
	return func() (AuthFunc, error) {
		f1, err := a()
		if err != nil {
			return nil, err
		}
		f2, err := b()
		if err != nil {
			return nil, err
		}
		return func(tag string) bool {
			return f1(tag) || f2(tag)
		}, nil
	}
}

// AuthAlways returns an authentication function that always returns true.
func AuthAlways() GetAuthFunc {
	return func() (AuthFunc, error) {
		return func(tag string) bool {
			return true
		}, nil
	}
}

// AuthNever returns an authentication function that never returns true.
func AuthNever() GetAuthFunc {
	return func() (AuthFunc, error) {
		return func(tag string) bool {
			return false
		}, nil
	}
}
<|endoftext|>


<commit_before>/*
   Copyright 2017 Simon J Mudd

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/

/*

Package collection holds routines for collecting "high frequency"
metrics and handling their auto-expiry based on a configured retention
time. This becomes more interesting as the number of MySQL servers
monitored by orchestrator increases.

Most monitoring systems look at different metrics over a period
like 1, 10, 30 or 60 seconds but even at second resolution orchestrator
may have polled a number of servers.

It can be helpful to collect the raw values, and then allow external
monitoring to pull via an http api call either pre-cooked aggregate
data or the raw data for custom analysis over the period requested.

This is expected to be used for the following types of metric:

* discovery metrics (time to poll a MySQL server and collect status)
* queue metrics (statistics within the discovery queue itself)
* query metrics (statistics on the number of queries made to the
  backend MySQL database)

Orchestrator code can just add a new metric without worrying about
removing it later, and other code which serves API requests can
pull out the data when needed for the requested time period.

For current metrics two api urls have been provided: one provides
the raw data and the other one provides a single set of aggregate
data which is suitable for easy collection by monitoring systems.

Expiry is triggered by default if the collection is created via
CreateOrReturnCollection() and uses an expiry period of
DiscoveryCollectionRetentionSeconds. It can also be enabled by
calling StartAutoExpiration() after setting the required expire
period with SetExpirePeriod().

This will trigger periodic calls (every second) to ensure the removal
of metrics which have passed the time specified. Not enabling expiry
will mean data is collected but never freed which will make
orchestrator run out of memory eventually.

Current code uses DiscoveryCollectionRetentionSeconds as the
time to keep metric data.

*/
package collection

import (
	"errors"
	"sync"
	"time"

	//	"vitess.io/vitess/go/vt/orchestrator/external/golib/log"

	"vitess.io/vitess/go/vt/orchestrator/config"
)

// Metric is an interface containing a metric
type Metric interface {
	When() time.Time // when the metric was taken
}

// Collection contains a collection of Metrics
type Collection struct {
	sync.Mutex        // for locking the structure
	monitoring   bool // am I monitoring the queue size?
	collection   []Metric
	done         chan struct{} // to indicate that we are finishing expiry processing
	expirePeriod time.Duration // time to keep the collection information for
}

// hard-coded at every second
const defaultExpireTickerPeriod = time.Second

// backendMetricCollection contains the last N backend "channelled"
// metrics which can then be accessed via an API call for monitoring.
var (
	namedCollection     map[string](*Collection)
	namedCollectionLock sync.Mutex
)

func init() {
	namedCollection = make(map[string](*Collection))
}

// StopMonitoring stops monitoring all the collections
func StopMonitoring() {
	for _, q := range namedCollection {
		q.StopAutoExpiration()
	}
}

// CreateOrReturnCollection allows for creation of a new collection or
// returning a pointer to an existing one given the name. This allows access
// to the data structure from the api interface (http/api.go) and also when writing (inst).
func CreateOrReturnCollection(name string) *Collection {
	namedCollectionLock.Lock()
	defer namedCollectionLock.Unlock()
	if q, found := namedCollection[name]; found {
		return q
	}

	qmc := &Collection{
		collection: nil,
		done:       make(chan struct{}),
		// WARNING: use a different configuration name
		expirePeriod: time.Duration(config.Config.DiscoveryCollectionRetentionSeconds) * time.Second,
	}
	go qmc.StartAutoExpiration()

	namedCollection[name] = qmc

	return qmc
}

// SetExpirePeriod determines after how long the collected data should be removed
func (c *Collection) SetExpirePeriod(duration time.Duration) {
	c.Lock()
	defer c.Unlock()

	c.expirePeriod = duration
}

// ExpirePeriod returns the currently configured expiration period
func (c *Collection) ExpirePeriod() time.Duration {
	c.Lock()
	defer c.Unlock()
	return c.expirePeriod
}

// StopAutoExpiration prepares to stop by terminating the auto-expiration process
func (c *Collection) StopAutoExpiration() {
	if c == nil {
		return
	}
	c.Lock()
	if !c.monitoring {
		c.Unlock()
		return
	}
	c.monitoring = false
	c.Unlock()

	// no locking here deliberately
	c.done <- struct{}{}
}

// StartAutoExpiration initiates the auto expiry procedure which
// periodically checks for metrics in the collection which need to
// be expired according to bc.ExpirePeriod.
func (c *Collection) StartAutoExpiration() {
	if c == nil {
		return
	}
	c.Lock()
	if c.monitoring {
		c.Unlock()
		return
	}
	c.monitoring = true
	c.Unlock()

	// log.Infof("StartAutoExpiration: %p with expirePeriod: %v", c, c.expirePeriod)
	ticker := time.NewTicker(defaultExpireTickerPeriod)

	for {
		select {
		case <-ticker.C: // do the periodic expiry
			c.removeBefore(time.Now().Add(-c.expirePeriod))
		case <-c.done: // stop the ticker and return
			ticker.Stop()
			return
		}
	}
}

// Metrics returns a slice containing all the metric values
func (c *Collection) Metrics() []Metric {
	if c == nil {
		return nil
	}
	c.Lock()
	defer c.Unlock()

	if len(c.collection) == 0 {
		return nil // nothing to return
	}
	return c.collection
}

// Since returns the Metrics on or after the given time. We assume
// the metrics are stored in ascending time.
// Iterate backwards until we reach the first value before the given time
// or the end of the array.
func (c *Collection) Since(t time.Time) ([]Metric, error) {
	if c == nil {
		return nil, errors.New("Collection.Since: c == nil")
	}
	c.Lock()
	defer c.Unlock()
	if len(c.collection) == 0 {
		return nil, nil // nothing to return
	}
	last := len(c.collection)
	first := last - 1

	done := false
	for !done {
		if c.collection[first].When().After(t) || c.collection[first].When().Equal(t) {
			if first == 0 {
				break // as can't go lower
			}
			first--
		} else {
			if first != last {
				first++ // go back one (except if we're already at the end)
			}
			break
		}
	}

	return c.collection[first:last], nil
}

// removeBefore is called by StartAutoExpiration and removes collection values
// before the given time.
func (c *Collection) removeBefore(t time.Time) error {
	if c == nil {
		return errors.New("Collection.removeBefore: c == nil")
	}
	c.Lock()
	defer c.Unlock()

	cLen := len(c.collection)
	if cLen == 0 {
		return nil // we have a collection but no data
	}
	// remove old data here.
	first := 0
	done := false
	for !done {
		if c.collection[first].When().Before(t) {
			first++
			if first == cLen {
				break
			}
		} else {
			first--
			break
		}
	}

	// get the interval we need.
	if first == len(c.collection) {
		c.collection = nil // remove all entries
	} else if first != -1 {
		c.collection = c.collection[first:]
	}
	return nil // no errors
}

// Append a new Metric to the existing collection
func (c *Collection) Append(m Metric) error {
	if c == nil {
		return errors.New("Collection.Append: c == nil")
	}
	c.Lock()         //nolint SA5011: possible nil pointer dereference
	defer c.Unlock() //nolint SA5011: possible nil pointer dereference
	// we don't want to add nil metrics
	if c == nil { //nolint SA5011: redundant nil pointer check - maybe this was supposed to check if m == nil ?
		return errors.New("Collection.Append: c == nil")
	}
	c.collection = append(c.collection, m)

	return nil
}
<commit_msg>[orchestrator] correctly check against `m == nil`, remove unneeded nolint directives<commit_after>/*
   Copyright 2017 Simon J Mudd

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/

/*

Package collection holds routines for collecting "high frequency"
metrics and handling their auto-expiry based on a configured retention
time. This becomes more interesting as the number of MySQL servers
monitored by orchestrator increases.

Most monitoring systems look at different metrics over a period
like 1, 10, 30 or 60 seconds but even at second resolution orchestrator
may have polled a number of servers.

It can be helpful to collect the raw values, and then allow external
monitoring to pull via an http api call either pre-cooked aggregate
data or the raw data for custom analysis over the period requested.

This is expected to be used for the following types of metric:

* discovery metrics (time to poll a MySQL server and collect status)
* queue metrics (statistics within the discovery queue itself)
* query metrics (statistics on the number of queries made to the
  backend MySQL database)

Orchestrator code can just add a new metric without worrying about
removing it later, and other code which serves API requests can
pull out the data when needed for the requested time period.

For current metrics two api urls have been provided: one provides
the raw data and the other one provides a single set of aggregate
data which is suitable for easy collection by monitoring systems.

Expiry is triggered by default if the collection is created via
CreateOrReturnCollection() and uses an expiry period of
DiscoveryCollectionRetentionSeconds. It can also be enabled by
calling StartAutoExpiration() after setting the required expire
period with SetExpirePeriod().

This will trigger periodic calls (every second) to ensure the removal
of metrics which have passed the time specified. Not enabling expiry
will mean data is collected but never freed which will make
orchestrator run out of memory eventually.

Current code uses DiscoveryCollectionRetentionSeconds as the
time to keep metric data.

*/
package collection

import (
	"errors"
	"sync"
	"time"

	//	"vitess.io/vitess/go/vt/orchestrator/external/golib/log"

	"vitess.io/vitess/go/vt/orchestrator/config"
)

// Metric is an interface containing a metric
type Metric interface {
	When() time.Time // when the metric was taken
}

// Collection contains a collection of Metrics
type Collection struct {
	sync.Mutex        // for locking the structure
	monitoring   bool // am I monitoring the queue size?
	collection   []Metric
	done         chan struct{} // to indicate that we are finishing expiry processing
	expirePeriod time.Duration // time to keep the collection information for
}

// hard-coded at every second
const defaultExpireTickerPeriod = time.Second

// backendMetricCollection contains the last N backend "channelled"
// metrics which can then be accessed via an API call for monitoring.
var (
	namedCollection     map[string](*Collection)
	namedCollectionLock sync.Mutex
)

func init() {
	namedCollection = make(map[string](*Collection))
}

// StopMonitoring stops monitoring all the collections
func StopMonitoring() {
	for _, q := range namedCollection {
		q.StopAutoExpiration()
	}
}

// CreateOrReturnCollection allows for creation of a new collection or
// returning a pointer to an existing one given the name. This allows access
// to the data structure from the api interface (http/api.go) and also when writing (inst).
func CreateOrReturnCollection(name string) *Collection {
	namedCollectionLock.Lock()
	defer namedCollectionLock.Unlock()
	if q, found := namedCollection[name]; found {
		return q
	}

	qmc := &Collection{
		collection: nil,
		done:       make(chan struct{}),
		// WARNING: use a different configuration name
		expirePeriod: time.Duration(config.Config.DiscoveryCollectionRetentionSeconds) * time.Second,
	}
	go qmc.StartAutoExpiration()

	namedCollection[name] = qmc

	return qmc
}

// SetExpirePeriod determines after how long the collected data should be removed
func (c *Collection) SetExpirePeriod(duration time.Duration) {
	c.Lock()
	defer c.Unlock()

	c.expirePeriod = duration
}

// ExpirePeriod returns the currently configured expiration period
func (c *Collection) ExpirePeriod() time.Duration {
	c.Lock()
	defer c.Unlock()
	return c.expirePeriod
}

// StopAutoExpiration prepares to stop by terminating the auto-expiration process
func (c *Collection) StopAutoExpiration() {
	if c == nil {
		return
	}
	c.Lock()
	if !c.monitoring {
		c.Unlock()
		return
	}
	c.monitoring = false
	c.Unlock()

	// no locking here deliberately
	c.done <- struct{}{}
}

// StartAutoExpiration initiates the auto expiry procedure which
// periodically checks for metrics in the collection which need to
// be expired according to bc.ExpirePeriod.
func (c *Collection) StartAutoExpiration() {
	if c == nil {
		return
	}
	c.Lock()
	if c.monitoring {
		c.Unlock()
		return
	}
	c.monitoring = true
	c.Unlock()

	// log.Infof("StartAutoExpiration: %p with expirePeriod: %v", c, c.expirePeriod)
	ticker := time.NewTicker(defaultExpireTickerPeriod)

	for {
		select {
		case <-ticker.C: // do the periodic expiry
			c.removeBefore(time.Now().Add(-c.expirePeriod))
		case <-c.done: // stop the ticker and return
			ticker.Stop()
			return
		}
	}
}

// Metrics returns a slice containing all the metric values
func (c *Collection) Metrics() []Metric {
	if c == nil {
		return nil
	}
	c.Lock()
	defer c.Unlock()

	if len(c.collection) == 0 {
		return nil // nothing to return
	}
	return c.collection
}

// Since returns the Metrics on or after the given time. We assume
// the metrics are stored in ascending time.
// Iterate backwards until we reach the first value before the given time
// or the end of the array.
func (c *Collection) Since(t time.Time) ([]Metric, error) {
	if c == nil {
		return nil, errors.New("Collection.Since: c == nil")
	}
	c.Lock()
	defer c.Unlock()
	if len(c.collection) == 0 {
		return nil, nil // nothing to return
	}
	last := len(c.collection)
	first := last - 1

	done := false
	for !done {
		if c.collection[first].When().After(t) || c.collection[first].When().Equal(t) {
			if first == 0 {
				break // as can't go lower
			}
			first--
		} else {
			if first != last {
				first++ // go back one (except if we're already at the end)
			}
			break
		}
	}

	return c.collection[first:last], nil
}

// removeBefore is called by StartAutoExpiration and removes collection values
// before the given time.
func (c *Collection) removeBefore(t time.Time) error {
	if c == nil {
		return errors.New("Collection.removeBefore: c == nil")
	}
	c.Lock()
	defer c.Unlock()

	cLen := len(c.collection)
	if cLen == 0 {
		return nil // we have a collection but no data
	}
	// remove old data here.
	first := 0
	done := false
	for !done {
		if c.collection[first].When().Before(t) {
			first++
			if first == cLen {
				break
			}
		} else {
			first--
			break
		}
	}

	// get the interval we need.
	if first == len(c.collection) {
		c.collection = nil // remove all entries
	} else if first != -1 {
		c.collection = c.collection[first:]
	}
	return nil // no errors
}

// Append a new Metric to the existing collection
func (c *Collection) Append(m Metric) error {
	if c == nil {
		return errors.New("Collection.Append: c == nil")
	}
	c.Lock()
	defer c.Unlock()
	// we don't want to add nil metrics
	if m == nil {
		return errors.New("Collection.Append: m == nil")
	}
	c.collection = append(c.collection, m)

	return nil
}
<|endoftext|>


<commit_before>package v1

import (
	"database/sql"

	"git.zxq.co/ripple/rippleapi/common"
)

type docFile struct {
	ID      int    `json:"id"`
	DocName string `json:"doc_name"`
	Public  bool   `json:"public"`
	IsRule  bool   `json:"is_rule"`
}

type docResponse struct {
	common.ResponseBase
	Files []docFile `json:"files"`
}

// DocGET retrieves a list of documentation files.
func DocGET(md common.MethodData) common.CodeMessager {
	var wc string
	if md.User.TokenPrivileges&common.PrivilegeBlog == 0 || md.Query("public") == "1" {
		wc = "WHERE public = '1'"
	}
	rows, err := md.DB.Query("SELECT id, doc_name, public, is_rule FROM docs " + wc)
	if err != nil {
		md.Err(err)
		return Err500
	}
	var r docResponse
	for rows.Next() {
		var f docFile
		err := rows.Scan(&f.ID, &f.DocName, &f.Public, &f.IsRule)
		if err != nil {
			md.Err(err)
			continue
		}
		r.Files = append(r.Files, f)
	}
	r.Code = 200
	return r
}

type docContentResponse struct {
	common.ResponseBase
	Content string `json:"content"`
}

// DocContentGET retrieves the raw markdown file of a doc file
func DocContentGET(md common.MethodData) common.CodeMessager {
	docID := common.Int(md.Query("id"))
	if docID == 0 {
		return common.SimpleResponse(404, "Documentation file not found!")
	}
	var wc string
	if md.User.TokenPrivileges&common.PrivilegeBlog == 0 || md.Query("public") == "1" {
		wc = "AND public = '1'"
	}
	var r docContentResponse
	err := md.DB.QueryRow("SELECT doc_contents FROM docs WHERE id = ? "+wc+" LIMIT 1", docID).Scan(&r.Content)
	switch {
	case err == sql.ErrNoRows:
		r.Code = 404
		r.Message = "Documentation file not found!"
	case err != nil:
		md.Err(err)
		return Err500
	default:
		r.Code = 200
	}
	return r
}

// DocRulesGET gets the rules.
func DocRulesGET(md common.MethodData) common.CodeMessager {
	var r docContentResponse
	err := md.DB.QueryRow("SELECT doc_contents FROM docs WHERE is_rule = '1' LIMIT 1").Scan(&r.Content)
	const ruleFree = "# This Ripple instance is rule-free! Yay!"
	switch {
	case err == sql.ErrNoRows:
		r.Content = ruleFree
	case err != nil:
		md.Err(err)
		return Err500
	case len(r.Content) == 0:
		r.Content = ruleFree
	}
	r.Code = 200
	return r
}
<commit_msg>Include doc title in documentation content get<commit_after>package v1

import (
	"database/sql"

	"git.zxq.co/ripple/rippleapi/common"
)

type docFile struct {
	ID      int    `json:"id"`
	DocName string `json:"doc_name"`
	Public  bool   `json:"public"`
	IsRule  bool   `json:"is_rule"`
}

type docResponse struct {
	common.ResponseBase
	Files []docFile `json:"files"`
}

// DocGET retrieves a list of documentation files.
func DocGET(md common.MethodData) common.CodeMessager {
	var wc string
	if md.User.TokenPrivileges&common.PrivilegeBlog == 0 || md.Query("public") == "1" {
		wc = "WHERE public = '1'"
	}
	rows, err := md.DB.Query("SELECT id, doc_name, public, is_rule FROM docs " + wc)
	if err != nil {
		md.Err(err)
		return Err500
	}
	var r docResponse
	for rows.Next() {
		var f docFile
		err := rows.Scan(&f.ID, &f.DocName, &f.Public, &f.IsRule)
		if err != nil {
			md.Err(err)
			continue
		}
		r.Files = append(r.Files, f)
	}
	r.Code = 200
	return r
}

type docContentResponse struct {
	common.ResponseBase
	Title   string `json:"title"`
	Content string `json:"content"`
}

// DocContentGET retrieves the raw markdown file of a doc file
func DocContentGET(md common.MethodData) common.CodeMessager {
	docID := common.Int(md.Query("id"))
	if docID == 0 {
		return common.SimpleResponse(404, "Documentation file not found!")
	}
	var wc string
	if md.User.TokenPrivileges&common.PrivilegeBlog == 0 || md.Query("public") == "1" {
		wc = "AND public = '1'"
	}
	var r docContentResponse
	err := md.DB.QueryRow("SELECT doc_name, doc_contents FROM docs WHERE id = ? "+wc+" LIMIT 1", docID).Scan(&r.Title, &r.Content)
	switch {
	case err == sql.ErrNoRows:
		r.Code = 404
		r.Message = "Documentation file not found!"
	case err != nil:
		md.Err(err)
		return Err500
	default:
		r.Code = 200
	}
	return r
}

// DocRulesGET gets the rules.
func DocRulesGET(md common.MethodData) common.CodeMessager {
	var r docContentResponse
	err := md.DB.QueryRow("SELECT doc_contents FROM docs WHERE is_rule = '1' LIMIT 1").Scan(&r.Content)
	const ruleFree = "# This Ripple instance is rule-free! Yay!"
	switch {
	case err == sql.ErrNoRows:
		r.Content = ruleFree
	case err != nil:
		md.Err(err)
		return Err500
	case len(r.Content) == 0:
		r.Content = ruleFree
	}
	r.Code = 200
	return r
}
<|endoftext|>


<commit_before>package fp

import (
  "fmt"
  "net/http"

  "appengine"
  "appengine/urlfetch"
)

const BASE = "https://fp-facilitator.org/reg/"

func ipHandler(w http.ResponseWriter, r *http.Request) {
  fmt.Fprintf(w, "%s", r.RemoteAddr)
}

func regHandler(w http.ResponseWriter, r *http.Request) {
  c := appengine.NewContext(r)
  blob := r.URL.String()[5:]
  client := urlfetch.Client(c)
  _, err := client.Get(BASE + blob)
  if err != nil {
    http.Error(w, err.Error(), http.StatusInternalServerError)
    return
  }
  fmt.Fprintf(w, "Thanks.")
}

func init() {
  http.HandleFunc("/ip", ipHandler)
  http.HandleFunc("/reg/", regHandler)
}
<commit_msg>Use URL.Path<commit_after>package fp

import (
  "fmt"
  "net/http"

  "appengine"
  "appengine/urlfetch"
)

const BASE = "https://fp-facilitator.org/reg/"

func ipHandler(w http.ResponseWriter, r *http.Request) {
  fmt.Fprintf(w, "%s", r.RemoteAddr)
}

func regHandler(w http.ResponseWriter, r *http.Request) {
  c := appengine.NewContext(r)
  blob := r.URL.Path[5:]
  client := urlfetch.Client(c)
  _, err := client.Get(BASE + blob)
  if err != nil {
    http.Error(w, err.Error(), http.StatusInternalServerError)
    return
  }
  fmt.Fprintf(w, "Thanks.")
}

func init() {
  http.HandleFunc("/ip", ipHandler)
  http.HandleFunc("/reg/", regHandler)
}
<|endoftext|>


<commit_before>package assert

import (
	"errors"
	"fmt"
)

func NotNull(message string, value interface{}) (error) {
	if value == nil {
		return errors.New(fmt.Sprintf("%s, expect not null value", message))
	}
	return nil
}

func Null(message string, value interface{}) (error) {
	if value != nil {
		return errors.New(fmt.Sprintf("%s, expect null value", message))
	}
	return nil
}

func EqualsString(message string, expected string, value string) (error) {
	if expected != value {
		return errors.New(fmt.Sprintf("%s, expected '%s' but got '%s'", message, expected, value))
	}
	return nil
}

func EqualsInt(message string, expected int, value int) (error) {
	if expected != value {
		return errors.New(fmt.Sprintf("%s, expected %d but got %d", message, expected, value))
	}
	return nil
}

func True(message string, value bool) (error) {
	if value != true {
		return errors.New(fmt.Sprintf("%s, expected true but got false", message))
	}
	return nil
}

func False(message string, value bool) (error) {
	if value != false {
		return errors.New(fmt.Sprintf("%s, expected false but got true", message))
	}
	return nil
}
<commit_msg>fix test + format<commit_after>package assert

import (
	"errors"
	"fmt"
)

func NotNull(message string, value interface{}) error {
	if value == nil {
		return errors.New(fmt.Sprintf("%s, expect not null value", message))
	}
	return nil
}

func Null(message string, value interface{}) error {
	if value != nil {
		return errors.New(fmt.Sprintf("%s, expect null value", message))
	}
	return nil
}

func EqualsString(message string, expected string, value string) error {
	if expected != value {
		return errors.New(fmt.Sprintf("%s, expected '%s' but got '%s'", message, expected, value))
	}
	return nil
}

func EqualsInt(message string, expected int, value int) error {
	if expected != value {
		return errors.New(fmt.Sprintf("%s, expected %d but got %d", message, expected, value))
	}
	return nil
}

func True(message string, value bool) error {
	if value != true {
		return errors.New(fmt.Sprintf("%s, expected true but got false", message))
	}
	return nil
}

func False(message string, value bool) error {
	if value != false {
		return errors.New(fmt.Sprintf("%s, expected false but got true", message))
	}
	return nil
}
<|endoftext|>