<commit_before>package dblentry

import "github.com/prataprc/goparsec"

var ytok_equal = parsec.Token("=", "EQUAL")
var ytok_currency = parsec.Token(`[^0-9 \t\r\n.,;:?!/@+*/^&|=<>(){}\[\]-]+`, "CURRENCY")
var ytok_amount = parsec.Token(`[0-9,.-]+`, "AMOUNT")
var ytok_commodity = parsec.Token(`[^0-9 \t\r\n.,;:?!/@+*/^&|=<>(){}\[\]-]+`, "COMMODITY")
var ytok_assert = parsec.Token("assert", "ASSERT")
var ytok_expr = parsec.Token(`\{.*\}`, "EXPRESSION")
var ytok_yearval = parsec.Token(`[0-9]{4}`, "YEAR")
var ytok_commentchar = parsec.Token(`[;|#*]`, "COMMENTCHAR")
var ytok_commentline = parsec.Token(`.*`, "COMMENTLINE")

//---- Transaction tokens
type Transnote string

var accchars = `~!^()_\{}\[\]:;"'<>,.?/-`
var ytok_accname = parsec.Token(`[a-zA-Z][0-9a-zA-Z `+accchars+`]*`, "FULLACCNM")
var ytok_vaccname = parsec.Token(`\([a-zA-Z][0-9a-zA-Z: `+accchars+`]*\)`, "VFULLACCNM")
var ytok_baccname = parsec.Token(`\[[a-zA-Z][0-9a-zA-Z: `+accchars+`]*\]`, "BFULLACCNM")

var ytok_prefix = parsec.Token(`\*|!`, "TRANSPREFIX")
var ytok_code = parsec.Token(`\(.*\)`, "TRANSCODE")
var ytok_payeestr = parsec.Token("([^ \t]+[ ]?)+", "TRANSPAYEE")
var ytok_transnote = parsec.Token(";.*", "TRANSNOTE")

//---- Posting tokens

var ytok_postacc1 = parsec.Token(`[a-zA-Z]([0-9a-zA-Z`+accchars+`]* )*([  ]|[\t])`, "POSTACCN1")
var ytok_postnote = parsec.Token(";[^;]+", "POSTNOTE")
var ytok_at = parsec.Token("@", "COSTAT")
var ytok_atat = parsec.Token("@@", "COSTATAT")
var ytok_openparan = parsec.Token("{", "OPENPARAN")
var ytok_openopenparan = parsec.Token("{{", "OPENOPENPARAN")
var ytok_closeparan = parsec.Token("}", "CLOSECLOSEPARAN")
var ytok_closecloseparan = parsec.Token("}}", "CLOSEPARAN")
var ytok_opensqrt = parsec.Token(`\[`, "OPENSQRT")
var ytok_closesqrt = parsec.Token(`\]`, "CLOSESQRT")

//---- Price tokens

var ytok_price = parsec.Token(";[^;]+", "TRANSPRICE")

//---- Directives
var ytok_account = parsec.Token("account", "DRTV_ACCOUNT")
var ytok_note = parsec.Token("note", "DRTV_ACCOUNT_NOTE")
var ytok_alias = parsec.Token("alias", "DRTV_ACCOUNT_ALIAS")
var ytok_payee = parsec.Token("payee", "DRTV_ACCOUNT_PAYEE")
var ytok_check = parsec.Token("check", "DRTV_ACCOUNT_CHECK")
var ytok_eval = parsec.Token("eval", "DRTV_ACCOUNT_EVAL")
var ytok_default = parsec.Token("default", "DRTV_ACCOUNT_DEFAULT")
var ytok_value = parsec.Token(".*", "DRTV_VALUE")

var ytok_apply = parsec.Token("apply", "DRTV_APPLY")
var ytok_aliasname = parsec.Token("[^=]+", "DRTV_ALIASNAME")
var ytok_end = parsec.Token("end", "DRTV_END")
var ytok_year = parsec.Token("year", "DRTV_YEAR")

// tags
var ytok_colon = parsec.Token(":", "COLON")
var ytok_tag = parsec.Token(":[^ \t\r\n]", "TAG")
var ytok_tagk = parsec.Token("[^ \t\r\n]:[ \t]", "TAGKEY")
var ytok_tagv = parsec.Token(".+", "TAGVALUE")

//
func maybenode(nodes []parsec.ParsecNode) parsec.ParsecNode {
	if nodes == nil || len(nodes) == 0 {
		return nil
	}
	return nodes[0]
}

func Vector2scalar(nodes []parsec.ParsecNode) parsec.ParsecNode {
	return nodes[0]
}
<commit_msg>parsing: support `&` char in account name.<commit_after>package dblentry

import "github.com/prataprc/goparsec"

var ytok_equal = parsec.Token("=", "EQUAL")
var ytok_currency = parsec.Token(`[^0-9 \t\r\n.,;:?!/@+*/^&|=<>(){}\[\]-]+`, "CURRENCY")
var ytok_amount = parsec.Token(`[0-9,.-]+`, "AMOUNT")
var ytok_commodity = parsec.Token(`[^0-9 \t\r\n.,;:?!/@+*/^&|=<>(){}\[\]-]+`, "COMMODITY")
var ytok_assert = parsec.Token("assert", "ASSERT")
var ytok_expr = parsec.Token(`\{.*\}`, "EXPRESSION")
var ytok_yearval = parsec.Token(`[0-9]{4}`, "YEAR")
var ytok_commentchar = parsec.Token(`[;|#*]`, "COMMENTCHAR")
var ytok_commentline = parsec.Token(`.*`, "COMMENTLINE")

//---- Transaction tokens
type Transnote string

var accchars = `\&~!^()_\{}\[\]:;"'<>,.?/-`
var ytok_accname = parsec.Token(`[a-zA-Z][0-9a-zA-Z `+accchars+`]*`, "FULLACCNM")
var ytok_vaccname = parsec.Token(`\([a-zA-Z][0-9a-zA-Z: `+accchars+`]*\)`, "VFULLACCNM")
var ytok_baccname = parsec.Token(`\[[a-zA-Z][0-9a-zA-Z: `+accchars+`]*\]`, "BFULLACCNM")

var ytok_prefix = parsec.Token(`\*|!`, "TRANSPREFIX")
var ytok_code = parsec.Token(`\(.*\)`, "TRANSCODE")
var ytok_payeestr = parsec.Token("([^ \t]+[ ]?)+", "TRANSPAYEE")
var ytok_transnote = parsec.Token(";.*", "TRANSNOTE")

//---- Posting tokens

var ytok_postacc1 = parsec.Token(`[a-zA-Z]([0-9a-zA-Z`+accchars+`]* )*([  ]|[\t])`, "POSTACCN1")
var ytok_postnote = parsec.Token(";[^;]+", "POSTNOTE")
var ytok_at = parsec.Token("@", "COSTAT")
var ytok_atat = parsec.Token("@@", "COSTATAT")
var ytok_openparan = parsec.Token("{", "OPENPARAN")
var ytok_openopenparan = parsec.Token("{{", "OPENOPENPARAN")
var ytok_closeparan = parsec.Token("}", "CLOSECLOSEPARAN")
var ytok_closecloseparan = parsec.Token("}}", "CLOSEPARAN")
var ytok_opensqrt = parsec.Token(`\[`, "OPENSQRT")
var ytok_closesqrt = parsec.Token(`\]`, "CLOSESQRT")

//---- Price tokens

var ytok_price = parsec.Token(";[^;]+", "TRANSPRICE")

//---- Directives
var ytok_account = parsec.Token("account", "DRTV_ACCOUNT")
var ytok_note = parsec.Token("note", "DRTV_ACCOUNT_NOTE")
var ytok_alias = parsec.Token("alias", "DRTV_ACCOUNT_ALIAS")
var ytok_payee = parsec.Token("payee", "DRTV_ACCOUNT_PAYEE")
var ytok_check = parsec.Token("check", "DRTV_ACCOUNT_CHECK")
var ytok_eval = parsec.Token("eval", "DRTV_ACCOUNT_EVAL")
var ytok_default = parsec.Token("default", "DRTV_ACCOUNT_DEFAULT")
var ytok_value = parsec.Token(".*", "DRTV_VALUE")

var ytok_apply = parsec.Token("apply", "DRTV_APPLY")
var ytok_aliasname = parsec.Token("[^=]+", "DRTV_ALIASNAME")
var ytok_end = parsec.Token("end", "DRTV_END")
var ytok_year = parsec.Token("year", "DRTV_YEAR")

// tags
var ytok_colon = parsec.Token(":", "COLON")
var ytok_tag = parsec.Token(":[^ \t\r\n]", "TAG")
var ytok_tagk = parsec.Token("[^ \t\r\n]:[ \t]", "TAGKEY")
var ytok_tagv = parsec.Token(".+", "TAGVALUE")

//
func maybenode(nodes []parsec.ParsecNode) parsec.ParsecNode {
	if nodes == nil || len(nodes) == 0 {
		return nil
	}
	return nodes[0]
}

func Vector2scalar(nodes []parsec.ParsecNode) parsec.ParsecNode {
	return nodes[0]
}
<|endoftext|>


<commit_before>package saml2

import (
	"bytes"
	"crypto/tls"
	"encoding/base64"
	"fmt"

	"github.com/beevik/etree"
	dsig "github.com/russellhaering/goxmldsig"
)

func (sp *SAMLServiceProvider) validationContext() *dsig.ValidationContext {
	return dsig.NewDefaultValidationContext(sp.IDPCertificateStore)
}

//ValidateEncodedResponse both decodes and validates, based on SP
//configuration, an encoded, signed response. It will also appropriately
//decrypt a response if the assertion was encrypted
func (sp *SAMLServiceProvider) ValidateEncodedResponse(encodedResponse string) (*etree.Element, error) {
	raw, err := base64.StdEncoding.DecodeString(encodedResponse)
	if err != nil {
		return nil, err
	}

	doc := etree.NewDocument()
	err = doc.ReadFromBytes(raw)
	if err != nil {
		return nil, err
	}

	response, err := sp.validationContext().Validate(doc.Root())
	if err != nil && !sp.SkipSignatureValidation {
		return nil, err
	}

	err = sp.Validate(response)
	if err == nil {
		//If there was no error, then return the response
		return response, nil
	}

	//If an error aside from missing assertion, return it.
	if err != ErrMissingAssertion {
		return nil, err
	}

	//If the error indicated a missing assertion, proceed to attempt decryption
	//of encrypted assertion.
	res, err := NewResponseFromReader(bytes.NewReader(raw))

	if err != nil {
		return nil, fmt.Errorf("Error getting response: %v", err)
	}

	var decryptCert tls.Certificate

	switch crt := sp.SPKeyStore.(type) {
	case dsig.TLSCertKeyStore:
		decryptCert = tls.Certificate(crt)
	default:
		pk, cert, err := sp.SPKeyStore.GetKeyPair()
		if err != nil {
			return nil, fmt.Errorf("error getting keypair: %v", err)
		}

		decryptCert = tls.Certificate{
			Certificate: [][]byte{cert},
			PrivateKey:  pk,
		}
	}

	docBytes, err := res.Decrypt(tls.Certificate(decryptCert))

	if err != nil {
		return nil, fmt.Errorf("Error decrypting assertion: %v", err)
	}

	resDoc := etree.NewDocument()
	err = resDoc.ReadFromBytes(docBytes)

	if err != nil {
		return nil, fmt.Errorf("Error reading decrypted assertion: %v", err)
	}

	el := etree.NewElement("DecryptedAssertion")
	el.AddChild(resDoc.Root())

	return el, nil
}
<commit_msg>Add comments for clarity, remove redundant type assertion<commit_after>package saml2

import (
	"bytes"
	"crypto/tls"
	"encoding/base64"
	"fmt"

	"github.com/beevik/etree"
	dsig "github.com/russellhaering/goxmldsig"
)

func (sp *SAMLServiceProvider) validationContext() *dsig.ValidationContext {
	return dsig.NewDefaultValidationContext(sp.IDPCertificateStore)
}

//ValidateEncodedResponse both decodes and validates, based on SP
//configuration, an encoded, signed response. It will also appropriately
//decrypt a response if the assertion was encrypted
func (sp *SAMLServiceProvider) ValidateEncodedResponse(encodedResponse string) (*etree.Element, error) {
	raw, err := base64.StdEncoding.DecodeString(encodedResponse)
	if err != nil {
		return nil, err
	}

	doc := etree.NewDocument()
	err = doc.ReadFromBytes(raw)
	if err != nil {
		return nil, err
	}

	response, err := sp.validationContext().Validate(doc.Root())
	if err != nil && !sp.SkipSignatureValidation {
		return nil, err
	}

	err = sp.Validate(response)
	if err == nil {
		//If there was no error, then return the response
		return response, nil
	}

	//If an error aside from missing assertion, return it.
	if err != ErrMissingAssertion {
		return nil, err
	}

	//If the error indicated a missing assertion, proceed to attempt decryption
	//of encrypted assertion.
	res, err := NewResponseFromReader(bytes.NewReader(raw))

	if err != nil {
		return nil, fmt.Errorf("Error getting response: %v", err)
	}

	//This is the tls.Certificate we'll use to decrypt
	var decryptCert tls.Certificate

	switch crt := sp.SPKeyStore.(type) {
	case dsig.TLSCertKeyStore:
		//Get the tls.Certificate directly if possible
		decryptCert = tls.Certificate(crt)
	default:
		//Otherwise, construct one from the results of GetKeyPair
		pk, cert, err := sp.SPKeyStore.GetKeyPair()
		if err != nil {
			return nil, fmt.Errorf("error getting keypair: %v", err)
		}

		decryptCert = tls.Certificate{
			Certificate: [][]byte{cert},
			PrivateKey:  pk,
		}
	}

	//Decrypt the xml contents of the assertion
	docBytes, err := res.Decrypt(decryptCert)

	if err != nil {
		return nil, fmt.Errorf("Error decrypting assertion: %v", err)
	}

	//Read the assertion and return it
	resDoc := etree.NewDocument()
	err = resDoc.ReadFromBytes(docBytes)

	if err != nil {
		return nil, fmt.Errorf("Error reading decrypted assertion: %v", err)
	}

	el := etree.NewElement("DecryptedAssertion")
	el.AddChild(resDoc.Root())

	return el, nil
}
<|endoftext|>


