From e006bd2fd1a722bf73fbf6decd9b1bd07b725829 Mon Sep 17 00:00:00 2001 From: Jeff Mitchell Date: Sun, 20 Sep 2020 12:03:28 -0400 Subject: [PATCH] Add session max seconds and connection limit to target resource (#44) --- go.mod | 2 +- go.sum | 4 +- internal/provider/resource_target.go | 74 ++++++++++++++++++++++++++-- 3 files changed, 72 insertions(+), 8 deletions(-) diff --git a/go.mod b/go.mod index 8336ab2d..7cbb1e9a 100644 --- a/go.mod +++ b/go.mod @@ -8,7 +8,7 @@ require ( github.com/aws/aws-sdk-go v1.34.16 // indirect github.com/cenkalti/backoff/v3 v3.2.2 // indirect github.com/containerd/continuity v0.0.0-20200710164510-efbc4488d8fe // indirect - github.com/hashicorp/boundary v0.0.0-20200916165641-8bef1d734a2d + github.com/hashicorp/boundary v0.0.0-20200920010556-223591d835ad github.com/hashicorp/go-kms-wrapping v0.5.14 github.com/hashicorp/hcl/v2 v2.6.0 // indirect github.com/hashicorp/terraform-plugin-sdk/v2 v2.0.3 diff --git a/go.sum b/go.sum index f08dd612..3b476338 100644 --- a/go.sum +++ b/go.sum @@ -584,8 +584,8 @@ github.com/grpc-ecosystem/grpc-gateway v1.9.5/go.mod h1:vNeuVxBJEsws4ogUvrchl83t github.com/grpc-ecosystem/grpc-gateway/v2 v2.0.0-beta.4.0.20200813083929-9bdca3d79b97 h1:BFGJgt5fEZhMtNE8VrTWmAMnLptKdn98qLgrSFNmDxY= github.com/grpc-ecosystem/grpc-gateway/v2 v2.0.0-beta.4.0.20200813083929-9bdca3d79b97/go.mod h1:wK7PFLUnfulN3kJW75Btnhb3iasamaju+L19L0bFXZk= github.com/hailocab/go-hostpool v0.0.0-20160125115350-e80d13ce29ed/go.mod h1:tMWxXQ9wFIaZeTI9F+hmhFiGpFmhOHzyShyFUhRm0H4= -github.com/hashicorp/boundary v0.0.0-20200916165641-8bef1d734a2d h1:EDtx9rdXK+zBtLBhrWGFA2+aMZopf4QffrcrT5biiEQ= -github.com/hashicorp/boundary v0.0.0-20200916165641-8bef1d734a2d/go.mod h1:o72yXNHyqJ6f1UEfJmWII4hIThwwU9L4L45jdmJgw8Y= +github.com/hashicorp/boundary v0.0.0-20200920010556-223591d835ad h1:WIREwB752ioyMB8YLBTy4QW22ZFi1BpzmV544O+klmM= +github.com/hashicorp/boundary v0.0.0-20200920010556-223591d835ad/go.mod h1:o72yXNHyqJ6f1UEfJmWII4hIThwwU9L4L45jdmJgw8Y= github.com/hashicorp/consul/api v1.1.0/go.mod h1:VmuI/Lkw1nC05EYQWNKwWGbkg+FbDBtguAZLlVdkD9Q= github.com/hashicorp/consul/api v1.3.0/go.mod h1:MmDNSzIMUjNpY/mQ398R4bk2FnqQLoPndWW5VkKPlCE= github.com/hashicorp/consul/sdk v0.1.1/go.mod h1:VKf9jXwCTEY1QZP2MOLRhb5i/I/ssyNV1vwHyQBF0x8= diff --git a/internal/provider/resource_target.go b/internal/provider/resource_target.go index 15968c7c..c96ef2e1 100644 --- a/internal/provider/resource_target.go +++ b/internal/provider/resource_target.go @@ -11,8 +11,10 @@ import ( ) const ( - targetHostSetIdsKey = "host_set_ids" - targetDefaultPortKey = "default_port" + targetHostSetIdsKey = "host_set_ids" + targetDefaultPortKey = "default_port" + targetSessionMaxSecondsKey = "session_max_seconds" + targetSessionConnectionLimitKey = "session_connection_limit" targetTypeTcp = "tcp" ) @@ -51,6 +53,16 @@ func resourceTarget() *schema.Resource { Optional: true, Elem: &schema.Schema{Type: schema.TypeString}, }, + targetSessionMaxSecondsKey: { + Type: schema.TypeInt, + Optional: true, + Computed: true, + }, + targetSessionConnectionLimitKey: { + Type: schema.TypeInt, + Optional: true, + Computed: true, + }, }, } } @@ -61,6 +73,8 @@ func setFromTargetResponseMap(d *schema.ResourceData, raw map[string]interface{} d.Set(ScopeIdKey, raw["scope_id"]) d.Set(TypeKey, raw["type"]) d.Set(targetHostSetIdsKey, raw["host_set_ids"]) + d.Set(targetSessionMaxSecondsKey, raw["session_max_seconds"]) + d.Set(targetSessionConnectionLimitKey, raw["session_connection_limit"]) switch raw["type"].(string) { case targetTypeTcp: @@ -112,15 +126,31 @@ func resourceTargetCreate(ctx context.Context, d *schema.ResourceData, meta inte opts = append(opts, targets.WithDescription(descStr)) } - var defaultPort *int defaultPortVal, ok := d.GetOk(targetDefaultPortKey) if ok { defaultPortInt := defaultPortVal.(int) if defaultPortInt < 0 { return diag.Errorf(`"default_port" cannot be less than zero`) } - defaultPort = &defaultPortInt - opts = append(opts, targets.WithTcpTargetDefaultPort(uint32(*defaultPort))) + opts = append(opts, targets.WithTcpTargetDefaultPort(uint32(defaultPortInt))) + } + + sessionMaxSecondsVal, ok := d.GetOk(targetSessionMaxSecondsKey) + if ok { + sessionMaxSecondsInt := sessionMaxSecondsVal.(int) + if sessionMaxSecondsInt < 0 { + return diag.Errorf(`"session_max_seconds" cannot be less than zero`) + } + opts = append(opts, targets.WithSessionMaxSeconds(uint32(sessionMaxSecondsInt))) + } + + sessionConnectionLimitVal, ok := d.GetOk(targetSessionConnectionLimitKey) + if ok { + sessionConnectionLimitInt := sessionConnectionLimitVal.(int) + if sessionConnectionLimitInt < 0 { + return diag.Errorf(`"session_connection_limit" cannot be less than zero`) + } + opts = append(opts, targets.WithSessionConnectionLimit(uint32(sessionConnectionLimitInt))) } var hostSetIds []string @@ -236,6 +266,34 @@ func resourceTargetUpdate(ctx context.Context, d *schema.ResourceData, meta inte } } + var sessionMaxSeconds *int + if d.HasChange(targetSessionMaxSecondsKey) { + opts = append(opts, targets.DefaultSessionMaxSeconds()) + sessionMaxSecondsVal, ok := d.GetOk(targetSessionMaxSecondsKey) + if ok { + sessionMaxSecondsInt := sessionMaxSecondsVal.(int) + if sessionMaxSecondsInt < 0 { + return diag.Errorf(`"session_max_seconds" cannot be less than zero`) + } + sessionMaxSeconds = &sessionMaxSecondsInt + opts = append(opts, targets.WithSessionMaxSeconds(uint32(sessionMaxSecondsInt))) + } + } + + var sessionConnectionLimit *int + if d.HasChange(targetSessionConnectionLimitKey) { + opts = append(opts, targets.DefaultSessionConnectionLimit()) + sessionConnectionLimitVal, ok := d.GetOk(targetSessionConnectionLimitKey) + if ok { + sessionConnectionLimitInt := sessionConnectionLimitVal.(int) + if sessionConnectionLimitInt < 0 { + return diag.Errorf(`"session_connection_limit" cannot be less than zero`) + } + sessionConnectionLimit = &sessionConnectionLimitInt + opts = append(opts, targets.WithSessionConnectionLimit(uint32(sessionConnectionLimitInt))) + } + } + if len(opts) > 0 { opts = append(opts, targets.WithAutomaticVersioning(true)) _, apiErr, err := tc.Update( @@ -260,6 +318,12 @@ func resourceTargetUpdate(ctx context.Context, d *schema.ResourceData, meta inte if d.HasChange(targetDefaultPortKey) { d.Set(targetDefaultPortKey, defaultPort) } + if d.HasChange(targetSessionMaxSecondsKey) { + d.Set(targetSessionMaxSecondsKey, sessionMaxSeconds) + } + if d.HasChange(targetSessionConnectionLimitKey) { + d.Set(targetSessionConnectionLimitKey, sessionConnectionLimit) + } // The above call may not actually happen, so we use d.Id() and automatic // versioning here