Skip to content

Commit

Permalink
Allow to specify image with version for all services
Browse files Browse the repository at this point in the history
  • Loading branch information
aliksend committed Nov 17, 2024
1 parent e4c1ea5 commit 952cc47
Show file tree
Hide file tree
Showing 22 changed files with 298 additions and 2 deletions.
1 change: 1 addition & 0 deletions docs/resources/clickhouse.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,4 @@ description: |-
### Optional

- `expose` (String) Port or IP:Port to expose service on
- `image` (String) Image to use in `image:version` format
1 change: 1 addition & 0 deletions docs/resources/couchdb.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,4 @@ description: |-
### Optional

- `expose` (String) Port or IP:Port to expose service on
- `image` (String) Image to use in `image:version` format
1 change: 1 addition & 0 deletions docs/resources/elasticsearch.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,4 @@ description: |-
### Optional

- `expose` (String) Port or IP:Port to expose service on
- `image` (String) Image to use in `image:version` format
1 change: 1 addition & 0 deletions docs/resources/mariadb.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,4 @@ description: |-
### Optional

- `expose` (String) Port or IP:Port to expose service on
- `image` (String) Image to use in `image:version` format
1 change: 1 addition & 0 deletions docs/resources/mongo.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,4 @@ description: |-
### Optional

- `expose` (String) Port or IP:Port to expose service on
- `image` (String) Image to use in `image:version` format
1 change: 1 addition & 0 deletions docs/resources/mysql.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,4 @@ description: |-
### Optional