<commit_before>package decor

import (
	"fmt"
	"io"
	"math"
	"strconv"
)

//go:generate stringer -type=SizeB1024 -trimprefix=_i
//go:generate stringer -type=SizeB1000 -trimprefix=_

const (
	_ib   SizeB1024 = iota + 1
	_iKiB SizeB1024 = 1 << (iota * 10)
	_iMiB
	_iGiB
	_iTiB
)

// SizeB1024 named type, which implements fmt.Formatter interface. It
// adjusts its value according to byte size multiple by 1024 and appends
// appropriate size marker (KiB, MiB, GiB, TiB).
type SizeB1024 int64

func (self SizeB1024) Format(st fmt.State, verb rune) {
	var prec int
	switch verb {
	case 'd':
	case 's':
		prec = -1
	default:
		if p, ok := st.Precision(); ok {
			prec = p
		} else {
			prec = 6
		}
	}

	var unit SizeB1024
	switch {
	case self < _iKiB:
		unit = _ib
	case self < _iMiB:
		unit = _iKiB
	case self < _iGiB:
		unit = _iMiB
	case self < _iTiB:
		unit = _iGiB
	case self <= math.MaxInt64:
		unit = _iTiB
	}

	io.WriteString(st, strconv.FormatFloat(float64(self)/float64(unit), 'f', prec, 64))

	if st.Flag(' ') {
		io.WriteString(st, " ")
	}
	io.WriteString(st, unit.String())
}

const (
	_b  SizeB1000 = 1
	_KB SizeB1000 = _b * 1000
	_MB SizeB1000 = _KB * 1000
	_GB SizeB1000 = _MB * 1000
	_TB SizeB1000 = _GB * 1000
)

// SizeB1000 named type, which implements fmt.Formatter interface. It
// adjusts its value according to byte size multiple by 1000 and appends
// appropriate size marker (KB, MB, GB, TB).
type SizeB1000 int64

func (self SizeB1000) Format(st fmt.State, verb rune) {
	var prec int
	switch verb {
	case 'd':
	case 's':
		prec = -1
	default:
		if p, ok := st.Precision(); ok {
			prec = p
		} else {
			prec = 6
		}
	}

	var unit SizeB1000
	switch {
	case self < _KB:
		unit = _b
	case self < _MB:
		unit = _KB
	case self < _GB:
		unit = _MB
	case self < _TB:
		unit = _GB
	case self <= math.MaxInt64:
		unit = _TB
	}

	io.WriteString(st, strconv.FormatFloat(float64(self)/float64(unit), 'f', prec, 64))

	if st.Flag(' ') {
		io.WriteString(st, " ")
	}
	io.WriteString(st, unit.String())
}
<commit_msg>it's default case<commit_after>package decor

import (
	"fmt"
	"io"
	"strconv"
)

//go:generate stringer -type=SizeB1024 -trimprefix=_i
//go:generate stringer -type=SizeB1000 -trimprefix=_

const (
	_ib   SizeB1024 = iota + 1
	_iKiB SizeB1024 = 1 << (iota * 10)
	_iMiB
	_iGiB
	_iTiB
)

// SizeB1024 named type, which implements fmt.Formatter interface. It
// adjusts its value according to byte size multiple by 1024 and appends
// appropriate size marker (KiB, MiB, GiB, TiB).
type SizeB1024 int64

func (self SizeB1024) Format(st fmt.State, verb rune) {
	var prec int
	switch verb {
	case 'd':
	case 's':
		prec = -1
	default:
		if p, ok := st.Precision(); ok {
			prec = p
		} else {
			prec = 6
		}
	}

	var unit SizeB1024
	switch {
	case self < _iKiB:
		unit = _ib
	case self < _iMiB:
		unit = _iKiB
	case self < _iGiB:
		unit = _iMiB
	case self < _iTiB:
		unit = _iGiB
	default:
		unit = _iTiB
	}

	io.WriteString(st, strconv.FormatFloat(float64(self)/float64(unit), 'f', prec, 64))

	if st.Flag(' ') {
		io.WriteString(st, " ")
	}
	io.WriteString(st, unit.String())
}

const (
	_b  SizeB1000 = 1
	_KB SizeB1000 = _b * 1000
	_MB SizeB1000 = _KB * 1000
	_GB SizeB1000 = _MB * 1000
	_TB SizeB1000 = _GB * 1000
)

// SizeB1000 named type, which implements fmt.Formatter interface. It
// adjusts its value according to byte size multiple by 1000 and appends
// appropriate size marker (KB, MB, GB, TB).
type SizeB1000 int64

func (self SizeB1000) Format(st fmt.State, verb rune) {
	var prec int
	switch verb {
	case 'd':
	case 's':
		prec = -1
	default:
		if p, ok := st.Precision(); ok {
			prec = p
		} else {
			prec = 6
		}
	}

	var unit SizeB1000
	switch {
	case self < _KB:
		unit = _b
	case self < _MB:
		unit = _KB
	case self < _GB:
		unit = _MB
	case self < _TB:
		unit = _GB
	default:
		unit = _TB
	}

	io.WriteString(st, strconv.FormatFloat(float64(self)/float64(unit), 'f', prec, 64))

	if st.Flag(' ') {
		io.WriteString(st, " ")
	}
	io.WriteString(st, unit.String())
}
<|endoftext|>


<commit_before>package compiler

import (
	py "github.com/mbergin/gotopython/pythonast"
	"go/ast"
	"go/parser"
	"go/token"
	"reflect"
	"testing"
)

const f = py.Identifier("f")

// Placeholders for statement blocks
var (
	s = []py.Stmt{&py.ExprStmt{Value: &py.Name{Id: py.Identifier("s")}}}
	t = []py.Stmt{&py.ExprStmt{Value: &py.Name{Id: py.Identifier("t")}}}
	u = []py.Stmt{&py.ExprStmt{Value: &py.Name{Id: py.Identifier("u")}}}
)

var (
	zero = &py.Num{N: "0"}
	one  = &py.Num{N: "1"}
	two  = &py.Num{N: "2"}
)

var stmtTests = []struct {
	golang string
	python []py.Stmt
}{
	// Expression statement
	{"x", []py.Stmt{&py.ExprStmt{Value: x}}},

	// IncDec statements
	{"x++", []py.Stmt{&py.AugAssign{Target: x, Op: py.Add, Value: one}}},
	{"x--", []py.Stmt{&py.AugAssign{Target: x, Op: py.Sub, Value: one}}},

	// Assignments
	{"x = y", []py.Stmt{&py.Assign{Targets: []py.Expr{x}, Value: y}}},
	{"x = y, z", []py.Stmt{&py.Assign{
		Targets: []py.Expr{x},
		Value:   &py.Tuple{Elts: []py.Expr{y, z}},
	}}},
	{"x, y = z", []py.Stmt{&py.Assign{
		Targets: []py.Expr{x, y},
		Value:   z,
	}}},
	{"x, y = y, x", []py.Stmt{&py.Assign{
		Targets: []py.Expr{x, y},
		Value:   &py.Tuple{Elts: []py.Expr{y, x}},
	}}},

	// Short variable declarations
	{"x := y", []py.Stmt{&py.Assign{Targets: []py.Expr{x}, Value: y}}},
	{"x := y, z", []py.Stmt{&py.Assign{
		Targets: []py.Expr{x},
		Value:   &py.Tuple{Elts: []py.Expr{y, z}},
	}}},
	{"x, y := z", []py.Stmt{&py.Assign{
		Targets: []py.Expr{x, y},
		Value:   z,
	}}},
	{"x, y := y, x", []py.Stmt{&py.Assign{
		Targets: []py.Expr{x, y},
		Value:   &py.Tuple{Elts: []py.Expr{y, x}},
	}}},

	// Branch statements
	{"break", []py.Stmt{&py.Break{}}},
	{"continue", []py.Stmt{&py.Continue{}}},

	// If statements
	{"if x {s}", []py.Stmt{&py.If{Test: x, Body: s}}},
	{"if s; x {t}", append(s, &py.If{Test: x, Body: t})},
	{"if x {s} else {t}", []py.Stmt{&py.If{Test: x, Body: s, Orelse: t}}},
	{"if x {s} else if y {t}", []py.Stmt{&py.If{
		Test:   x,
		Body:   s,
		Orelse: []py.Stmt{&py.If{Test: y, Body: t}},
	}}},
	{"if x {s} else if t; y {u}", []py.Stmt{&py.If{
		Test:   x,
		Body:   s,
		Orelse: append(t, &py.If{Test: y, Body: u}),
	}}},

	// Range for
	{"for x := range y {s}", []py.Stmt{
		// for x in range(len(y)): s
		&py.For{
			Target: x,
			Iter: &py.Call{
				Func: pyRange,
				Args: []py.Expr{&py.Call{Func: pyLen, Args: []py.Expr{y}}},
			},
			Body: s,
		},
	}},
	{"for x, y := range z {s}", []py.Stmt{
		// for x, y in enumerate(z): s
		&py.For{
			Target: &py.Tuple{
				Elts: []py.Expr{x, y},
			},
			Iter: &py.Call{
				Func: pyEnumerate,
				Args: []py.Expr{z},
			},
			Body: s,
		},
	}},
	{"for _, x := range y {s}", []py.Stmt{
		// for x in y: s
		&py.For{
			Target: x,
			Iter:   y,
			Body:   s,
		},
	}},

	// Declaration statements
	{"var x int", []py.Stmt{
		&py.Assign{
			Targets: []py.Expr{x},
			Value:   zero,
		},
	}},
	{"var x, y int", []py.Stmt{
		&py.Assign{
			Targets: []py.Expr{x, y},
			Value: &py.Tuple{
				Elts: []py.Expr{zero, zero},
			},
		},
	}},
	{"var x int = 1", []py.Stmt{
		&py.Assign{
			Targets: []py.Expr{x},
			Value:   one,
		},
	}},
	{"var x, y int = 1, 2", []py.Stmt{
		&py.Assign{
			Targets: []py.Expr{x, y},
			Value: &py.Tuple{
				Elts: []py.Expr{one, two},
			},
		},
	}},
	{"var x, y int = z()", []py.Stmt{
		&py.Assign{
			Targets: []py.Expr{x, y},
			Value:   &py.Call{Func: z},
		},
	}},
}

func parseStmt(stmt string) (ast.Stmt, error) {
	stmt = "package file\nfunc f() {\n" + stmt + "\n}\n"
	fset := token.NewFileSet()
	file, err := parser.ParseFile(fset, "file.go", stmt, 0)
	if err != nil {
		return nil, err
	}
	pkg := &ast.Package{
		Name:  "file",
		Files: map[string]*ast.File{"file.go": file},
	}
	if err != nil {
		return nil, err
	}
	return pkg.Files["file.go"].Decls[0].(*ast.FuncDecl).Body.List[0], nil
}

func TestStmt(t *testing.T) {

	for _, test := range stmtTests {
		goStmt, err := parseStmt(test.golang)
		if err != nil {
			t.Errorf("failed to parse Go stmt %q: %s", test.golang, err)
			continue
		}
		pyStmt := compileStmt(goStmt)
		if !reflect.DeepEqual(pyStmt, test.python) {
			t.Errorf("%q\nwant \n%s got \n%s", test.golang, sp.Sdump(test.python), sp.Sdump(pyStmt))
		}
	}
}
<commit_msg>Augmented assignment test<commit_after>package compiler

import (
	py "github.com/mbergin/gotopython/pythonast"
	"go/ast"
	"go/parser"
	"go/token"
	"reflect"
	"testing"
)

const f = py.Identifier("f")

// Placeholders for statement blocks
var (
	s = []py.Stmt{&py.ExprStmt{Value: &py.Name{Id: py.Identifier("s")}}}
	t = []py.Stmt{&py.ExprStmt{Value: &py.Name{Id: py.Identifier("t")}}}
	u = []py.Stmt{&py.ExprStmt{Value: &py.Name{Id: py.Identifier("u")}}}
)

var (
	zero = &py.Num{N: "0"}
	one  = &py.Num{N: "1"}
	two  = &py.Num{N: "2"}
)

