Skip to content

Commit 58c3c1a

Browse files
committed
wip
1 parent 2882a27 commit 58c3c1a

File tree

13 files changed

+73
-19
lines changed

13 files changed

+73
-19
lines changed

go.mod

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module go.clever-cloud.com/terraform-provider
22

3-
go 1.23.7
3+
go 1.24.0
44

55
toolchain go1.24.5
66

@@ -93,6 +93,7 @@ require (
9393
github.com/mitchellh/go-wordwrap v1.0.0 // indirect
9494
github.com/mitchellh/mapstructure v1.5.0 // indirect
9595
github.com/mitchellh/reflectwalk v1.0.2 // indirect
96+
github.com/miton18/helper v0.0.2-0.20251001163320-ee4c86a86792
9697
github.com/mtibben/percent v0.2.1 // indirect
9798
github.com/oklog/run v1.0.0 // indirect
9899
github.com/philhofer/fwd v1.2.0 // indirect

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -425,6 +425,8 @@ github.com/mitchellh/reflectwalk v1.0.0/go.mod h1:mSTlrgnPZtwu0c4WaC2kGObEpuNDbx
425425
github.com/mitchellh/reflectwalk v1.0.1/go.mod h1:mSTlrgnPZtwu0c4WaC2kGObEpuNDbx0jmZXqmk4esnw=
426426
github.com/mitchellh/reflectwalk v1.0.2 h1:G2LzWKi524PWgd3mLHV8Y5k7s6XUvT0Gef6zxSIeXaQ=
427427
github.com/mitchellh/reflectwalk v1.0.2/go.mod h1:mSTlrgnPZtwu0c4WaC2kGObEpuNDbx0jmZXqmk4esnw=
428+
github.com/miton18/helper v0.0.2-0.20251001163320-ee4c86a86792 h1:XeA098d0RWNJacxHe2zs7nQxVb8Ls67bv161EMsUu/g=
429+
github.com/miton18/helper v0.0.2-0.20251001163320-ee4c86a86792/go.mod h1:BpVJIypihdfdxycOj7PqtgCvyhNppV97AlmDrGgOjec=
428430
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg=
429431
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
430432
github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M=

pkg/attributes/addon.go

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
package attributes
22

33
import (
4+
"context"
5+
"regexp"
6+
47
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
58
"github.com/hashicorp/terraform-plugin-framework/resource/schema/int64planmodifier"
69
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
710
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringdefault"
811
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
12+
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
913
"github.com/hashicorp/terraform-plugin-framework/types"
1014
"go.clever-cloud.com/terraform-provider/pkg"
1115
)
@@ -21,7 +25,11 @@ type Addon struct {
2125
var addonCommon = map[string]schema.Attribute{
2226
"id": schema.StringAttribute{Computed: true, MarkdownDescription: "Generated unique identifier", PlanModifiers: []planmodifier.String{stringplanmodifier.UseStateForUnknown()}},
2327
"name": schema.StringAttribute{Required: true, MarkdownDescription: "Name of the service"},
24-
"plan": schema.StringAttribute{Required: true, MarkdownDescription: "Database size and spec"},
28+
"plan": schema.StringAttribute{
29+
Required: true,
30+
MarkdownDescription: "Database size and spec",
31+
Validators: []validator.String{slugValidator},
32+
},
2533
"region": schema.StringAttribute{
2634
Optional: true,
2735
Computed: true,
@@ -34,3 +42,17 @@ var addonCommon = map[string]schema.Attribute{
3442
func WithAddonCommons(runtimeSpecifics map[string]schema.Attribute) map[string]schema.Attribute {
3543
return pkg.Merge(addonCommon, runtimeSpecifics)
3644
}
45+
46+
// https://regex101.com/r/bMOotf/1
47+
var slugRegex = regexp.MustCompile(`^[a-z1-9_]*$`)
48+
var slugValidator = pkg.NewStringValidator(
49+
"expect slug value",
50+
func(ctx context.Context, req validator.StringRequest, res *validator.StringResponse) {
51+
if req.ConfigValue.IsNull() || req.ConfigValue.IsUnknown() {
52+
return
53+
}
54+
55+
if !slugRegex.MatchString(req.ConfigValue.ValueString()) {
56+
res.Diagnostics.AddAttributeError(req.Path, "expect lowercase and underscore characters only", "Invalid slug, expect something like `xs_tny`")
57+
}
58+
})

pkg/attributes/runtime.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,12 @@ type Runtime struct {
6161
Environment types.Map `tfsdk:"environment"`
6262
}
6363

64+
/*func (r *Runtime) SetEnvironment(env map[string]string) {
65+
r.Environment = types.MapValueMust(
66+
types.StringType,
67+
maps.MapValue())
68+
}*/
69+
6470
type RuntimeV0 struct {
6571
ID types.String `tfsdk:"id"`
6672
Name types.String `tfsdk:"name"`

pkg/resources/addon/crud.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ func (r *ResourceAddon) Read(ctx context.Context, req resource.ReadRequest, resp
105105

106106
a := addonRes.Payload()
107107
ad.Name = pkg.FromStr(a.Name)
108-
ad.Plan = pkg.FromStr(a.Plan.Slug)
108+
ad.Plan = pkg.FromStr(strings.ToLower(a.Plan.Slug))
109109
ad.Region = pkg.FromStr(a.Region)
110110
ad.ThirdPartyProvider = pkg.FromStr(a.Provider.ID)
111111
ad.CreationDate = pkg.FromI(a.CreationDate)

pkg/resources/frankenphp/crud.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ func (r *ResourceFrankenPHP) Read(ctx context.Context, req resource.ReadRequest,
106106
state.BiggestFlavor = pkg.FromStr(appFrankenPHP.App.Instance.MaxFlavor.Name)
107107
state.Region = pkg.FromStr(appFrankenPHP.App.Zone)
108108
state.DeployURL = pkg.FromStr(appFrankenPHP.App.DeployURL)
109-
109+
state.fromEnv(ctx, appFrankenPHP.EnvAsMap())
110110
state.VHosts = helper.VHostsFromAPIHosts(ctx, appFrankenPHP.App.Vhosts.AsString(), state.VHosts, &resp.Diagnostics)
111111

112112
resp.Diagnostics.Append(resp.State.Set(ctx, state)...)

pkg/resources/frankenphp/frankenphp.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ type ResourceFrankenPHP struct {
1111
helper.Configurer
1212
}
1313

14+
const CC_PHP_DEV_DEPENDENCIES = "CC_PHP_DEV_DEPENDENCIES"
15+
1416
func NewResourceFrankenPHP() resource.Resource {
1517
return &ResourceFrankenPHP{}
1618
}

pkg/resources/frankenphp/schema.go

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import (
44
"context"
55
_ "embed"
66

7+
"github.com/miton18/helper/maps"
8+
79
"github.com/go-git/go-git/v5/plumbing/transport/http"
810
"github.com/hashicorp/terraform-plugin-framework/diag"
911
"github.com/hashicorp/terraform-plugin-framework/resource"
@@ -53,14 +55,30 @@ func (fp *FrankenPHP) toEnv(ctx context.Context, diags *diag.Diagnostics) map[st
5355

5456
pkg.IfIsSetB(fp.DevDependencies, func(devDeps bool) {
5557
if devDeps {
56-
env["CC_PHP_DEV_DEPENDENCIES"] = "install"
58+
env[CC_PHP_DEV_DEPENDENCIES] = "install"
5759
}
5860
})
5961
env = pkg.Merge(env, fp.Hooks.ToEnv())
6062

6163
return env
6264
}
6365

66+
// fromEnv iter on environment set on the clever application and
67+
// handle language specific env vars
68+
// put the others on Environment field
69+
func (fp *FrankenPHP) fromEnv(ctx context.Context, env map[string]string) diag.Diagnostics {
70+
diags := diag.Diagnostics{}
71+
m := maps.NewMap(env)
72+
73+
fp.DevDependencies = pkg.FromBool(m.Pop(CC_PHP_DEV_DEPENDENCIES) == "install")
74+
75+
x, d := types.MapValueFrom(ctx, types.StringType, m)
76+
diags.Append(d...)
77+
fp.Environment = x
78+
79+
return diags
80+
}
81+
6482
func (fp *FrankenPHP) toDeployment(gitAuth *http.BasicAuth) *application.Deployment {
6583
if fp.Deployment == nil || fp.Deployment.Repository.IsNull() {
6684
return nil

pkg/resources/mongodb/crud.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,15 @@ func (r *ResourceMongoDB) Create(ctx context.Context, req resource.CreateRequest
4242
resp.Diagnostics.AddError("failed to create addon", res.Error().Error())
4343
return
4444
}
45+
addon := res.Payload()
4546

46-
mg.ID = pkg.FromStr(res.Payload().RealID)
47-
mg.CreationDate = pkg.FromI(res.Payload().CreationDate)
48-
mg.Plan = pkg.FromStr(res.Payload().Plan.Slug)
47+
mg.ID = pkg.FromStr(addon.RealID)
48+
mg.CreationDate = pkg.FromI(addon.CreationDate)
49+
mg.Plan = pkg.FromStr(strings.ToLower(addon.Plan.Slug))
4950

5051
resp.Diagnostics.Append(resp.State.Set(ctx, mg)...)
5152

52-
mgInfoRes := tmp.GetMongoDB(ctx, r.Client(), res.Payload().ID)
53+
mgInfoRes := tmp.GetMongoDB(ctx, r.Client(), addon.ID)
5354
if mgInfoRes.HasError() {
5455
resp.Diagnostics.AddError("failed to get MongoDB connection infos", mgInfoRes.Error().Error())
5556
return

pkg/resources/mysql/crud.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ func (r *ResourceMySQL) Create(ctx context.Context, req resource.CreateRequest,
8989

9090
my.ID = pkg.FromStr(createdMy.RealID)
9191
my.CreationDate = pkg.FromI(createdMy.CreationDate)
92-
my.Plan = pkg.FromStr(createdMy.Plan.Slug)
92+
my.Plan = pkg.FromStr(strings.ToLower(createdMy.Plan.Slug))
9393

9494
resp.Diagnostics.Append(resp.State.Set(ctx, my)...)
9595

@@ -164,7 +164,7 @@ func (r *ResourceMySQL) Read(ctx context.Context, req resource.ReadRequest, resp
164164
tflog.Debug(ctx, "API", map[string]any{"my": addonMy})
165165
my.ID = pkg.FromStr(addon.RealID)
166166
my.Name = pkg.FromStr(addon.Name)
167-
my.Plan = pkg.FromStr(addonMy.Plan)
167+
my.Plan = pkg.FromStr(strings.ToLower(addonMy.Plan))
168168
my.Region = pkg.FromStr(addonMy.Zone)
169169
my.CreationDate = pkg.FromI(addon.CreationDate)
170170
my.Host = pkg.FromStr(addonMy.Host)

0 commit comments

Comments
 (0)