Skip to content

Commit

Permalink
feat: python update
Browse files Browse the repository at this point in the history
  • Loading branch information
LeCrabe committed Feb 21, 2025
1 parent c679c2c commit e592dfe
Show file tree
Hide file tree
Showing 2 changed files with 124 additions and 23 deletions.
94 changes: 93 additions & 1 deletion pkg/resources/python/crud.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ import (

"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-log/tflog"
"go.clever-cloud.com/terraform-provider/pkg"
"go.clever-cloud.com/terraform-provider/pkg/application"
"go.clever-cloud.com/terraform-provider/pkg/helper"
"go.clever-cloud.com/terraform-provider/pkg/provider"
"go.clever-cloud.com/terraform-provider/pkg/tmp"
)
Expand Down Expand Up @@ -135,7 +137,97 @@ func (r *ResourcePython) Read(ctx context.Context, req resource.ReadRequest, res

// Update resource
func (r *ResourcePython) Update(ctx context.Context, req resource.UpdateRequest, res *resource.UpdateResponse) {
// TODO
tflog.Debug(ctx, "ResourcePython.Update()")

// Retrieve values from plan and state
plan := helper.PlanFrom[Python](ctx, req.Plan, res.Diagnostics)
if res.Diagnostics.HasError() {
return
}
state := helper.StateFrom[Python](ctx, req.State, res.Diagnostics)
if res.Diagnostics.HasError() {
return
}

// Retrieve instance of the app from context
instance := application.LookupInstance(ctx, r.cc, "python", "Python", res.Diagnostics)
if res.Diagnostics.HasError() {
return
}

// Retriev all env values by extracting ctx env viriables and merge it with the app env variables
environment := plan.toEnv(ctx, res.Diagnostics)
if res.Diagnostics.HasError() {
return
}

// Same as env but with vhosts
vhosts := []string{}
if res.Diagnostics.Append(plan.AdditionalVHosts.ElementsAs(ctx, &vhosts, false)...); res.Diagnostics.HasError() {
return
}

// Get the updated values from plan and instance
updateAppReq := application.UpdateReq{
ID: state.ID.ValueString(),
Client: r.cc,
Organization: r.org,
Application: tmp.UpdateAppReq{
Name: plan.Name.ValueString(),
Deploy: "git",
Description: plan.Description.ValueString(),
InstanceType: instance.Type,
InstanceVariant: instance.Variant.ID,
InstanceVersion: instance.Version,
BuildFlavor: plan.BuildFlavor.ValueString(),
MinFlavor: plan.SmallestFlavor.ValueString(),
MaxFlavor: plan.BiggestFlavor.ValueString(),
MinInstances: plan.MinInstanceCount.ValueInt64(),
MaxInstances: plan.MaxInstanceCount.ValueInt64(),
StickySessions: plan.StickySessions.ValueBool(),
ForceHttps: application.FromForceHTTPS(plan.RedirectHTTPS.ValueBool()),
Zone: plan.Region.ValueString(),
CancelOnPush: false,
},
Environment: environment,
VHosts: vhosts,
Deployment: plan.toDeployment(),
}

// Correctly named: update the app (via PUT Method)
_, diags := application.UpdateApp(ctx, updateAppReq)
res.Diagnostics.Append(diags...)
if res.Diagnostics.HasError() {
return
}

//
hasDefaultVHost := pkg.HasSome(updateAppReq.VHosts, func(vhost string) bool {
return pkg.VhostCleverAppsRegExp.MatchString(vhost)
})
if hasDefaultVHost {
cleverapps := *pkg.First(vhosts, func(vhost string) bool {
return pkg.VhostCleverAppsRegExp.MatchString(vhost)
})
plan.VHost = pkg.FromStr(cleverapps)
} else {
plan.VHost = types.StringNull()
}

vhostsWithoutDefault := pkg.Filter(updateAppReq.VHosts, func(vhost string) bool {
ok := pkg.VhostCleverAppsRegExp.MatchString(vhost)
return !ok
})
if len(vhostsWithoutDefault) > 0 {
plan.AdditionalVHosts = pkg.FromListString(vhostsWithoutDefault)
} else {
plan.AdditionalVHosts = types.ListNull(types.StringType)
}

res.Diagnostics.Append(res.State.Set(ctx, plan)...)
if res.Diagnostics.HasError() {
return
}
}

// Delete resource
Expand Down
53 changes: 31 additions & 22 deletions pkg/resources/python/python_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,28 @@ func TestAccPython_basic(t *testing.T) {
fullName2 := fmt.Sprintf("clevercloud_python.%s", rName2)
cc := client.New(client.WithAutoOauthConfig())
org := os.Getenv("ORGANISATION")
providerBlock := helper.NewProvider("clevercloud").SetOrganisation(org)
pythonBlock := helper.NewRessource(
"clevercloud_python",
rName,
helper.SetKeyValues(map[string]any{
"name": rName,
"region": "par",
"min_instance_count": 1,
"max_instance_count": 2,
"smallest_flavor": "XS",
"biggest_flavor": "M",
"redirect_https": true,
"sticky_sessions": true,
"app_folder": "./app",
"python_version": "2.7",
"pip_requirements": "requirements.txt",
"environment": map[string]any{
"MY_KEY": "myval",
},
}),
helper.SetBlockValues("hooks", map[string]any{"post_build": "echo \"build is OK!\""}),
)

resource.Test(t, resource.TestCase{
PreCheck: func() {
Expand Down Expand Up @@ -60,28 +82,7 @@ func TestAccPython_basic(t *testing.T) {
},
Steps: []resource.TestStep{{
ResourceName: rName,
Config: helper.NewProvider("clevercloud").
SetOrganisation(org).String() + helper.NewRessource(
"clevercloud_python",
rName,
helper.SetKeyValues(map[string]any{
"name": rName,
"region": "par",
"min_instance_count": 1,
"max_instance_count": 2,
"smallest_flavor": "XS",
"biggest_flavor": "M",
"redirect_https": true,
"sticky_sessions": true,
"app_folder": "./app",
"python_version": "2.7",
"pip_requirements": "requirements.txt",
"environment": map[string]any{
"MY_KEY": "myval",
},
}),
helper.SetBlockValues("hooks", map[string]any{"post_build": "echo \"build is OK!\""}),
).String(),
Config: providerBlock.Append(pythonBlock).String(),
Check: resource.ComposeAggregateTestCheckFunc(
// Test the state for provider's populated values
resource.TestMatchResourceAttr(fullName, "id", regexp.MustCompile(`^app_.*$`)),
Expand Down Expand Up @@ -159,6 +160,14 @@ func TestAccPython_basic(t *testing.T) {
return nil
},
),
}, {
ResourceName: rName,
Config: providerBlock.Append(
pythonBlock.SetOneValue("biggest_flavor", "XS"),
).String(),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr(fullName, "biggest_flavor", "XS"),
),
}, {
ResourceName: rName2,
Config: helper.NewProvider("clevercloud").SetOrganisation(org).String() + helper.NewRessource("clevercloud_python",
Expand Down

0 comments on commit e592dfe

Please sign in to comment.