var stmtTests = []struct {
	golang string
	python []py.Stmt
}{
	// Expression statement
	{"x", []py.Stmt{&py.ExprStmt{Value: x}}},

	// IncDec statements
	{"x++", []py.Stmt{&py.AugAssign{Target: x, Op: py.Add, Value: one}}},
	{"x--", []py.Stmt{&py.AugAssign{Target: x, Op: py.Sub, Value: one}}},

	// Assignments
	{"x = y", []py.Stmt{&py.Assign{Targets: []py.Expr{x}, Value: y}}},
	{"x = y, z", []py.Stmt{&py.Assign{
		Targets: []py.Expr{x},
		Value:   &py.Tuple{Elts: []py.Expr{y, z}},
	}}},
	{"x, y = z", []py.Stmt{&py.Assign{
		Targets: []py.Expr{x, y},
		Value:   z,
	}}},
	{"x, y = y, x", []py.Stmt{&py.Assign{
		Targets: []py.Expr{x, y},
		Value:   &py.Tuple{Elts: []py.Expr{y, x}},
	}}},

	// Short variable declarations
	{"x := y", []py.Stmt{&py.Assign{Targets: []py.Expr{x}, Value: y}}},
	{"x := y, z", []py.Stmt{&py.Assign{
		Targets: []py.Expr{x},
		Value:   &py.Tuple{Elts: []py.Expr{y, z}},
	}}},
	{"x, y := z", []py.Stmt{&py.Assign{
		Targets: []py.Expr{x, y},
		Value:   z,
	}}},
	{"x, y := y, x", []py.Stmt{&py.Assign{
		Targets: []py.Expr{x, y},
		Value:   &py.Tuple{Elts: []py.Expr{y, x}},
	}}},

	// Augmented assignments
	{"x +=  y", []py.Stmt{&py.AugAssign{Op: py.Add, Target: x, Value: y}}},
	{"x -=  y", []py.Stmt{&py.AugAssign{Op: py.Sub, Target: x, Value: y}}},
	{"x |=  y", []py.Stmt{&py.AugAssign{Op: py.BitOr, Target: x, Value: y}}},
	{"x ^=  y", []py.Stmt{&py.AugAssign{Op: py.BitXor, Target: x, Value: y}}},
	{"x *=  y", []py.Stmt{&py.AugAssign{Op: py.Mult, Target: x, Value: y}}},
	{"x /=  y", []py.Stmt{&py.AugAssign{Op: py.FloorDiv, Target: x, Value: y}}}, // TODO py.Div for floats
	{"x %=  y", []py.Stmt{&py.AugAssign{Op: py.Mod, Target: x, Value: y}}},
	{"x <<= y", []py.Stmt{&py.AugAssign{Op: py.LShift, Target: x, Value: y}}},
	{"x >>= y", []py.Stmt{&py.AugAssign{Op: py.RShift, Target: x, Value: y}}},
	{"x &=  y", []py.Stmt{&py.AugAssign{Op: py.BitAnd, Target: x, Value: y}}},
	{"x &^= y", []py.Stmt{&py.AugAssign{Op: py.BitAnd, Target: x, Value: &py.UnaryOpExpr{Op: py.Invert, Operand: y}}}},

	// Branch statements
	{"break", []py.Stmt{&py.Break{}}},
	{"continue", []py.Stmt{&py.Continue{}}},

	// If statements
	{"if x {s}", []py.Stmt{&py.If{Test: x, Body: s}}},
	{"if s; x {t}", append(s, &py.If{Test: x, Body: t})},
	{"if x {s} else {t}", []py.Stmt{&py.If{Test: x, Body: s, Orelse: t}}},
	{"if x {s} else if y {t}", []py.Stmt{&py.If{
		Test:   x,
		Body:   s,
		Orelse: []py.Stmt{&py.If{Test: y, Body: t}},
	}}},
	{"if x {s} else if t; y {u}", []py.Stmt{&py.If{
		Test:   x,
		Body:   s,
		Orelse: append(t, &py.If{Test: y, Body: u}),
	}}},

	// Range for
	{"for x := range y {s}", []py.Stmt{
		// for x in range(len(y)): s
		&py.For{
			Target: x,
			Iter: &py.Call{
				Func: pyRange,
				Args: []py.Expr{&py.Call{Func: pyLen, Args: []py.Expr{y}}},
			},
			Body: s,
		},
	}},
	{"for x, y := range z {s}", []py.Stmt{
		// for x, y in enumerate(z): s
		&py.For{
			Target: &py.Tuple{
				Elts: []py.Expr{x, y},
			},
			Iter: &py.Call{
				Func: pyEnumerate,
				Args: []py.Expr{z},
			},
			Body: s,
		},
	}},
	{"for _, x := range y {s}", []py.Stmt{
		// for x in y: s
		&py.For{
			Target: x,
			Iter:   y,
			Body:   s,
		},
	}},

	// Var declaration statements
	{"var x int", []py.Stmt{
		&py.Assign{
			Targets: []py.Expr{x},
			Value:   zero,
		},
	}},
	{"var x, y int", []py.Stmt{
		&py.Assign{
			Targets: []py.Expr{x, y},
			Value: &py.Tuple{
				Elts: []py.Expr{zero, zero},
			},
		},
	}},
	{"var x int = 1", []py.Stmt{
		&py.Assign{
			Targets: []py.Expr{x},
			Value:   one,
		},
	}},
	{"var x, y int = 1, 2", []py.Stmt{
		&py.Assign{
			Targets: []py.Expr{x, y},
			Value: &py.Tuple{
				Elts: []py.Expr{one, two},
			},
		},
	}},
	{"var x, y int = z()", []py.Stmt{
		&py.Assign{
			Targets: []py.Expr{x, y},
			Value:   &py.Call{Func: z},
		},
	}},
}

func parseStmt(stmt string) (ast.Stmt, error) {
	stmt = "package file\nfunc f() {\n" + stmt + "\n}\n"
	fset := token.NewFileSet()
	file, err := parser.ParseFile(fset, "file.go", stmt, 0)
	if err != nil {
		return nil, err
	}
	pkg := &ast.Package{
		Name:  "file",
		Files: map[string]*ast.File{"file.go": file},
	}
	if err != nil {
		return nil, err
	}
	return pkg.Files["file.go"].Decls[0].(*ast.FuncDecl).Body.List[0], nil
}

func TestStmt(t *testing.T) {

	for _, test := range stmtTests {
		goStmt, err := parseStmt(test.golang)
		if err != nil {
			t.Errorf("failed to parse Go stmt %q: %s", test.golang, err)
			continue
		}
		pyStmt := compileStmt(goStmt)
		if !reflect.DeepEqual(pyStmt, test.python) {
			t.Errorf("%q\nwant \n%s got \n%s", test.golang, sp.Sdump(test.python), sp.Sdump(pyStmt))
		}
	}
}
<|endoftext|>


<commit_before>package main

import (
    "fmt"
    "sync"
)

const (
    bufferSize = 100
)

type Requester interface {
    // Добавя заявка за изпълнение и я изпълнява, ако това е необходимо, при първа възможност.
    AddRequest(request Request)

    // Спира 'Заявчика'. Това означава, че изчаква всички вече започнали заявки да завършат
    // и извиква `SetResult` на тези заявки, които вече са били добавени, но "равни" на тях вече са били изпълнявание.
    // Нови заявки не трябва да бъдат започвани през това време, нито вече започнати, равни на тях, да бъдат добавяни за извикване на `SetResult`.
    Stop()
}

type SimpleRequester struct {
    queue chan Request
    cache *RingBuffer
    throttle chan struct{}
    lock sync.RWMutex
    waiter sync.WaitGroup
    isStopped bool
    classes map[string]chan Request
}

type CachedResult struct {
    ID string
    Result interface{}
    Err error
}

type RequestNotFoundError struct {
    ID string
}

func (rnfe *RequestNotFoundError) Error() string {
    return fmt.Sprintf("could not find result of request with ID %v in cache", rnfe.ID)
}

func (sr *SimpleRequester) FindResult(id string) (interface{}, error) {
    cacheLen := sr.cache.Length()

    for i := 0; i < cacheLen; i++ {
        cacheItem, _ := sr.cache.Item(i)

        if result, ok := cacheItem.(CachedResult); ok && result.ID == id {
            return result, nil
        }
    }

    return nil, &RequestNotFoundError{id}
}

func (sr *SimpleRequester) AddRequest(request Request) {
    if !sr.isStopped {
        sr.queue <- request
    }
}

func (sr *SimpleRequester) Stop() {
    sr.isStopped = true
    close(sr.queue)
    for range sr.queue {}
    sr.waiter.Wait()
    close(sr.throttle)
    for range sr.throttle {}
}

// Връща нов заявчик, който кешира отговорите на до cacheSize заявки,
// изпълнявайки не повече от throttleSize заявки едновременно.
func NewRequester(cacheSize int, throttleSize int) Requester {
    sr := &SimpleRequester{
        cache: NewRingBuffer(cacheSize),
        queue: make(chan Request, bufferSize),
        throttle: make(chan struct{}, throttleSize),
        classes: make(map[string]chan Request),
    }

    for i := 0; i < throttleSize; i++ {
        sr.throttle <- struct{}{}
    }

    sr.waiter.Add(1)

    go func() {
        for request := range sr.queue {
            sr.waiter.Add(1)

            go func() {
                defer sr.waiter.Done()
                sr.lock.Lock()
                class, ok := sr.classes[id]
                sr.lock.Unlock()

                if ok {
                    class <- request
                    return
                }

                id := request.ID()
                cachedResult, _ := sr.FindResult(id)

                if cachedResult != nil {
                    request.SetResult(cachedResult.Result, cachedResult.Err)
                    return
                }

                <-sr.throttle
                class = make(chan Request, bufferSize)
                sr.lock.Lock()
                sr.classes[id] = class
                sr.lock.Unlock()
                result, err := request.Run()
                close(class)

                if request.Cacheable() {
                    sr.cache.Append(&CachedResult{
                        ID: id,
                        Result: result,
                        Err: err,
                    })

                    for identicalRequest := range class {
                        identicalRequest.SetResult(result, err)
                    }
                } else if !sr.isStopped {
                    for identicalRequest := range class {
                        sr.queue <- identicalRequest
                    }
                }

                sr.lock.Lock()
                delete(sr.classes, id)
                sr.lock.Unlock()
                sr.throttle <- struct{}{}
            }()
        }

        sr.waiter.Done()
    }()
}
<commit_msg>fix: expanded scope of id to accommodate all references to it<commit_after>package main

import (
    "fmt"
    "sync"
)

const (
    bufferSize = 100
)

type Requester interface {
    // Добавя заявка за изпълнение и я изпълнява, ако това е необходимо, при първа възможност.
    AddRequest(request Request)

    // Спира 'Заявчика'. Това означава, че изчаква всички вече започнали заявки да завършат
    // и извиква `SetResult` на тези заявки, които вече са били добавени, но "равни" на тях вече са били изпълнявание.
    // Нови заявки не трябва да бъдат започвани през това време, нито вече започнати, равни на тях, да бъдат добавяни за извикване на `SetResult`.
    Stop()
}

type SimpleRequester struct {
    queue chan Request
    cache *RingBuffer
    throttle chan struct{}
    lock sync.RWMutex
    waiter sync.WaitGroup
    isStopped bool
    classes map[string]chan Request
}

type CachedResult struct {
    ID string
    Result interface{}
    Err error
}

type RequestNotFoundError struct {
    ID string
}

func (rnfe *RequestNotFoundError) Error() string {
    return fmt.Sprintf("could not find result of request with ID %v in cache", rnfe.ID)
}

func (sr *SimpleRequester) FindResult(id string) (interface{}, error) {
    cacheLen := sr.cache.Length()

    for i := 0; i < cacheLen; i++ {
        cacheItem, _ := sr.cache.Item(i)

        if result, ok := cacheItem.(CachedResult); ok && result.ID == id {
            return result, nil
        }
    }

    return nil, &RequestNotFoundError{id}
}

func (sr *SimpleRequester) AddRequest(request Request) {
    if !sr.isStopped {
        sr.queue <- request
    }
}

func (sr *SimpleRequester) Stop() {
    sr.isStopped = true
    close(sr.queue)
    for range sr.queue {}
    sr.waiter.Wait()
    close(sr.throttle)
    for range sr.throttle {}
}

// Връща нов заявчик, който кешира отговорите на до cacheSize заявки,
// изпълнявайки не повече от throttleSize заявки едновременно.
func NewRequester(cacheSize int, throttleSize int) Requester {
    sr := &SimpleRequester{
        cache: NewRingBuffer(cacheSize),
        queue: make(chan Request, bufferSize),
        throttle: make(chan struct{}, throttleSize),
        classes: make(map[string]chan Request),
    }

    for i := 0; i < throttleSize; i++ {
        sr.throttle <- struct{}{}
    }

    sr.waiter.Add(1)

    go func() {
        for request := range sr.queue {
            sr.waiter.Add(1)

            go func() {
                defer sr.waiter.Done()
                id := request.ID()
                sr.lock.Lock()
                class, ok := sr.classes[id]
                sr.lock.Unlock()

                if ok {
                    class <- request
                    return
                }

                cachedResult, _ := sr.FindResult(id)

                if cachedResult != nil {
                    request.SetResult(cachedResult.Result, cachedResult.Err)
                    return
                }

                <-sr.throttle
                class = make(chan Request, bufferSize)
                sr.lock.Lock()
                sr.classes[id] = class
                sr.lock.Unlock()
                result, err := request.Run()
                close(class)

                if request.Cacheable() {
                    sr.cache.Append(&CachedResult{
                        ID: id,
                        Result: result,
                        Err: err,
                    })

                    for identicalRequest := range class {
                        identicalRequest.SetResult(result, err)
                    }
                } else if !sr.isStopped {
                    for identicalRequest := range class {
                        sr.queue <- identicalRequest
                    }
                }

                sr.lock.Lock()
                delete(sr.classes, id)
                sr.lock.Unlock()
                sr.throttle <- struct{}{}
            }()
        }

        sr.waiter.Done()
    }()
}
<|endoftext|>


<commit_before>package csvtrans

import (
	"encoding/csv"
	"errors"
	"fmt"
	"io"
	"os"
)

type RowTransformer func(i int, inRow []string) (outRow []string, err error)

func RunFile(inFile, outFile string, f RowTransformer) error {
	if inFile == outFile {
		return errors.New("inFile and outFile must be different")
	}

	in, err := os.Open(inFile) // Read access
	if err != nil {
		return fmt.Errorf("Error reading %s: %v", inFile, err)
	}
	defer in.Close()

	out, err := os.Create(outFile)
	if err != nil {
		return fmt.Errorf("Error opening %s for write: %v", outFile, err)
	}
	defer out.Close()

	err = processCsv(in, out, f)
	if err != nil {
		return err
	}
	return nil
}

