Skip to content

Commit

Permalink
Merge pull request #33 from gelleson/feat/add-alias-to-link
Browse files Browse the repository at this point in the history
Add alias option into various services and update helper
  • Loading branch information
aliksend authored Jan 22, 2024
2 parents 91b8fee + 25aebec commit 915a882
Show file tree
Hide file tree
Showing 37 changed files with 256 additions and 36 deletions.
2 changes: 0 additions & 2 deletions docs/resources/chickhouse.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,3 @@ description: |-
### Required

- `service_name` (String) Service name to create


2 changes: 2 additions & 0 deletions docs/resources/clickhouse_link.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,6 @@ description: |-
- `app_name` (String) App name to apply link service to
- `service_name` (String) Service name to link

### Optional

- `alias` (String) Alias is dokku's resource alias to provide as env XXXX_URL
2 changes: 0 additions & 2 deletions docs/resources/couchdb.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,3 @@ description: |-
### Required

- `service_name` (String) Service name to create


2 changes: 2 additions & 0 deletions docs/resources/couchdb_link.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,6 @@ description: |-
- `app_name` (String) App name to apply link service to
- `service_name` (String) Service name to link

### Optional

- `alias` (String) Alias is dokku's resource alias to provide as env XXXX_URL
2 changes: 0 additions & 2 deletions docs/resources/elasticsearch.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,3 @@ description: |-
### Required

- `service_name` (String) Service name to create


2 changes: 2 additions & 0 deletions docs/resources/elasticsearch_link.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,6 @@ description: |-
- `app_name` (String) App name to apply link service to
- `service_name` (String) Service name to link

### Optional

- `alias` (String) Alias is dokku's resource alias to provide as env XXXX_URL
2 changes: 0 additions & 2 deletions docs/resources/mariadb.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,3 @@ description: |-
### Required

- `service_name` (String) Service name to create


2 changes: 2 additions & 0 deletions docs/resources/mariadb_link.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,6 @@ description: |-
- `app_name` (String) App name to apply link service to
- `service_name` (String) Service name to link

### Optional

- `alias` (String) Alias is dokku's resource alias to provide as env XXXX_URL
2 changes: 0 additions & 2 deletions docs/resources/mongo.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,3 @@ description: |-
### Required

- `service_name` (String) Service name to create


2 changes: 2 additions & 0 deletions docs/resources/mongo_link.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,6 @@ description: |-
- `app_name` (String) App name to apply link service to
- `service_name` (String) Service name to link

### Optional

- `alias` (String) Alias is dokku's resource alias to provide as env XXXX_URL
2 changes: 0 additions & 2 deletions docs/resources/mysql.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,3 @@ description: |-
### Required

- `service_name` (String) Service name to create


2 changes: 2 additions & 0 deletions docs/resources/mysql_link.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,6 @@ description: |-
- `app_name` (String) App name to apply link service to
- `service_name` (String) Service name to link

### Optional

- `alias` (String) Alias is dokku's resource alias to provide as env XXXX_URL
2 changes: 0 additions & 2 deletions docs/resources/nats.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,3 @@ description: |-
### Optional

- `config_options` (String) Config options to create service with


2 changes: 2 additions & 0 deletions docs/resources/nats_link.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,6 @@ description: |-
- `app_name` (String) App name to apply link service to
- `service_name` (String) Service name to link

### Optional

- `alias` (String) Alias is dokku's resource alias to provide as env XXXX_URL
2 changes: 0 additions & 2 deletions docs/resources/plugin.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,3 @@ description: |-

- `name` (String) Name of plugin to install
- `url` (String) URL of plugin to install


