Skip to content

Commit

Permalink
feat(PHP): implem update
Browse files Browse the repository at this point in the history
  • Loading branch information
miton18 committed Dec 26, 2024
1 parent ec59f20 commit 71ec074
Show file tree
Hide file tree
Showing 22 changed files with 286 additions and 73 deletions.
61 changes: 61 additions & 0 deletions pkg/application/creation.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,17 @@ type CreateReq struct {
Dependencies []string
}

type UpdateReq struct {
ID string
Client *client.Client
Organization string
Application tmp.UpdateAppReq
Environment map[string]string
VHosts []string
Deployment *Deployment
Dependencies []string
}

type Deployment struct {
Repository string
Commit *string
Expand Down Expand Up @@ -79,6 +90,56 @@ func CreateApp(ctx context.Context, req CreateReq) (*CreateRes, diag.Diagnostics
return res, diags
}

func UpdateApp(ctx context.Context, req UpdateReq) (*CreateRes, diag.Diagnostics) {
diags := diag.Diagnostics{}

// Application
res := &CreateRes{}

appRes := tmp.UpdateApp(ctx, req.Client, req.Organization, req.ID, req.Application)
if appRes.HasError() {
diags.AddError("failed to update application", appRes.Error().Error())
tflog.Error(ctx, "failed to update app", map[string]interface{}{"error": appRes.Error().Error(), "payload": fmt.Sprintf("%+v", req.Application)})
return nil, diags
}

res.Application = *appRes.Payload()

// Environment
envRes := tmp.UpdateAppEnv(ctx, req.Client, req.Organization, res.Application.ID, req.Environment)
if envRes.HasError() {
diags.AddError("failed to configure application environment", envRes.Error().Error())
}

// VHosts
for _, vhost := range req.VHosts {
addVhostRes := tmp.AddAppVHost(ctx, req.Client, req.Organization, res.Application.ID, vhost)
if addVhostRes.HasError() {
diags.AddError("failed to add additional vhost", addVhostRes.Error().Error())
}
}
// TODO: old vhost need to be cleaned

// Git Deployment
if req.Deployment != nil {
diags.Append(gitDeploy(ctx, *req.Deployment, req.Client, res.Application.DeployURL)...)
}

// Dependencies
for _, dependency := range req.Dependencies {
// TODO: support another apps as dependency

depRes := tmp.AddAppLinkedAddons(ctx, req.Client, req.Organization, res.Application.ID, dependency)
if depRes.HasError() {
tflog.Error(ctx, "ERROR: "+dependency, map[string]interface{}{"err": depRes.Error().Error()})
diags.AddError("failed to add dependency", depRes.Error().Error())
}
}
// TODO: unlink unneeded deps

return res, diags
}

// on clever side, it's an enum
func FromForceHTTPS(force bool) string {
if force {
Expand Down
7 changes: 5 additions & 2 deletions pkg/attributes/addon.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ package attributes

import (
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/int64planmodifier"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringdefault"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
"github.com/hashicorp/terraform-plugin-framework/types"
"go.clever-cloud.com/terraform-provider/pkg"
)
Expand All @@ -16,7 +19,7 @@ type Addon struct {
}

var addonCommon = map[string]schema.Attribute{
"id": schema.StringAttribute{Computed: true, MarkdownDescription: "Generated unique identifier"},
"id": schema.StringAttribute{Computed: true, MarkdownDescription: "Generated unique identifier", PlanModifiers: []planmodifier.String{stringplanmodifier.UseStateForUnknown()}},
"name": schema.StringAttribute{Required: true, MarkdownDescription: "Name of the service"},
"plan": schema.StringAttribute{Required: true, MarkdownDescription: "Database size and spec"},
"region": schema.StringAttribute{
Expand All @@ -25,7 +28,7 @@ var addonCommon = map[string]schema.Attribute{
Default: stringdefault.StaticString("par"),
MarkdownDescription: "Geographical region where the data will be stored",
},
"creation_date": schema.Int64Attribute{Computed: true, MarkdownDescription: "Date of database creation"},
"creation_date": schema.Int64Attribute{Computed: true, MarkdownDescription: "Date of database creation", PlanModifiers: []planmodifier.Int64{int64planmodifier.UseStateForUnknown()}},
}

func WithAddonCommons(runtimeSpecifics map[string]schema.Attribute) map[string]schema.Attribute {
Expand Down
4 changes: 4 additions & 0 deletions pkg/attributes/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ import (
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"

"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringdefault"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
"github.com/hashicorp/terraform-plugin-framework/types"
"go.clever-cloud.com/terraform-provider/pkg"
)
Expand Down Expand Up @@ -98,10 +100,12 @@ var runtimeCommon = map[string]schema.Attribute{
"id": schema.StringAttribute{
Computed: true,
MarkdownDescription: "Unique identifier generated during application creation",
PlanModifiers: []planmodifier.String{stringplanmodifier.UseStateForUnknown()},
},
"deploy_url": schema.StringAttribute{
Computed: true,
MarkdownDescription: "Git URL used to push source code",
PlanModifiers: []planmodifier.String{stringplanmodifier.UseStateForUnknown()},
},
// cleverapps one
"vhost": schema.StringAttribute{
Expand Down
16 changes: 15 additions & 1 deletion pkg/helper/provider_block.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
package helper

import (
"fmt"

"go.clever-cloud.com/terraform-provider/pkg"
)

// Provider structur
type Provider struct {
provider string
organisation string
blocks []fmt.Stringer
}

// New function type that accepts pointer to Provider
Expand Down Expand Up @@ -37,6 +44,11 @@ func (p *Provider) SetOrganisation(orgName string) *Provider {
return p
}

func (p *Provider) Append(blocks ...fmt.Stringer) *Provider {
p.blocks = blocks
return p
}

// Provider block
// - desc: chained function that stringify Provider into a terraform block
// - args: none
Expand All @@ -45,6 +57,8 @@ func (p *Provider) String() string {
s := `provider "` + p.provider + `" {
organisation = "` + p.organisation + `"
}
`
` + pkg.Reduce[fmt.Stringer, string](p.blocks, "", func(acc string, block fmt.Stringer) string {
return acc + block.String() + "\n"
})
return s
}
20 changes: 20 additions & 0 deletions pkg/helper/state.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package helper

import (
"context"

"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
)

func PlanFrom[T any](ctx context.Context, p tfsdk.Plan, diags diag.Diagnostics) T {
var t T
diags.Append(p.Get(ctx, &t)...)
return t
}

func StateFrom[T any](ctx context.Context, s tfsdk.State, diags diag.Diagnostics) T {
var t T
diags.Append(s.Get(ctx, &t)...)
return t
}
6 changes: 3 additions & 3 deletions pkg/resources/addon/addon_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func TestAccAddon_basic(t *testing.T) {
fullName := fmt.Sprintf("clevercloud_addon.%s", rName)
cc := client.New(client.WithAutoOauthConfig())
org := os.Getenv("ORGANISATION")
providerBlock := helper.NewProvider("clevercloud").SetOrganisation(org).String()
providerBlock := helper.NewProvider("clevercloud").SetOrganisation(org)
addonBlock := helper.NewRessource(
"clevercloud_addon",
rName,
Expand All @@ -37,7 +37,7 @@ func TestAccAddon_basic(t *testing.T) {
"region": "par",
"plan": "clever_solo",
"third_party_provider": "mailpace",
})).String()
}))

resource.Test(t, resource.TestCase{
PreCheck: func() {
Expand All @@ -62,7 +62,7 @@ func TestAccAddon_basic(t *testing.T) {
},
Steps: []resource.TestStep{{
ResourceName: rName,
Config: providerBlock + addonBlock,
Config: providerBlock.Append(addonBlock).String(),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestMatchResourceAttr(fullName, "id", regexp.MustCompile(`^addon_.*`)),
//resource.TestMatchResourceAttr(fullName, "password", regexp.MustCompile(`^[a-zA-Z0-9]+$`)),
Expand Down
6 changes: 3 additions & 3 deletions pkg/resources/cellar/bucket/bucket_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func TestAccCellarBucket_basic(t *testing.T) {
rName := fmt.Sprintf("my-bucket-%d", time.Now().UnixMilli())
cc := client.New(client.WithAutoOauthConfig())
org := os.Getenv("ORGANISATION")
providerBlock := helper.NewProvider("clevercloud").SetOrganisation(org).String()
providerBlock := helper.NewProvider("clevercloud").SetOrganisation(org)

cellar := &tmp.AddonResponse{}
if os.Getenv("TF_ACC") == "1" {
Expand Down Expand Up @@ -59,7 +59,7 @@ func TestAccCellarBucket_basic(t *testing.T) {
helper.SetKeyValues(map[string]any{
"id": rName,
"cellar_id": cellar.RealID,
})).String()
}))

resource.Test(t, resource.TestCase{
PreCheck: func() {
Expand All @@ -73,7 +73,7 @@ func TestAccCellarBucket_basic(t *testing.T) {
ProtoV6ProviderFactories: TestProtoV6Provider,
Steps: []resource.TestStep{{
ResourceName: "cellar_bucket_" + rName,
Config: providerBlock + cellarBucketBlock,
Config: providerBlock.Append(cellarBucketBlock).String(),
Check: resource.ComposeAggregateTestCheckFunc(
func(*terraform.State) error {
return nil
Expand Down
6 changes: 3 additions & 3 deletions pkg/resources/cellar/cellar_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@ func TestAccCellar_basic(t *testing.T) {
fullName := fmt.Sprintf("clevercloud_cellar.%s", rName)
cc := client.New(client.WithAutoOauthConfig())
org := os.Getenv("ORGANISATION")
providerBlock := helper.NewProvider("clevercloud").SetOrganisation(org).String()
providerBlock := helper.NewProvider("clevercloud").SetOrganisation(org)
cellarBlock := helper.NewRessource(
"clevercloud_cellar",
rName,
helper.SetKeyValues(map[string]any{
"name": rName,
"region": "par",
})).String()
}))

resource.Test(t, resource.TestCase{
PreCheck: func() {
Expand All @@ -47,7 +47,7 @@ func TestAccCellar_basic(t *testing.T) {
ProtoV6ProviderFactories: TestProtoV6Provider,
Steps: []resource.TestStep{{
ResourceName: "cellar_" + rName,
Config: providerBlock + cellarBlock,
Config: providerBlock.Append(cellarBlock).String(),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestMatchResourceAttr(fullName, "id", regexp.MustCompile(`^cellar_.*`)),
resource.TestMatchResourceAttr(fullName, "host", regexp.MustCompile(`^.*\.services.clever-cloud.com$`)),
Expand Down
6 changes: 4 additions & 2 deletions pkg/resources/cellar/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ import (

"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringdefault"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
"github.com/hashicorp/terraform-plugin-framework/types"
)

Expand Down Expand Up @@ -39,8 +41,8 @@ func (r ResourceCellar) Schema(_ context.Context, req resource.SchemaRequest, re
},

// provider
"id": schema.StringAttribute{Computed: true, MarkdownDescription: "Generated unique identifier"},
"host": schema.StringAttribute{Computed: true, MarkdownDescription: "S3 compatible Cellar endpoint"},
"id": schema.StringAttribute{Computed: true, MarkdownDescription: "Generated unique identifier", PlanModifiers: []planmodifier.String{stringplanmodifier.UseStateForUnknown()}},
"host": schema.StringAttribute{Computed: true, MarkdownDescription: "S3 compatible Cellar endpoint", PlanModifiers: []planmodifier.String{stringplanmodifier.UseStateForUnknown()}},
"key_id": schema.StringAttribute{Computed: true, MarkdownDescription: "Key ID used to authenticate"},
"key_secret": schema.StringAttribute{Computed: true, Sensitive: true, MarkdownDescription: "Key secret used to authenticate"},
},
Expand Down
6 changes: 3 additions & 3 deletions pkg/resources/docker/resource_docker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func TestAccDocker_basic(t *testing.T) {
fullName := fmt.Sprintf("clevercloud_docker.%s", rName)
cc := client.New(client.WithAutoOauthConfig())
org := os.Getenv("ORGANISATION")
providerBlock := helper.NewProvider("clevercloud").SetOrganisation(org).String()
providerBlock := helper.NewProvider("clevercloud").SetOrganisation(org)
dockerBlock := helper.NewRessource(
"clevercloud_docker",
rName,
Expand All @@ -41,7 +41,7 @@ func TestAccDocker_basic(t *testing.T) {
"smallest_flavor": "XS",
"biggest_flavor": "M",
"additional_vhosts": [1]string{"toto-tf5283457829345.com"},
})).String()
}))

resource.Test(t, resource.TestCase{
PreCheck: func() {
Expand All @@ -53,7 +53,7 @@ func TestAccDocker_basic(t *testing.T) {
Steps: []resource.TestStep{{
Destroy: false,
ResourceName: rName,
Config: providerBlock + dockerBlock,
Config: providerBlock.Append(dockerBlock).String(),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestMatchResourceAttr(fullName, "id", regexp.MustCompile(`^app_.*$`)),
resource.TestMatchResourceAttr(fullName, "deploy_url", regexp.MustCompile(`^git\+ssh.*\.git$`)),
Expand Down
6 changes: 3 additions & 3 deletions pkg/resources/java/java_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func TestAccJava_basic(t *testing.T) {
fullName := fmt.Sprintf("clevercloud_java_war.%s", rName)
cc := client.New(client.WithAutoOauthConfig())
org := os.Getenv("ORGANISATION")
providerBlock := helper.NewProvider("clevercloud").SetOrganisation(org).String()
providerBlock := helper.NewProvider("clevercloud").SetOrganisation(org)
javaBlock := helper.NewRessource(
"clevercloud_java_war",
rName,
Expand All @@ -40,7 +40,7 @@ func TestAccJava_basic(t *testing.T) {
"max_instance_count": 2,
"smallest_flavor": "XS",
"biggest_flavor": "M",
})).String()
}))

resource.Test(t, resource.TestCase{
PreCheck: func() {
Expand All @@ -52,7 +52,7 @@ func TestAccJava_basic(t *testing.T) {
Steps: []resource.TestStep{{
Destroy: false,
ResourceName: rName,
Config: providerBlock + javaBlock,
Config: providerBlock.Append(javaBlock).String(),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestMatchResourceAttr(fullName, "id", regexp.MustCompile(`^app_.*$`)),
resource.TestMatchResourceAttr(fullName, "deploy_url", regexp.MustCompile(`^git\+ssh.*\.git$`)),
Expand Down
10 changes: 7 additions & 3 deletions pkg/resources/keycloak/keycloak_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,12 @@ func TestAccKeycloak_basic(t *testing.T) {
fullName := fmt.Sprintf("clevercloud_keycloak.%s", rName)
cc := client.New(client.WithAutoOauthConfig())
org := os.Getenv("ORGANISATION")
providerBlock := helper.NewProvider("clevercloud").SetOrganisation(org).String()
materiakvBlock := helper.NewRessource("clevercloud_keycloak", rName, helper.SetKeyValues(map[string]any{"name": rName, "region": "par"})).String()
providerBlock := helper.NewProvider("clevercloud").SetOrganisation(org)
materiakvBlock := helper.NewRessource(
"clevercloud_keycloak",
rName,
helper.SetKeyValues(map[string]any{"name": rName, "region": "par"}),
)

resource.Test(t, resource.TestCase{
PreCheck: func() {
Expand All @@ -55,7 +59,7 @@ func TestAccKeycloak_basic(t *testing.T) {
},
Steps: []resource.TestStep{{
ResourceName: rName,
Config: providerBlock + materiakvBlock,
Config: providerBlock.Append(materiakvBlock).String(),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestMatchResourceAttr(fullName, "id", regexp.MustCompile(`^keycloak_.*`)),
resource.TestMatchResourceAttr(fullName, "host", regexp.MustCompile(`^.*clever-cloud.com$`)),
Expand Down
6 changes: 3 additions & 3 deletions pkg/resources/materiakv/materiakv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ func TestAccMateriaKV_basic(t *testing.T) {
fullName := fmt.Sprintf("clevercloud_materia_kv.%s", rName)
cc := client.New(client.WithAutoOauthConfig())
org := os.Getenv("ORGANISATION")
providerBlock := helper.NewProvider("clevercloud").SetOrganisation(org).String()
materiakvBlock := helper.NewRessource("clevercloud_materia_kv", rName, helper.SetKeyValues(map[string]any{"name": rName, "region": "par"})).String()
providerBlock := helper.NewProvider("clevercloud").SetOrganisation(org)
materiakvBlock := helper.NewRessource("clevercloud_materia_kv", rName, helper.SetKeyValues(map[string]any{"name": rName, "region": "par"}))

resource.Test(t, resource.TestCase{
PreCheck: func() {
Expand Down Expand Up @@ -58,7 +58,7 @@ func TestAccMateriaKV_basic(t *testing.T) {
},
Steps: []resource.TestStep{{
ResourceName: rName,
Config: providerBlock + materiakvBlock,
Config: providerBlock.Append(materiakvBlock).String(),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestMatchResourceAttr(fullName, "id", regexp.MustCompile(`^kv_.*`)),
resource.TestMatchResourceAttr(fullName, "host", regexp.MustCompile(`^.*clever-cloud.com$`)),
Expand Down
Loading

0 comments on commit 71ec074

Please sign in to comment.