func Run(in io.Reader, out io.Writer, f RowTransformer) error {
	inCsv := csv.NewReader(in)
	outCsv := csv.NewWriter(out)

	defer outCsv.Flush()
	i := 0
	for {
		row, err := inCsv.Read()

		if err == io.EOF {
			return nil
		} else if err != nil {
			return fmt.Errorf("Error reading CSV row at index %d: %v", i, err)
		}

		transformed, err := f(i, row)
		if err != nil {
			return fmt.Errorf("Error transforming row at index %d: %v", i, err)
		}

		err = outCsv.Write(transformed)
		if err != nil {
			return fmt.Errorf("Error writing CSV row at index %d: %v", i, err)
		}
		i += 1
	}
}
<commit_msg>Fix typo processCsv -> Run<commit_after>package csvtrans

import (
	"encoding/csv"
	"errors"
	"fmt"
	"io"
	"os"
)

type RowTransformer func(i int, inRow []string) (outRow []string, err error)

func RunFile(inFile, outFile string, f RowTransformer) error {
	if inFile == outFile {
		return errors.New("inFile and outFile must be different")
	}

	in, err := os.Open(inFile) // Read access
	if err != nil {
		return fmt.Errorf("Error reading %s: %v", inFile, err)
	}
	defer in.Close()

	out, err := os.Create(outFile)
	if err != nil {
		return fmt.Errorf("Error opening %s for write: %v", outFile, err)
	}
	defer out.Close()

	err = Run(in, out, f)
	if err != nil {
		return err
	}
	return nil
}

func Run(in io.Reader, out io.Writer, f RowTransformer) error {
	inCsv := csv.NewReader(in)
	outCsv := csv.NewWriter(out)

	defer outCsv.Flush()
	i := 0
	for {
		row, err := inCsv.Read()

		if err == io.EOF {
			return nil
		} else if err != nil {
			return fmt.Errorf("Error reading CSV row at index %d: %v", i, err)
		}

		transformed, err := f(i, row)
		if err != nil {
			return fmt.Errorf("Error transforming row at index %d: %v", i, err)
		}

		err = outCsv.Write(transformed)
		if err != nil {
			return fmt.Errorf("Error writing CSV row at index %d: %v", i, err)
		}
		i += 1
	}
}
<|endoftext|>


<commit_before>// Copyright 2015 Pablo Karlssnon. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file at https://github.com/PabloK/revel_react_boiler.

/*
  Package configmanager implements configuration management with prioritized overwrite from the environment.

  When instantiated configmanager imports configuration from a JSON config file and the environment and makes it
  available trough its interface. To use this package the environmnet variable RRB_CONFIG_FILE needs to point to
  a JSON config file.

  Override of JSON configuration will takeplace at instantiation if the corresponding variable is set in the environment.

  Variables not present in the JSON file will not be imported from the environment.

  For an example JSON file see env.json found in the rrb/conf folder at https://github.com/PabloK/revel_react_boiler.
*/
package configmanager

import (
	"bytes"
	"encoding/json"
	"github.com/revel/revel"
	"io/ioutil"
	"os"
	"strings"
)

type configManagerType struct {
	O interface{}
}

type ConfigHash map[string]string
type confighash map[string]interface{}

var instantiated *configManagerType = nil
var config ConfigHash

// New instantiates the configmanager and reads the configuration into memory.
func New(arguments ...string) *configManagerType {

	config = make(map[string]string)
	var configFilePath = "conf/environment.json"
	if len(arguments) > 0 {
		configFilePath = arguments[0]
	}

	if instantiated == nil {

		var msg bytes.Buffer
		msg.WriteString("Reading configuration from ")
		msg.WriteString(configFilePath)
		revel.INFO.Printf("%s", msg.String())
		// Get configuration from JSON file
		var configJSON = getJSONConfig(configFilePath)

		revel.TRACE.Printf("%s", "Reading configuration from ENV")
		// Get configuration from ENV
		var configENV = getENVConfig()

		// prioritize according to source
		var prioConf = prioritizeConfig(configENV, configJSON)

		for k, v := range prioConf {
			config[k] = v.(string)
		}

		instantiated = new(configManagerType)
	}
	return instantiated
}

// GetConfig returns the configuration as string map
func (c *configManagerType) GetConfig() ConfigHash {
	return config
}

// discardConfig throws away the current configuration
func discardConfig() {
	instantiated = nil
}

// getJSONConfig returns the config found in the specified JSON file.
func getJSONConfig(configFilePath string) confighash {
	var data confighash = make(map[string]interface{})

	file, err := ioutil.ReadFile(configFilePath)
	if err != nil {
		revel.ERROR.Printf("%s", err)
	}
	err = json.Unmarshal(file, &data)
	if err != nil {
		revel.ERROR.Printf("%s", err)
	}
	return data
}

// getENVConfig returns variables set in the environment.
func getENVConfig() confighash {
	var envconfig confighash = make(map[string]interface{})
	env := os.Environ()
	for _, str := range env {
		envarr := strings.Split(str, "=")
		k := envarr[0]
		v := envarr[1]
		envconfig[k] = v
	}
	return envconfig
}

// Overwrites JSON config with config values from the environment if corresponding variable exists in the environment config.
func prioritizeConfig(env confighash, json confighash) confighash {
	for k, _ := range json {
		_, keyExists := env[k]
		if keyExists {
			json[k] = env[k]
		}
	}
	return json
}
<commit_msg>Update configmanager.go<commit_after>// Copyright 2015 Pablo Karlsson. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file at https://github.com/PabloK/revel_react_boiler.

/*
  Package configmanager implements configuration management with prioritized overwrite from the environment.

  When instantiated configmanager imports configuration from a JSON config file and the environment and makes it
  available trough its interface. To use this package the environmnet variable RRB_CONFIG_FILE needs to point to
  a JSON config file.

  Override of JSON configuration will takeplace at instantiation if the corresponding variable is set in the environment.

  Variables not present in the JSON file will not be imported from the environment.

  For an example JSON file see env.json found in the rrb/conf folder at https://github.com/PabloK/revel_react_boiler.
*/
package configmanager

import (
	"bytes"
	"encoding/json"
	"github.com/revel/revel"
	"io/ioutil"
	"os"
	"strings"
)

type configManagerType struct {
	O interface{}
}

type ConfigHash map[string]string
type confighash map[string]interface{}

var instantiated *configManagerType = nil
var config ConfigHash

// New instantiates the configmanager and reads the configuration into memory.
func New(arguments ...string) *configManagerType {

	config = make(map[string]string)
	var configFilePath = "conf/environment.json"
	if len(arguments) > 0 {
		configFilePath = arguments[0]
	}

	if instantiated == nil {

		var msg bytes.Buffer
		msg.WriteString("Reading configuration from ")
		msg.WriteString(configFilePath)
		revel.INFO.Printf("%s", msg.String())
		// Get configuration from JSON file
		var configJSON = getJSONConfig(configFilePath)

		revel.TRACE.Printf("%s", "Reading configuration from ENV")
		// Get configuration from ENV
		var configENV = getENVConfig()

		// prioritize according to source
		var prioConf = prioritizeConfig(configENV, configJSON)

		for k, v := range prioConf {
			config[k] = v.(string)
		}

		instantiated = new(configManagerType)
	}
	return instantiated
}

// GetConfig returns the configuration as string map
func (c *configManagerType) GetConfig() ConfigHash {
	return config
}

// discardConfig throws away the current configuration
func discardConfig() {
	instantiated = nil
}

// getJSONConfig returns the config found in the specified JSON file.
func getJSONConfig(configFilePath string) confighash {
	var data confighash = make(map[string]interface{})

	file, err := ioutil.ReadFile(configFilePath)
	if err != nil {
		revel.ERROR.Printf("%s", err)
	}
	err = json.Unmarshal(file, &data)
	if err != nil {
		revel.ERROR.Printf("%s", err)
	}
	return data
}

// getENVConfig returns variables set in the environment.
func getENVConfig() confighash {
	var envconfig confighash = make(map[string]interface{})
	env := os.Environ()
	for _, str := range env {
		envarr := strings.Split(str, "=")
		k := envarr[0]
		v := envarr[1]
		envconfig[k] = v
	}
	return envconfig
}

// Overwrites JSON config with config values from the environment if corresponding variable exists in the environment config.
func prioritizeConfig(env confighash, json confighash) confighash {
	for k, _ := range json {
		_, keyExists := env[k]
		if keyExists {
			json[k] = env[k]
		}
	}
	return json
}
<|endoftext|>


<commit_before>package data

import (
	"encoding/csv"
	"time"
	"fmt"
	"strconv"
	"io"
)

func CSVParse(file io.Reader) (labels []string, data []Record) {
	labels, data = csvParse(file)
	return
}

func csvParse(file io.Reader) (labels []string, data []Record) {
	reader := csv.NewReader (file)
	tmpdata, err := reader.ReadAll()
	if  err != nil {
		fmt.Println(err)
	}
	labels = tmpdata[0]
	data = make([]Record, len(tmpdata)-1)
	for i := 1; i<len(tmpdata); i++ {
		data[i-1].Time, _ = time.Parse(ISO, tmpdata[i][0])
		data[i-1].Radiation, err = strconv.ParseFloat(tmpdata[i][1], 64)
		if err != nil {
			data[i-1].empty = true
		}
		data[i-1].Humidity, err = strconv.ParseFloat(tmpdata[i][2], 64)
		if err != nil {
			data[i-1].empty = true
		}
		data[i-1].Temperature, err = strconv.ParseFloat(tmpdata[i][2], 64)
		if err != nil {
			data[i-1].empty = true
		}
		data[i-1].Wind, err = strconv.ParseFloat(tmpdata[i][2], 64)
		if err != nil {
			data[i-1].empty = true
		}
		data[i-1].Power, err = strconv.ParseFloat(tmpdata[i][2], 64)
		if err != nil {
			data[i-1].Null = true
		}
	}
	data = fillRecords (data)
	return
}

func fillRecords (emptyData []Record) (data []Record){
	gradRad, gradHumidity, gradTemp, gradWind := 0.0, 0.0, 0.0, 0.0
	for i := 0; i<len(emptyData); i++ {
		if emptyData[i].empty {
			emptyData[i].Radiation = emptyData[i-1].Radiation + gradRad
			emptyData[i].Humidity = emptyData[i-1].Humidity + gradHumidity
			emptyData[i].Temperature = emptyData[i-1].Temperature + gradTemp
			emptyData[i].Wind = emptyData[i-1].Wind + gradWind
			emptyData[i].empty = false
		} else {
			if i + 4 < len (emptyData) {
				gradRad = (emptyData[i+4].Radiation - emptyData[i].Radiation)/4
				gradHumidity = (emptyData[i+4].Humidity - emptyData[i].Humidity)/4
				gradTemp = (emptyData[i+4].Temperature - emptyData[i].Temperature)/4
				gradWind = (emptyData[i+4].Wind - emptyData[i].Wind)/4
			} else {
				gradRad = 0
				gradHumidity = 0
				gradTemp = 0
				gradWind = 0
			}
		}
	}
	return
}
<commit_msg>Now an index error?<commit_after>package data

import (
	"encoding/csv"
	"time"
	"fmt"
	"strconv"
	"io"
)

func CSVParse(file io.Reader) (labels []string, data []Record) {
	labels, data = csvParse(file)
	return
}

func csvParse(file io.Reader) (labels []string, data []Record) {
	reader := csv.NewReader (file)
	tmpdata, err := reader.ReadAll()
	if  err != nil {
		fmt.Println(err)
	}
	labels = tmpdata[0]
	data = make([]Record, len(tmpdata)-1)
	for i := 1; i<len(tmpdata); i++ {
		data[i-1].Time, _ = time.Parse(ISO, tmpdata[i][0])
		data[i-1].Radiation, err = strconv.ParseFloat(tmpdata[i][1], 64)
		if err != nil {
			data[i-1].empty = true
		}
		data[i-1].Humidity, err = strconv.ParseFloat(tmpdata[i][2], 64)
		if err != nil {
			data[i-1].empty = true
		}
		data[i-1].Temperature, err = strconv.ParseFloat(tmpdata[i][2], 64)
		if err != nil {
			data[i-1].empty = true
		}
		data[i-1].Wind, err = strconv.ParseFloat(tmpdata[i][2], 64)
		if err != nil {
			data[i-1].empty = true
		}
		data[i-1].Power, err = strconv.ParseFloat(tmpdata[i][2], 64)
		if err != nil {
			data[i-1].Null = true
		}
	}
	data = fillRecords (data)
	return
}

func fillRecords (emptyData []Record) (data []Record){
	gradRad, gradHumidity, gradTemp, gradWind := 0.0, 0.0, 0.0, 0.0
	for i := 0; i<len(emptyData); i++ {
		if emptyData[i].empty && i > 0 {
			emptyData[i].Radiation = emptyData[i-1].Radiation + gradRad
			emptyData[i].Humidity = emptyData[i-1].Humidity + gradHumidity
			emptyData[i].Temperature = emptyData[i-1].Temperature + gradTemp
			emptyData[i].Wind = emptyData[i-1].Wind + gradWind
			emptyData[i].empty = false
		} else {
			if i + 4 < len (emptyData) {
				gradRad = (emptyData[i+4].Radiation - emptyData[i].Radiation)/4
				gradHumidity = (emptyData[i+4].Humidity - emptyData[i].Humidity)/4
				gradTemp = (emptyData[i+4].Temperature - emptyData[i].Temperature)/4
				gradWind = (emptyData[i+4].Wind - emptyData[i].Wind)/4
			} else {
				gradRad = 0
				gradHumidity = 0
				gradTemp = 0
				gradWind = 0
			}
		}
	}
	return
}
<|endoftext|>


<commit_before>package aws

