<commit_before>package prometheus

import (
	"bytes"
	"fmt"
	"github.com/spf13/afero"
	"os"
	"strings"
	"text/template"
)

// WriteConfig creates Prometheus configuration (`/etc/prometheus/prometheus.yml`) and rules (`/etc/prometheus/alert.rules`) files.
func WriteConfig(scrapes map[string]Scrape, alerts map[string]Alert) {
	FS.MkdirAll("/etc/prometheus", 0755)
	gc := GetGlobalConfig()
	sc := GetScrapeConfig(scrapes)
	rc := GetRemoteConfig()
	ruleFiles := ""
	if len(alerts) > 0 {
		logPrintf("Writing to alert.rules")
		ruleFiles = "\nrule_files:\n  - 'alert.rules'\n"
		afero.WriteFile(FS, "/etc/prometheus/alert.rules", []byte(GetAlertConfig(alerts)), 0644)
	}
	config := gc + "\n" + sc + "\n" + rc + ruleFiles
	logPrintf("Writing to prometheus.yml")
	afero.WriteFile(FS, "/etc/prometheus/prometheus.yml", []byte(config), 0644)
}

// GetRemoteConfig returns remote_write and remote_read configs
func GetRemoteConfig() string {
	rw := getDataFromEnvVars("REMOTE_WRITE")
	config := getConfigSection("remote_write", rw)

	rr := getDataFromEnvVars("REMOTE_READ")
	config += getConfigSection("remote_read", rr)

	return config
}

// GetGlobalConfig returns global section of the configuration
func GetGlobalConfig() string {
	data := getDataFromEnvVars("GLOBAL")
	return getConfigSection("global", data)
}

// GetScrapeConfig returns scrapes section of the configuration
func GetScrapeConfig(scrapes map[string]Scrape) string {
	config := getScrapeConfigFromMap(scrapes) + getScrapeConfigFromDir()
	if len(config) > 0 {
		if !strings.HasPrefix(config, "\n") {
			config = "\n" + config
		}
		config = `
scrape_configs:` + config
	}
	return config
}

func getDataFromEnvVars(prefix string) map[string]map[string]string {
	data := map[string]map[string]string{}
	for _, e := range os.Environ() {
		if key, value := getArgFromEnv(e, prefix); len(key) > 0 {
			realKey := key
			subKey := ""
			if strings.Contains(key, "-") {
				keys := strings.Split(key, "-")
				realKey = keys[0]
				subKey = keys[1]
			}
			if _, ok := data[realKey]; !ok {
				data[realKey] = map[string]string{}
			}
			subData := data[realKey]
			subData[subKey] = value
		}
	}
	return data
}

func getScrapeConfigFromDir() string {
	config := ""
	dir := "/run/secrets/"
	if len(os.Getenv("CONFIGS_DIR")) > 0 {
		dir = os.Getenv("CONFIGS_DIR")
	}
	if !strings.HasSuffix(dir, "/") {
		dir += "/"
	}
	if files, err := afero.ReadDir(FS, dir); err == nil {
		for _, file := range files {
			if !strings.HasPrefix(file.Name(), "scrape_") {
				continue
			}
			if content, err := afero.ReadFile(FS, dir+file.Name()); err == nil {
				config += string(content)
				if !strings.HasSuffix(config, "\n") {
					config += "\n"
				}
			}
		}
	}
	return config
}

func getScrapeConfigFromMap(scrapes map[string]Scrape) string {
	if len(scrapes) != 0 {
		templateString := `{{range .}}
  - job_name: "{{.ServiceName}}"
    metrics_path: {{if .MetricsPath}}{{.MetricsPath}}{{else}}/metrics{{end}}
{{- if .ScrapeType}}
    {{.ScrapeType}}:
      - targets:
        - {{.ServiceName}}:{{- .ScrapePort}}
{{- else}}
    dns_sd_configs:
      - names: ["tasks.{{.ServiceName}}"]
        type: A
        port: {{.ScrapePort -}}{{end -}}
{{end}}
`
		tmpl, _ := template.New("").Parse(templateString)
		var b bytes.Buffer
		tmpl.Execute(&b, scrapes)
		return b.String()

	}
	return ""
}

func getConfigSection(section string, data map[string]map[string]string) string {
	if len(data) == 0 {
		return ""
	}
	config := fmt.Sprintf(`
%s:`,
		section,
	)
	for key, values := range data {
		if len(values[""]) > 0 {
			config += "\n  " + key + ": " + values[""]
		} else {
			config += "\n  " + key + ":"
			for subKey, value := range values {
				config += "\n    " + subKey + ": " + value
			}
		}
	}
	return config
}
<commit_msg>1. introduced changes to enable internal endpoint to grafana,2. change the order of deployment<commit_after>package prometheus

import (
	"bytes"
	"fmt"
	"github.com/spf13/afero"
	"os"
	"strings"
	"text/template"
)

// WriteConfig creates Prometheus configuration (`/etc/prometheus/prometheus.yml`) and rules (`/etc/prometheus/alert.rules`) files.
func WriteConfig(scrapes map[string]Scrape, alerts map[string]Alert) {
	FS.MkdirAll("/etc/prometheus", 0755)
	gc := GetGlobalConfig()
	sc := GetScrapeConfig(scrapes)
	rc := GetRemoteConfig()
	ruleFiles := ""
	if len(alerts) > 0 {
		logPrintf("Writing to alert.rules")
		ruleFiles = "\nrule_files:\n  - 'alert.rules'\n"
		afero.WriteFile(FS, "/etc/prometheus/alert.rules", []byte(GetAlertConfig(alerts)), 0644)
	}
	config := gc + "\n" + sc + "\n" + rc + ruleFiles
	logPrintf("Writing to prometheus.yml")
	afero.WriteFile(FS, "/etc/prometheus/prometheus.yml", []byte(config), 0644)
}

// GetRemoteConfig returns remote_write and remote_read configs
func GetRemoteConfig() string {
	rw := getDataFromEnvVars("REMOTE_WRITE")
	config := getConfigSection("remote_write", rw)

	rr := getDataFromEnvVars("REMOTE_READ")
	config += getConfigSection("remote_read", rr)

	return config
}

// GetGlobalConfig returns global section of the configuration
func GetGlobalConfig() string {
	data := getDataFromEnvVars("GLOBAL")
	return getConfigSection("global", data)
}

// GetScrapeConfig returns scrapes section of the configuration
func GetScrapeConfig(scrapes map[string]Scrape) string {
	config := getScrapeConfigFromMap(scrapes) + getScrapeConfigFromDir()
	if len(config) > 0 {
		if !strings.HasPrefix(config, "\n") {
			config = "\n" + config
		}
		config = `
scrape_configs:` + config
	}
	return config
}

func getDataFromEnvVars(prefix string) map[string]map[string]string {
	data := map[string]map[string]string{}
	for _, e := range os.Environ() {
		if key, value := getArgFromEnv(e, prefix); len(key) > 0 {
			realKey := key
			subKey := ""
			if strings.Contains(key, "-") {
				keys := strings.Split(key, "-")
				realKey = keys[0]
				subKey = keys[1]
			}
			if _, ok := data[realKey]; !ok {
				data[realKey] = map[string]string{}
			}
			subData := data[realKey]
			subData[subKey] = value
		}
	}
	return data
}

func getScrapeConfigFromDir() string {
	config := ""
	dir := "/run/secrets/"
	if len(os.Getenv("CONFIGS_DIR")) > 0 {
		dir = os.Getenv("CONFIGS_DIR")
	}
	if !strings.HasSuffix(dir, "/") {
		dir += "/"
	}
	if files, err := afero.ReadDir(FS, dir); err == nil {
		for _, file := range files {
			if !strings.HasPrefix(file.Name(), "scrape_") {
				continue
			}
			if content, err := afero.ReadFile(FS, dir+file.Name()); err == nil {
				config += string(content)
				if !strings.HasSuffix(config, "\n") {
					config += "\n"
				}
			}
		}
	}
	return config
}

func getScrapeConfigFromMap(scrapes map[string]Scrape) string {
	if len(scrapes) != 0 {
		templateString := `{{range .}}
  - job_name: "{{.ServiceName}}"
    metrics_path: {{if .MetricsPath}}{{.MetricsPath}}{{else}}/metrics{{end}}
{{- if .ScrapeType}}
    {{.ScrapeType}}:
      - targets:
        - ["{{.ServiceName}}:{{- .ScrapePort}}"]
{{- else}}
    dns_sd_configs:
      - names: ["tasks.{{.ServiceName}}"]
        type: A
        port: {{.ScrapePort -}}{{end -}}
{{end}}
`
		tmpl, _ := template.New("").Parse(templateString)
		var b bytes.Buffer
		tmpl.Execute(&b, scrapes)
		return b.String()

	}
	return ""
}

func getConfigSection(section string, data map[string]map[string]string) string {
	if len(data) == 0 {
		return ""
	}
	config := fmt.Sprintf(`
%s:`,
		section,
	)
	for key, values := range data {
		if len(values[""]) > 0 {
			config += "\n  " + key + ": " + values[""]
		} else {
			config += "\n  " + key + ":"
			for subKey, value := range values {
				config += "\n    " + subKey + ": " + value
			}
		}
	}
	return config
}
<|endoftext|>


<commit_before>// Utiltities for formatting Ripple data in a terminal
package terminal

import (
	"fmt"
	"reflect"

	"github.com/fatih/color"
	"github.com/rubblelabs/ripple/data"
	"github.com/rubblelabs/ripple/websockets"
)

type Flag uint32

const (
	Indent Flag = 1 << iota
	DoubleIndent
	TripleIndent

	ShowLedgerSequence
	ShowTransactionId
)

var Default Flag

var (
	ledgerStyle     = color.New(color.FgRed, color.Underline)
	leStyle         = color.New(color.FgWhite)
	txStyle         = color.New(color.FgGreen)
	proposalStyle   = color.New(color.FgYellow)
	validationStyle = color.New(color.FgYellow, color.Bold)
	tradeStyle      = color.New(color.FgBlue)
	balanceStyle    = color.New(color.FgMagenta)
	pathStyle       = color.New(color.FgYellow)
	offerStyle      = color.New(color.FgYellow)
	lineStyle       = color.New(color.FgYellow)
	infoStyle       = color.New(color.FgRed)
)

func BoolSymbol(v bool) string {
	if v {
		return "✓"
	}
	return "✗"
}

func MemoSymbol(tx data.Transaction) string {
	return BoolSymbol(len(tx.GetBase().Memos) > 0)
}

func SignSymbol(s data.Signer) string {
	valid, err := data.CheckSignature(s)
	return BoolSymbol(!valid || err != nil)
}

type bundle struct {
	color  *color.Color
	format string
	values []interface{}
	flag   Flag
}

func newLeBundle(v interface{}, flag Flag) (*bundle, error) {
	var (
		format = "%-11s "
		values = []interface{}{v.(data.LedgerEntry).GetLedgerEntryType()}
	)
	switch le := v.(type) {
	case *data.AccountRoot:
		format += "%-34s %08X %s"
		values = append(values, []interface{}{le.Account, *le.Flags, le.Balance}...)
	case *data.LedgerHashes:
		format += "%d hashes"
		values = append(values, []interface{}{len(*le.Hashes)}...)
	case *data.RippleState:
		format += "%s %s %s"
		values = append(values, []interface{}{le.Balance, le.HighLimit, le.LowLimit}...)
	case *data.Offer:
		format += "%-34s %-60s %-60s %-18s"
		values = append(values, []interface{}{le.Account, le.TakerPays, le.TakerGets, le.Ratio()}...)
	case *data.FeeSettings:
		format += "%d %d %d %d"
		values = append(values, []interface{}{le.BaseFee, le.ReferenceFeeUnits, le.ReserveBase, le.ReserveIncrement}...)
	case *data.Amendments:
		format += "%s"
		values = append(values, []interface{}{le.Amendments}...)
	default:
		return nil, fmt.Errorf("Unknown Ledger Entry Type")
	}
	return &bundle{
		color:  leStyle,
		format: format,
		values: values,
		flag:   flag,
	}, nil
}

func newTxBundle(v data.Transaction, insert string, flag Flag) (*bundle, error) {
	var (
		base   = v.GetBase()
		format = "%s %-11s %-8s %s%s %-34s %-9d "
		values = []interface{}{SignSymbol(v), base.GetType(), base.Fee, insert, MemoSymbol(v), base.Account, base.Sequence}
	)
	if flag&ShowTransactionId > 0 {
		txId, err := data.NodeId(v)
		if err != nil {
			return nil, err
		}
		format = "%s " + format
		values = append([]interface{}{txId}, values...)
	}
	switch tx := v.(type) {
	case *data.Payment:
		format += "=> %-34s %-60s %-60s"
		values = append(values, []interface{}{tx.Destination, tx.Amount, tx.SendMax}...)
	case *data.OfferCreate:
		format += "%-60s %-60s %-18s"
		values = append(values, []interface{}{tx.TakerPays, tx.TakerGets, tx.Ratio()}...)
	case *data.OfferCancel:
		format += "%-9d"
		values = append(values, tx.Sequence)
	case *data.AccountSet:
		format += "%-9d"
		values = append(values, tx.Sequence)
	case *data.TrustSet:
		format += "%-60s %d %d"
		values = append(values, tx.LimitAmount, tx.QualityIn, tx.QualityOut)
	}
	return &bundle{
		color:  txStyle,
		format: format,
		values: values,
		flag:   flag,
	}, nil
}

