From d009d796960333e45278a36f404ac3701368461c Mon Sep 17 00:00:00 2001 From: Caleb Tennis Date: Thu, 13 Aug 2015 15:32:40 -0400 Subject: [PATCH] Fix #533, add a reader for lease values (#529) and an acceptance test for mysql to prove it works --- builtin/logical/aws/path_config_lease.go | 22 +++++++++- builtin/logical/mysql/backend_test.go | 40 +++++++++++++++++++ builtin/logical/mysql/path_config_lease.go | 22 +++++++++- .../logical/postgresql/path_config_lease.go | 22 +++++++++- 4 files changed, 103 insertions(+), 3 deletions(-) diff --git a/builtin/logical/aws/path_config_lease.go b/builtin/logical/aws/path_config_lease.go index 1c5d6d820b37..94ec05f72040 100644 --- a/builtin/logical/aws/path_config_lease.go +++ b/builtin/logical/aws/path_config_lease.go @@ -24,6 +24,7 @@ func pathConfigLease(b *backend) *framework.Path { }, Callbacks: map[logical.Operation]framework.OperationFunc{ + logical.ReadOperation: b.pathLeaseRead, logical.WriteOperation: b.pathLeaseWrite, }, @@ -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 { @@ -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 diff --git a/builtin/logical/mysql/backend_test.go b/builtin/logical/mysql/backend_test.go index 937b054132a7..6045b39d6640 100644 --- a/builtin/logical/mysql/backend_test.go +++ b/builtin/logical/mysql/backend_test.go @@ -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") @@ -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}}'@'%'; diff --git a/builtin/logical/mysql/path_config_lease.go b/builtin/logical/mysql/path_config_lease.go index d2f0950d7164..0ad390d94dfc 100644 --- a/builtin/logical/mysql/path_config_lease.go +++ b/builtin/logical/mysql/path_config_lease.go @@ -24,6 +24,7 @@ func pathConfigLease(b *backend) *framework.Path { }, Callbacks: map[logical.Operation]framework.OperationFunc{ + logical.ReadOperation: b.pathLeaseRead, logical.WriteOperation: b.pathLeaseWrite, }, @@ -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 { @@ -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 diff --git a/builtin/logical/postgresql/path_config_lease.go b/builtin/logical/postgresql/path_config_lease.go index e13bb20891e8..3d352c71b0f3 100644 --- a/builtin/logical/postgresql/path_config_lease.go +++ b/builtin/logical/postgresql/path_config_lease.go @@ -24,6 +24,7 @@ func pathConfigLease(b *backend) *framework.Path { }, Callbacks: map[logical.Operation]framework.OperationFunc{ + logical.ReadOperation: b.pathLeaseRead, logical.WriteOperation: b.pathLeaseWrite, }, @@ -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 { @@ -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