import (
	"fmt"
	"log"
	"time"

	"github.com/hashicorp/aws-sdk-go/aws"
	"github.com/hashicorp/aws-sdk-go/gen/ec2"
	"github.com/hashicorp/terraform/helper/resource"
	"github.com/hashicorp/terraform/helper/schema"
)

func resourceAwsVpnGateway() *schema.Resource {
	return &schema.Resource{
		Create: resourceAwsVpnGatewayCreate,
		Read:   resourceAwsVpnGatewayRead,
		Update: resourceAwsVpnGatewayUpdate,
		Delete: resourceAwsVpnGatewayDelete,

		Schema: map[string]*schema.Schema{
			"availability_zone": &schema.Schema{
				Type:     schema.TypeString,
				Optional: true,
				ForceNew: true,
			},

			"type": &schema.Schema{
				Type:     schema.TypeString,
                Default:  "ipsec.1",
                Optional: true,
				ForceNew: true,
			},

            "vpc_id": &schema.Schema{
                Type:     schema.TypeString,
                Optional: true,
            },

            "tags": tagsSchema(),

		},
	}
}

func resourceAwsVpnGatewayCreate(d *schema.ResourceData, meta interface{}) error {
    ec2conn := meta.(*AWSClient).awsEC2conn

    createOpts := &ec2.CreateVPNGatewayRequest{
        AvailabilityZone: aws.String(d.Get("availability_zone").(string)),
        Type:             aws.String(d.Get("type").(string)),
    }

    // Create the VPN gateway
    log.Printf("[DEBUG] Creating VPN gateway")
    resp, err := ec2conn.CreateVPNGateway(createOpts)
    if err != nil {
        return fmt.Errorf("Error creating VPN gateway: %s", err)
    }

    // Get the ID and store it
    vpnGateway := resp.VPNGateway
    d.SetId(*vpnGateway.VPNGatewayID)
    log.Printf("[INFO] VPN Gateway ID: %s", *vpnGateway.VPNGatewayID)

    // Attach the VPN gateway to the correct VPC
    return resourceAwsVpnGatewayUpdate(d, meta)
}

func resourceAwsVpnGatewayRead(d *schema.ResourceData, meta interface{}) error {
    ec2conn := meta.(*AWSClient).awsEC2conn

	vpnGatewayRaw, _, err := VpnGatewayStateRefreshFunc(ec2conn, d.Id())()
	if err != nil {
		return err
	}
	if vpnGatewayRaw == nil {
		// Seems we have lost our VPN gateway
		d.SetId("")
		return nil
	}

	vpnGateway := vpnGatewayRaw.(*ec2.VPNGateway)
	if len(vpnGateway.VPCAttachments) == 0 {
		// Gateway exists but not attached to the VPC
		d.Set("vpc_id", "")
	} else {
		d.Set("vpc_id", vpnGateway.VPCAttachments[0].VPCID)
	}
    d.Set("availability_zone", vpnGateway.AvailabilityZone)
    d.Set("type", vpnGateway.Type)
    d.Set("tags", tagsToMapSDK(vpnGateway.Tags))

    return nil
}

func resourceAwsVpnGatewayUpdate(d *schema.ResourceData, meta interface{}) error {
    if d.HasChange("vpc_id") {
        // If we're already attached, detach it first
        if err := resourceAwsVpnGatewayDetach(d, meta); err != nil {
            return err
        }

        // Attach the VPN gateway to the new vpc
        if err := resourceAwsVpnGatewayAttach(d, meta); err != nil {
            return err
        }
    }

    ec2conn := meta.(*AWSClient).awsEC2conn

    if err := setTagsSDK(ec2conn, d); err != nil {
        return err
    }

    d.SetPartial("tags")

    return resourceAwsVpnGatewayRead(d, meta)
}

func resourceAwsVpnGatewayDelete(d *schema.ResourceData, meta interface{}) error {
    ec2conn := meta.(*AWSClient).awsEC2conn

    // Detach if it is attached
    if err := resourceAwsVpnGatewayDetach(d, meta); err != nil {
        return err
    }

    log.Printf("[INFO] Deleting VPN gateway: %s", d.Id())

    return resource.Retry(5*time.Minute, func() error {
        err := ec2conn.DeleteVPNGateway(&ec2.DeleteVPNGatewayRequest{
            VPNGatewayID: aws.String(d.Id()),
        })
        if err == nil {
            return nil
        }

        ec2err, ok := err.(*aws.APIError)
        if !ok {
            return err
        }

        switch ec2err.Code {
        case "InvalidVpnGatewayID.NotFound":
            return nil
        case "DependencyViolation":
            return err // retry
        }

        return resource.RetryError{Err: err}
    })
}

func resourceAwsVpnGatewayAttach(d *schema.ResourceData, meta interface{}) error {
    ec2conn := meta.(*AWSClient).awsEC2conn

    if d.Get("vpc_id").(string) == "" {
        log.Printf(
            "[DEBUG] Not attaching VPN Gateway '%s' as no VPC ID is set",
            d.Id())
        return nil
    }

    log.Printf(
        "[INFO] Attaching VPN Gateway '%s' to VPC '%s'",
        d.Id(),
        d.Get("vpc_id").(string))

    _, err := ec2conn.AttachVPNGateway(&ec2.AttachVPNGatewayRequest{
        VPNGatewayID: aws.String(d.Id()),
        VPCID:        aws.String(d.Get("vpc_id").(string)),
    })
    if err != nil {
        return err
    }

	// A note on the states below: the AWS docs (as of July, 2014) say
	// that the states would be: attached, attaching, detached, detaching,
	// but when running, I noticed that the state is usually "available" when
	// it is attached.

	// Wait for it to be fully attached before continuing
    log.Printf("[DEBUG] Waiting for VPN gateway (%s) to attach", d.Id())
    stateConf := &resource.StateChangeConf{
        Pending: []string{"detached", "attaching"},
        Target:  "available",
        Refresh: VpnGatewayAttachStateRefreshFunc(ec2conn, d.Id(), "available"),
        Timeout: 1 * time.Minute,
    }
    if _, err := stateConf.WaitForState(); err != nil {
        return fmt.Errorf(
            "Error waiting for VPN gateway (%s) to attach: %s",
            d.Id(), err)
    }

    return nil
}

func resourceAwsVpnGatewayDetach(d *schema.ResourceData, meta interface{}) error {
    ec2conn := meta.(*AWSClient).awsEC2conn

    // Get the old VPC ID to detach from
    vpcID, _ := d.GetChange("vpc_id")

    if vpcID.(string) == "" {
        log.Printf(
            "[DEBUG] Not detaching VPN Gateway '%s' as no VPC ID is set",
            d.Id())
        return nil
    }

    log.Printf(
        "[INFO] Detaching VPN Gateway '%s' from VPC '%s'",
        d.Id(),
        vpcID.(string))

    wait := true
    err := ec2conn.DetachVPNGateway(&ec2.DetachVPNGatewayRequest{
        VPNGatewayID: aws.String(d.Id()),
        VPCID:        aws.String(d.Get("vpc_id").(string)),
    })
    if err != nil {
        ec2err, ok := err.(*aws.APIError)
        if ok {
            if ec2err.Code == "InvalidVpnGatewayID.NotFound" {
                err = nil
                wait = false
            } else if ec2err.Code == "InvalidVpnGatewayAttachment.NotFound" {
                err = nil
                wait = false
            }
        }

        if err != nil {
            return err
        }
    }

    if !wait {
        return nil
    }

    // Wait for it to be fully detached before continuing
    log.Printf("[DEBUG] Waiting for VPN gateway (%s) to detach", d.Id())
    stateConf := &resource.StateChangeConf{
        Pending: []string{"attached", "detaching", "available"},
        Target:  "detached",
        Refresh: VpnGatewayAttachStateRefreshFunc(ec2conn, d.Id(), "detached"),
        Timeout: 1 * time.Minute,
    }
    if _, err := stateConf.WaitForState(); err != nil {
        return fmt.Errorf(
            "Error waiting for vpn gateway (%s) to detach: %s",
            d.Id(), err)
    }

    return nil
}

// VpnGatewayStateRefreshFunc returns a resource.StateRefreshFunc that is used to watch a VPNGateway.
func VpnGatewayStateRefreshFunc(conn *ec2.EC2, id string) resource.StateRefreshFunc {
    return func() (interface{}, string, error) {
        resp, err := conn.DescribeVPNGateways(&ec2.DescribeVPNGatewaysRequest{
            VPNGatewayIDs: []string{id},
        })
        if err != nil {
            if ec2err, ok := err.(aws.APIError); ok && ec2err.Code == "InvalidVpnGatewayID.NotFound" {
                resp = nil
            } else {
                log.Printf("[ERROR] Error on VpnGatewayStateRefresh: %s", err)
                return nil, "", err
            }
        }

        if resp == nil {
            // Sometimes AWS just has consistency issues and doesn't see
            // our instance yet. Return an empty state.
            return nil, "", nil
        }

        vpnGateway := &resp.VPNGateways[0]
        return vpnGateway, *vpnGateway.State, nil
    }
}

// VpnGatewayAttachStateRefreshFunc returns a resource.StateRefreshFunc that is used to watch
// the state of a VPN gateway's attachment
func VpnGatewayAttachStateRefreshFunc(conn *ec2.EC2, id string, expected string) resource.StateRefreshFunc {
    var start time.Time
    return func() (interface{}, string, error) {
        if start.IsZero() {
            start = time.Now()
        }

        resp, err := conn.DescribeVPNGateways(&ec2.DescribeVPNGatewaysRequest{
            VPNGatewayIDs: []string{id},
        })
        if err != nil {
            if ec2err, ok := err.(aws.APIError); ok && ec2err.Code == "InvalidVpnGatewayID.NotFound" {
                resp = nil
            } else {
                log.Printf("[ERROR] Error on VpnGatewayStateRefresh: %s", err)
                return nil, "", err
            }
        }

        if resp == nil {
            // Sometimes AWS just has consistency issues and doesn't see
            // our instance yet. Return an empty state.
            return nil, "", nil
        }

        vpnGateway := &resp.VPNGateways[0]

        if time.Now().Sub(start) > 10*time.Second {
            return vpnGateway, expected, nil
        }

        if len(vpnGateway.VPCAttachments) == 0 {
            // No attachments, we're detached
            return vpnGateway, "detached", nil
        }

        return vpnGateway, *vpnGateway.VPCAttachments[0].State, nil
    }
}
<commit_msg>Hardcode type parameter value.<commit_after>package aws

import (
	"fmt"
	"log"
	"time"

	"github.com/hashicorp/aws-sdk-go/aws"
	"github.com/hashicorp/aws-sdk-go/gen/ec2"
	"github.com/hashicorp/terraform/helper/resource"
	"github.com/hashicorp/terraform/helper/schema"
)

func resourceAwsVpnGateway() *schema.Resource {
	return &schema.Resource{
		Create: resourceAwsVpnGatewayCreate,
		Read:   resourceAwsVpnGatewayRead,
		Update: resourceAwsVpnGatewayUpdate,
		Delete: resourceAwsVpnGatewayDelete,

		Schema: map[string]*schema.Schema{
			"availability_zone": &schema.Schema{
				Type:     schema.TypeString,
				Optional: true,
				ForceNew: true,
			},

            "vpc_id": &schema.Schema{
                Type:     schema.TypeString,
                Optional: true,
            },

            "tags": tagsSchema(),

		},
	}
}

func resourceAwsVpnGatewayCreate(d *schema.ResourceData, meta interface{}) error {
    ec2conn := meta.(*AWSClient).awsEC2conn

    createOpts := &ec2.CreateVPNGatewayRequest{
        AvailabilityZone: aws.String(d.Get("availability_zone").(string)),
        Type:             aws.String("ipsec.1"),
    }

    // Create the VPN gateway
    log.Printf("[DEBUG] Creating VPN gateway")
    resp, err := ec2conn.CreateVPNGateway(createOpts)
    if err != nil {
        return fmt.Errorf("Error creating VPN gateway: %s", err)
    }

    // Get the ID and store it
    vpnGateway := resp.VPNGateway
    d.SetId(*vpnGateway.VPNGatewayID)
    log.Printf("[INFO] VPN Gateway ID: %s", *vpnGateway.VPNGatewayID)

    // Attach the VPN gateway to the correct VPC
    return resourceAwsVpnGatewayUpdate(d, meta)
}

func resourceAwsVpnGatewayRead(d *schema.ResourceData, meta interface{}) error {
    ec2conn := meta.(*AWSClient).awsEC2conn

	vpnGatewayRaw, _, err := VpnGatewayStateRefreshFunc(ec2conn, d.Id())()
	if err != nil {
		return err
	}
	if vpnGatewayRaw == nil {
		// Seems we have lost our VPN gateway
		d.SetId("")
		return nil
	}

	vpnGateway := vpnGatewayRaw.(*ec2.VPNGateway)
	if len(vpnGateway.VPCAttachments) == 0 {
		// Gateway exists but not attached to the VPC
		d.Set("vpc_id", "")
	} else {
		d.Set("vpc_id", vpnGateway.VPCAttachments[0].VPCID)
	}
    d.Set("availability_zone", vpnGateway.AvailabilityZone)
    d.Set("tags", tagsToMapSDK(vpnGateway.Tags))

    return nil
}

func resourceAwsVpnGatewayUpdate(d *schema.ResourceData, meta interface{}) error {
    if d.HasChange("vpc_id") {
        // If we're already attached, detach it first
        if err := resourceAwsVpnGatewayDetach(d, meta); err != nil {
            return err
        }

        // Attach the VPN gateway to the new vpc
        if err := resourceAwsVpnGatewayAttach(d, meta); err != nil {
            return err
        }
    }

    ec2conn := meta.(*AWSClient).awsEC2conn

    if err := setTagsSDK(ec2conn, d); err != nil {
        return err
    }

    d.SetPartial("tags")

    return resourceAwsVpnGatewayRead(d, meta)
}

