Skip to content

Commit

Permalink
Fix #533, add a reader for lease values (#529) and an acceptance test…
Browse files Browse the repository at this point in the history
… for mysql to prove it works
  • Loading branch information
ctennis committed Aug 13, 2015
1 parent 6d05df1 commit d009d79
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 3 deletions.
22 changes: 21 additions & 1 deletion builtin/logical/aws/path_config_lease.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ func pathConfigLease(b *backend) *framework.Path {
},

Callbacks: map[logical.Operation]framework.OperationFunc{
logical.ReadOperation: b.pathLeaseRead,
logical.WriteOperation: b.pathLeaseWrite,
},

Expand Down Expand Up @@ -53,7 +54,7 @@ func (b *backend) Lease(s logical.Storage) (*configLease, error) {
func (b *backend) pathLeaseWrite(
req *logical.Request, d *framework.FieldData) (*logical.Response, error) {
leaseRaw := d.Get("lease").(string)
leaseMaxRaw := d.Get("lease").(string)
leaseMaxRaw := d.Get("lease_max").(string)

lease, err := time.ParseDuration(leaseRaw)
if err != nil {
Expand Down Expand Up @@ -81,6 +82,25 @@ func (b *backend) pathLeaseWrite(
return nil, nil
}

func (b *backend) pathLeaseRead(
req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
lease, err := b.Lease(req.Storage)

if err != nil {
return nil, err
}
if lease == nil {
return nil, nil
}

return &logical.Response{
Data: map[string]interface{}{
"lease": lease.Lease.String(),
"lease_max": lease.LeaseMax.String(),
},
}, nil
}

type configLease struct {
Lease time.Duration
LeaseMax time.Duration
Expand Down
40 changes: 40 additions & 0 deletions builtin/logical/mysql/backend_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,21 @@ func TestBackend_roleCrud(t *testing.T) {
})
}

func TestBackend_leaseWriteRead(t *testing.T) {
b := Backend()

logicaltest.Test(t, logicaltest.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Backend: b,
Steps: []logicaltest.TestStep{
testAccStepConfig(t),
testAccStepWriteLease(t),
testAccStepReadLease(t),
},
})

}

func testAccPreCheck(t *testing.T) {
if v := os.Getenv("MYSQL_DSN"); v == "" {
t.Fatal("MYSQL_DSN must be set for acceptance tests")
Expand Down Expand Up @@ -122,6 +137,31 @@ func testAccStepReadRole(t *testing.T, name string, sql string) logicaltest.Test
}
}

func testAccStepWriteLease(t *testing.T) logicaltest.TestStep {
return logicaltest.TestStep{
Operation: logical.WriteOperation,
Path: "config/lease",
Data: map[string]interface{}{
"lease": "1h5m",
"lease_max": "24h",
},
}
}

func testAccStepReadLease(t *testing.T) logicaltest.TestStep {
return logicaltest.TestStep{
Operation: logical.ReadOperation,
Path: "config/lease",
Check: func(resp *logical.Response) error {
if resp.Data["lease"] != "1h5m0s" || resp.Data["lease_max"] != "24h0m0s" {
return fmt.Errorf("bad: %#v", resp)
}

return nil
},
}
}

const testRole = `
CREATE USER '{{name}}'@'%' IDENTIFIED BY '{{password}}';
GRANT SELECT ON *.* TO '{{name}}'@'%';
Expand Down
22 changes: 21 additions & 1 deletion builtin/logical/mysql/path_config_lease.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ func pathConfigLease(b *backend) *framework.Path {
},

Callbacks: map[logical.Operation]framework.OperationFunc{
logical.ReadOperation: b.pathLeaseRead,
logical.WriteOperation: b.pathLeaseWrite,
},

Expand All @@ -35,7 +36,7 @@ func pathConfigLease(b *backend) *framework.Path {
func (b *backend) pathLeaseWrite(
req *logical.Request, d *framework.FieldData) (*logical.Response, error) {
leaseRaw := d.Get("lease").(string)
leaseMaxRaw := d.Get("lease").(string)
leaseMaxRaw := d.Get("lease_max").(string)

lease, err := time.ParseDuration(leaseRaw)
if err != nil {
Expand Down Expand Up @@ -63,6 +64,25 @@ func (b *backend) pathLeaseWrite(
return nil, nil
}

func (b *backend) pathLeaseRead(
req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
lease, err := b.Lease(req.Storage)

if err != nil {
return nil, err
}
if lease == nil {
return nil, nil
}

return &logical.Response{
Data: map[string]interface{}{
"lease": lease.Lease.String(),
"lease_max": lease.LeaseMax.String(),
},
}, nil
}

type configLease struct {
Lease time.Duration
LeaseMax time.Duration
Expand Down
22 changes: 21 additions & 1 deletion builtin/logical/postgresql/path_config_lease.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ func pathConfigLease(b *backend) *framework.Path {
},

Callbacks: map[logical.Operation]framework.OperationFunc{
logical.ReadOperation: b.pathLeaseRead,
logical.WriteOperation: b.pathLeaseWrite,
},

Expand All @@ -35,7 +36,7 @@ func pathConfigLease(b *backend) *framework.Path {
func (b *backend) pathLeaseWrite(
req *logical.Request, d *framework.FieldData) (*logical.Response, error) {
leaseRaw := d.Get("lease").(string)
leaseMaxRaw := d.Get("lease").(string)
leaseMaxRaw := d.Get("lease_max").(string)

lease, err := time.ParseDuration(leaseRaw)
if err != nil {
Expand Down Expand Up @@ -63,6 +64,25 @@ func (b *backend) pathLeaseWrite(
return nil, nil
}

func (b *backend) pathLeaseRead(
req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
lease, err := b.Lease(req.Storage)

if err != nil {
return nil, err
}
if lease == nil {
return nil, nil
}

return &logical.Response{
Data: map[string]interface{}{
"lease": lease.Lease.String(),
"lease_max": lease.LeaseMax.String(),
},
}, nil
}

type configLease struct {
Lease time.Duration
LeaseMax time.Duration
Expand Down

0 comments on commit d009d79

Please sign in to comment.