func newTxmBundle(txm *data.TransactionWithMetaData, flag Flag) (*bundle, error) {
	insert := fmt.Sprintf("%s ", txm.MetaData.TransactionResult.Symbol())
	if flag&ShowLedgerSequence > 0 {
		insert = fmt.Sprintf("%-9d %s", txm.LedgerSequence, insert)
	}
	b, err := newTxBundle(txm.Transaction, insert, flag)
	if err != nil {
		return nil, err
	}
	if len(txm.MetaData.AffectedNodes) == 0 {
		// Likely a proposed transaction
		b.color = proposalStyle
	} else if !txm.MetaData.TransactionResult.Success() {
		b.color = infoStyle
	}
	return b, nil
}

func newBundle(value interface{}, flag Flag) (*bundle, error) {
	switch v := value.(type) {
	case *data.TransactionWithMetaData:
		return newTxmBundle(v, flag)
	case data.Transaction:
		return newTxBundle(v, "", flag)
	case data.LedgerEntry:
		return newLeBundle(v, flag)
	}
	switch v := reflect.Indirect(reflect.ValueOf(value)).Interface().(type) {
	case websockets.LedgerStreamMsg:
		return &bundle{
			color:  ledgerStyle,
			format: "Ledger %d closed at %s with %d transactions",
			values: []interface{}{v.LedgerSequence, v.LedgerTime.String(), v.TxnCount},
			flag:   flag,
		}, nil
	case websockets.ServerStreamMsg:
		return &bundle{
			color:  infoStyle,
			format: "Server Status: %s (%d/%d)",
			values: []interface{}{v.Status, v.LoadFactor, v.LoadBase},
			flag:   flag,
		}, nil
	case data.Ledger:
		return &bundle{
			color:  ledgerStyle,
			format: "Ledger %d closed at %s",
			values: []interface{}{v.LedgerSequence, v.CloseTime.String()},
			flag:   flag,
		}, nil
	case data.InnerNode:
		return &bundle{
			color:  leStyle,
			format: "%s: %d hashes",
			values: []interface{}{v.Type, v.Count()},
			flag:   flag,
		}, nil
	case data.Proposal:
		return &bundle{
			color:  proposalStyle,
			format: "%s Proposal:   %s %s %s %s",
			values: []interface{}{SignSymbol(&v), v.PublicKey.NodePublicKey(), v.CloseTime.String(), v.PreviousLedger, v.LedgerHash},
			flag:   flag,
		}, nil
	case data.Validation:
		return &bundle{
			color:  validationStyle,
			format: "%s Validation: %s %s %s %-8d %08X %s",
			values: []interface{}{SignSymbol(&v), v.SigningPubKey.NodePublicKey(), v.SigningTime.String(), v.LedgerHash, v.LedgerSequence, v.Flags, v.Amendments},
			flag:   flag,
		}, nil
	case data.Trade:
		return &bundle{
			color:  tradeStyle,
			format: "Trade: %-34s => %-34s %-18s %60s => %-60s",
			values: []interface{}{v.Seller, v.Buyer, v.Price(), v.Paid, v.Got},
			flag:   flag,
		}, nil
	case data.Balance:
		return &bundle{
			color:  balanceStyle,
			format: "Balance: %-34s  Currency: %s Balance: %20s Change: %20s",
			values: []interface{}{v.Account, v.Currency, v.Balance, v.Change},
			flag:   flag,
		}, nil
	case data.Path:
		sig, err := v.Signature()
		if err != nil {
			return nil, err
		}
		return &bundle{
			color:  pathStyle,
			format: "Path: %08X %s",
			values: []interface{}{sig, v.String()},
			flag:   flag,
		}, nil
	case data.OrderBookOffer:
		return &bundle{
			color:  offerStyle,
			format: "Offer: %34s %8d %s %25s %62s %62s",
			values: []interface{}{v.Account, *v.Sequence, BoolSymbol(v.Expiration != nil && *v.Expiration > 0), v.Quality, v.TakerPays, v.TakerGets},
			flag:   flag,
		}, nil
	case data.AccountOffer:
		return &bundle{
			color:  offerStyle,
			format: "Offer: %08X %9d %34s %62s %62s",
			values: []interface{}{v.Flags, v.Sequence, v.Quality, v.TakerPays, v.TakerGets},
			flag:   flag,
		}, nil
	case data.AccountLine:
		return &bundle{
			color:  lineStyle,
			format: "Line: %20s %3s %34s %34s %20s %s %s %8d %8d",
			values: []interface{}{v.Balance, v.Currency, v.Account, v.Limit, v.LimitPeer, BoolSymbol(v.NoRipple), BoolSymbol(v.NoRipplePeer), v.QualityIn, v.QualityOut},
			flag:   flag,
		}, nil
	default:
		return &bundle{
			color:  infoStyle,
			format: "%s",
			values: []interface{}{v},
			flag:   flag,
		}, nil
	}
}

func indent(flag Flag) string {
	switch {
	case flag&Indent > 0:
		return "    "
	case flag&DoubleIndent > 0:
		return "        "
	case flag&TripleIndent > 0:
		return "           "
	default:
		return ""
	}
}

func println(value interface{}, flag Flag) (int, error) {
	b, err := newBundle(value, flag)
	if err != nil {
		return 0, err
	}
	return b.color.Printf(indent(flag)+b.format+"\n", b.values...)
}

func Println(value interface{}, flag Flag) {
	if _, err := println(value, flag); err != nil {
		infoStyle.Println(err.Error())
	}
}

func Sprint(value interface{}, flag Flag) string {
	b, err := newBundle(value, flag)
	if err != nil {
		return fmt.Sprintf("Cannot format: %+v", value)
	}
	return b.color.SprintfFunc()(indent(flag)+b.format, b.values...)
}
<commit_msg>Fix some Offer formatting in terminal<commit_after>// Utiltities for formatting Ripple data in a terminal
package terminal

import (
	"fmt"
	"reflect"

	"github.com/fatih/color"
	"github.com/rubblelabs/ripple/data"
	"github.com/rubblelabs/ripple/websockets"
)

type Flag uint32

const (
	Indent Flag = 1 << iota
	DoubleIndent
	TripleIndent

	ShowLedgerSequence
	ShowTransactionId
)

var Default Flag

var (
	ledgerStyle     = color.New(color.FgRed, color.Underline)
	leStyle         = color.New(color.FgWhite)
	txStyle         = color.New(color.FgGreen)
	proposalStyle   = color.New(color.FgYellow)
	validationStyle = color.New(color.FgYellow, color.Bold)
	tradeStyle      = color.New(color.FgBlue)
	balanceStyle    = color.New(color.FgMagenta)
	pathStyle       = color.New(color.FgYellow)
	offerStyle      = color.New(color.FgYellow)
	lineStyle       = color.New(color.FgYellow)
	infoStyle       = color.New(color.FgRed)
)

func defaultUint32(v *uint32) uint32 {
	if v != nil {
		return *v
	}
	return 0
}

func BoolSymbol(v bool) string {
	if v {
		return "✓"
	}
	return "✗"
}

func MemoSymbol(tx data.Transaction) string {
	return BoolSymbol(len(tx.GetBase().Memos) > 0)
}

func SignSymbol(s data.Signer) string {
	valid, err := data.CheckSignature(s)
	return BoolSymbol(!valid || err != nil)
}

type bundle struct {
	color  *color.Color
	format string
	values []interface{}
	flag   Flag
}

func newLeBundle(v interface{}, flag Flag) (*bundle, error) {
	var (
		format = "%-11s "
		values = []interface{}{v.(data.LedgerEntry).GetLedgerEntryType()}
	)
	switch le := v.(type) {
	case *data.AccountRoot:
		format += "%-34s %08X %s"
		values = append(values, []interface{}{le.Account, *le.Flags, le.Balance}...)
	case *data.LedgerHashes:
		format += "%d hashes"
		values = append(values, []interface{}{len(*le.Hashes)}...)
	case *data.RippleState:
		format += "%s %s %s"
		values = append(values, []interface{}{le.Balance, le.HighLimit, le.LowLimit}...)
	case *data.Offer:
		format += "%-34s %-60s %-60s %-18s"
		values = append(values, []interface{}{le.Account, le.TakerPays, le.TakerGets, le.Ratio()}...)
	case *data.FeeSettings:
		format += "%d %d %d %d"
		values = append(values, []interface{}{le.BaseFee, le.ReferenceFeeUnits, le.ReserveBase, le.ReserveIncrement}...)
	case *data.Amendments:
		format += "%s"
		values = append(values, []interface{}{le.Amendments}...)
	default:
		return nil, fmt.Errorf("Unknown Ledger Entry Type")
	}
	return &bundle{
		color:  leStyle,
		format: format,
		values: values,
		flag:   flag,
	}, nil
}

func newTxBundle(v data.Transaction, insert string, flag Flag) (*bundle, error) {
	var (
		base   = v.GetBase()
		format = "%s %-11s %-8s %s%s %-34s %-9d "
		values = []interface{}{SignSymbol(v), base.GetType(), base.Fee, insert, MemoSymbol(v), base.Account, base.Sequence}
	)
	if flag&ShowTransactionId > 0 {
		txId, err := data.NodeId(v)
		if err != nil {
			return nil, err
		}
		format = "%s " + format
		values = append([]interface{}{txId}, values...)
	}
	switch tx := v.(type) {
	case *data.Payment:
		format += "=> %-34s %-60s %-60s"
		values = append(values, []interface{}{tx.Destination, tx.Amount, tx.SendMax}...)
	case *data.OfferCreate:
		format += "%-9d %-60s %-60s %-18s"
		values = append(values, []interface{}{defaultUint32(tx.OfferSequence), tx.TakerPays, tx.TakerGets, tx.Ratio()}...)
	case *data.OfferCancel:
		format += "%-9d"
		values = append(values, tx.OfferSequence)
	case *data.AccountSet:
		format += "%-9d"
		values = append(values, tx.Sequence)
	case *data.TrustSet:
		format += "%-60s %d %d"
		values = append(values, tx.LimitAmount, tx.QualityIn, tx.QualityOut)
	}
	return &bundle{
		color:  txStyle,
		format: format,
		values: values,
		flag:   flag,
	}, nil
}

func newTxmBundle(txm *data.TransactionWithMetaData, flag Flag) (*bundle, error) {
	insert := fmt.Sprintf("%s ", txm.MetaData.TransactionResult.Symbol())
	if flag&ShowLedgerSequence > 0 {
		insert = fmt.Sprintf("%-9d %s", txm.LedgerSequence, insert)
	}
	b, err := newTxBundle(txm.Transaction, insert, flag)
	if err != nil {
		return nil, err
	}
	if len(txm.MetaData.AffectedNodes) == 0 {
		// Likely a proposed transaction
		b.color = proposalStyle
	} else if !txm.MetaData.TransactionResult.Success() {
		b.color = infoStyle
	}
	return b, nil
}

func newBundle(value interface{}, flag Flag) (*bundle, error) {
	switch v := value.(type) {
	case *data.TransactionWithMetaData:
		return newTxmBundle(v, flag)
	case data.Transaction:
		return newTxBundle(v, "", flag)
	case data.LedgerEntry:
		return newLeBundle(v, flag)
	}
	switch v := reflect.Indirect(reflect.ValueOf(value)).Interface().(type) {
	case websockets.LedgerStreamMsg:
		return &bundle{
			color:  ledgerStyle,
			format: "Ledger %d closed at %s with %d transactions",
			values: []interface{}{v.LedgerSequence, v.LedgerTime.String(), v.TxnCount},
			flag:   flag,
		}, nil
	case websockets.ServerStreamMsg:
		return &bundle{
			color:  infoStyle,
			format: "Server Status: %s (%d/%d)",
			values: []interface{}{v.Status, v.LoadFactor, v.LoadBase},
			flag:   flag,
		}, nil
	case data.Ledger:
		return &bundle{
			color:  ledgerStyle,
			format: "Ledger %d closed at %s",
			values: []interface{}{v.LedgerSequence, v.CloseTime.String()},
			flag:   flag,
		}, nil
	case data.InnerNode:
		return &bundle{
			color:  leStyle,
			format: "%s: %d hashes",
			values: []interface{}{v.Type, v.Count()},
			flag:   flag,
		}, nil
	case data.Proposal:
		return &bundle{
			color:  proposalStyle,
			format: "%s Proposal:   %s %s %s %s",
			values: []interface{}{SignSymbol(&v), v.PublicKey.NodePublicKey(), v.CloseTime.String(), v.PreviousLedger, v.LedgerHash},
			flag:   flag,
		}, nil
	case data.Validation:
		return &bundle{
			color:  validationStyle,
			format: "%s Validation: %s %s %s %-8d %08X %s",
			values: []interface{}{SignSymbol(&v), v.SigningPubKey.NodePublicKey(), v.SigningTime.String(), v.LedgerHash, v.LedgerSequence, v.Flags, v.Amendments},
			flag:   flag,
		}, nil
	case data.Trade:
		return &bundle{
			color:  tradeStyle,
			format: "Trade: %-34s => %-34s %-18s %60s => %-60s",
			values: []interface{}{v.Seller, v.Buyer, v.Price(), v.Paid, v.Got},
			flag:   flag,
		}, nil
	case data.Balance:
		return &bundle{
			color:  balanceStyle,
			format: "Balance: %-34s  Currency: %s Balance: %20s Change: %20s",
			values: []interface{}{v.Account, v.Currency, v.Balance, v.Change},
			flag:   flag,
		}, nil
	case data.Path:
		sig, err := v.Signature()
		if err != nil {
			return nil, err
		}
		return &bundle{
			color:  pathStyle,
			format: "Path: %08X %s",
			values: []interface{}{sig, v.String()},
			flag:   flag,
		}, nil
	case data.OrderBookOffer:
		return &bundle{
			color:  offerStyle,
			format: "Offer: %34s %8d %s %25s %62s %62s",
			values: []interface{}{v.Account, v.Sequence, BoolSymbol(v.Expiration != nil && *v.Expiration > 0), v.Quality, v.TakerPays, v.TakerGets},
			flag:   flag,
		}, nil
	case data.AccountOffer:
		return &bundle{
			color:  offerStyle,
			format: "Offer: %08X %9d %34s %62s %62s",
			values: []interface{}{v.Flags, v.Sequence, v.Quality, v.TakerPays, v.TakerGets},
			flag:   flag,
		}, nil
	case data.AccountLine:
		return &bundle{
			color:  lineStyle,
			format: "Line: %20s %3s %34s %34s %20s %s %s %8d %8d",
			values: []interface{}{v.Balance, v.Currency, v.Account, v.Limit, v.LimitPeer, BoolSymbol(v.NoRipple), BoolSymbol(v.NoRipplePeer), v.QualityIn, v.QualityOut},
			flag:   flag,
		}, nil
	default:
		return &bundle{
			color:  infoStyle,
			format: "%s",
			values: []interface{}{v},
			flag:   flag,
		}, nil
	}
}