func resourceAwsVpnGatewayDelete(d *schema.ResourceData, meta interface{}) error {
    ec2conn := meta.(*AWSClient).awsEC2conn

    // Detach if it is attached
    if err := resourceAwsVpnGatewayDetach(d, meta); err != nil {
        return err
    }

    log.Printf("[INFO] Deleting VPN gateway: %s", d.Id())

    return resource.Retry(5*time.Minute, func() error {
        err := ec2conn.DeleteVPNGateway(&ec2.DeleteVPNGatewayRequest{
            VPNGatewayID: aws.String(d.Id()),
        })
        if err == nil {
            return nil
        }

        ec2err, ok := err.(*aws.APIError)
        if !ok {
            return err
        }

        switch ec2err.Code {
        case "InvalidVpnGatewayID.NotFound":
            return nil
        case "DependencyViolation":
            return err // retry
        }

        return resource.RetryError{Err: err}
    })
}

func resourceAwsVpnGatewayAttach(d *schema.ResourceData, meta interface{}) error {
    ec2conn := meta.(*AWSClient).awsEC2conn

    if d.Get("vpc_id").(string) == "" {
        log.Printf(
            "[DEBUG] Not attaching VPN Gateway '%s' as no VPC ID is set",
            d.Id())
        return nil
    }

    log.Printf(
        "[INFO] Attaching VPN Gateway '%s' to VPC '%s'",
        d.Id(),
        d.Get("vpc_id").(string))

    _, err := ec2conn.AttachVPNGateway(&ec2.AttachVPNGatewayRequest{
        VPNGatewayID: aws.String(d.Id()),
        VPCID:        aws.String(d.Get("vpc_id").(string)),
    })
    if err != nil {
        return err
    }

	// A note on the states below: the AWS docs (as of July, 2014) say
	// that the states would be: attached, attaching, detached, detaching,
	// but when running, I noticed that the state is usually "available" when
	// it is attached.

	// Wait for it to be fully attached before continuing
    log.Printf("[DEBUG] Waiting for VPN gateway (%s) to attach", d.Id())
    stateConf := &resource.StateChangeConf{
        Pending: []string{"detached", "attaching"},
        Target:  "available",
        Refresh: VpnGatewayAttachStateRefreshFunc(ec2conn, d.Id(), "available"),
        Timeout: 1 * time.Minute,
    }
    if _, err := stateConf.WaitForState(); err != nil {
        return fmt.Errorf(
            "Error waiting for VPN gateway (%s) to attach: %s",
            d.Id(), err)
    }

    return nil
}

func resourceAwsVpnGatewayDetach(d *schema.ResourceData, meta interface{}) error {
    ec2conn := meta.(*AWSClient).awsEC2conn

    // Get the old VPC ID to detach from
    vpcID, _ := d.GetChange("vpc_id")

    if vpcID.(string) == "" {
        log.Printf(
            "[DEBUG] Not detaching VPN Gateway '%s' as no VPC ID is set",
            d.Id())
        return nil
    }

    log.Printf(
        "[INFO] Detaching VPN Gateway '%s' from VPC '%s'",
        d.Id(),
        vpcID.(string))

    wait := true
    err := ec2conn.DetachVPNGateway(&ec2.DetachVPNGatewayRequest{
        VPNGatewayID: aws.String(d.Id()),
        VPCID:        aws.String(d.Get("vpc_id").(string)),
    })
    if err != nil {
        ec2err, ok := err.(*aws.APIError)
        if ok {
            if ec2err.Code == "InvalidVpnGatewayID.NotFound" {
                err = nil
                wait = false
            } else if ec2err.Code == "InvalidVpnGatewayAttachment.NotFound" {
                err = nil
                wait = false
            }
        }

        if err != nil {
            return err
        }
    }

    if !wait {
        return nil
    }

    // Wait for it to be fully detached before continuing
    log.Printf("[DEBUG] Waiting for VPN gateway (%s) to detach", d.Id())
    stateConf := &resource.StateChangeConf{
        Pending: []string{"attached", "detaching", "available"},
        Target:  "detached",
        Refresh: VpnGatewayAttachStateRefreshFunc(ec2conn, d.Id(), "detached"),
        Timeout: 1 * time.Minute,
    }
    if _, err := stateConf.WaitForState(); err != nil {
        return fmt.Errorf(
            "Error waiting for vpn gateway (%s) to detach: %s",
            d.Id(), err)
    }

    return nil
}

// VpnGatewayStateRefreshFunc returns a resource.StateRefreshFunc that is used to watch a VPNGateway.
func VpnGatewayStateRefreshFunc(conn *ec2.EC2, id string) resource.StateRefreshFunc {
    return func() (interface{}, string, error) {
        resp, err := conn.DescribeVPNGateways(&ec2.DescribeVPNGatewaysRequest{
            VPNGatewayIDs: []string{id},
        })
        if err != nil {
            if ec2err, ok := err.(aws.APIError); ok && ec2err.Code == "InvalidVpnGatewayID.NotFound" {
                resp = nil
            } else {
                log.Printf("[ERROR] Error on VpnGatewayStateRefresh: %s", err)
                return nil, "", err
            }
        }

        if resp == nil {
            // Sometimes AWS just has consistency issues and doesn't see
            // our instance yet. Return an empty state.
            return nil, "", nil
        }

        vpnGateway := &resp.VPNGateways[0]
        return vpnGateway, *vpnGateway.State, nil
    }
}

// VpnGatewayAttachStateRefreshFunc returns a resource.StateRefreshFunc that is used to watch
// the state of a VPN gateway's attachment
func VpnGatewayAttachStateRefreshFunc(conn *ec2.EC2, id string, expected string) resource.StateRefreshFunc {
    var start time.Time
    return func() (interface{}, string, error) {
        if start.IsZero() {
            start = time.Now()
        }

        resp, err := conn.DescribeVPNGateways(&ec2.DescribeVPNGatewaysRequest{
            VPNGatewayIDs: []string{id},
        })
        if err != nil {
            if ec2err, ok := err.(aws.APIError); ok && ec2err.Code == "InvalidVpnGatewayID.NotFound" {
                resp = nil
            } else {
                log.Printf("[ERROR] Error on VpnGatewayStateRefresh: %s", err)
                return nil, "", err
            }
        }

        if resp == nil {
            // Sometimes AWS just has consistency issues and doesn't see
            // our instance yet. Return an empty state.
            return nil, "", nil
        }

        vpnGateway := &resp.VPNGateways[0]

        if time.Now().Sub(start) > 10*time.Second {
            return vpnGateway, expected, nil
        }

        if len(vpnGateway.VPCAttachments) == 0 {
            // No attachments, we're detached
            return vpnGateway, "detached", nil
        }

        return vpnGateway, *vpnGateway.VPCAttachments[0].State, nil
    }
}
<|endoftext|>


<commit_before>package main

import (
	"fmt"
	"log"
	"github.com/hashicorp/terraform/helper/schema"
    "golang.org/x/net/context"
    "github.com/vmware/govmomi/find"
    "github.com/vmware/govmomi/object"
    "github.com/vmware/govmomi/property"
    "github.com/vmware/govmomi/vim25"
	"github.com/vmware/govmomi/vim25/mo"
	"github.com/vmware/govmomi/vim25/types"
    "strings"
)

func resourceVirtualMachine() *schema.Resource {
	return &schema.Resource{
		Create: resourceVirtualMachineCreate,
		Read:   resourceVirtualMachineRead,
		Delete: resourceVirtualMachineDelete,

		Schema: map[string]*schema.Schema{
			"name": &schema.Schema{
				Type:     schema.TypeString,
				Required: true,
                ForceNew: true,
			},
			"image": &schema.Schema{
				Type:     schema.TypeString,
				Required: true,
                ForceNew: true,
			},

			"datacenter": &schema.Schema{
				Type:     schema.TypeString,
				Optional: true,
				Computed: true,
			},
			"folder": &schema.Schema{
				Type:     schema.TypeString,
				Optional: true,
				Computed: true,
			},
			"host": &schema.Schema{
				Type:     schema.TypeString,
				Optional: true,
                Computed: true,
			},
			"resource_pool": &schema.Schema{
				Type:     schema.TypeString,
				Optional: true,
                Computed: true,
            },

			"linked_clone": &schema.Schema{
				Type:     schema.TypeBool,
				Optional: true,
                Default: false,
                ForceNew: true,
			},
			"cpus": &schema.Schema{
				Type:     schema.TypeInt,
				Optional: true,
                Computed: true,
			},
			"memory": &schema.Schema{
				Type:     schema.TypeInt,
				Optional: true,
                Computed: true,
			},
			"domain": &schema.Schema{
				Type:     schema.TypeString,
				Optional: true,
			},
            "ip_address": &schema.Schema{
                Type:     schema.TypeString,
                Optional: true,
                Computed: true,
            },
            "subnet_mask": &schema.Schema{
                Type:     schema.TypeString,
                Optional: true,
            },
            "gateway": &schema.Schema{
                Type:     schema.TypeString,
                Optional: true,
            },
            "configuration_parameters": &schema.Schema{
				Type:     schema.TypeMap,
				Optional: true,
                ForceNew: true,
			},
			"power_on": &schema.Schema{
				Type:     schema.TypeBool,
				Optional: true,
                Default:  true,
			},
        },
	}
}

func resourceVirtualMachineCreate(d *schema.ResourceData, meta interface{}) error {
	client := meta.(*vim25.Client)

    dc_name := d.Get("datacenter").(string)
    if dc_name == "" {
        finder := find.NewFinder(client, false)
        dc, err := finder.DefaultDatacenter(context.TODO())
        if err != nil {
            return fmt.Errorf("Error reading default datacenter: %s", err)
        }
        var dc_mo mo.Datacenter
        err = dc.Properties(context.TODO(), dc.Reference(), []string{"name"}, &dc_mo)
        if err != nil {
            return fmt.Errorf("Error reading datacenter name: %s", err)
        }
        dc_name = dc_mo.Name
        d.Set("datacenter", dc_name)
    }

	image_name := d.Get("image").(string)
    image_ref, err := object.NewSearchIndex(client).FindByInventoryPath(context.TODO(), fmt.Sprintf("%s/vm/%s", dc_name, image_name))
	if err != nil {
		return fmt.Errorf("Error reading vm: %s", err)
	}
    if image_ref == nil {
        return fmt.Errorf("Cannot find image %s", image_name)
    }
    image := image_ref.(*object.VirtualMachine)

    var image_mo mo.VirtualMachine
    err = image.Properties(context.TODO(), image.Reference(), []string{"parent", "config.template", "resourcePool", "snapshot", "guest.toolsVersionStatus2", "config.guestFullName"}, &image_mo)
    if err != nil {
        return fmt.Errorf("Error reading base VM properties: %s", err)
    }

	var folder_ref object.Reference
    var folder *object.Folder
    if d.Get("folder").(string) != "" {
        folder_ref, err = object.NewSearchIndex(client).FindByInventoryPath(context.TODO(), fmt.Sprintf("%v/vm/%v", dc_name, d.Get("folder").(string)))
        if err != nil {
            return fmt.Errorf("Error reading folder: %s", err)
        }
        if folder_ref == nil {
            return fmt.Errorf("Cannot find folder %s", d.Get("folder").(string))
        }

        folder = folder_ref.(*object.Folder)
    } else {
        folder = object.NewFolder(client, *image_mo.Parent)
    }

    host_name := d.Get("host").(string)
    if host_name == "" {
        if image_mo.Config.Template == true {
            return fmt.Errorf("Image is a template, 'host' is a required")
        } else {
            var pool_mo mo.ResourcePool
            err = property.DefaultCollector(client).RetrieveOne(context.TODO(), *image_mo.ResourcePool, []string{"owner"}, &pool_mo)
            if err != nil {
                return fmt.Errorf("Error reading resource pool of base VM: %s", err)
            }

            if strings.Contains(pool_mo.Owner.Value, "domain-s") {
                var host_mo mo.ComputeResource
                err = property.DefaultCollector(client).RetrieveOne(context.TODO(), pool_mo.Owner, []string{"name"}, &host_mo)
                if err != nil {
                    return fmt.Errorf("Error reading host of base VM: %s", err)
                }
                host_name = host_mo.Name
            } else if strings.Contains(pool_mo.Owner.Value, "domain-c") {
                var cluster_mo mo.ClusterComputeResource
                err = property.DefaultCollector(client).RetrieveOne(context.TODO(), pool_mo.Owner, []string{"name"}, &cluster_mo)
                if err != nil {
                    return fmt.Errorf("Error reading cluster of base VM: %s", err)
                }
                host_name = cluster_mo.Name
            } else {
                return fmt.Errorf("Unknown compute resource format of base VM: %s", pool_mo.Owner.Value)
            }
        }
    }

    pool_name := d.Get("resource_pool").(string)
    pool_ref, err := object.NewSearchIndex(client).FindByInventoryPath(context.TODO(), fmt.Sprintf("%v/host/%v/Resources/%v", dc_name, host_name, pool_name))
    if err != nil {
        return fmt.Errorf("Error reading resource pool: %s", err)
    }
    if pool_ref == nil {
        return fmt.Errorf("Cannot find resource pool %s", pool_name)
    }

	var relocateSpec types.VirtualMachineRelocateSpec
    var pool_mor types.ManagedObjectReference
    pool_mor = pool_ref.Reference()
    relocateSpec.Pool = &pool_mor

	if d.Get("linked_clone").(bool) {
		relocateSpec.DiskMoveType = "createNewChildDiskBacking"
	}
    var confSpec types.VirtualMachineConfigSpec
    if d.Get("cpus") != nil {
        confSpec.NumCPUs = d.Get("cpus").(int)
    }
    if d.Get("memory") != nil {
        confSpec.MemoryMB = int64(d.Get("memory").(int))
    }

    params := d.Get("configuration_parameters").(map[string]interface{})
    var ov []types.BaseOptionValue
    if len(params) > 0 {
        for k, v := range params {
            key := k
            value := v
            o := types.OptionValue{
                Key:   key,
                Value: &value,
            }
            ov = append(ov, &o)
        }
        confSpec.ExtraConfig = ov
    }

	cloneSpec := types.VirtualMachineCloneSpec{
		Location: relocateSpec,
        Config:   &confSpec,
        PowerOn:  d.Get("power_on").(bool),
	}
    if d.Get("linked_clone").(bool) {
        if image_mo.Snapshot == nil {
            return fmt.Errorf("`linked_clone=true`, but image VM has no snapshots")
        }
        cloneSpec.Snapshot = image_mo.Snapshot.CurrentSnapshot
    }

    domain := d.Get("domain").(string)
    ip_address := d.Get("ip_address").(string)
    if domain != "" {
        if image_mo.Guest.ToolsVersionStatus2 == "guestToolsNotInstalled" {
            return fmt.Errorf("VMware tools are not installed in base VM")
        }
        if !strings.Contains(image_mo.Config.GuestFullName, "Linux") && !strings.Contains(image_mo.Config.GuestFullName, "CentOS") {
            return fmt.Errorf("Guest customization is supported only for Linux. Base image OS is: %s", image_mo.Config.GuestFullName)
        }
        customizationSpec := types.CustomizationSpec{
            GlobalIPSettings: types.CustomizationGlobalIPSettings{},
            Identity: &types.CustomizationLinuxPrep{
                HostName: &types.CustomizationVirtualMachineName{},
                Domain: domain,
            },
            NicSettingMap: []types.CustomizationAdapterMapping {
                {
                    Adapter: types.CustomizationIPSettings{},
                },
            },
        }
        if ip_address != "" {
            mask := d.Get("subnet_mask").(string)
            if mask == "" {
                return fmt.Errorf("'subnet_mask' must be set, if static 'ip_address' is specified")
            }
            customizationSpec.NicSettingMap[0].Adapter.Ip = &types.CustomizationFixedIp{
                IpAddress: ip_address,
            }
            customizationSpec.NicSettingMap[0].Adapter.SubnetMask = d.Get("subnet_mask").(string)
            gateway := d.Get("gateway").(string)
            if gateway != "" {
                customizationSpec.NicSettingMap[0].Adapter.Gateway = []string{gateway}
            }
        } else {
            customizationSpec.NicSettingMap[0].Adapter.Ip = &types.CustomizationDhcpIpGenerator{}
        }
        cloneSpec.Customization = &customizationSpec
    } else if ip_address != "" {
        return fmt.Errorf("'domain' must be set, if static 'ip_address' is specified")
    }

	task, err := image.Clone(context.TODO(), folder, d.Get("name").(string), cloneSpec)
	if err != nil {
		return fmt.Errorf("Error clonning vm: %s", err)
	}
	info, err := task.WaitForResult(context.TODO(), nil)
	if err != nil {
		return fmt.Errorf("Error clonning vm: %s", err)
	}

	vm_mor := info.Result.(types.ManagedObjectReference)
    d.SetId(vm_mor.Value)
    vm := object.NewVirtualMachine(client, vm_mor)
    // workaround for https://github.com/vmware/govmomi/issues/218
    if ip_address=="" && d.Get("power_on").(bool) {
        ip, err := vm.WaitForIP(context.TODO())
        if err != nil {
            log.Printf("[ERROR] Cannot read ip address: %s", err)
        } else {
            d.Set("ip_address", ip)
            d.SetConnInfo(map[string]string{
                "type": "ssh",
                "host": ip,
            })
        }
    }

    return nil
}