5 changes: 5 additions & 0 deletions docs/resources/postgres_link.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ resource "dokku_app" "demo" {
resource "dokku_postgres_link" "demo" {
app_name = "demo-app"
service_name = "demo-service"
alias = "DATABASE"
}
```

Expand All @@ -35,6 +36,10 @@ resource "dokku_postgres_link" "demo" {
- `app_name` (String) App name to apply link service to
- `service_name` (String) Service name to link

### Optional

- `alias` (String) Alias is dokku's resource alias to provide as env XXXX_URL

## Import

Import is supported using the following syntax:
Expand Down
2 changes: 0 additions & 2 deletions docs/resources/rabbitmq.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,3 @@ description: |-
### Required

- `service_name` (String) Service name to create


2 changes: 2 additions & 0 deletions docs/resources/rabbitmq_link.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,6 @@ description: |-
- `app_name` (String) App name to apply link service to
- `service_name` (String) Service name to link

### Optional

- `alias` (String) Alias is dokku's resource alias to provide as env XXXX_URL
2 changes: 0 additions & 2 deletions docs/resources/redis.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,3 @@ description: |-
### Required

- `service_name` (String) Service name to create


2 changes: 2 additions & 0 deletions docs/resources/redis_link.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,6 @@ description: |-
- `app_name` (String) App name to apply link service to
- `service_name` (String) Service name to link

### Optional

- `alias` (String) Alias is dokku's resource alias to provide as env XXXX_URL
2 changes: 0 additions & 2 deletions docs/resources/rethinkdb.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,3 @@ description: |-
### Required

- `service_name` (String) Service name to create


2 changes: 2 additions & 0 deletions docs/resources/rethinkdb_link.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,6 @@ description: |-
- `app_name` (String) App name to apply link service to
- `service_name` (String) Service name to link

### Optional

- `alias` (String) Alias is dokku's resource alias to provide as env XXXX_URL
1 change: 1 addition & 0 deletions examples/resources/dokku_postgres_link/resource.tf
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ resource "dokku_app" "demo" {
resource "dokku_postgres_link" "demo" {
app_name = "demo-app"
service_name = "demo-service"
alias = "DATABASE"
}
7 changes: 7 additions & 0 deletions internal/provider/dokku_client/helper.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package dokkuclient

import "fmt"

func DoubleDashArg[T any](key string, value T) string {
return fmt.Sprintf("--%s %s", key, value)
}
4 changes: 2 additions & 2 deletions internal/provider/dokku_client/simple_service_link.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ func (c *Client) SimpleServiceLinkExists(ctx context.Context, servicePluginName
return true, nil
}

func (c *Client) SimpleServiceLinkCreate(ctx context.Context, servicePluginName string, serviceName string, appName string) error {
_, _, err := c.RunQuiet(ctx, fmt.Sprintf("%s:link %s %s", servicePluginName, serviceName, appName))
func (c *Client) SimpleServiceLinkCreate(ctx context.Context, servicePluginName string, serviceName string, appName string, args ...string) error {
_, _, err := c.RunQuiet(ctx, fmt.Sprintf("%s:link %s %s %s", servicePluginName, serviceName, appName, strings.Join(args, " ")))
return err
}

Expand Down
2 changes: 1 addition & 1 deletion internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ func (p *dokkuProvider) Configure(ctx context.Context, req provider.ConfigureReq
return
}

testedVersions := ">=0.24.0 <=0.32.0"
testedVersions := ">=0.24.0 < 0.33.0"
testedErrMsg := fmt.Sprintf("This provider has not been tested against Dokku version %s. Tested version range: %s", rawVersion, testedVersions)

if err == nil {
Expand Down
21 changes: 20 additions & 1 deletion internal/provider/services/clickhouse_link_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ type clickhouseLinkResource struct {
type clickhouseLinkResourceModel struct {
AppName types.String `tfsdk:"app_name"`
ServiceName types.String `tfsdk:"service_name"`
Alias types.String `tfsdk:"alias"`
}

// Metadata returns the resource type name.
Expand Down Expand Up @@ -75,6 +76,16 @@ func (r *clickhouseLinkResource) Schema(_ context.Context, _ resource.SchemaRequ
stringvalidator.RegexMatches(regexp.MustCompile(`^[a-z][a-z0-9-]*$`), "invalid service_name"),
},
},
"alias": schema.StringAttribute{
Optional: true,
Description: "Alias is dokku's resource alias to provide as env XXXX_URL",
Validators: []validator.String{
stringvalidator.RegexMatches(regexp.MustCompile(`^[A-Z_]+$`), "invalid alias"),
},
PlanModifiers: []planmodifier.String{
stringplanmodifier.RequiresReplace(),
},
},
},
}
}
Expand Down Expand Up @@ -151,8 +162,13 @@ func (r *clickhouseLinkResource) Create(ctx context.Context, req resource.Create
return
}

args := make([]string, 0)
if !plan.Alias.IsNull() {
args = append(args, dokkuclient.DoubleDashArg("alias", plan.Alias.ValueString()))
}

// Create link
err = r.client.SimpleServiceLinkCreate(ctx, "clickhouse", plan.ServiceName.ValueString(), plan.AppName.ValueString())
err = r.client.SimpleServiceLinkCreate(ctx, "clickhouse", plan.ServiceName.ValueString(), plan.AppName.ValueString(), args...)
if err != nil {
resp.Diagnostics.AddError("Unable to create clickhouse link", "Unable to create clickhouse link. "+err.Error())
return
Expand Down Expand Up @@ -213,4 +229,7 @@ func (r *clickhouseLinkResource) ImportState(ctx context.Context, req resource.I
parts := strings.Split(req.ID, " ")
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("app_name"), parts[0])...)
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("service_name"), parts[1])...)
if len(parts) == 3 {
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("alias"), parts[2])...)
}
}
21 changes: 20 additions & 1 deletion internal/provider/services/couchdb_link_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ type couchDBLinkResource struct {
type couchDBLinkResourceModel struct {
AppName types.String `tfsdk:"app_name"`
ServiceName types.String `tfsdk:"service_name"`
Alias types.String `tfsdk:"alias"`
}

// Metadata returns the resource type name.
Expand Down Expand Up @@ -75,6 +76,16 @@ func (r *couchDBLinkResource) Schema(_ context.Context, _ resource.SchemaRequest
stringvalidator.RegexMatches(regexp.MustCompile(`^[a-z][a-z0-9-]*$`), "invalid service_name"),
},
},
"alias": schema.StringAttribute{
Optional: true,
Description: "Alias is dokku's resource alias to provide as env XXXX_URL",
Validators: []validator.String{
stringvalidator.RegexMatches(regexp.MustCompile(`^[A-Z_]+$`), "invalid alias"),
},
PlanModifiers: []planmodifier.String{
stringplanmodifier.RequiresReplace(),
},
},
},
}
}
Expand Down Expand Up @@ -151,8 +162,13 @@ func (r *couchDBLinkResource) Create(ctx context.Context, req resource.CreateReq
return
}

args := make([]string, 0)
if !plan.Alias.IsNull() {
args = append(args, dokkuclient.DoubleDashArg("alias", plan.Alias.ValueString()))
}

// Create link
err = r.client.SimpleServiceLinkCreate(ctx, "couchdb", plan.ServiceName.ValueString(), plan.AppName.ValueString())
err = r.client.SimpleServiceLinkCreate(ctx, "couchdb", plan.ServiceName.ValueString(), plan.AppName.ValueString(), args...)
if err != nil {
resp.Diagnostics.AddError("Unable to create couchDB link", "Unable to create couchDB link. "+err.Error())
return
Expand Down Expand Up @@ -213,4 +229,7 @@ func (r *couchDBLinkResource) ImportState(ctx context.Context, req resource.Impo
parts := strings.Split(req.ID, " ")
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("app_name"), parts[0])...)
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("service_name"), parts[1])...)
if len(parts) == 3 {
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("alias"), parts[2])...)
}
}
21 changes: 20 additions & 1 deletion internal/provider/services/elasticsearch_link_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ type elasticsearchLinkResource struct {
type elasticsearchLinkResourceModel struct {
AppName types.String `tfsdk:"app_name"`
ServiceName types.String `tfsdk:"service_name"`
Alias types.String `tfsdk:"alias"`
}

// Metadata returns the resource type name.
Expand Down Expand Up @@ -75,6 +76,16 @@ func (r *elasticsearchLinkResource) Schema(_ context.Context, _ resource.SchemaR
stringvalidator.RegexMatches(regexp.MustCompile(`^[a-z][a-z0-9-]*$`), "invalid service_name"),
},
},
"alias": schema.StringAttribute{
Optional: true,
Description: "Alias is dokku's resource alias to provide as env XXXX_URL",
Validators: []validator.String{
stringvalidator.RegexMatches(regexp.MustCompile(`^[A-Z_]+$`), "invalid alias"),
},
PlanModifiers: []planmodifier.String{
stringplanmodifier.RequiresReplace(),
},
},
},
}
}
Expand Down Expand Up @@ -151,8 +162,13 @@ func (r *elasticsearchLinkResource) Create(ctx context.Context, req resource.Cre
return
}

args := make([]string, 0)
if !plan.Alias.IsNull() {
args = append(args, dokkuclient.DoubleDashArg("alias", plan.Alias.ValueString()))
}

// Create link
err = r.client.SimpleServiceLinkCreate(ctx, "elasticsearch", plan.ServiceName.ValueString(), plan.AppName.ValueString())
err = r.client.SimpleServiceLinkCreate(ctx, "elasticsearch", plan.ServiceName.ValueString(), plan.AppName.ValueString(), args...)
if err != nil {
resp.Diagnostics.AddError("Unable to create elasticsearch link", "Unable to create elasticsearch link. "+err.Error())
return
Expand Down Expand Up @@ -213,4 +229,7 @@ func (r *elasticsearchLinkResource) ImportState(ctx context.Context, req resourc
parts := strings.Split(req.ID, " ")
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("app_name"), parts[0])...)
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("service_name"), parts[1])...)
if len(parts) == 3 {
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("alias"), parts[2])...)
}
}
21 changes: 20 additions & 1 deletion internal/provider/services/mariadb_link_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ type mariaDBLinkResource struct {
type mariaDBLinkResourceModel struct {
AppName types.String `tfsdk:"app_name"`
ServiceName types.String `tfsdk:"service_name"`
Alias types.String `tfsdk:"alias"`
}

// Metadata returns the resource type name.
Expand Down Expand Up @@ -75,6 +76,16 @@ func (r *mariaDBLinkResource) Schema(_ context.Context, _ resource.SchemaRequest
stringvalidator.RegexMatches(regexp.MustCompile(`^[a-z][a-z0-9-]*$`), "invalid service_name"),
},
},
"alias": schema.StringAttribute{
Optional: true,
Description: "Alias is dokku's resource alias to provide as env XXXX_URL",
Validators: []validator.String{
stringvalidator.RegexMatches(regexp.MustCompile(`^[A-Z_]+$`), "invalid alias"),
},
PlanModifiers: []planmodifier.String{
stringplanmodifier.RequiresReplace(),
},
},
},
}
}
Expand Down Expand Up @@ -151,8 +162,13 @@ func (r *mariaDBLinkResource) Create(ctx context.Context, req resource.CreateReq
return
}

args := make([]string, 0)
if !plan.Alias.IsNull() {
args = append(args, dokkuclient.DoubleDashArg("alias", plan.Alias.ValueString()))
}

// Create link
err = r.client.SimpleServiceLinkCreate(ctx, "mariadb", plan.ServiceName.ValueString(), plan.AppName.ValueString())
err = r.client.SimpleServiceLinkCreate(ctx, "mariadb", plan.ServiceName.ValueString(), plan.AppName.ValueString(), args...)
if err != nil {
resp.Diagnostics.AddError("Unable to create mariaDB link", "Unable to create mariaDB link. "+err.Error())
return
Expand Down Expand Up @@ -213,4 +229,7 @@ func (r *mariaDBLinkResource) ImportState(ctx context.Context, req resource.Impo
parts := strings.Split(req.ID, " ")
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("app_name"), parts[0])...)
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("service_name"), parts[1])...)
if len(parts) == 3 {
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("alias"), parts[2])...)
}
}
Loading

0 comments on commit 915a882

Please sign in to comment.