func indent(flag Flag) string {
	switch {
	case flag&Indent > 0:
		return "    "
	case flag&DoubleIndent > 0:
		return "        "
	case flag&TripleIndent > 0:
		return "           "
	default:
		return ""
	}
}

func println(value interface{}, flag Flag) (int, error) {
	b, err := newBundle(value, flag)
	if err != nil {
		return 0, err
	}
	return b.color.Printf(indent(flag)+b.format+"\n", b.values...)
}

func Println(value interface{}, flag Flag) {
	if _, err := println(value, flag); err != nil {
		infoStyle.Println(err.Error())
	}
}

func Sprint(value interface{}, flag Flag) string {
	b, err := newBundle(value, flag)
	if err != nil {
		return fmt.Sprintf("Cannot format: %+v", value)
	}
	return b.color.SprintfFunc()(indent(flag)+b.format, b.values...)
}
<|endoftext|>


<commit_before>package session

import (
	"crypto/tls"
	"fmt"
	"net/url"
	"time"

	"github.com/catatsuy/isucon6-final/bench/http"
	"github.com/catatsuy/isucon6-final/bench/http/cookiejar"
)

const (
	DefaultTimeout      = time.Duration(10) * time.Second
	MaxIdleConnsPerHost = 6
)

type Session struct {
	Scheme    string
	Host      string
	UserAgent string
	Client    *http.Client
	Transport *http.Transport
}

func New(baseURL string) *Session {
	s := &Session{}

	s.Transport = &http.Transport{
		TLSClientConfig: &tls.Config{
			InsecureSkipVerify: true,
		},
		MaxIdleConnsPerHost: MaxIdleConnsPerHost,
	}

	jar, _ := cookiejar.New(nil)

	s.Client = &http.Client{
		Transport: s.Transport,
		Jar:       jar,
		Timeout:   DefaultTimeout,
		CheckRedirect: func(req *http.Request, via []*http.Request) error {
			return fmt.Errorf("redirect attempted")
		},
	}

	s.UserAgent = "benchmarker"

	u, err := url.Parse(baseURL)
	if err != nil {
		panic(err) // should be cared at initialization
	}
	s.Scheme = u.Scheme
	s.Host = u.Host

	return s
}

func (s *Session) Bye() {
	s.Transport.CloseIdleConnections()
}
<commit_msg>Make http client timeout to 5 secs<commit_after>package session

import (
	"crypto/tls"
	"fmt"
	"net/url"
	"time"

	"github.com/catatsuy/isucon6-final/bench/http"
	"github.com/catatsuy/isucon6-final/bench/http/cookiejar"
)

const (
	DefaultTimeout      = time.Duration(5) * time.Second
	MaxIdleConnsPerHost = 6
)

type Session struct {
	Scheme    string
	Host      string
	UserAgent string
	Client    *http.Client
	Transport *http.Transport
}

func New(baseURL string) *Session {
	s := &Session{}

	s.Transport = &http.Transport{
		TLSClientConfig: &tls.Config{
			InsecureSkipVerify: true,
		},
		MaxIdleConnsPerHost: MaxIdleConnsPerHost,
	}

	jar, _ := cookiejar.New(nil)

	s.Client = &http.Client{
		Transport: s.Transport,
		Jar:       jar,
		Timeout:   DefaultTimeout,
		CheckRedirect: func(req *http.Request, via []*http.Request) error {
			return fmt.Errorf("redirect attempted")
		},
	}

	s.UserAgent = "benchmarker"

	u, err := url.Parse(baseURL)
	if err != nil {
		panic(err) // should be cared at initialization
	}
	s.Scheme = u.Scheme
	s.Host = u.Host

	return s
}

func (s *Session) Bye() {
	s.Transport.CloseIdleConnections()
}
<|endoftext|>


<commit_before>package logstash

import (
	"encoding/json"
	"errors"
	"io/ioutil"
	"log"
	"net"
	"net/http"
	"os"

	"github.com/gliderlabs/logspout/router"
)

func init() {
	router.AdapterFactories.Register(NewLogstashAdapter, "logstash")
}

// LogstashAdapter is an adapter that streams UDP JSON to Logstash.
type LogstashAdapter struct {
	conn  net.Conn
	route *router.Route
}

func getopt(name, dfault string) string {
	value := os.Getenv(name)
	if value == "" {
		value = dfault
	}
	return value
}

// NewLogstashAdapter creates a LogstashAdapter with UDP as the default transport.
func NewLogstashAdapter(route *router.Route) (router.LogAdapter, error) {
	transport, found := router.AdapterTransports.Lookup(route.AdapterTransport("udp"))
	if !found {
		return nil, errors.New("unable to find adapter: " + route.Adapter)
	}

	conn, err := transport.Dial(route.Address, route.Options)
	if err != nil {
		return nil, err
	}

	return &LogstashAdapter{
		route: route,
		conn:  conn,
	}, nil
}

// Stream implements the router.LogAdapter interface.
func (a *LogstashAdapter) Stream(logstream chan *router.Message) {

	opt_string := getopt("OPTIONS", "")
	var options map[string]string

	if opt_string != "" {
		b := []byte(opt_string)

		json.Unmarshal(b, &options)
	}

	resp, err := http.Get("http://169.254.169.254/latest/meta-data/instance-id")
	instance_id := ""
	if err == nil {
		value, err := ioutil.ReadAll(resp.Body)
		if err == nil {
			instance_id = string(value)
		}
	}
	resp.Body.Close()

	for m := range logstream {
		msg := LogstashMessage{
			Message:    m.Data,
			Name:       m.Container.Name,
			ID:         m.Container.ID,
			Image:      m.Container.Config.Image,
			Hostname:   m.Container.Config.Hostname,
			Args:       m.Container.Args,
			InstanceId: instance_id,
			Options:    options,
		}
		js, err := json.Marshal(msg)
		if err != nil {
			log.Println("logstash:", err)
			continue
		}
		_, err = a.conn.Write(js)
		if err != nil {
			log.Println("logstash:", err)
			continue
		}
	}
}

// LogstashMessage is a simple JSON input to Logstash.
type LogstashMessage struct {
	Message    string            `json:"message"`
	Name       string            `json:"docker.name"`
	ID         string            `json:"docker.id"`
	Image      string            `json:"docker.image"`
	Hostname   string            `json:"docker.hostname"`
	Args       []string          `json:"docker.args,omitempty"`
	Options    map[string]string `json:"options,omitempty"`
	InstanceId string            `json:"instance-id,omitempty"`
}
<commit_msg>moving body close to if no error<commit_after>package logstash

import (
	"encoding/json"
	"errors"
	"io/ioutil"
	"log"
	"net"
	"net/http"
	"os"

	"github.com/gliderlabs/logspout/router"
)

func init() {
	router.AdapterFactories.Register(NewLogstashAdapter, "logstash")
}

// LogstashAdapter is an adapter that streams UDP JSON to Logstash.
type LogstashAdapter struct {
	conn  net.Conn
	route *router.Route
}

func getopt(name, dfault string) string {
	value := os.Getenv(name)
	if value == "" {
		value = dfault
	}
	return value
}

// NewLogstashAdapter creates a LogstashAdapter with UDP as the default transport.
func NewLogstashAdapter(route *router.Route) (router.LogAdapter, error) {
	transport, found := router.AdapterTransports.Lookup(route.AdapterTransport("udp"))
	if !found {
		return nil, errors.New("unable to find adapter: " + route.Adapter)
	}

	conn, err := transport.Dial(route.Address, route.Options)
	if err != nil {
		return nil, err
	}

	return &LogstashAdapter{
		route: route,
		conn:  conn,
	}, nil
}

// Stream implements the router.LogAdapter interface.
func (a *LogstashAdapter) Stream(logstream chan *router.Message) {

	opt_string := getopt("OPTIONS", "")
	var options map[string]string

	if opt_string != "" {
		b := []byte(opt_string)

		json.Unmarshal(b, &options)
	}

	resp, err := http.Get("http://169.254.169.254/latest/meta-data/instance-id")
	instance_id := ""
	if err == nil {
		value, err := ioutil.ReadAll(resp.Body)
		if err == nil {
			instance_id = string(value)
		}
		resp.Body.Close()
	}

	for m := range logstream {
		msg := LogstashMessage{
			Message:    m.Data,
			Name:       m.Container.Name,
			ID:         m.Container.ID,
			Image:      m.Container.Config.Image,
			Hostname:   m.Container.Config.Hostname,
			Args:       m.Container.Args,
			InstanceId: instance_id,
			Options:    options,
		}
		js, err := json.Marshal(msg)
		if err != nil {
			log.Println("logstash:", err)
			continue
		}
		_, err = a.conn.Write(js)
		if err != nil {
			log.Println("logstash:", err)
			continue
		}
	}
}

// LogstashMessage is a simple JSON input to Logstash.
type LogstashMessage struct {
	Message    string            `json:"message"`
	Name       string            `json:"docker.name"`
	ID         string            `json:"docker.id"`
	Image      string            `json:"docker.image"`
	Hostname   string            `json:"docker.hostname"`
	Args       []string          `json:"docker.args,omitempty"`
	Options    map[string]string `json:"options,omitempty"`
	InstanceId string            `json:"instance-id,omitempty"`
}
<|endoftext|>