func resourceVirtualMachineRead(d *schema.ResourceData, meta interface{}) error {
    client := meta.(*vim25.Client)
    vm_mor := types.ManagedObjectReference{Type: "VirtualMachine", Value: d.Id() }
    vm := object.NewVirtualMachine(client, vm_mor)

    var vm_mo mo.VirtualMachine
    err := vm.Properties(context.TODO(), vm.Reference(), []string{"summary"}, &vm_mo)
    if err != nil {
        log.Printf("[INFO] Cannot read VM properties: %s", err)
        d.SetId("")
        return nil
    }
    d.Set("name", vm_mo.Summary.Config.Name)
    d.Set("cpus", vm_mo.Summary.Config.NumCpu)
    d.Set("memory", vm_mo.Summary.Config.MemorySizeMB)

    if vm_mo.Summary.Runtime.PowerState == "poweredOn" {
        d.Set("power_on", true)
    } else {
        d.Set("power_on", false)
    }

    if d.Get("power_on").(bool) {
        ip, err := vm.WaitForIP(context.TODO())
        if err != nil {
            log.Printf("[ERROR] Cannot read ip address: %s", err)
        } else {
            d.Set("ip_address", ip)
        }
    }

	return nil
}

func resourceVirtualMachineDelete(d *schema.ResourceData, meta interface{}) error {
    client := meta.(*vim25.Client)
    vm_mor := types.ManagedObjectReference{Type: "VirtualMachine", Value: d.Id() }
    vm := object.NewVirtualMachine(client, vm_mor)

    task, err := vm.PowerOff(context.TODO())
    if err != nil {
        return fmt.Errorf("Error powering vm off: %s", err)
    }
    task.WaitForResult(context.TODO(), nil)

    task, err = vm.Destroy(context.TODO())
    if err != nil {
        return fmt.Errorf("Error deleting vm: %s", err)
    }
    _, err = task.WaitForResult(context.TODO(), nil)
    if err != nil {
        return fmt.Errorf("Error deleting vm: %s", err)
    }

    return nil
}
<commit_msg>Replace dots inside 'configuration_parameters'<commit_after>package main

import (
	"fmt"
	"log"
	"github.com/hashicorp/terraform/helper/schema"
    "golang.org/x/net/context"
    "github.com/vmware/govmomi/find"
    "github.com/vmware/govmomi/object"
    "github.com/vmware/govmomi/property"
    "github.com/vmware/govmomi/vim25"
	"github.com/vmware/govmomi/vim25/mo"
	"github.com/vmware/govmomi/vim25/types"
    "strings"
)

func resourceVirtualMachine() *schema.Resource {
	return &schema.Resource{
		Create: resourceVirtualMachineCreate,
		Read:   resourceVirtualMachineRead,
		Delete: resourceVirtualMachineDelete,

		Schema: map[string]*schema.Schema{
			"name": &schema.Schema{
				Type:     schema.TypeString,
				Required: true,
                ForceNew: true,
			},
			"image": &schema.Schema{
				Type:     schema.TypeString,
				Required: true,
                ForceNew: true,
			},

			"datacenter": &schema.Schema{
				Type:     schema.TypeString,
				Optional: true,
				Computed: true,
			},
			"folder": &schema.Schema{
				Type:     schema.TypeString,
				Optional: true,
				Computed: true,
			},
			"host": &schema.Schema{
				Type:     schema.TypeString,
				Optional: true,
                Computed: true,
			},
			"resource_pool": &schema.Schema{
				Type:     schema.TypeString,
				Optional: true,
                Computed: true,
            },

			"linked_clone": &schema.Schema{
				Type:     schema.TypeBool,
				Optional: true,
                Default: false,
                ForceNew: true,
			},
			"cpus": &schema.Schema{
				Type:     schema.TypeInt,
				Optional: true,
                Computed: true,
			},
			"memory": &schema.Schema{
				Type:     schema.TypeInt,
				Optional: true,
                Computed: true,
			},
			"domain": &schema.Schema{
				Type:     schema.TypeString,
				Optional: true,
                ForceNew: true,
			},
            "ip_address": &schema.Schema{
                Type:     schema.TypeString,
                Optional: true,
                Computed: true,
            },
            "subnet_mask": &schema.Schema{
                Type:     schema.TypeString,
                Optional: true,
                ForceNew: true,
            },
            "gateway": &schema.Schema{
                Type:     schema.TypeString,
                Optional: true,
                ForceNew: true,
            },
            "configuration_parameters": &schema.Schema{
				Type:     schema.TypeMap,
				Optional: true,
                ForceNew: true,
			},
			"power_on": &schema.Schema{
				Type:     schema.TypeBool,
				Optional: true,
                Default:  true,
                ForceNew: true,
			},
        },
	}
}

func resourceVirtualMachineCreate(d *schema.ResourceData, meta interface{}) error {
	client := meta.(*vim25.Client)

    dc_name := d.Get("datacenter").(string)
    if dc_name == "" {
        finder := find.NewFinder(client, false)
        dc, err := finder.DefaultDatacenter(context.TODO())
        if err != nil {
            return fmt.Errorf("Error reading default datacenter: %s", err)
        }
        var dc_mo mo.Datacenter
        err = dc.Properties(context.TODO(), dc.Reference(), []string{"name"}, &dc_mo)
        if err != nil {
            return fmt.Errorf("Error reading datacenter name: %s", err)
        }
        dc_name = dc_mo.Name
        d.Set("datacenter", dc_name)
    }

	image_name := d.Get("image").(string)
    image_ref, err := object.NewSearchIndex(client).FindByInventoryPath(context.TODO(), fmt.Sprintf("%s/vm/%s", dc_name, image_name))
	if err != nil {
		return fmt.Errorf("Error reading vm: %s", err)
	}
    if image_ref == nil {
        return fmt.Errorf("Cannot find image %s", image_name)
    }
    image := image_ref.(*object.VirtualMachine)

    var image_mo mo.VirtualMachine
    err = image.Properties(context.TODO(), image.Reference(), []string{"parent", "config.template", "resourcePool", "snapshot", "guest.toolsVersionStatus2", "config.guestFullName"}, &image_mo)
    if err != nil {
        return fmt.Errorf("Error reading base VM properties: %s", err)
    }

	var folder_ref object.Reference
    var folder *object.Folder
    if d.Get("folder").(string) != "" {
        folder_ref, err = object.NewSearchIndex(client).FindByInventoryPath(context.TODO(), fmt.Sprintf("%v/vm/%v", dc_name, d.Get("folder").(string)))
        if err != nil {
            return fmt.Errorf("Error reading folder: %s", err)
        }
        if folder_ref == nil {
            return fmt.Errorf("Cannot find folder %s", d.Get("folder").(string))
        }

        folder = folder_ref.(*object.Folder)
    } else {
        folder = object.NewFolder(client, *image_mo.Parent)
    }

    host_name := d.Get("host").(string)
    if host_name == "" {
        if image_mo.Config.Template == true {
            return fmt.Errorf("Image is a template, 'host' is a required")
        } else {
            var pool_mo mo.ResourcePool
            err = property.DefaultCollector(client).RetrieveOne(context.TODO(), *image_mo.ResourcePool, []string{"owner"}, &pool_mo)
            if err != nil {
                return fmt.Errorf("Error reading resource pool of base VM: %s", err)
            }

            if strings.Contains(pool_mo.Owner.Value, "domain-s") {
                var host_mo mo.ComputeResource
                err = property.DefaultCollector(client).RetrieveOne(context.TODO(), pool_mo.Owner, []string{"name"}, &host_mo)
                if err != nil {
                    return fmt.Errorf("Error reading host of base VM: %s", err)
                }
                host_name = host_mo.Name
            } else if strings.Contains(pool_mo.Owner.Value, "domain-c") {
                var cluster_mo mo.ClusterComputeResource
                err = property.DefaultCollector(client).RetrieveOne(context.TODO(), pool_mo.Owner, []string{"name"}, &cluster_mo)
                if err != nil {
                    return fmt.Errorf("Error reading cluster of base VM: %s", err)
                }
                host_name = cluster_mo.Name
            } else {
                return fmt.Errorf("Unknown compute resource format of base VM: %s", pool_mo.Owner.Value)
            }
        }
    }

    pool_name := d.Get("resource_pool").(string)
    pool_ref, err := object.NewSearchIndex(client).FindByInventoryPath(context.TODO(), fmt.Sprintf("%v/host/%v/Resources/%v", dc_name, host_name, pool_name))
    if err != nil {
        return fmt.Errorf("Error reading resource pool: %s", err)
    }
    if pool_ref == nil {
        return fmt.Errorf("Cannot find resource pool %s", pool_name)
    }

	var relocateSpec types.VirtualMachineRelocateSpec
    var pool_mor types.ManagedObjectReference
    pool_mor = pool_ref.Reference()
    relocateSpec.Pool = &pool_mor

	if d.Get("linked_clone").(bool) {
		relocateSpec.DiskMoveType = "createNewChildDiskBacking"
	}
    var confSpec types.VirtualMachineConfigSpec
    if d.Get("cpus") != nil {
        confSpec.NumCPUs = d.Get("cpus").(int)
    }
    if d.Get("memory") != nil {
        confSpec.MemoryMB = int64(d.Get("memory").(int))
    }

    params := d.Get("configuration_parameters").(map[string]interface{})
    var ov []types.BaseOptionValue
    if len(params) > 0 {
        for k, v := range params {
            key := strings.Replace(k, "_",".", -1)
            value := v
            o := types.OptionValue{
                Key:   key,
                Value: &value,
            }
            ov = append(ov, &o)
        }
        confSpec.ExtraConfig = ov
    }

	cloneSpec := types.VirtualMachineCloneSpec{
		Location: relocateSpec,
        Config:   &confSpec,
        PowerOn:  d.Get("power_on").(bool),
	}
    if d.Get("linked_clone").(bool) {
        if image_mo.Snapshot == nil {
            return fmt.Errorf("`linked_clone=true`, but image VM has no snapshots")
        }
        cloneSpec.Snapshot = image_mo.Snapshot.CurrentSnapshot
    }

    domain := d.Get("domain").(string)
    ip_address := d.Get("ip_address").(string)
    if domain != "" {
        if image_mo.Guest.ToolsVersionStatus2 == "guestToolsNotInstalled" {
            return fmt.Errorf("VMware tools are not installed in base VM")
        }
        if !strings.Contains(image_mo.Config.GuestFullName, "Linux") && !strings.Contains(image_mo.Config.GuestFullName, "CentOS") {
            return fmt.Errorf("Guest customization is supported only for Linux. Base image OS is: %s", image_mo.Config.GuestFullName)
        }
        customizationSpec := types.CustomizationSpec{
            GlobalIPSettings: types.CustomizationGlobalIPSettings{},
            Identity: &types.CustomizationLinuxPrep{
                HostName: &types.CustomizationVirtualMachineName{},
                Domain: domain,
            },
            NicSettingMap: []types.CustomizationAdapterMapping {
                {
                    Adapter: types.CustomizationIPSettings{},
                },
            },
        }
        if ip_address != "" {
            mask := d.Get("subnet_mask").(string)
            if mask == "" {
                return fmt.Errorf("'subnet_mask' must be set, if static 'ip_address' is specified")
            }
            customizationSpec.NicSettingMap[0].Adapter.Ip = &types.CustomizationFixedIp{
                IpAddress: ip_address,
            }
            customizationSpec.NicSettingMap[0].Adapter.SubnetMask = d.Get("subnet_mask").(string)
            gateway := d.Get("gateway").(string)
            if gateway != "" {
                customizationSpec.NicSettingMap[0].Adapter.Gateway = []string{gateway}
            }
        } else {
            customizationSpec.NicSettingMap[0].Adapter.Ip = &types.CustomizationDhcpIpGenerator{}
        }
        cloneSpec.Customization = &customizationSpec
    } else if ip_address != "" {
        return fmt.Errorf("'domain' must be set, if static 'ip_address' is specified")
    }

	task, err := image.Clone(context.TODO(), folder, d.Get("name").(string), cloneSpec)
	if err != nil {
		return fmt.Errorf("Error clonning vm: %s", err)
	}
	info, err := task.WaitForResult(context.TODO(), nil)
	if err != nil {
		return fmt.Errorf("Error clonning vm: %s", err)
	}

	vm_mor := info.Result.(types.ManagedObjectReference)
    d.SetId(vm_mor.Value)
    vm := object.NewVirtualMachine(client, vm_mor)
    // workaround for https://github.com/vmware/govmomi/issues/218
    if ip_address=="" && d.Get("power_on").(bool) {
        ip, err := vm.WaitForIP(context.TODO())
        if err != nil {
            log.Printf("[ERROR] Cannot read ip address: %s", err)
        } else {
            d.Set("ip_address", ip)
            d.SetConnInfo(map[string]string{
                "type": "ssh",
                "host": ip,
            })
        }
    }

    return nil
}

