Skip to content

Commit

Permalink
feat: java update
Browse files Browse the repository at this point in the history
  • Loading branch information
LeCrabe committed Feb 21, 2025
1 parent fa1de1a commit 63717c8
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 2 deletions.
93 changes: 92 additions & 1 deletion pkg/resources/java/crud.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"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 @@ -175,7 +176,97 @@ func (r *ResourceJava) Read(ctx context.Context, req resource.ReadRequest, resp

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

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

// Retrieve instance of the app from context
instance := application.LookupInstance(ctx, r.cc, "java", r.toProductName(), 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
10 changes: 9 additions & 1 deletion pkg/resources/java/java_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ var protoV6Provider = map[string]func() (tfprotov6.ProviderServer, error){

func TestAccJava_basic(t *testing.T) {
ctx := context.Background()
rName := fmt.Sprintf("tf-java-php-%d", time.Now().UnixMilli())
rName := fmt.Sprintf("tf-test-java-%d", time.Now().UnixMilli())
fullName := fmt.Sprintf("clevercloud_java_war.%s", rName)
cc := client.New(client.WithAutoOauthConfig())
org := os.Getenv("ORGANISATION")
Expand Down Expand Up @@ -58,6 +58,14 @@ func TestAccJava_basic(t *testing.T) {
resource.TestMatchResourceAttr(fullName, "deploy_url", regexp.MustCompile(`^git\+ssh.*\.git$`)),
resource.TestCheckResourceAttr(fullName, "region", "par"),
),
}, {
ResourceName: rName,
Config: providerBlock.Append(
javaBlock.SetOneValue("biggest_flavor", "XS"),
).String(),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr(fullName, "biggest_flavor", "XS"),
),
}},
CheckDestroy: func(state *terraform.State) error {
for _, resource := range state.RootModule().Resources {
Expand Down

0 comments on commit 63717c8

Please sign in to comment.