<commit_before>/*
Copyright 2016 The Kubernetes Authors.

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 main

import (
	"errors"
	"flag"
	"fmt"
	"os"
	"strings"
	"time"

	"github.com/golang/glog"
	"github.com/kubernetes-incubator/external-storage/lib/controller"
	"k8s.io/client-go/kubernetes"
	"k8s.io/client-go/pkg/api/resource"
	"k8s.io/client-go/pkg/api/v1"
	"k8s.io/client-go/pkg/types"
	"k8s.io/client-go/pkg/util/uuid"
	"k8s.io/client-go/pkg/util/wait"
	"k8s.io/client-go/rest"
	"k8s.io/client-go/tools/clientcmd"

	"github.com/avagin/ploop-flexvol/volume"
)

const (
	resyncPeriod              = 15 * time.Second
	provisionerName           = "kubernetes.io/virtuozzo-storage"
	exponentialBackOffOnError = false
	failedRetryThreshold      = 5
	provisionerIDAnn          = "vzFSProvisionerIdentity"
	vzShareAnn                = "vzShare"
)

type provisionOutput struct {
	Path string `json:"path"`
}

type vzFSProvisioner struct {
	// Kubernetes Client. Use to retrieve Ceph admin secret
	client kubernetes.Interface
	// Identity of this vzFSProvisioner, generated. Used to identify "this"
	// provisioner's PVs.
	identity types.UID
}

func newVzFSProvisioner(client kubernetes.Interface) controller.Provisioner {
	return &vzFSProvisioner{
		client:   client,
		identity: uuid.NewUUID(),
	}
}

var _ controller.Provisioner = &vzFSProvisioner{}

// Provision creates a storage asset and returns a PV object representing it.
func (p *vzFSProvisioner) Provision(options controller.VolumeOptions) (*v1.PersistentVolume, error) {
	var (
		capacity resource.Quantity
		labels   map[string]string
	)

	capacity = options.PVC.Spec.Resources.Requests[v1.ResourceName(v1.ResourceStorage)]
	bytes := capacity.Value()

	if options.PVC.Spec.Selector != nil && options.PVC.Spec.Selector.MatchExpressions != nil {
		return nil, fmt.Errorf("claim Selector.matchExpressions is not supported")
	}
	share := fmt.Sprintf("kubernetes-dynamic-pvc-%s", uuid.NewUUID())

	glog.Infof("Add %s %s", share, capacity.Value())

	if options.PVC.Spec.Selector != nil && options.PVC.Spec.Selector.MatchLabels != nil {
		labels = options.PVC.Spec.Selector.MatchLabels
	}

	ploop_options := options.Parameters

	ploop_options["volumeId"] = share
	ploop_options["size"] = fmt.Sprintf("%d", bytes)

	if labels != nil {
		for k, v := range labels {
			switch k {
			case "vzsReplicas":
				v = strings.Replace(v, ".", ":", 1)
				v = strings.Replace(v, ".", "/", 1)
				ploop_options[k] = v
			case "vzsTier":
				fallthrough
			case "vzsEncoding":
				v = strings.Replace(v, ".", "+", 1)
				v = strings.Replace(v, ".", "/", 1)
				ploop_options[k] = v
			case "vzsFailureDomain":
				ploop_options[k] = v
			default:
				glog.Infof("Skip %s = %s", k, v)
			}
		}
	}

	if err := volume.Create(ploop_options); err != nil {
		return nil, err
	}

	pv := &v1.PersistentVolume{
		ObjectMeta: v1.ObjectMeta{
			Name: options.PVName,
			Annotations: map[string]string{
				provisionerIDAnn: string(p.identity),
				vzShareAnn:       share,
			},
			Labels: labels,
		},
		Spec: v1.PersistentVolumeSpec{
			PersistentVolumeReclaimPolicy: options.PersistentVolumeReclaimPolicy,
			AccessModes:                   options.PVC.Spec.AccessModes,
			Capacity: v1.ResourceList{
				v1.ResourceName(v1.ResourceStorage): options.PVC.Spec.Resources.Requests[v1.ResourceName(v1.ResourceStorage)],
			},
			PersistentVolumeSource: v1.PersistentVolumeSource{
				FlexVolume: &v1.FlexVolumeSource{
					Driver:  "virtuozzo/ploop",
					Options: ploop_options,
				},
			},
		},
	}

	glog.Infof("successfully created virtuozzo storage share: %s", share)

	return pv, nil
}

// Delete removes the storage asset that was created by Provision represented
// by the given PV.
func (p *vzFSProvisioner) Delete(volume *v1.PersistentVolume) error {
	ann, ok := volume.Annotations[provisionerIDAnn]
	if !ok {
		return errors.New("identity annotation not found on PV")
	}
	if ann != string(p.identity) {
		return &controller.IgnoredError{"identity annotation on PV does not match ours"}
	}
	share, ok := volume.Annotations[vzShareAnn]
	if !ok {
		return errors.New("vz share annotation not found on PV")
	}

	options := volume.Spec.PersistentVolumeSource.FlexVolume.Options
	path := options["volumePath"] + "/" + options["volumeId"]
	glog.Infof("Delete: %s", path)
	err := os.RemoveAll(path)
	if err != nil {
		return err
	}

	glog.Infof("successfully delete virtuozzo storage share: %s", share)

	return nil
}

var (
	master     = flag.String("master", "", "Master URL")
	kubeconfig = flag.String("kubeconfig", "", "Absolute path to the kubeconfig")
)

func main() {
	flag.Parse()
	flag.Set("logtostderr", "true")

	var config *rest.Config
	var err error
	if *master != "" || *kubeconfig != "" {
		config, err = clientcmd.BuildConfigFromFlags(*master, *kubeconfig)
	} else {
		config, err = rest.InClusterConfig()
	}

	if err != nil {
		glog.Fatalf("Failed to create config: %v", err)
	}
	clientset, err := kubernetes.NewForConfig(config)
	if err != nil {
		glog.Fatalf("Failed to create client: %v", err)
	}

	// The controller needs to know what the server version is because out-of-tree
	// provisioners aren't officially supported until 1.5
	serverVersion, err := clientset.Discovery().ServerVersion()
	if err != nil {
		glog.Fatalf("Error getting server version: %v", err)
	}

	// Create the provisioner: it implements the Provisioner interface expected by
	// the controller
	vzFSProvisioner := newVzFSProvisioner(clientset)

	// Start the provision controller which will dynamically provision cephFS
	// PVs
	pc := controller.NewProvisionController(clientset, resyncPeriod, provisionerName, vzFSProvisioner, serverVersion.GitVersion, exponentialBackOffOnError, failedRetryThreshold, 2*resyncPeriod, resyncPeriod, resyncPeriod/2, 2*resyncPeriod)

	pc.Run(wait.NeverStop)
}
<commit_msg>virtuozzo-provisioner.go: copy VolumeOptions.Parameters into ploop_options<commit_after>/*
Copyright 2016 The Kubernetes Authors.

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 main

import (
	"errors"
	"flag"
	"fmt"
	"os"
	"strings"
	"time"

	"github.com/golang/glog"
	"github.com/kubernetes-incubator/external-storage/lib/controller"
	"k8s.io/client-go/kubernetes"
	"k8s.io/client-go/pkg/api/resource"
	"k8s.io/client-go/pkg/api/v1"
	"k8s.io/client-go/pkg/types"
	"k8s.io/client-go/pkg/util/uuid"
	"k8s.io/client-go/pkg/util/wait"
	"k8s.io/client-go/rest"
	"k8s.io/client-go/tools/clientcmd"

	"github.com/avagin/ploop-flexvol/volume"
)

const (
	resyncPeriod              = 15 * time.Second
	provisionerName           = "kubernetes.io/virtuozzo-storage"
	exponentialBackOffOnError = false
	failedRetryThreshold      = 5
	provisionerIDAnn          = "vzFSProvisionerIdentity"
	vzShareAnn                = "vzShare"
)

type provisionOutput struct {
	Path string `json:"path"`
}

type vzFSProvisioner struct {
	// Kubernetes Client. Use to retrieve Ceph admin secret
	client kubernetes.Interface
	// Identity of this vzFSProvisioner, generated. Used to identify "this"
	// provisioner's PVs.
	identity types.UID
}

func newVzFSProvisioner(client kubernetes.Interface) controller.Provisioner {
	return &vzFSProvisioner{
		client:   client,
		identity: uuid.NewUUID(),
	}
}

var _ controller.Provisioner = &vzFSProvisioner{}

// Provision creates a storage asset and returns a PV object representing it.
func (p *vzFSProvisioner) Provision(options controller.VolumeOptions) (*v1.PersistentVolume, error) {
	var (
		capacity resource.Quantity
		labels   map[string]string
	)

	capacity = options.PVC.Spec.Resources.Requests[v1.ResourceName(v1.ResourceStorage)]
	bytes := capacity.Value()

	if options.PVC.Spec.Selector != nil && options.PVC.Spec.Selector.MatchExpressions != nil {
		return nil, fmt.Errorf("claim Selector.matchExpressions is not supported")
	}
	share := fmt.Sprintf("kubernetes-dynamic-pvc-%s", uuid.NewUUID())

	glog.Infof("Add %s %s", share, capacity.Value())

	if options.PVC.Spec.Selector != nil && options.PVC.Spec.Selector.MatchLabels != nil {
		labels = options.PVC.Spec.Selector.MatchLabels
	}

	ploop_options := map[string]string{}
	for k,v := range options.Parameters {
		ploop_options[k] = v
	}

	ploop_options["volumeId"] = share
	ploop_options["size"] = fmt.Sprintf("%d", bytes)

	if labels != nil {
		for k, v := range labels {
			switch k {
			case "vzsReplicas":
				v = strings.Replace(v, ".", ":", 1)
				v = strings.Replace(v, ".", "/", 1)
				ploop_options[k] = v
			case "vzsTier":
				fallthrough
			case "vzsEncoding":
				v = strings.Replace(v, ".", "+", 1)
				v = strings.Replace(v, ".", "/", 1)
				ploop_options[k] = v
			case "vzsFailureDomain":
				ploop_options[k] = v
			default:
				glog.Infof("Skip %s = %s", k, v)
			}
		}
	}

	if err := volume.Create(ploop_options); err != nil {
		return nil, err
	}

	pv := &v1.PersistentVolume{
		ObjectMeta: v1.ObjectMeta{
			Name: options.PVName,
			Annotations: map[string]string{
				provisionerIDAnn: string(p.identity),
				vzShareAnn:       share,
			},
			Labels: labels,
		},
		Spec: v1.PersistentVolumeSpec{
			PersistentVolumeReclaimPolicy: options.PersistentVolumeReclaimPolicy,
			AccessModes:                   options.PVC.Spec.AccessModes,
			Capacity: v1.ResourceList{
				v1.ResourceName(v1.ResourceStorage): options.PVC.Spec.Resources.Requests[v1.ResourceName(v1.ResourceStorage)],
			},
			PersistentVolumeSource: v1.PersistentVolumeSource{
				FlexVolume: &v1.FlexVolumeSource{
					Driver:  "virtuozzo/ploop",
					Options: ploop_options,
				},
			},
		},
	}

	glog.Infof("successfully created virtuozzo storage share: %s", share)

	return pv, nil
}

// Delete removes the storage asset that was created by Provision represented
// by the given PV.
func (p *vzFSProvisioner) Delete(volume *v1.PersistentVolume) error {
	ann, ok := volume.Annotations[provisionerIDAnn]
	if !ok {
		return errors.New("identity annotation not found on PV")
	}
	if ann != string(p.identity) {
		return &controller.IgnoredError{"identity annotation on PV does not match ours"}
	}
	share, ok := volume.Annotations[vzShareAnn]
	if !ok {
		return errors.New("vz share annotation not found on PV")
	}

	options := volume.Spec.PersistentVolumeSource.FlexVolume.Options
	path := options["volumePath"] + "/" + options["volumeId"]
	glog.Infof("Delete: %s", path)
	err := os.RemoveAll(path)
	if err != nil {
		return err
	}

	glog.Infof("successfully delete virtuozzo storage share: %s", share)

	return nil
}

var (
	master     = flag.String("master", "", "Master URL")
	kubeconfig = flag.String("kubeconfig", "", "Absolute path to the kubeconfig")
)

func main() {
	flag.Parse()
	flag.Set("logtostderr", "true")

	var config *rest.Config
	var err error
	if *master != "" || *kubeconfig != "" {
		config, err = clientcmd.BuildConfigFromFlags(*master, *kubeconfig)
	} else {
		config, err = rest.InClusterConfig()
	}

	if err != nil {
		glog.Fatalf("Failed to create config: %v", err)
	}
	clientset, err := kubernetes.NewForConfig(config)
	if err != nil {
		glog.Fatalf("Failed to create client: %v", err)
	}

	// The controller needs to know what the server version is because out-of-tree
	// provisioners aren't officially supported until 1.5
	serverVersion, err := clientset.Discovery().ServerVersion()
	if err != nil {
		glog.Fatalf("Error getting server version: %v", err)
	}

	// Create the provisioner: it implements the Provisioner interface expected by
	// the controller
	vzFSProvisioner := newVzFSProvisioner(clientset)

	// Start the provision controller which will dynamically provision cephFS
	// PVs
	pc := controller.NewProvisionController(clientset, resyncPeriod, provisionerName, vzFSProvisioner, serverVersion.GitVersion, exponentialBackOffOnError, failedRetryThreshold, 2*resyncPeriod, resyncPeriod, resyncPeriod/2, 2*resyncPeriod)

	pc.Run(wait.NeverStop)
}
<|endoftext|>


<commit_before>package channel

import (
	"errors"
	"net/http"
	"net/url"
	"socialapi/models"
	"socialapi/workers/api/modules/helpers"

	"github.com/jinzhu/gorm"
)

func validateChannelRequest(c *models.Channel) error {
	if c.GroupName == "" {
		return errors.New("Group name is not set")
	}

	if c.Name == "" {
		return errors.New("Channel name is not set")
	}

	if c.CreatorId == 0 {
		return errors.New("Creator id is not set")
	}

	return nil
}

func Create(u *url.URL, h http.Header, req *models.Channel) (int, http.Header, interface{}, error) {
	if req.GroupName == "" {
		req.GroupName = models.Channel_KODING_NAME
	}

	if err := validateChannelRequest(req); err != nil {
		return helpers.NewBadRequestResponse(err)
	}

	if err := req.Create(); err != nil {
		return helpers.NewBadRequestResponse(err)
	}

	return helpers.NewOKResponse(req)
}

func List(u *url.URL, h http.Header, _ interface{}) (int, http.Header, interface{}, error) {
	c := models.NewChannel()
	q := helpers.GetQuery(u)
	q.Type = models.Channel_TYPE_TOPIC
	list, err := c.List(q)

	if err != nil {
		return helpers.NewBadRequestResponse(err)
	}

	return helpers.NewOKResponse(list)
}

func Delete(u *url.URL, h http.Header, req *models.Channel) (int, http.Header, interface{}, error) {

	id, err := helpers.GetURIInt64(u, "id")
	if err != nil {
		return helpers.NewBadRequestResponse(err)
	}

	req.Id = id
	if err := req.Fetch(); err != nil {
		return helpers.NewBadRequestResponse(err)
	}

	if req.TypeConstant == models.Channel_TYPE_GROUP {
		return helpers.NewBadRequestResponse(errors.New("You can not delete group channel"))
	}
	if err := req.Delete(); err != nil {
		return helpers.NewBadRequestResponse(err)
	}
	// yes it is deleted but not removed completely from our system
	return helpers.NewDeletedResponse()
}

func Update(u *url.URL, h http.Header, req *models.Channel) (int, http.Header, interface{}, error) {
	id, err := helpers.GetURIInt64(u, "id")
	if err != nil {
		return helpers.NewBadRequestResponse(err)
	}
	req.Id = id

	if req.Id == 0 {
		return helpers.NewBadRequestResponse(err)
	}

	existingOne := models.NewChannel()
	existingOne.Id = id
	if err := existingOne.Fetch(); err != nil {
		return helpers.NewBadRequestResponse(err)
	}

	if existingOne.CreatorId != req.CreatorId {
		return helpers.NewBadRequestResponse(errors.New("CreatorId doesnt match"))
	}

	// only allow purpose and name to be updated
	if req.Purpose != "" {
		existingOne.Purpose = req.Purpose
	}

	if req.Name != "" {
		existingOne.Name = req.Name
	}

	if err := req.Update(); err != nil {
		return helpers.NewBadRequestResponse(err)
	}

	return helpers.NewOKResponse(req)
}

func Get(u *url.URL, h http.Header, req *models.Channel) (int, http.Header, interface{}, error) {
	id, err := helpers.GetURIInt64(u, "id")
	if err != nil {
		return helpers.NewBadRequestResponse(err)
	}

	req.Id = id
	if err := req.Fetch(); err != nil {
		if err == gorm.RecordNotFound {
			return helpers.NewNotFoundResponse()
		}
		return helpers.NewBadRequestResponse(err)
	}

	return helpers.NewOKResponse(req)
}

func PostMessage(u *url.URL, h http.Header, req *models.Channel) (int, http.Header, interface{}, error) {
	// id, err := helpers.GetURIInt64(u, "id")
	// if err != nil {
	// 	return helpers.NewBadRequestResponse(err)
	// }

	// req.Id = id
	// // TODO - check if the user is member of the channel

	// if err := req.Fetch(); err != nil {
	// 	if err == gorm.RecordNotFound {
	// 		return helpers.NewNotFoundResponse()
	// 	}
	// 	return helpers.NewBadRequestResponse(err)
	// }

	return helpers.NewOKResponse(req)
}
<commit_msg>Social: populate channels with participation data<commit_after>package channel

import (
	"errors"
	"net/http"
	"net/url"
	"socialapi/models"
	"socialapi/workers/api/modules/helpers"

	"github.com/jinzhu/gorm"
)

func validateChannelRequest(c *models.Channel) error {
	if c.GroupName == "" {
		return errors.New("Group name is not set")
	}

	if c.Name == "" {
		return errors.New("Channel name is not set")
	}

	if c.CreatorId == 0 {
		return errors.New("Creator id is not set")
	}

	return nil
}

func Create(u *url.URL, h http.Header, req *models.Channel) (int, http.Header, interface{}, error) {
	if req.GroupName == "" {
		req.GroupName = models.Channel_KODING_NAME
	}

	if err := validateChannelRequest(req); err != nil {
		return helpers.NewBadRequestResponse(err)
	}

	if err := req.Create(); err != nil {
		return helpers.NewBadRequestResponse(err)
	}

	return helpers.NewOKResponse(req)
}

func List(u *url.URL, h http.Header, _ interface{}) (int, http.Header, interface{}, error) {
	c := models.NewChannel()
	q := helpers.GetQuery(u)
	q.Type = models.Channel_TYPE_TOPIC
	channelList, err := c.List(q)

	if err != nil {
		return helpers.NewBadRequestResponse(err)
	}

	res := models.PopulateChannelContainer(channelList, q.AccountId)
	return helpers.NewOKResponse(res)
}

func Delete(u *url.URL, h http.Header, req *models.Channel) (int, http.Header, interface{}, error) {

	id, err := helpers.GetURIInt64(u, "id")
	if err != nil {
		return helpers.NewBadRequestResponse(err)
	}

	req.Id = id
	if err := req.Fetch(); err != nil {
		return helpers.NewBadRequestResponse(err)
	}

	if req.TypeConstant == models.Channel_TYPE_GROUP {
		return helpers.NewBadRequestResponse(errors.New("You can not delete group channel"))
	}
	if err := req.Delete(); err != nil {
		return helpers.NewBadRequestResponse(err)
	}
	// yes it is deleted but not removed completely from our system
	return helpers.NewDeletedResponse()
}

func Update(u *url.URL, h http.Header, req *models.Channel) (int, http.Header, interface{}, error) {
	id, err := helpers.GetURIInt64(u, "id")
	if err != nil {
		return helpers.NewBadRequestResponse(err)
	}
	req.Id = id

	if req.Id == 0 {
		return helpers.NewBadRequestResponse(err)
	}

	existingOne := models.NewChannel()
	existingOne.Id = id
	if err := existingOne.Fetch(); err != nil {
		return helpers.NewBadRequestResponse(err)
	}

	if existingOne.CreatorId != req.CreatorId {
		return helpers.NewBadRequestResponse(errors.New("CreatorId doesnt match"))
	}

	// only allow purpose and name to be updated
	if req.Purpose != "" {
		existingOne.Purpose = req.Purpose
	}

	if req.Name != "" {
		existingOne.Name = req.Name
	}

	if err := req.Update(); err != nil {
		return helpers.NewBadRequestResponse(err)
	}

	return helpers.NewOKResponse(req)
}

func Get(u *url.URL, h http.Header, req *models.Channel) (int, http.Header, interface{}, error) {
	id, err := helpers.GetURIInt64(u, "id")
	if err != nil {
		return helpers.NewBadRequestResponse(err)
	}

	req.Id = id
	if err := req.Fetch(); err != nil {
		if err == gorm.RecordNotFound {
			return helpers.NewNotFoundResponse()
		}
		return helpers.NewBadRequestResponse(err)
	}

	return helpers.NewOKResponse(req)
}

func PostMessage(u *url.URL, h http.Header, req *models.Channel) (int, http.Header, interface{}, error) {
	// id, err := helpers.GetURIInt64(u, "id")
	// if err != nil {
	// 	return helpers.NewBadRequestResponse(err)
	// }

	// req.Id = id
	// // TODO - check if the user is member of the channel

	// if err := req.Fetch(); err != nil {
	// 	if err == gorm.RecordNotFound {
	// 		return helpers.NewNotFoundResponse()
	// 	}
	// 	return helpers.NewBadRequestResponse(err)
	// }

	return helpers.NewOKResponse(req)
}
<|endoftext|>


<commit_before>/*
Copyright 2015 The Kubernetes Authors.

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 fuzzer

import (
	"fmt"

	"github.com/google/gofuzz"

	apitesting "k8s.io/apimachinery/pkg/api/testing"
	"k8s.io/apimachinery/pkg/api/testing/fuzzer"
	"k8s.io/apimachinery/pkg/runtime"
	runtimeserializer "k8s.io/apimachinery/pkg/runtime/serializer"
	"k8s.io/apiserver/pkg/apis/example"
	examplev1 "k8s.io/apiserver/pkg/apis/example/v1"
)

// overrideMetaFuncs override some generic fuzzer funcs from k8s.io/apiserver in order to have more realistic
// values in a Kubernetes context.
func overrideMetaFuncs(codecs runtimeserializer.CodecFactory) []interface{} {
	return []interface{}{
		func(j *runtime.Object, c fuzz.Continue) {
			// TODO: uncomment when round trip starts from a versioned object
			if true { //c.RandBool() {
				*j = &runtime.Unknown{
					// We do not set TypeMeta here because it is not carried through a round trip
					Raw:         []byte(`{"apiVersion":"unknown.group/unknown","kind":"Something","someKey":"someValue"}`),
					ContentType: runtime.ContentTypeJSON,
				}
			} else {
				types := []runtime.Object{&example.Pod{}}
				t := types[c.Rand.Intn(len(types))]
				c.Fuzz(t)
				*j = t
			}
		},
		func(r *runtime.RawExtension, c fuzz.Continue) {
			// Pick an arbitrary type and fuzz it
			types := []runtime.Object{&example.Pod{}}
			obj := types[c.Rand.Intn(len(types))]
			c.Fuzz(obj)

			// Convert the object to raw bytes
			bytes, err := runtime.Encode(apitesting.TestCodec(codecs, examplev1.SchemeGroupVersion), obj)
			if err != nil {
				panic(fmt.Sprintf("Failed to encode object: %v", err))
				return
			}

			// Set the bytes field on the RawExtension
			r.Raw = bytes
		},
	}
}

func exampleFuncs(codecs runtimeserializer.CodecFactory) []interface{} {
	return []interface{}{
		func(s *example.PodSpec, c fuzz.Continue) {
			c.FuzzNoCustom(s)
			// has a default value
			ttl := int64(30)
			if c.RandBool() {
				ttl = int64(c.Uint32())
			}
			s.TerminationGracePeriodSeconds = &ttl

			if s.SchedulerName == "" {
				s.SchedulerName = "default-scheduler"
			}
		},
		func(j *example.PodPhase, c fuzz.Continue) {
			statuses := []example.PodPhase{"Pending", "Running", "Succeeded", "Failed", "Unknown"}
			*j = statuses[c.Rand.Intn(len(statuses))]
		},
		func(rp *example.RestartPolicy, c fuzz.Continue) {
			policies := []example.RestartPolicy{"Always", "Never", "OnFailure"}
			*rp = policies[c.Rand.Intn(len(policies))]
		},
	}
}

// Funcs returns the fuzzer functions for the example api group.
var Funcs = fuzzer.MergeFuzzerFuncs(
	overrideMetaFuncs,
	exampleFuncs,
)
<commit_msg>fuzzer: remove unreachable code<commit_after>/*
Copyright 2015 The Kubernetes Authors.

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 fuzzer

import (
	"fmt"

	"github.com/google/gofuzz"

	apitesting "k8s.io/apimachinery/pkg/api/testing"
	"k8s.io/apimachinery/pkg/api/testing/fuzzer"
	"k8s.io/apimachinery/pkg/runtime"
	runtimeserializer "k8s.io/apimachinery/pkg/runtime/serializer"
	"k8s.io/apiserver/pkg/apis/example"
	examplev1 "k8s.io/apiserver/pkg/apis/example/v1"
)

// overrideMetaFuncs override some generic fuzzer funcs from k8s.io/apiserver in order to have more realistic
// values in a Kubernetes context.
func overrideMetaFuncs(codecs runtimeserializer.CodecFactory) []interface{} {
	return []interface{}{
		func(j *runtime.Object, c fuzz.Continue) {
			// TODO: uncomment when round trip starts from a versioned object
			if true { //c.RandBool() {
				*j = &runtime.Unknown{
					// We do not set TypeMeta here because it is not carried through a round trip
					Raw:         []byte(`{"apiVersion":"unknown.group/unknown","kind":"Something","someKey":"someValue"}`),
					ContentType: runtime.ContentTypeJSON,
				}
			} else {
				types := []runtime.Object{&example.Pod{}}
				t := types[c.Rand.Intn(len(types))]
				c.Fuzz(t)
				*j = t
			}
		},
		func(r *runtime.RawExtension, c fuzz.Continue) {
			// Pick an arbitrary type and fuzz it
			types := []runtime.Object{&example.Pod{}}
			obj := types[c.Rand.Intn(len(types))]
			c.Fuzz(obj)

			// Convert the object to raw bytes
			bytes, err := runtime.Encode(apitesting.TestCodec(codecs, examplev1.SchemeGroupVersion), obj)
			if err != nil {
				panic(fmt.Sprintf("Failed to encode object: %v", err))
			}

			// Set the bytes field on the RawExtension
			r.Raw = bytes
		},
	}
}

func exampleFuncs(codecs runtimeserializer.CodecFactory) []interface{} {
	return []interface{}{
		func(s *example.PodSpec, c fuzz.Continue) {
			c.FuzzNoCustom(s)
			// has a default value
			ttl := int64(30)
			if c.RandBool() {
				ttl = int64(c.Uint32())
			}
			s.TerminationGracePeriodSeconds = &ttl

			if s.SchedulerName == "" {
				s.SchedulerName = "default-scheduler"
			}
		},
		func(j *example.PodPhase, c fuzz.Continue) {
			statuses := []example.PodPhase{"Pending", "Running", "Succeeded", "Failed", "Unknown"}
			*j = statuses[c.Rand.Intn(len(statuses))]
		},
		func(rp *example.RestartPolicy, c fuzz.Continue) {
			policies := []example.RestartPolicy{"Always", "Never", "OnFailure"}
			*rp = policies[c.Rand.Intn(len(policies))]
		},
	}
}

// Funcs returns the fuzzer functions for the example api group.
var Funcs = fuzzer.MergeFuzzerFuncs(
	overrideMetaFuncs,
	exampleFuncs,
)
<|endoftext|>


<commit_before>package aws

import (
	"fmt"
	"log"
	"strings"
	"time"

	"github.com/aws/aws-sdk-go/aws"
	"github.com/aws/aws-sdk-go/service/s3"
	"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
	"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
	"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
	"github.com/terraform-providers/terraform-provider-aws/aws/internal/keyvaluetags"
)

func resourceAwsS3BucketAnalyticsConfiguration() *schema.Resource {
	return &schema.Resource{
		Create: resourceAwsS3BucketAnalyticsConfigurationPut,
		Read:   resourceAwsS3BucketAnalyticsConfigurationRead,
		Update: resourceAwsS3BucketAnalyticsConfigurationPut,
		Delete: resourceAwsS3BucketAnalyticsConfigurationDelete,
		Importer: &schema.ResourceImporter{
			State: schema.ImportStatePassthrough,
		},

		Schema: map[string]*schema.Schema{
			"name": {
				Type:     schema.TypeString,
				Required: true,
				ForceNew: true,
			},
			"bucket": {
				Type:     schema.TypeString,
				Required: true,
				ForceNew: true,
			},
			"filter": {
				Type:     schema.TypeList,
				Optional: true,
				MaxItems: 1,
				Elem: &schema.Resource{
					Schema: map[string]*schema.Schema{
						"prefix": {
							Type:         schema.TypeString,
							Optional:     true,
							AtLeastOneOf: filterAtLeastOneOfKeys,
						},
						"tags": {
							Type:         schema.TypeMap,
							Optional:     true,
							AtLeastOneOf: filterAtLeastOneOfKeys,
							Elem:         &schema.Schema{Type: schema.TypeString},
						},
					},
				},
			},
			"storage_class_analysis": {
				Type:     schema.TypeList,
				Optional: true,
				MaxItems: 1,
				Elem: &schema.Resource{
					Schema: map[string]*schema.Schema{
						"data_export": {
							Type:     schema.TypeList,
							Required: true,
							MaxItems: 1,
							Elem: &schema.Resource{
								Schema: map[string]*schema.Schema{
									"output_schema_version": {
										Type:         schema.TypeString,
										Optional:     true,
										Default:      s3.StorageClassAnalysisSchemaVersionV1,
										ValidateFunc: validation.StringInSlice([]string{s3.StorageClassAnalysisSchemaVersionV1}, false),
									},
									"destination": {
										Type:     schema.TypeList,
										Required: true,
										MaxItems: 1,
										Elem: &schema.Resource{
											Schema: map[string]*schema.Schema{
												"s3_bucket_destination": {
													Type:     schema.TypeList,
													Required: true,
													MaxItems: 1,
													Elem: &schema.Resource{
														Schema: map[string]*schema.Schema{
															"bucket_arn": {
																Type:         schema.TypeString,
																Required:     true,
																ValidateFunc: validateArn,
															},
															"bucket_account_id": {
																Type:         schema.TypeString,
																Optional:     true,
																ValidateFunc: validateAwsAccountId,
															},
															"format": {
																Type:         schema.TypeString,
																Optional:     true,
																Default:      s3.AnalyticsS3ExportFileFormatCsv,
																ValidateFunc: validation.StringInSlice([]string{s3.AnalyticsS3ExportFileFormatCsv}, false),
															},
															"prefix": {
																Type:     schema.TypeString,
																Optional: true,
															},
														},
													},
												},
											},
										},
									},
								},
							},
						},
					},
				},
			},
		},
	}
}

var filterAtLeastOneOfKeys = []string{"filter.0.prefix", "filter.0.tags"}

func resourceAwsS3BucketAnalyticsConfigurationPut(d *schema.ResourceData, meta interface{}) error {
	s3conn := meta.(*AWSClient).s3conn

	bucket := d.Get("bucket").(string)
	name := d.Get("name").(string)

	log.Printf("[DEBUG] S3 bucket %q, add analytics configuration %q", bucket, name)

	analyticsConfiguration := &s3.AnalyticsConfiguration{
		Id:                   aws.String(name),
		Filter:               expandS3AnalyticsFilter(d.Get("filter").([]interface{})),
		StorageClassAnalysis: expandS3StorageClassAnalysis(d.Get("storage_class_analysis").([]interface{})),
	}

	input := &s3.PutBucketAnalyticsConfigurationInput{
		Bucket:                 aws.String(bucket),
		Id:                     aws.String(name),
		AnalyticsConfiguration: analyticsConfiguration,
	}

	err := resource.Retry(1*time.Minute, func() *resource.RetryError {
		_, err := s3conn.PutBucketAnalyticsConfiguration(input)
		if err != nil {
			if isAWSErr(err, s3.ErrCodeNoSuchBucket, "") {
				return resource.RetryableError(err)
			}
			return resource.NonRetryableError(err)
		}
		return nil
	})
	if isResourceTimeoutError(err) {
		_, err = s3conn.PutBucketAnalyticsConfiguration(input)
	}
	if err != nil {
		return fmt.Errorf("Error adding S3 analytics configuration: %w", err)
	}

	d.SetId(fmt.Sprintf("%s:%s", bucket, name))

	return resourceAwsS3BucketAnalyticsConfigurationRead(d, meta)
}

func resourceAwsS3BucketAnalyticsConfigurationRead(d *schema.ResourceData, meta interface{}) error {
	conn := meta.(*AWSClient).s3conn

	bucket, name, err := resourceAwsS3BucketAnalyticsConfigurationParseID(d.Id())
	if err != nil {
		return err
	}

	d.Set("bucket", bucket)
	d.Set("name", name)

	input := &s3.GetBucketAnalyticsConfigurationInput{
		Bucket: aws.String(bucket),
		Id:     aws.String(name),
	}

	log.Printf("[DEBUG] Reading S3 bucket analytics configuration: %s", input)
	output, err := conn.GetBucketAnalyticsConfiguration(input)
	if err != nil {
		if isAWSErr(err, s3.ErrCodeNoSuchBucket, "") || isAWSErr(err, "NoSuchConfiguration", "The specified configuration does not exist.") {
			log.Printf("[WARN] %s S3 bucket analytics configuration not found, removing from state.", d.Id())
			d.SetId("")
			return nil
		}
		return fmt.Errorf("error getting S3 Bucket Analytics Configuration %q: %w", d.Id(), err)
	}

	if err := d.Set("filter", flattenS3AnalyticsFilter(output.AnalyticsConfiguration.Filter)); err != nil {
		return fmt.Errorf("error setting filter: %w", err)
	}

	if err = d.Set("storage_class_analysis", flattenS3StorageClassAnalysis(output.AnalyticsConfiguration.StorageClassAnalysis)); err != nil {
		return fmt.Errorf("error setting storage class anyalytics: %w", err)
	}

	return nil
}

func resourceAwsS3BucketAnalyticsConfigurationDelete(d *schema.ResourceData, meta interface{}) error {
	conn := meta.(*AWSClient).s3conn

	bucket, name, err := resourceAwsS3BucketAnalyticsConfigurationParseID(d.Id())
	if err != nil {
		return err
	}

	input := &s3.DeleteBucketAnalyticsConfigurationInput{
		Bucket: aws.String(bucket),
		Id:     aws.String(name),
	}

	log.Printf("[DEBUG] Deleting S3 bucket analytics configuration: %s", input)
	_, err = conn.DeleteBucketAnalyticsConfiguration(input)
	if err != nil {
		if isAWSErr(err, s3.ErrCodeNoSuchBucket, "") || isAWSErr(err, "NoSuchConfiguration", "The specified configuration does not exist.") {
			return nil
		}
		return fmt.Errorf("Error deleting S3 analytics configuration: %w", err)
	}

	return waitForDeleteS3BucketAnalyticsConfiguration(conn, bucket, name, 1*time.Minute)
}

func resourceAwsS3BucketAnalyticsConfigurationParseID(id string) (string, string, error) {
	idParts := strings.Split(id, ":")
	if len(idParts) != 2 {
		return "", "", fmt.Errorf("please make sure the ID is in the form BUCKET:NAME (i.e. my-bucket:EntireBucket")
	}
	bucket := idParts[0]
	name := idParts[1]
	return bucket, name, nil
}

func expandS3AnalyticsFilter(l []interface{}) *s3.AnalyticsFilter {
	if len(l) == 0 || l[0] == nil {
		return nil
	}

	m := l[0].(map[string]interface{})

	var prefix string
	if v, ok := m["prefix"]; ok {
		prefix = v.(string)
	}

	var tags []*s3.Tag
	if v, ok := m["tags"]; ok {
		tags = keyvaluetags.New(v).IgnoreAws().S3Tags()
	}

	if prefix == "" && len(tags) == 0 {
		return nil
	}
	analyticsFilter := &s3.AnalyticsFilter{}
	if prefix != "" && len(tags) > 0 {
		analyticsFilter.And = &s3.AnalyticsAndOperator{
			Prefix: aws.String(prefix),
			Tags:   tags,
		}
	} else if len(tags) > 1 {
		analyticsFilter.And = &s3.AnalyticsAndOperator{
			Tags: tags,
		}
	} else if len(tags) == 1 {
		analyticsFilter.Tag = tags[0]
	} else {
		analyticsFilter.Prefix = aws.String(prefix)
	}
	return analyticsFilter
}

func expandS3StorageClassAnalysis(l []interface{}) *s3.StorageClassAnalysis {
	result := &s3.StorageClassAnalysis{}

	if len(l) == 0 || l[0] == nil {
		return result
	}

	m := l[0].(map[string]interface{})
	if v, ok := m["data_export"]; ok {
		dataExport := &s3.StorageClassAnalysisDataExport{}
		result.DataExport = dataExport

		foo := v.([]interface{})
		if len(foo) != 0 && foo[0] != nil {
			bar := foo[0].(map[string]interface{})
			if v, ok := bar["output_schema_version"]; ok {
				dataExport.OutputSchemaVersion = aws.String(v.(string))
			}

			dataExport.Destination = expandS3AnalyticsExportDestination(bar["destination"].([]interface{}))
		}
	}

	return result
}

func expandS3AnalyticsExportDestination(edl []interface{}) *s3.AnalyticsExportDestination {
	result := &s3.AnalyticsExportDestination{}

	if len(edl) != 0 && edl[0] != nil {
		edm := edl[0].(map[string]interface{})
		result.S3BucketDestination = expandS3AnalyticsS3BucketDestination(edm["s3_bucket_destination"].([]interface{}))
	}
	return result
}

func expandS3AnalyticsS3BucketDestination(bdl []interface{}) *s3.AnalyticsS3BucketDestination {
	result := &s3.AnalyticsS3BucketDestination{}

	if len(bdl) != 0 && bdl[0] != nil {
		bdm := bdl[0].(map[string]interface{})
		result.Bucket = aws.String(bdm["bucket_arn"].(string))
		result.Format = aws.String(bdm["format"].(string))

		if v, ok := bdm["bucket_account_id"]; ok && v != "" {
			result.BucketAccountId = aws.String(v.(string))
		}

		if v, ok := bdm["prefix"]; ok && v != "" {
			result.Prefix = aws.String(v.(string))
		}
	}

	return result
}

func flattenS3AnalyticsFilter(analyticsFilter *s3.AnalyticsFilter) []map[string]interface{} {
	if analyticsFilter == nil {
		return nil
	}

	result := make(map[string]interface{})
	if analyticsFilter.And != nil {
		and := *analyticsFilter.And
		if and.Prefix != nil {
			result["prefix"] = *and.Prefix
		}
		if and.Tags != nil {
			result["tags"] = keyvaluetags.S3KeyValueTags(and.Tags).IgnoreAws().Map()
		}
	} else if analyticsFilter.Prefix != nil {
		result["prefix"] = *analyticsFilter.Prefix
	} else if analyticsFilter.Tag != nil {
		tags := []*s3.Tag{
			analyticsFilter.Tag,
		}
		result["tags"] = keyvaluetags.S3KeyValueTags(tags).IgnoreAws().Map()
	} else {
		return nil
	}
	return []map[string]interface{}{result}
}

func flattenS3StorageClassAnalysis(storageClassAnalysis *s3.StorageClassAnalysis) []map[string]interface{} {
	if storageClassAnalysis == nil || storageClassAnalysis.DataExport == nil {
		return []map[string]interface{}{}
	}

	dataExport := storageClassAnalysis.DataExport
	de := make(map[string]interface{})
	if dataExport.OutputSchemaVersion != nil {
		de["output_schema_version"] = aws.StringValue(dataExport.OutputSchemaVersion)
	}
	if dataExport.Destination != nil {
		de["destination"] = flattenS3AnalyticsExportDestination(dataExport.Destination)
	}
	result := map[string]interface{}{
		"data_export": []interface{}{de},
	}

	return []map[string]interface{}{result}
}

func flattenS3AnalyticsExportDestination(destination *s3.AnalyticsExportDestination) []interface{} {
	if destination == nil || destination.S3BucketDestination == nil {
		return []interface{}{}
	}

	return []interface{}{
		map[string]interface{}{
			"s3_bucket_destination": flattenS3AnalyticsS3BucketDestination(destination.S3BucketDestination),
		},
	}
}

func flattenS3AnalyticsS3BucketDestination(bucketDestination *s3.AnalyticsS3BucketDestination) []interface{} {
	if bucketDestination == nil {
		return nil
	}

	result := map[string]interface{}{
		"bucket_arn": aws.StringValue(bucketDestination.Bucket),
		"format":     aws.StringValue(bucketDestination.Format),
	}
	if bucketDestination.BucketAccountId != nil {
		result["bucket_account_id"] = aws.StringValue(bucketDestination.BucketAccountId)
	}
	if bucketDestination.Prefix != nil {
		result["prefix"] = aws.StringValue(bucketDestination.Prefix)
	}

	return []interface{}{result}
}

func waitForDeleteS3BucketAnalyticsConfiguration(conn *s3.S3, bucket, name string, timeout time.Duration) error {
	err := resource.Retry(timeout, func() *resource.RetryError {
		input := &s3.GetBucketAnalyticsConfigurationInput{
			Bucket: aws.String(bucket),
			Id:     aws.String(name),
		}
		log.Printf("[DEBUG] Reading S3 bucket analytics configuration: %s", input)
		output, err := conn.GetBucketAnalyticsConfiguration(input)
		if err != nil {
			if isAWSErr(err, s3.ErrCodeNoSuchBucket, "") || isAWSErr(err, "NoSuchConfiguration", "The specified configuration does not exist.") {
				return nil
			}
			return resource.NonRetryableError(err)
		}
		if output.AnalyticsConfiguration != nil {
			return resource.RetryableError(fmt.Errorf("S3 bucket analytics configuration exists: %v", output))
		}

		return nil
	})

	if err != nil {
		return fmt.Errorf("error deleting S3 Bucket Analytics Configuration \"%s:%s\": %w", bucket, name, err)
	}
	return nil
}
<commit_msg>resource/aws_s3_bucket_analytics_configuration: Check resource.TimeoutError on deletion (#15529)<commit_after>package aws

import (
	"fmt"
	"log"
	"strings"
	"time"

	"github.com/aws/aws-sdk-go/aws"
	"github.com/aws/aws-sdk-go/service/s3"
	"github.com/hashicorp/aws-sdk-go-base/tfawserr"
	"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
	"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
	"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
	"github.com/terraform-providers/terraform-provider-aws/aws/internal/keyvaluetags"
	"github.com/terraform-providers/terraform-provider-aws/aws/internal/tfresource"
)

func resourceAwsS3BucketAnalyticsConfiguration() *schema.Resource {
	return &schema.Resource{
		Create: resourceAwsS3BucketAnalyticsConfigurationPut,
		Read:   resourceAwsS3BucketAnalyticsConfigurationRead,
		Update: resourceAwsS3BucketAnalyticsConfigurationPut,
		Delete: resourceAwsS3BucketAnalyticsConfigurationDelete,
		Importer: &schema.ResourceImporter{
			State: schema.ImportStatePassthrough,
		},

		Schema: map[string]*schema.Schema{
			"name": {
				Type:     schema.TypeString,
				Required: true,
				ForceNew: true,
			},
			"bucket": {
				Type:     schema.TypeString,
				Required: true,
				ForceNew: true,
			},
			"filter": {
				Type:     schema.TypeList,
				Optional: true,
				MaxItems: 1,
				Elem: &schema.Resource{
					Schema: map[string]*schema.Schema{
						"prefix": {
							Type:         schema.TypeString,
							Optional:     true,
							AtLeastOneOf: filterAtLeastOneOfKeys,
						},
						"tags": {
							Type:         schema.TypeMap,
							Optional:     true,
							AtLeastOneOf: filterAtLeastOneOfKeys,
							Elem:         &schema.Schema{Type: schema.TypeString},
						},
					},
				},
			},
			"storage_class_analysis": {
				Type:     schema.TypeList,
				Optional: true,
				MaxItems: 1,
				Elem: &schema.Resource{
					Schema: map[string]*schema.Schema{
						"data_export": {
							Type:     schema.TypeList,
							Required: true,
							MaxItems: 1,
							Elem: &schema.Resource{
								Schema: map[string]*schema.Schema{
									"output_schema_version": {
										Type:         schema.TypeString,
										Optional:     true,
										Default:      s3.StorageClassAnalysisSchemaVersionV1,
										ValidateFunc: validation.StringInSlice([]string{s3.StorageClassAnalysisSchemaVersionV1}, false),
									},
									"destination": {
										Type:     schema.TypeList,
										Required: true,
										MaxItems: 1,
										Elem: &schema.Resource{
											Schema: map[string]*schema.Schema{
												"s3_bucket_destination": {
													Type:     schema.TypeList,
													Required: true,
													MaxItems: 1,
													Elem: &schema.Resource{
														Schema: map[string]*schema.Schema{
															"bucket_arn": {
																Type:         schema.TypeString,
																Required:     true,
																ValidateFunc: validateArn,
															},
															"bucket_account_id": {
																Type:         schema.TypeString,
																Optional:     true,
																ValidateFunc: validateAwsAccountId,
															},
															"format": {
																Type:         schema.TypeString,
																Optional:     true,
																Default:      s3.AnalyticsS3ExportFileFormatCsv,
																ValidateFunc: validation.StringInSlice([]string{s3.AnalyticsS3ExportFileFormatCsv}, false),
															},
															"prefix": {
																Type:     schema.TypeString,
																Optional: true,
															},
														},
													},
												},
											},
										},
									},
								},
							},
						},
					},
				},
			},
		},
	}
}

var filterAtLeastOneOfKeys = []string{"filter.0.prefix", "filter.0.tags"}

func resourceAwsS3BucketAnalyticsConfigurationPut(d *schema.ResourceData, meta interface{}) error {
	s3conn := meta.(*AWSClient).s3conn

	bucket := d.Get("bucket").(string)
	name := d.Get("name").(string)

	log.Printf("[DEBUG] S3 bucket %q, add analytics configuration %q", bucket, name)

	analyticsConfiguration := &s3.AnalyticsConfiguration{
		Id:                   aws.String(name),
		Filter:               expandS3AnalyticsFilter(d.Get("filter").([]interface{})),
		StorageClassAnalysis: expandS3StorageClassAnalysis(d.Get("storage_class_analysis").([]interface{})),
	}

	input := &s3.PutBucketAnalyticsConfigurationInput{
		Bucket:                 aws.String(bucket),
		Id:                     aws.String(name),
		AnalyticsConfiguration: analyticsConfiguration,
	}

	err := resource.Retry(1*time.Minute, func() *resource.RetryError {
		_, err := s3conn.PutBucketAnalyticsConfiguration(input)
		if err != nil {
			if isAWSErr(err, s3.ErrCodeNoSuchBucket, "") {
				return resource.RetryableError(err)
			}
			return resource.NonRetryableError(err)
		}
		return nil
	})
	if isResourceTimeoutError(err) {
		_, err = s3conn.PutBucketAnalyticsConfiguration(input)
	}
	if err != nil {
		return fmt.Errorf("Error adding S3 analytics configuration: %w", err)
	}

	d.SetId(fmt.Sprintf("%s:%s", bucket, name))

	return resourceAwsS3BucketAnalyticsConfigurationRead(d, meta)
}

func resourceAwsS3BucketAnalyticsConfigurationRead(d *schema.ResourceData, meta interface{}) error {
	conn := meta.(*AWSClient).s3conn

	bucket, name, err := resourceAwsS3BucketAnalyticsConfigurationParseID(d.Id())
	if err != nil {
		return err
	}

	d.Set("bucket", bucket)
	d.Set("name", name)

	input := &s3.GetBucketAnalyticsConfigurationInput{
		Bucket: aws.String(bucket),
		Id:     aws.String(name),
	}

	log.Printf("[DEBUG] Reading S3 bucket analytics configuration: %s", input)
	output, err := conn.GetBucketAnalyticsConfiguration(input)
	if err != nil {
		if isAWSErr(err, s3.ErrCodeNoSuchBucket, "") || isAWSErr(err, "NoSuchConfiguration", "The specified configuration does not exist.") {
			log.Printf("[WARN] %s S3 bucket analytics configuration not found, removing from state.", d.Id())
			d.SetId("")
			return nil
		}
		return fmt.Errorf("error getting S3 Bucket Analytics Configuration %q: %w", d.Id(), err)
	}

	if err := d.Set("filter", flattenS3AnalyticsFilter(output.AnalyticsConfiguration.Filter)); err != nil {
		return fmt.Errorf("error setting filter: %w", err)
	}

	if err = d.Set("storage_class_analysis", flattenS3StorageClassAnalysis(output.AnalyticsConfiguration.StorageClassAnalysis)); err != nil {
		return fmt.Errorf("error setting storage class anyalytics: %w", err)
	}

	return nil
}

func resourceAwsS3BucketAnalyticsConfigurationDelete(d *schema.ResourceData, meta interface{}) error {
	conn := meta.(*AWSClient).s3conn

	bucket, name, err := resourceAwsS3BucketAnalyticsConfigurationParseID(d.Id())
	if err != nil {
		return err
	}

	input := &s3.DeleteBucketAnalyticsConfigurationInput{
		Bucket: aws.String(bucket),
		Id:     aws.String(name),
	}

	log.Printf("[DEBUG] Deleting S3 bucket analytics configuration: %s", input)
	_, err = conn.DeleteBucketAnalyticsConfiguration(input)
	if err != nil {
		if isAWSErr(err, s3.ErrCodeNoSuchBucket, "") || isAWSErr(err, "NoSuchConfiguration", "The specified configuration does not exist.") {
			return nil
		}
		return fmt.Errorf("Error deleting S3 analytics configuration: %w", err)
	}

	return waitForDeleteS3BucketAnalyticsConfiguration(conn, bucket, name, 1*time.Minute)
}

func resourceAwsS3BucketAnalyticsConfigurationParseID(id string) (string, string, error) {
	idParts := strings.Split(id, ":")
	if len(idParts) != 2 {
		return "", "", fmt.Errorf("please make sure the ID is in the form BUCKET:NAME (i.e. my-bucket:EntireBucket")
	}
	bucket := idParts[0]
	name := idParts[1]
	return bucket, name, nil
}

func expandS3AnalyticsFilter(l []interface{}) *s3.AnalyticsFilter {
	if len(l) == 0 || l[0] == nil {
		return nil
	}

	m := l[0].(map[string]interface{})

	var prefix string
	if v, ok := m["prefix"]; ok {
		prefix = v.(string)
	}

	var tags []*s3.Tag
	if v, ok := m["tags"]; ok {
		tags = keyvaluetags.New(v).IgnoreAws().S3Tags()
	}

	if prefix == "" && len(tags) == 0 {
		return nil
	}
	analyticsFilter := &s3.AnalyticsFilter{}
	if prefix != "" && len(tags) > 0 {
		analyticsFilter.And = &s3.AnalyticsAndOperator{
			Prefix: aws.String(prefix),
			Tags:   tags,
		}
	} else if len(tags) > 1 {
		analyticsFilter.And = &s3.AnalyticsAndOperator{
			Tags: tags,
		}
	} else if len(tags) == 1 {
		analyticsFilter.Tag = tags[0]
	} else {
		analyticsFilter.Prefix = aws.String(prefix)
	}
	return analyticsFilter
}

func expandS3StorageClassAnalysis(l []interface{}) *s3.StorageClassAnalysis {
	result := &s3.StorageClassAnalysis{}

	if len(l) == 0 || l[0] == nil {
		return result
	}

	m := l[0].(map[string]interface{})
	if v, ok := m["data_export"]; ok {
		dataExport := &s3.StorageClassAnalysisDataExport{}
		result.DataExport = dataExport

		foo := v.([]interface{})
		if len(foo) != 0 && foo[0] != nil {
			bar := foo[0].(map[string]interface{})
			if v, ok := bar["output_schema_version"]; ok {
				dataExport.OutputSchemaVersion = aws.String(v.(string))
			}

			dataExport.Destination = expandS3AnalyticsExportDestination(bar["destination"].([]interface{}))
		}
	}

	return result
}

func expandS3AnalyticsExportDestination(edl []interface{}) *s3.AnalyticsExportDestination {
	result := &s3.AnalyticsExportDestination{}

	if len(edl) != 0 && edl[0] != nil {
		edm := edl[0].(map[string]interface{})
		result.S3BucketDestination = expandS3AnalyticsS3BucketDestination(edm["s3_bucket_destination"].([]interface{}))
	}
	return result
}

func expandS3AnalyticsS3BucketDestination(bdl []interface{}) *s3.AnalyticsS3BucketDestination {
	result := &s3.AnalyticsS3BucketDestination{}

	if len(bdl) != 0 && bdl[0] != nil {
		bdm := bdl[0].(map[string]interface{})
		result.Bucket = aws.String(bdm["bucket_arn"].(string))
		result.Format = aws.String(bdm["format"].(string))

		if v, ok := bdm["bucket_account_id"]; ok && v != "" {
			result.BucketAccountId = aws.String(v.(string))
		}

		if v, ok := bdm["prefix"]; ok && v != "" {
			result.Prefix = aws.String(v.(string))
		}
	}

	return result
}

func flattenS3AnalyticsFilter(analyticsFilter *s3.AnalyticsFilter) []map[string]interface{} {
	if analyticsFilter == nil {
		return nil
	}

	result := make(map[string]interface{})
	if analyticsFilter.And != nil {
		and := *analyticsFilter.And
		if and.Prefix != nil {
			result["prefix"] = *and.Prefix
		}
		if and.Tags != nil {
			result["tags"] = keyvaluetags.S3KeyValueTags(and.Tags).IgnoreAws().Map()
		}
	} else if analyticsFilter.Prefix != nil {
		result["prefix"] = *analyticsFilter.Prefix
	} else if analyticsFilter.Tag != nil {
		tags := []*s3.Tag{
			analyticsFilter.Tag,
		}
		result["tags"] = keyvaluetags.S3KeyValueTags(tags).IgnoreAws().Map()
	} else {
		return nil
	}
	return []map[string]interface{}{result}
}

func flattenS3StorageClassAnalysis(storageClassAnalysis *s3.StorageClassAnalysis) []map[string]interface{} {
	if storageClassAnalysis == nil || storageClassAnalysis.DataExport == nil {
		return []map[string]interface{}{}
	}

	dataExport := storageClassAnalysis.DataExport
	de := make(map[string]interface{})
	if dataExport.OutputSchemaVersion != nil {
		de["output_schema_version"] = aws.StringValue(dataExport.OutputSchemaVersion)
	}
	if dataExport.Destination != nil {
		de["destination"] = flattenS3AnalyticsExportDestination(dataExport.Destination)
	}
	result := map[string]interface{}{
		"data_export": []interface{}{de},
	}

	return []map[string]interface{}{result}
}

func flattenS3AnalyticsExportDestination(destination *s3.AnalyticsExportDestination) []interface{} {
	if destination == nil || destination.S3BucketDestination == nil {
		return []interface{}{}
	}

	return []interface{}{
		map[string]interface{}{
			"s3_bucket_destination": flattenS3AnalyticsS3BucketDestination(destination.S3BucketDestination),
		},
	}
}

func flattenS3AnalyticsS3BucketDestination(bucketDestination *s3.AnalyticsS3BucketDestination) []interface{} {
	if bucketDestination == nil {
		return nil
	}

	result := map[string]interface{}{
		"bucket_arn": aws.StringValue(bucketDestination.Bucket),
		"format":     aws.StringValue(bucketDestination.Format),
	}
	if bucketDestination.BucketAccountId != nil {
		result["bucket_account_id"] = aws.StringValue(bucketDestination.BucketAccountId)
	}
	if bucketDestination.Prefix != nil {
		result["prefix"] = aws.StringValue(bucketDestination.Prefix)
	}

	return []interface{}{result}
}

func waitForDeleteS3BucketAnalyticsConfiguration(conn *s3.S3, bucket, name string, timeout time.Duration) error {
	input := &s3.GetBucketAnalyticsConfigurationInput{
		Bucket: aws.String(bucket),
		Id:     aws.String(name),
	}

	err := resource.Retry(timeout, func() *resource.RetryError {
		output, err := conn.GetBucketAnalyticsConfiguration(input)

		if err != nil {
			return resource.NonRetryableError(err)
		}

		if output != nil && output.AnalyticsConfiguration != nil {
			return resource.RetryableError(fmt.Errorf("S3 bucket analytics configuration exists: %v", output))
		}

		return nil
	})

	if tfresource.TimedOut(err) {
		_, err = conn.GetBucketAnalyticsConfiguration(input)
	}

	if tfawserr.ErrCodeEquals(err, s3.ErrCodeNoSuchBucket) || tfawserr.ErrMessageContains(err, "NoSuchConfiguration", "The specified configuration does not exist.") {
		return nil
	}

	if err != nil {
		return fmt.Errorf("error deleting S3 Bucket Analytics Configuration \"%s:%s\": %w", bucket, name, err)
	}

	return nil
}
<|endoftext|>


<commit_before>package settings

import (
	"errors"
	"fmt"
	"gopkg.in/yaml.v2"
	"io"
	"io/ioutil"
	"os"
	"strconv"
	"strings"
)

var KeyError error = errors.New("key not found")
var TypeError error = errors.New("invalid type conversion")

type Settings struct {
	Key    string
	Values map[interface{}]interface{}
}

// Parse the provided YAML into a new Settings object.
func Parse(data []byte) (*Settings, error) {
	values := make(map[interface{}]interface{})
	if err := yaml.Unmarshal(data, values); err == nil {
		return &Settings{Values: values}, nil
	} else {
		return nil, err
	}
}

// Read and parse settings from the provided reader.
func Read(reader io.Reader) (*Settings, error) {
	if data, err := ioutil.ReadAll(reader); err == nil {
		return Parse(data)
	} else {
		return nil, err
	}
}

// Load and parse settings from the file at the provided path.
func Load(path string) (*Settings, error) {
	if file, err := os.Open(path); err == nil {
		defer file.Close()
		return Read(file)
	} else {
		return nil, err
	}
}

// Get a value from the settings object.
func (s *Settings) Raw(key string) (interface{}, error) {
	names := strings.Split(key, ".")
	var data interface{} = s.Values
	for _, name := range names {
		if items, ok := data.(map[interface{}]interface{}); ok {
			if data, ok = items[name]; !ok {
				return nil, KeyError
			}
		} else if items, ok := data.([]interface{}); ok {
			if n, err := strconv.Atoi(name); err == nil {
				if n < len(items) {
					data = items[n]
				} else {
					return nil, KeyError
				}
			} else {
				return nil, KeyError
			}
		} else {
			return nil, TypeError
		}
	}
	return data, nil
}

// Get a settings object.
func (s *Settings) Object(key string) (*Settings, error) {
	if value, err := s.Raw(key); err == nil {
		if mapping, ok := value.(map[interface{}]interface{}); ok {
			return &Settings{Key: key, Values: mapping}, nil
		} else {
			return nil, TypeError
		}
	} else {
		return nil, err
	}
}

// Get an array of settings objects.
func (s *Settings) ObjectArray(key string) ([]*Settings, error) {
	if value, err := s.Raw(key); err == nil {
		if items, ok := value.([]interface{}); ok {
			array := make([]*Settings, len(items))
			for n, item := range items {
				if mapping, ok := item.(map[interface{}]interface{}); ok {
					settingsKey := fmt.Sprintf("%s.%d", key, n)
					array[n] = &Settings{Key: settingsKey, Values: mapping}
				} else {
					return nil, TypeError
				}
			}
			return array, nil
		} else {
			return nil, TypeError
		}
	} else {
		return nil, err
	}
}

// Get a string value.
func (s *Settings) String(key string) (string, error) {
	if value, err := s.Raw(key); err == nil {
		if valueStr, ok := value.(string); ok {
			return valueStr, nil
		} else {
			return "", TypeError
		}
	} else {
		return "", err
	}
}

// Get an array of string values.
func (s *Settings) StringArray(key string) ([]string, error) {
	if value, err := s.Raw(key); err == nil {
		if items, ok := value.([]interface{}); ok {
			array := make([]string, len(items))
			for n, item := range items {
				switch item.(type) {
				case string:
					array[n] = item.(string)
				default:
					return nil, TypeError
				}
			}
			return array, nil
		} else {
			return nil, TypeError
		}
	} else {
		return nil, err
	}
}

// Get an integer value.
func (s *Settings) Int(key string) (int, error) {
	if value, err := s.Raw(key); err == nil {
		if valueInt, ok := value.(int); ok {
			return valueInt, nil
		} else {
			return 0, TypeError
		}
	} else {
		return 0, err
	}
}

// Get an array of integer values.
func (s *Settings) IntArray(key string) ([]int, error) {
	if value, err := s.Raw(key); err == nil {
		if items, ok := value.([]interface{}); ok {
			array := make([]int, len(items))
			for n, item := range items {
				switch item.(type) {
				case int:
					array[n] = item.(int)
				default:
					return nil, TypeError
				}
			}
			return array, nil
		} else {
			return nil, TypeError
		}
	} else {
		return nil, err
	}
}

// Get a float value.
func (s *Settings) Float(key string) (float64, error) {
	if value, err := s.Raw(key); err == nil {
		switch value.(type) {
		case float64:
			return value.(float64), nil
		case int:
			return float64(value.(int)), nil
		default:
			return 0, TypeError
		}
	} else {
		return 0, err
	}
}

// Get an array of float values.
func (s *Settings) FloatArray(key string) ([]float64, error) {
	if value, err := s.Raw(key); err == nil {
		if items, ok := value.([]interface{}); ok {
			array := make([]float64, len(items))
			for n, item := range items {
				switch item.(type) {
				case float64:
					array[n] = item.(float64)
				case int:
					array[n] = float64(item.(int))
				default:
					return nil, TypeError
				}
			}
			return array, nil
		} else {
			return nil, TypeError
		}
	} else {
		return nil, err
	}
}

// Get a boolean value.
func (s *Settings) Bool(key string) (bool, error) {
	if value, err := s.Raw(key); err == nil {
		switch value.(type) {
		case bool:
			return value.(bool), nil
		case int:
			return value.(int) != 0, nil
		case float64:
			return value.(float64) != 0, nil
		case string:
			if valueBool, err := strconv.ParseBool(value.(string)); err == nil {
				return valueBool, nil
			} else {
				return false, TypeError
			}
		default:
			return false, TypeError
		}
	} else {
		return false, err
	}
}

// Get an array of boolean values.
func (s *Settings) BoolArray(key string) ([]bool, error) {
	if value, err := s.Raw(key); err == nil {
		if items, ok := value.([]interface{}); ok {
			array := make([]bool, len(items))
			for n, item := range items {
				switch item.(type) {
				case bool:
					array[n] = item.(bool)
				case int:
					array[n] = item.(int) != 0
				case float64:
					array[n] = item.(float64) != 0
				case string:
					if valueBool, err := strconv.ParseBool(item.(string)); err == nil {
						array[n] = valueBool
					} else {
						return nil, TypeError
					}
				default:
					return nil, TypeError
				}
			}
			return array, nil
		} else {
			return nil, TypeError
		}
	} else {
		return nil, err
	}
}
<commit_msg>Add LoadOrExit function.<commit_after>package settings

import (
	"errors"
	"fmt"
	"gopkg.in/yaml.v2"
	"io"
	"io/ioutil"
	"os"
	"strconv"
	"strings"
)

var KeyError error = errors.New("key not found")
var TypeError error = errors.New("invalid type conversion")

type Settings struct {
	Key    string
	Values map[interface{}]interface{}
}

// Parse the provided YAML into a new Settings object.
func Parse(data []byte) (*Settings, error) {
	values := make(map[interface{}]interface{})
	if err := yaml.Unmarshal(data, values); err == nil {
		return &Settings{Values: values}, nil
	} else {
		return nil, err
	}
}

// Read and parse settings from the provided reader.
func Read(reader io.Reader) (*Settings, error) {
	if data, err := ioutil.ReadAll(reader); err == nil {
		return Parse(data)
	} else {
		return nil, err
	}
}

// Load and parse settings from the file at the provided path.
func Load(path string) (*Settings, error) {
	if file, err := os.Open(path); err == nil {
		defer file.Close()
		return Read(file)
	} else {
		return nil, err
	}
}

// Load and parse settings from the file at the provided path. If an error
// occurs print it to stderr and call os.Exit(1).
func LoadOrExit(path string) *Settings {
	settings, err := Load(path)
	if err != nil {
		fmt.Fprintln(os.Stderr, err)
		os.Exit(1)
	}
	return settings
}

// Get a value from the settings object.
func (s *Settings) Raw(key string) (interface{}, error) {
	names := strings.Split(key, ".")
	var data interface{} = s.Values
	for _, name := range names {
		if items, ok := data.(map[interface{}]interface{}); ok {
			if data, ok = items[name]; !ok {
				return nil, KeyError
			}
		} else if items, ok := data.([]interface{}); ok {
			if n, err := strconv.Atoi(name); err == nil {
				if n < len(items) {
					data = items[n]
				} else {
					return nil, KeyError
				}
			} else {
				return nil, KeyError
			}
		} else {
			return nil, TypeError
		}
	}
	return data, nil
}

// Get a settings object.
func (s *Settings) Object(key string) (*Settings, error) {
	if value, err := s.Raw(key); err == nil {
		if mapping, ok := value.(map[interface{}]interface{}); ok {
			return &Settings{Key: key, Values: mapping}, nil
		} else {
			return nil, TypeError
		}
	} else {
		return nil, err
	}
}

// Get an array of settings objects.
func (s *Settings) ObjectArray(key string) ([]*Settings, error) {
	if value, err := s.Raw(key); err == nil {
		if items, ok := value.([]interface{}); ok {
			array := make([]*Settings, len(items))
			for n, item := range items {
				if mapping, ok := item.(map[interface{}]interface{}); ok {
					settingsKey := fmt.Sprintf("%s.%d", key, n)
					array[n] = &Settings{Key: settingsKey, Values: mapping}
				} else {
					return nil, TypeError
				}
			}
			return array, nil
		} else {
			return nil, TypeError
		}
	} else {
		return nil, err
	}
}

// Get a string value.
func (s *Settings) String(key string) (string, error) {
	if value, err := s.Raw(key); err == nil {
		if valueStr, ok := value.(string); ok {
			return valueStr, nil
		} else {
			return "", TypeError
		}
	} else {
		return "", err
	}
}

// Get an array of string values.
func (s *Settings) StringArray(key string) ([]string, error) {
	if value, err := s.Raw(key); err == nil {
		if items, ok := value.([]interface{}); ok {
			array := make([]string, len(items))
			for n, item := range items {
				switch item.(type) {
				case string:
					array[n] = item.(string)
				default:
					return nil, TypeError
				}
			}
			return array, nil
		} else {
			return nil, TypeError
		}
	} else {
		return nil, err
	}
}

// Get an integer value.
func (s *Settings) Int(key string) (int, error) {
	if value, err := s.Raw(key); err == nil {
		if valueInt, ok := value.(int); ok {
			return valueInt, nil
		} else {
			return 0, TypeError
		}
	} else {
		return 0, err
	}
}

// Get an array of integer values.
func (s *Settings) IntArray(key string) ([]int, error) {
	if value, err := s.Raw(key); err == nil {
		if items, ok := value.([]interface{}); ok {
			array := make([]int, len(items))
			for n, item := range items {
				switch item.(type) {
				case int:
					array[n] = item.(int)
				default:
					return nil, TypeError
				}
			}
			return array, nil
		} else {
			return nil, TypeError
		}
	} else {
		return nil, err
	}
}

// Get a float value.
func (s *Settings) Float(key string) (float64, error) {
	if value, err := s.Raw(key); err == nil {
		switch value.(type) {
		case float64:
			return value.(float64), nil
		case int:
			return float64(value.(int)), nil
		default:
			return 0, TypeError
		}
	} else {
		return 0, err
	}
}

// Get an array of float values.
func (s *Settings) FloatArray(key string) ([]float64, error) {
	if value, err := s.Raw(key); err == nil {
		if items, ok := value.([]interface{}); ok {
			array := make([]float64, len(items))
			for n, item := range items {
				switch item.(type) {
				case float64:
					array[n] = item.(float64)
				case int:
					array[n] = float64(item.(int))
				default:
					return nil, TypeError
				}
			}
			return array, nil
		} else {
			return nil, TypeError
		}
	} else {
		return nil, err
	}
}

// Get a boolean value.
func (s *Settings) Bool(key string) (bool, error) {
	if value, err := s.Raw(key); err == nil {
		switch value.(type) {
		case bool:
			return value.(bool), nil
		case int:
			return value.(int) != 0, nil
		case float64:
			return value.(float64) != 0, nil
		case string:
			if valueBool, err := strconv.ParseBool(value.(string)); err == nil {
				return valueBool, nil
			} else {
				return false, TypeError
			}
		default:
			return false, TypeError
		}
	} else {
		return false, err
	}
}

// Get an array of boolean values.
func (s *Settings) BoolArray(key string) ([]bool, error) {
	if value, err := s.Raw(key); err == nil {
		if items, ok := value.([]interface{}); ok {
			array := make([]bool, len(items))
			for n, item := range items {
				switch item.(type) {
				case bool:
					array[n] = item.(bool)
				case int:
					array[n] = item.(int) != 0
				case float64:
					array[n] = item.(float64) != 0
				case string:
					if valueBool, err := strconv.ParseBool(item.(string)); err == nil {
						array[n] = valueBool
					} else {
						return nil, TypeError
					}
				default:
					return nil, TypeError
				}
			}
			return array, nil
		} else {
			return nil, TypeError
		}
	} else {
		return nil, err
	}
}
<|endoftext|>