func resourceVirtualMachineRead(d *schema.ResourceData, meta interface{}) error {
    client := meta.(*vim25.Client)
    vm_mor := types.ManagedObjectReference{Type: "VirtualMachine", Value: d.Id() }
    vm := object.NewVirtualMachine(client, vm_mor)

    var vm_mo mo.VirtualMachine
    err := vm.Properties(context.TODO(), vm.Reference(), []string{"summary"}, &vm_mo)
    if err != nil {
        log.Printf("[INFO] Cannot read VM properties: %s", err)
        d.SetId("")
        return nil
    }
    d.Set("name", vm_mo.Summary.Config.Name)
    d.Set("cpus", vm_mo.Summary.Config.NumCpu)
    d.Set("memory", vm_mo.Summary.Config.MemorySizeMB)

    if vm_mo.Summary.Runtime.PowerState == "poweredOn" {
        d.Set("power_on", true)
    } else {
        d.Set("power_on", false)
    }

    if d.Get("power_on").(bool) {
        ip, err := vm.WaitForIP(context.TODO())
        if err != nil {
            log.Printf("[ERROR] Cannot read ip address: %s", err)
        } else {
            d.Set("ip_address", ip)
        }
    }

	return nil
}

func resourceVirtualMachineDelete(d *schema.ResourceData, meta interface{}) error {
    client := meta.(*vim25.Client)
    vm_mor := types.ManagedObjectReference{Type: "VirtualMachine", Value: d.Id() }
    vm := object.NewVirtualMachine(client, vm_mor)

    task, err := vm.PowerOff(context.TODO())
    if err != nil {
        return fmt.Errorf("Error powering vm off: %s", err)
    }
    task.WaitForResult(context.TODO(), nil)

    task, err = vm.Destroy(context.TODO())
    if err != nil {
        return fmt.Errorf("Error deleting vm: %s", err)
    }
    _, err = task.WaitForResult(context.TODO(), nil)
    if err != nil {
        return fmt.Errorf("Error deleting vm: %s", err)
    }

    return nil
}
<|endoftext|>


<commit_before>// +build darwin linux

package utils

import (
	"fmt"
	"syscall"
)

// OpenFileLimit returns a string displaying the current open file limit
// for the process or unknown if it's not possible to detect it
func OpenFileLimit() (limitStr string) {
	var rLimit syscall.Rlimit

	if err := syscall.Getrlimit(syscall.RLIMIT_NOFILE, &rLimit); err != nil {
		limitStr = "unknown"
	} else {
		limitStr = fmt.Sprintf("%d", rLimit.Cur)
	}

	return
}
<commit_msg>fix: build tags for os_limits<commit_after>// +build !windows

package utils

import (
	"fmt"
	"syscall"
)

// OpenFileLimit returns a string displaying the current open file limit
// for the process or unknown if it's not possible to detect it
func OpenFileLimit() (limitStr string) {
	var rLimit syscall.Rlimit

	if err := syscall.Getrlimit(syscall.RLIMIT_NOFILE, &rLimit); err != nil {
		limitStr = "unknown"
	} else {
		limitStr = fmt.Sprintf("%d", rLimit.Cur)
	}

	return
}
<|endoftext|>


<commit_before>// +build integration
// To turn on this test use -tags=integration in go test command

package credentials

import (
	"bytes"
	"fmt"
	"io"
	"os"
	"os/exec"
	"os/user"
	"testing"

	"github.com/stretchr/testify/assert"
	"gopkg.in/jcmturner/gokrb5.v4/iana/nametype"
	"gopkg.in/jcmturner/gokrb5.v4/testdata"
	"gopkg.in/jcmturner/gokrb5.v4/types"
)

const (
	kinitCmd = "kinit"
	kvnoCmd  = "kvno"
	spn      = "HTTP/host.test.gokrb5"
)

func login() error {
	file, err := os.Create("/etc/krb5.conf")
	if err != nil {
		return fmt.Errorf("cannot open krb5.conf: %v", err)
	}
	defer file.Close()
	fmt.Fprintf(file, testdata.TEST_KRB5CONF)

	cmd := exec.Command(kinitCmd, "testuser1@TEST.GOKRB5")

	stdinR, stdinW := io.Pipe()
	stderrR, stderrW := io.Pipe()
	cmd.Stdin = stdinR
	cmd.Stderr = stderrW

	err = cmd.Start()
	if err != nil {
		return fmt.Errorf("could not start %s command: %v", kinitCmd, err)
	}

	go func() {
		io.WriteString(stdinW, "passwordvalue")
		stdinW.Close()
	}()
	errBuf := new(bytes.Buffer)
	go func() {
		io.Copy(errBuf, stderrR)
		stderrR.Close()
	}()

	err = cmd.Wait()
	if err != nil {
		return fmt.Errorf("%s did not run successfully: %v stderr: %s", kinitCmd, err, string(errBuf.Bytes()))
	}
	return nil
}

func getServiceTkt() error {
	cmd := exec.Command(kvnoCmd, spn)
	err := cmd.Start()
	if err != nil {
		return fmt.Errorf("could not start %s command: %v", kvnoCmd, err)
	}
	err = cmd.Wait()
	if err != nil {
		return fmt.Errorf("%s did not run successfully: %v", kvnoCmd, err)
	}
	return nil
}

func loadCCache() (CCache, error) {
	usr, _ := user.Current()
	cpath := "/tmp/krb5cc_" + usr.Uid
	return LoadCCache(cpath)
}

func TestLoadCCache(t *testing.T) {
	err := login()
	if err != nil {
		t.Fatalf("error logging in with kinit: %v", err)
	}
	c, err := loadCCache()
	if err != nil {
		t.Errorf("error loading CCache: %v", err)
	}
	pn := c.GetClientPrincipalName()
	assert.Equal(t, "testuser1", pn.GetPrincipalNameString(), "principal not as expected")
	assert.Equal(t, "TEST.GOKRB5", c.GetClientRealm(), "realm not as expected")
}

func TestCCacheEntries(t *testing.T) {
	err := login()
	if err != nil {
		t.Fatalf("error logging in with kinit: %v", err)
	}
	err = getServiceTkt()
	if err != nil {
		t.Fatalf("error getting service ticket: %v", err)
	}
	c, err := loadCCache()
	if err != nil {
		t.Errorf("error loading CCache: %v", err)
	}
	creds := c.GetEntries()
	var found bool
	n := types.NewPrincipalName(nametype.KRB_NT_PRINCIPAL, spn)
	for _, cred := range creds {
		if cred.Server.PrincipalName.Equal(n) {
			found = true
			break
		}
	}
	if !found {
		t.Errorf("Entry for %s not found in CCache", spn)
	}
}
<commit_msg>ccache tests<commit_after>/////// +build integration
// To turn on this test use -tags=integration in go test command

package credentials

import (
	"bytes"
	"fmt"
	"io"
	"os"
	"os/exec"
	"os/user"
	"testing"

	"github.com/stretchr/testify/assert"
	"gopkg.in/jcmturner/gokrb5.v4/iana/nametype"
	"gopkg.in/jcmturner/gokrb5.v4/testdata"
	"gopkg.in/jcmturner/gokrb5.v4/types"
)

const (
	kinitCmd = "kinit"
	kvnoCmd  = "kvno"
	klistCmd = "klist"
	spn      = "HTTP/host.test.gokrb5"
)

func login() error {
	file, err := os.Create("/etc/krb5.conf")
	if err != nil {
		return fmt.Errorf("cannot open krb5.conf: %v", err)
	}
	defer file.Close()
	fmt.Fprintf(file, testdata.TEST_KRB5CONF)

	cmd := exec.Command(kinitCmd, "testuser1@TEST.GOKRB5")

	stdinR, stdinW := io.Pipe()
	stderrR, stderrW := io.Pipe()
	cmd.Stdin = stdinR
	cmd.Stderr = stderrW

	err = cmd.Start()
	if err != nil {
		return fmt.Errorf("could not start %s command: %v", kinitCmd, err)
	}

	go func() {
		io.WriteString(stdinW, "passwordvalue")
		stdinW.Close()
	}()
	errBuf := new(bytes.Buffer)
	go func() {
		io.Copy(errBuf, stderrR)
		stderrR.Close()
	}()

	err = cmd.Wait()
	if err != nil {
		return fmt.Errorf("%s did not run successfully: %v stderr: %s", kinitCmd, err, string(errBuf.Bytes()))
	}
	return nil
}

func getServiceTkt() error {
	cmd := exec.Command(kvnoCmd, spn)
	err := cmd.Start()
	if err != nil {
		return fmt.Errorf("could not start %s command: %v", kvnoCmd, err)
	}
	err = cmd.Wait()
	if err != nil {
		return fmt.Errorf("%s did not run successfully: %v", kvnoCmd, err)
	}
	return nil
}

func klist() (string, error) {
	cmd := exec.Command(klistCmd, "-Aef")

	stdoutR, stdoutW := io.Pipe()
	cmd.Stdout = stdoutW

	err := cmd.Start()
	if err != nil {
		return "", fmt.Errorf("could not start %s command: %v", klistCmd, err)
	}
	outBuf := new(bytes.Buffer)
	go func() {
		io.Copy(outBuf, stdoutR)
		stdoutR.Close()
	}()

	err = cmd.Wait()
	if err != nil {
		return "", fmt.Errorf("%s did not run successfully: %v", klistCmd, err)
	}
	return string(outBuf.Bytes()), nil
}

func loadCCache() (CCache, error) {
	usr, _ := user.Current()
	cpath := "/tmp/krb5cc_" + usr.Uid
	return LoadCCache(cpath)
}

func TestLoadCCache(t *testing.T) {
	err := login()
	if err != nil {
		t.Fatalf("error logging in with kinit: %v", err)
	}
	c, err := loadCCache()
	if err != nil {
		t.Errorf("error loading CCache: %v", err)
	}
	pn := c.GetClientPrincipalName()
	assert.Equal(t, "testuser1", pn.GetPrincipalNameString(), "principal not as expected")
	assert.Equal(t, "TEST.GOKRB5", c.GetClientRealm(), "realm not as expected")
}

func TestCCacheEntries(t *testing.T) {
	err := login()
	if err != nil {
		t.Fatalf("error logging in with kinit: %v", err)
	}
	err = getServiceTkt()
	if err != nil {
		t.Fatalf("error getting service ticket: %v", err)
	}
	clist, _ := klist()
	t.Logf("OS Creds Cache contents:\n%s", clist)
	c, err := loadCCache()
	if err != nil {
		t.Errorf("error loading CCache: %v", err)
	}
	creds := c.GetEntries()
	var found bool
	n := types.NewPrincipalName(nametype.KRB_NT_PRINCIPAL, spn)
	for _, cred := range creds {
		if cred.Server.PrincipalName.Equal(n) {
			found = true
			break
		}
	}
	if !found {
		t.Errorf("Entry for %s not found in CCache", spn)
	}
}
<|endoftext|>