Skip to content

Commit

Permalink
adds connection_pooler to tembo_instance resource
Browse files Browse the repository at this point in the history
  • Loading branch information
shahadarsh committed Oct 31, 2023
1 parent 4e790ee commit e2677d9
Show file tree
Hide file tree
Showing 3 changed files with 154 additions and 21 deletions.
31 changes: 31 additions & 0 deletions docs/resources/instance.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ output "instance" {

### Optional

- `connection_pooler` (Attributes) (see [below for nested schema](#nestedatt--connection_pooler))
- `extensions` (Attributes List) Extensions to install in the instance (see [below for nested schema](#nestedatt--extensions))
- `extra_domains_rw` (List of String) Custom domain. Read more [here](https://tembo.io/docs/tembo-cloud/custom-domains/)
- `ip_allow_list` (List of String) Allowed IP list
Expand All @@ -91,6 +92,36 @@ output "instance" {
- `last_updated` (String) Last updated date time in UTC
- `state` (String) Instance state. Values: Submitted, Up, Configuring, Error, Restarting, Starting, Stopping, Stopped, Deleting, Deleted

<a id="nestedatt--connection_pooler"></a>
### Nested Schema for `connection_pooler`

Required:

- `enabled` (Boolean)

Optional:

- `pooler` (Attributes) (see [below for nested schema](#nestedatt--connection_pooler--pooler))

<a id="nestedatt--connection_pooler--pooler"></a>
### Nested Schema for `connection_pooler.pooler`

Required:

- `parameters` (Attributes List) Parameters (see [below for nested schema](#nestedatt--connection_pooler--pooler--parameters))
- `pool_mode` (String)

<a id="nestedatt--connection_pooler--pooler--parameters"></a>
### Nested Schema for `connection_pooler.pooler.parameters`

Required:

- `name` (String) Parameter name
- `value` (String) Parameter value




<a id="nestedatt--extensions"></a>
### Nested Schema for `extensions`

Expand Down
18 changes: 17 additions & 1 deletion examples/resource-creation/main.tf
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
resource "tembo_instance" "test_db" {
instance_name = "tfprovider-de2345"
instance_name = "tfprovider-4"
org_id = "org_2UJ2WPYFsE42Cos6mlmIuwIIJ4V"
cpu = "1"
stack_type = "Standard"
Expand Down Expand Up @@ -63,6 +63,22 @@ resource "tembo_instance" "test_db" {
}
]
}]
connection_pooler = {
enabled = true,
pooler = {
pool_mode = "transaction",
parameters = [
{
name = "max_client_conn"
value = "50"
},
{
name = "default_pool_size"
value = "5000"
}
]
}
}
}

data "tembo_instance_secrets" "test" {
Expand Down
126 changes: 106 additions & 20 deletions internal/provider/instance_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,25 +43,35 @@ const (

// temboInstanceResourceModel maps the resource schema data.
type temboInstanceResourceModel struct {
InstanceID types.String `tfsdk:"instance_id"`
InstanceName types.String `tfsdk:"instance_name"`
OrgId types.String `tfsdk:"org_id"`
CPU types.String `tfsdk:"cpu"`
StackType types.String `tfsdk:"stack_type"`
Environment types.String `tfsdk:"environment"`
Replicas types.Int64 `tfsdk:"replicas"`
Memory types.String `tfsdk:"memory"`
Storage types.String `tfsdk:"storage"`
LastUpdated types.String `tfsdk:"last_updated"`
State types.String `tfsdk:"state"`
ExtraDomainsRw []types.String `tfsdk:"extra_domains_rw"`
PostgresConfigs []PostGresConfig `tfsdk:"postgres_configs"`
TrunkInstalls []TrunkInstall `tfsdk:"trunk_installs"`
Extensions []Extension `tfsdk:"extensions"`
IpAllowList []types.String `tfsdk:"ip_allow_list"`
InstanceID types.String `tfsdk:"instance_id"`
InstanceName types.String `tfsdk:"instance_name"`
OrgId types.String `tfsdk:"org_id"`
CPU types.String `tfsdk:"cpu"`
StackType types.String `tfsdk:"stack_type"`
Environment types.String `tfsdk:"environment"`
Replicas types.Int64 `tfsdk:"replicas"`
Memory types.String `tfsdk:"memory"`
Storage types.String `tfsdk:"storage"`
LastUpdated types.String `tfsdk:"last_updated"`
State types.String `tfsdk:"state"`
ExtraDomainsRw []types.String `tfsdk:"extra_domains_rw"`
PostgresConfigs []KeyValue `tfsdk:"postgres_configs"`
TrunkInstalls []TrunkInstall `tfsdk:"trunk_installs"`
Extensions []Extension `tfsdk:"extensions"`
IpAllowList []types.String `tfsdk:"ip_allow_list"`
ConnectionPooler ConnectionPooler `tfsdk:"connection_pooler"`
}

type PostGresConfig struct {
type ConnectionPooler struct {
Enabled types.Bool `tfsdk:"enabled"`
Pooler PgBouncer `tfsdk:"pooler"`
}
type PgBouncer struct {
Parameters []KeyValue `tfsdk:"parameters"`
PoolMode types.String `tfsdk:"pool_mode"`
}

type KeyValue struct {
Name types.String `tfsdk:"name"`
Value types.String `tfsdk:"value"`
}
Expand Down Expand Up @@ -269,6 +279,38 @@ func (r *temboInstanceResource) Schema(_ context.Context, _ resource.SchemaReque
Optional: true,
ElementType: types.StringType,
},
"connection_pooler": schema.SingleNestedAttribute{
Attributes: map[string]schema.Attribute{
"enabled": schema.BoolAttribute{
Required: true,
},
"pooler": schema.SingleNestedAttribute{
Optional: true,
Attributes: map[string]schema.Attribute{
"pool_mode": schema.StringAttribute{
Required: true,
},
"parameters": schema.ListNestedAttribute{
MarkdownDescription: "Parameters",
Required: true,
NestedObject: schema.NestedAttributeObject{
Attributes: map[string]schema.Attribute{
"name": schema.StringAttribute{
MarkdownDescription: "Parameter name",
Required: true,
},
"value": schema.StringAttribute{
MarkdownDescription: "Parameter value",
Required: true,
},
},
},
},
},
},
},
Optional: true,
},
},
}
}
Expand Down Expand Up @@ -306,6 +348,8 @@ func (r *temboInstanceResource) Create(ctx context.Context, req resource.CreateR

createInstance.SetIpAllowList(getStringArray(plan.IpAllowList))

createInstance.SetConnectionPooler(getConnectionPooler(plan.ConnectionPooler))

// TODO: Figure out a better way to set this so it doesn't have to be be called in each method.
ctx = context.WithValue(ctx, temboclient.ContextAccessToken, r.temboInstanceConfig.accessToken)

Expand Down Expand Up @@ -404,6 +448,7 @@ func (r *temboInstanceResource) Update(ctx context.Context, req resource.UpdateR
updateInstance.SetTrunkInstalls(getTemboTrunkInstalls(plan.TrunkInstalls))
updateInstance.SetExtensions(getTemboExtensions(plan.Extensions))
updateInstance.SetIpAllowList(getStringArray(plan.IpAllowList))
updateInstance.SetConnectionPooler(getConnectionPooler(plan.ConnectionPooler))

ctx = context.WithValue(ctx, temboclient.ContextAccessToken, r.temboInstanceConfig.accessToken)

Expand Down Expand Up @@ -521,9 +566,9 @@ func setTemboInstanceResourceModel(instanceResourceModel *temboInstanceResourceM
}

if len(instance.PostgresConfigs) > 0 {
var localPGConfigs []PostGresConfig
var localPGConfigs []KeyValue
for _, pgConfig := range instance.PostgresConfigs {
localPGConfigs = append(localPGConfigs, PostGresConfig{Name: types.StringValue(pgConfig.Name), Value: types.StringValue(pgConfig.Value)})
localPGConfigs = append(localPGConfigs, KeyValue{Name: types.StringValue(pgConfig.Name), Value: types.StringValue(pgConfig.Value)})
}
instanceResourceModel.PostgresConfigs = localPGConfigs
}
Expand Down Expand Up @@ -562,6 +607,15 @@ func setTemboInstanceResourceModel(instanceResourceModel *temboInstanceResourceM
instanceResourceModel.IpAllowList = localIpAllowList
}

//if instance.HasConnectionPooler() {
var localConnectionPooler ConnectionPooler
cp := instance.ConnectionPooler.Get()
localConnectionPooler.Enabled = types.BoolValue(*cp.Enabled)
localConnectionPooler.Pooler.PoolMode = types.StringValue(string(*cp.Pooler.PoolMode.Ptr()))
localConnectionPooler.Pooler.Parameters = getParameterKV(cp.Pooler.Parameters)
instanceResourceModel.ConnectionPooler = localConnectionPooler
//}

}

func getStringArray(inputArray []basetypes.StringValue) []string {
Expand All @@ -574,7 +628,7 @@ func getStringArray(inputArray []basetypes.StringValue) []string {
return localStringArray
}

func getPgConfig(postgresConfigs []PostGresConfig) []temboclient.PgConfig {
func getPgConfig(postgresConfigs []KeyValue) []temboclient.PgConfig {
var localPGConfigs []temboclient.PgConfig
if len(postgresConfigs) > 0 {
for _, pgConfig := range postgresConfigs {
Expand Down Expand Up @@ -603,6 +657,38 @@ func getTemboTrunkInstall(trunkInstall TrunkInstall) temboclient.TrunkInstall {
return localTrunkInstall
}

func getConnectionPooler(connectionPooler ConnectionPooler) temboclient.ConnectionPooler {
localConnectionPooler := temboclient.ConnectionPooler{
Enabled: connectionPooler.Enabled.ValueBoolPointer(),
}

localConnectionPooler.Pooler = &temboclient.PgBouncer{
Parameters: getParameterMap(connectionPooler.Pooler.Parameters),
PoolMode: (*temboclient.PoolerPgbouncerPoolMode)(connectionPooler.Pooler.PoolMode.ValueStringPointer()),
}

return localConnectionPooler
}

func getParameterMap(parameters []KeyValue) map[string]string {
localParameters := make(map[string]string)

for _, kv := range parameters {
localParameters[kv.Name.ValueString()] = kv.Value.ValueString()
}
return localParameters
}

func getParameterKV(parameters map[string]string) []KeyValue {
var localParams []KeyValue

for name, value := range parameters {
localParams = append(localParams, KeyValue{Name: types.StringValue(name), Value: types.StringValue(value)})
}

return localParams
}

func getTemboExtensions(extensions []Extension) []temboclient.Extension {
var tcExtensions []temboclient.Extension

Expand Down

0 comments on commit e2677d9

Please sign in to comment.