Skip to content

Commit 550fd16

Browse files
authored
Update sdk version for terraform plugin (#6284)
Signed-off-by: khanhtc1202 <khanhtc1202@gmail.com>
1 parent 4c7945c commit 550fd16

File tree

6 files changed

+43
-23
lines changed

6 files changed

+43
-23
lines changed

pkg/app/pipedv1/plugin/terraform/deployment/apply.go

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,22 +21,27 @@ import (
2121
"github.com/pipe-cd/pipecd/pkg/app/pipedv1/plugin/terraform/provider"
2222

2323
sdk "github.com/pipe-cd/piped-plugin-sdk-go"
24+
"go.uber.org/zap"
2425
)
2526

2627
// TODO: add test
2728
func (p *Plugin) executeApplyStage(ctx context.Context, input *sdk.ExecuteStageInput[config.ApplicationConfigSpec], dts []*sdk.DeployTarget[config.DeployTargetConfig]) sdk.StageStatus {
28-
lp := input.Client.LogPersister()
29+
slp, err := input.Client.StageLogPersister()
30+
if err != nil {
31+
input.Logger.Error("No stage log persister available", zap.Error(err))
32+
return sdk.StageStatusFailure
33+
}
2934
cmd, err := provider.NewTerraformCommand(ctx, input.Client, input.Request.TargetDeploymentSource, dts[0])
3035
if err != nil {
31-
lp.Errorf("Failed to initialize Terraform command (%v)", err)
36+
slp.Errorf("Failed to initialize Terraform command (%v)", err)
3237
return sdk.StageStatusFailure
3338
}
3439

35-
if err = cmd.Apply(ctx, lp); err != nil {
36-
lp.Errorf("Failed to apply changes (%v)", err)
40+
if err = cmd.Apply(ctx, slp); err != nil {
41+
slp.Errorf("Failed to apply changes (%v)", err)
3742
return sdk.StageStatusFailure
3843
}
3944

40-
lp.Success("Successfully applied changes")
45+
slp.Success("Successfully applied changes")
4146
return sdk.StageStatusSuccess
4247
}

pkg/app/pipedv1/plugin/terraform/deployment/plan.go

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,37 +22,42 @@ import (
2222
"github.com/pipe-cd/pipecd/pkg/app/pipedv1/plugin/terraform/provider"
2323

2424
sdk "github.com/pipe-cd/piped-plugin-sdk-go"
25+
"go.uber.org/zap"
2526
)
2627

2728
// TODO: add test
2829
func (p *Plugin) executePlanStage(ctx context.Context, input *sdk.ExecuteStageInput[config.ApplicationConfigSpec], dts []*sdk.DeployTarget[config.DeployTargetConfig]) sdk.StageStatus {
29-
lp := input.Client.LogPersister()
30+
slp, err := input.Client.StageLogPersister()
31+
if err != nil {
32+
input.Logger.Error("No stage log persister available", zap.Error(err))
33+
return sdk.StageStatusFailure
34+
}
3035
cmd, err := provider.NewTerraformCommand(ctx, input.Client, input.Request.TargetDeploymentSource, dts[0])
3136
if err != nil {
32-
lp.Errorf("Failed to initialize Terraform command (%v)", err)
37+
slp.Errorf("Failed to initialize Terraform command (%v)", err)
3338
return sdk.StageStatusFailure
3439
}
3540

3641
stageConfig := config.TerraformPlanStageOptions{}
3742
if err := json.Unmarshal(input.Request.StageConfig, &stageConfig); err != nil {
38-
lp.Errorf("Failed to unmarshal stage config (%v)", err)
43+
slp.Errorf("Failed to unmarshal stage config (%v)", err)
3944
return sdk.StageStatusFailure
4045
}
4146

42-
planResult, err := cmd.Plan(ctx, lp)
47+
planResult, err := cmd.Plan(ctx, slp)
4348
if err != nil {
44-
lp.Errorf("Failed to plan (%v)", err)
49+
slp.Errorf("Failed to plan (%v)", err)
4550
return sdk.StageStatusFailure
4651
}
4752

4853
if planResult.NoChanges() {
49-
lp.Success("No changes to apply")
54+
slp.Success("No changes to apply")
5055
if stageConfig.ExitOnNoChanges {
5156
return sdk.StageStatusExited
5257
}
5358
return sdk.StageStatusSuccess
5459
}
5560

56-
lp.Successf("Detected %d import, %d add, %d change, %d destroy.", planResult.Imports, planResult.Adds, planResult.Changes, planResult.Destroys)
61+
slp.Successf("Detected %d import, %d add, %d change, %d destroy.", planResult.Imports, planResult.Adds, planResult.Changes, planResult.Destroys)
5762
return sdk.StageStatusSuccess
5863
}

pkg/app/pipedv1/plugin/terraform/deployment/rollback.go

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,30 +21,35 @@ import (
2121
"github.com/pipe-cd/pipecd/pkg/app/pipedv1/plugin/terraform/provider"
2222

2323
sdk "github.com/pipe-cd/piped-plugin-sdk-go"
24+
"go.uber.org/zap"
2425
)
2526

2627
// TODO: add test
2728
func (p *Plugin) executeRollbackStage(ctx context.Context, input *sdk.ExecuteStageInput[config.ApplicationConfigSpec], dts []*sdk.DeployTarget[config.DeployTargetConfig]) sdk.StageStatus {
28-
lp := input.Client.LogPersister()
29+
slp, err := input.Client.StageLogPersister()
30+
if err != nil {
31+
input.Logger.Error("No stage log persister available", zap.Error(err))
32+
return sdk.StageStatusFailure
33+
}
2934
rds := input.Request.RunningDeploymentSource
3035

3136
if rds.CommitHash == "" {
32-
lp.Errorf("Unable to determine the last deployed commit to rollback. It seems this is the first deployment.")
37+
slp.Errorf("Unable to determine the last deployed commit to rollback. It seems this is the first deployment.")
3338
return sdk.StageStatusFailure
3439
}
3540

3641
cmd, err := provider.NewTerraformCommand(ctx, input.Client, rds, dts[0])
3742
if err != nil {
38-
lp.Errorf("Failed to initialize Terraform command (%v)", err)
43+
slp.Errorf("Failed to initialize Terraform command (%v)", err)
3944
return sdk.StageStatusFailure
4045
}
4146

42-
lp.Infof("Start rolling back to the state defined at commit %s", rds.CommitHash)
43-
if err = cmd.Apply(ctx, lp); err != nil {
44-
lp.Errorf("Failed to apply changes (%v)", err)
47+
slp.Infof("Start rolling back to the state defined at commit %s", rds.CommitHash)
48+
if err = cmd.Apply(ctx, slp); err != nil {
49+
slp.Errorf("Failed to apply changes (%v)", err)
4550
return sdk.StageStatusFailure
4651
}
4752

48-
lp.Success("Successfully rolled back the changes")
53+
slp.Success("Successfully rolled back the changes")
4954
return sdk.StageStatusSuccess
5055
}

pkg/app/pipedv1/plugin/terraform/go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ go 1.25.0
44

55
require (
66
github.com/hashicorp/hcl/v2 v2.0.0
7-
github.com/pipe-cd/piped-plugin-sdk-go v0.2.0
7+
github.com/pipe-cd/piped-plugin-sdk-go v0.3.0
88
github.com/stretchr/testify v1.10.0
99
go.uber.org/zap v1.19.1
1010
)

pkg/app/pipedv1/plugin/terraform/go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,8 @@ github.com/pipe-cd/pipecd v0.54.0-rc1.0.20250912082650-0b949bb7aac9 h1:kyFMfrjAS
228228
github.com/pipe-cd/pipecd v0.54.0-rc1.0.20250912082650-0b949bb7aac9/go.mod h1:etCJcXHbrFxuh9fG3MNBTZLKG8EQ1v+ZEGn9Rb/mK1o=
229229
github.com/pipe-cd/piped-plugin-sdk-go v0.2.0 h1:Le7IREhbLTm+PNiLcTcRUQ5Kep+OcvQbFa0tjgD/7gc=
230230
github.com/pipe-cd/piped-plugin-sdk-go v0.2.0/go.mod h1:qoRDN5uSt2kUs5hcNfvs8QIQYCnPVTKyKqUMf80RFFA=
231+
github.com/pipe-cd/piped-plugin-sdk-go v0.3.0 h1:pXBEHqKxsYZVXav9J1SkT36LNCNLHXGK99J6YfX1oQ4=
232+
github.com/pipe-cd/piped-plugin-sdk-go v0.3.0/go.mod h1:qoRDN5uSt2kUs5hcNfvs8QIQYCnPVTKyKqUMf80RFFA=
231233
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
232234
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
233235
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=

pkg/app/pipedv1/plugin/terraform/provider/initialize.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,13 @@ func NewTerraformCommand(ctx context.Context, client *sdk.Client, ds sdk.Deploym
3434
)
3535

3636
var infoWriter io.Writer
37-
infoWriter = client.LogPersister()
38-
if infoWriter == nil {
39-
// logPersister is nil when this functions is called outside deployment.
37+
slp, err := client.StageLogPersister()
38+
if err != nil {
39+
// stage log persister is not available when this functions is called outside deployment.
40+
// so we use io.Discard to avoid logging errors.
4041
infoWriter = io.Discard
42+
} else {
43+
infoWriter = slp
4144
}
4245

4346
tr := toolregistry.NewRegistry(client.ToolRegistry())

0 commit comments

Comments
 (0)