- `expose` (String) Port or IP:Port to expose service on
- `image` (String) Image to use in `image:version` format
1 change: 1 addition & 0 deletions docs/resources/nats.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ resource "dokku_nats" "demo" {

- `config_options` (String) Config options to create service with
- `expose` (String) Port or IP:Port to expose service on
- `image` (String) Image to use in `image:version` format

## Import

Expand Down
1 change: 1 addition & 0 deletions docs/resources/postgres.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ resource "dokku_postgres" "demo" {
### Optional

- `expose` (String) Port or IP:Port to expose service on
- `image` (String) Image to use in `image:version` format

## Import

Expand Down
1 change: 1 addition & 0 deletions docs/resources/rabbitmq.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,4 @@ description: |-
### Optional

- `expose` (String) Port or IP:Port to expose service on
- `image` (String) Image to use in `image:version` format
1 change: 1 addition & 0 deletions docs/resources/redis.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,4 @@ description: |-
### Optional

- `expose` (String) Port or IP:Port to expose service on
- `image` (String) Image to use in `image:version` format
1 change: 1 addition & 0 deletions docs/resources/rethinkdb.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,4 @@ description: |-
### Optional

- `expose` (String) Port or IP:Port to expose service on
- `image` (String) Image to use in `image:version` format
28 changes: 27 additions & 1 deletion provider/services/clickhouse_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package services

import (
"context"
"fmt"
"regexp"

dokkuclient "github.com/aliksend/terraform-provider-dokku/provider/dokku_client"
Expand Down Expand Up @@ -33,6 +34,7 @@ type clickhouseResource struct {

type clickhouseResourceModel struct {
ServiceName types.String `tfsdk:"service_name"`
Image types.String `tfsdk:"image"`
Expose types.String `tfsdk:"expose"`
}

Expand Down Expand Up @@ -65,6 +67,16 @@ func (r *clickhouseResource) Schema(_ context.Context, _ resource.SchemaRequest,
stringvalidator.RegexMatches(regexp.MustCompile(`^[a-z][a-z0-9-]*$`), "invalid service_name"),
},
},
"image": schema.StringAttribute{
Optional: true,
Description: "Image to use in `image:version` format",
PlanModifiers: []planmodifier.String{
stringplanmodifier.RequiresReplace(),
},
Validators: []validator.String{
stringvalidator.RegexMatches(regexp.MustCompile(`^(.+):(.+)$`), "invalid image"),
},
},
"expose": schema.StringAttribute{
Optional: true,
Description: "Port or IP:Port to expose service on",
Expand Down Expand Up @@ -115,6 +127,8 @@ func (r *clickhouseResource) Read(ctx context.Context, req resource.ReadRequest,
} else {
state.Expose = basetypes.NewStringNull()
}
infoVersion := info["Version"]
state.Image = basetypes.NewStringValue(infoVersion)

// Set refreshed state
diags = resp.State.Set(ctx, &state)
Expand Down Expand Up @@ -145,7 +159,19 @@ func (r *clickhouseResource) Create(ctx context.Context, req resource.CreateRequ
return
}

err = r.client.SimpleServiceCreate(ctx, "clickhouse", plan.ServiceName.ValueString())
var args []string

if !plan.Image.IsNull() {
r := regexp.MustCompile(`^(.+):(.+)$`)
m := r.FindStringSubmatch(plan.Image.ValueString())
if len(m) == 3 {
args = append(args, fmt.Sprintf("--image %s", m[1]), fmt.Sprintf("--image-version %s", m[2]))
} else {
resp.Diagnostics.AddError("Invalid image format", "Invalid image format")
}
}

err = r.client.SimpleServiceCreate(ctx, "clickhouse", plan.ServiceName.ValueString(), args...)
if err != nil {
resp.Diagnostics.AddError("Unable to create clickhouse service", "Unable to create clickhouse service. "+err.Error())
return
Expand Down
26 changes: 26 additions & 0 deletions provider/services/couchdb_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package services

import (
"context"
"fmt"
"regexp"

dokkuclient "github.com/aliksend/terraform-provider-dokku/provider/dokku_client"
Expand Down Expand Up @@ -33,6 +34,7 @@ type couchDBResource struct {

type couchDBResourceModel struct {
ServiceName types.String `tfsdk:"service_name"`
Image types.String `tfsdk:"image"`
Expose types.String `tfsdk:"expose"`
}

Expand Down Expand Up @@ -65,6 +67,16 @@ func (r *couchDBResource) Schema(_ context.Context, _ resource.SchemaRequest, re
stringvalidator.RegexMatches(regexp.MustCompile(`^[a-z][a-z0-9-]*$`), "invalid service_name"),
},
},
"image": schema.StringAttribute{
Optional: true,
Description: "Image to use in `image:version` format",
PlanModifiers: []planmodifier.String{
stringplanmodifier.RequiresReplace(),
},
Validators: []validator.String{
stringvalidator.RegexMatches(regexp.MustCompile(`^(.+):(.+)$`), "invalid image"),
},
},
"expose": schema.StringAttribute{
Optional: true,
Description: "Port or IP:Port to expose service on",
Expand Down Expand Up @@ -115,6 +127,8 @@ func (r *couchDBResource) Read(ctx context.Context, req resource.ReadRequest, re
} else {
state.Expose = basetypes.NewStringNull()
}
infoVersion := info["Version"]
state.Image = basetypes.NewStringValue(infoVersion)

// Set refreshed state
diags = resp.State.Set(ctx, &state)
Expand Down Expand Up @@ -145,6 +159,18 @@ func (r *couchDBResource) Create(ctx context.Context, req resource.CreateRequest
return
}

var args []string

if !plan.Image.IsNull() {
r := regexp.MustCompile(`^(.+):(.+)$`)
m := r.FindStringSubmatch(plan.Image.ValueString())
if len(m) == 3 {
args = append(args, fmt.Sprintf("--image %s", m[1]), fmt.Sprintf("--image-version %s", m[2]))
} else {
resp.Diagnostics.AddError("Invalid image format", "Invalid image format")
}
}

err = r.client.SimpleServiceCreate(ctx, "couchdb", plan.ServiceName.ValueString())
if err != nil {
resp.Diagnostics.AddError("Unable to create couchDB service", "Unable to create couchDB service. "+err.Error())
Expand Down
26 changes: 26 additions & 0 deletions provider/services/elasticsearch_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package services

import (
"context"
"fmt"
"regexp"

dokkuclient "github.com/aliksend/terraform-provider-dokku/provider/dokku_client"
Expand Down Expand Up @@ -33,6 +34,7 @@ type elasticsearchResource struct {

type elasticsearchResourceModel struct {
ServiceName types.String `tfsdk:"service_name"`
Image types.String `tfsdk:"image"`
Expose types.String `tfsdk:"expose"`
}

Expand Down Expand Up @@ -65,6 +67,16 @@ func (r *elasticsearchResource) Schema(_ context.Context, _ resource.SchemaReque
stringvalidator.RegexMatches(regexp.MustCompile(`^[a-z][a-z0-9-]*$`), "invalid service_name"),
},
},
"image": schema.StringAttribute{
Optional: true,
Description: "Image to use in `image:version` format",
PlanModifiers: []planmodifier.String{
stringplanmodifier.RequiresReplace(),
},
Validators: []validator.String{
stringvalidator.RegexMatches(regexp.MustCompile(`^(.+):(.+)$`), "invalid image"),
},
},
"expose": schema.StringAttribute{
Optional: true,
Description: "Port or IP:Port to expose service on",
Expand Down Expand Up @@ -115,6 +127,8 @@ func (r *elasticsearchResource) Read(ctx context.Context, req resource.ReadReque
} else {
state.Expose = basetypes.NewStringNull()
}
infoVersion := info["Version"]
state.Image = basetypes.NewStringValue(infoVersion)

// Set refreshed state
diags = resp.State.Set(ctx, &state)
Expand Down Expand Up @@ -145,6 +159,18 @@ func (r *elasticsearchResource) Create(ctx context.Context, req resource.CreateR
return
}

var args []string

if !plan.Image.IsNull() {
r := regexp.MustCompile(`^(.+):(.+)$`)
m := r.FindStringSubmatch(plan.Image.ValueString())
if len(m) == 3 {
args = append(args, fmt.Sprintf("--image %s", m[1]), fmt.Sprintf("--image-version %s", m[2]))
} else {
resp.Diagnostics.AddError("Invalid image format", "Invalid image format")
}
}

err = r.client.SimpleServiceCreate(ctx, "elasticsearch", plan.ServiceName.ValueString())
if err != nil {
resp.Diagnostics.AddError("Unable to create elasticsearch service", "Unable to create elasticsearch service. "+err.Error())
Expand Down
26 changes: 26 additions & 0 deletions provider/services/mariadb_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package services

import (
"context"
"fmt"
"regexp"

dokkuclient "github.com/aliksend/terraform-provider-dokku/provider/dokku_client"
Expand Down Expand Up @@ -33,6 +34,7 @@ type mariaDBResource struct {

type mariaDBResourceModel struct {
ServiceName types.String `tfsdk:"service_name"`
Image types.String `tfsdk:"image"`
Expose types.String `tfsdk:"expose"`
}

Expand Down Expand Up @@ -65,6 +67,16 @@ func (r *mariaDBResource) Schema(_ context.Context, _ resource.SchemaRequest, re
stringvalidator.RegexMatches(regexp.MustCompile(`^[a-z][a-z0-9-]*$`), "invalid service_name"),
},
},
"image": schema.StringAttribute{
Optional: true,
Description: "Image to use in `image:version` format",
PlanModifiers: []planmodifier.String{
stringplanmodifier.RequiresReplace(),
},
Validators: []validator.String{
stringvalidator.RegexMatches(regexp.MustCompile(`^(.+):(.+)$`), "invalid image"),
},
},
"expose": schema.StringAttribute{
Optional: true,
Description: "Port or IP:Port to expose service on",
Expand Down Expand Up @@ -115,6 +127,8 @@ func (r *mariaDBResource) Read(ctx context.Context, req resource.ReadRequest, re
} else {
state.Expose = basetypes.NewStringNull()
}
infoVersion := info["Version"]
state.Image = basetypes.NewStringValue(infoVersion)

// Set refreshed state
diags = resp.State.Set(ctx, &state)
Expand Down Expand Up @@ -145,6 +159,18 @@ func (r *mariaDBResource) Create(ctx context.Context, req resource.CreateRequest
return
}

var args []string

if !plan.Image.IsNull() {
r := regexp.MustCompile(`^(.+):(.+)$`)
m := r.FindStringSubmatch(plan.Image.ValueString())
if len(m) == 3 {
args = append(args, fmt.Sprintf("--image %s", m[1]), fmt.Sprintf("--image-version %s", m[2]))
} else {
resp.Diagnostics.AddError("Invalid image format", "Invalid image format")
}
}

err = r.client.SimpleServiceCreate(ctx, "mariadb", plan.ServiceName.ValueString())
if err != nil {
resp.Diagnostics.AddError("Unable to create mariaDB service", "Unable to create mariaDB service. "+err.Error())
Expand Down
25 changes: 25 additions & 0 deletions provider/services/mongo_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ type mongoResource struct {

type mongoResourceModel struct {
ServiceName types.String `tfsdk:"service_name"`
Image types.String `tfsdk:"image"`
Expose types.String `tfsdk:"expose"`
}

Expand Down Expand Up @@ -66,6 +67,16 @@ func (r *mongoResource) Schema(_ context.Context, _ resource.SchemaRequest, resp
stringvalidator.RegexMatches(regexp.MustCompile(`^[a-z][a-z0-9-]*$`), "invalid service_name"),
},
},
"image": schema.StringAttribute{
Optional: true,
Description: "Image to use in `image:version` format",
PlanModifiers: []planmodifier.String{
stringplanmodifier.RequiresReplace(),
},
Validators: []validator.String{
stringvalidator.RegexMatches(regexp.MustCompile(`^(.+):(.+)$`), "invalid image"),
},
},
"expose": schema.StringAttribute{
Optional: true,
Description: "Port or IP:Port to expose service on",
Expand Down Expand Up @@ -116,6 +127,8 @@ func (r *mongoResource) Read(ctx context.Context, req resource.ReadRequest, resp
} else {
state.Expose = basetypes.NewStringNull()
}
infoVersion := info["Version"]
state.Image = basetypes.NewStringValue(infoVersion)

// Set refreshed state
diags = resp.State.Set(ctx, &state)
Expand Down Expand Up @@ -146,6 +159,18 @@ func (r *mongoResource) Create(ctx context.Context, req resource.CreateRequest,
return
}

var args []string

if !plan.Image.IsNull() {
r := regexp.MustCompile(`^(.+):(.+)$`)
m := r.FindStringSubmatch(plan.Image.ValueString())
if len(m) == 3 {
args = append(args, fmt.Sprintf("--image %s", m[1]), fmt.Sprintf("--image-version %s", m[2]))
} else {
resp.Diagnostics.AddError("Invalid image format", "Invalid image format")
}
}

err = r.client.SimpleServiceCreate(ctx, "mongo", plan.ServiceName.ValueString())
if err != nil {
resp.Diagnostics.AddError("Unable to create mongo service", "Unable to create mongo service. "+err.Error())
Expand Down
Loading

0 comments on commit 952cc47

Please sign in to comment.