Skip to content

Commit

Permalink
Move the password check for SDC (#212)
Browse files Browse the repository at this point in the history
  • Loading branch information
shekhar-j authored Aug 22, 2024
1 parent 28b9e53 commit a70d9f7
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 19 deletions.
3 changes: 1 addition & 2 deletions powerflex/helper/sdc_host_dpkg.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"context"
"errors"
"fmt"
"path/filepath"
"strings"
"terraform-provider-powerflex/client"
"terraform-provider-powerflex/powerflex/models"
Expand Down Expand Up @@ -84,7 +83,7 @@ func (r *SdcHostResource) CreateUbuntu(ctx context.Context, plan models.SdcHostM

// upload sw
scpProv := client.NewScpProvisioner(sshP)
pkgTarget := filepath.Join(dir, "emc-sdc-package.tar")
pkgTarget := strings.TrimSuffix(dir, "/") + "/" + "emc-sdc-package.tar"
err := scpProv.Upload(plan.Pkg.ValueString(), pkgTarget, "")
if err != nil {
respDiagnostics.AddError(
Expand Down
3 changes: 1 addition & 2 deletions powerflex/helper/sdc_host_esxi.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package helper
import (
"context"
"fmt"
"path/filepath"
"regexp"
"strings"
"terraform-provider-powerflex/client"
Expand Down Expand Up @@ -78,7 +77,7 @@ func (r *SdcHostResource) CreateEsxi(ctx context.Context, plan models.SdcHostMod
}
defer sshP.Close()

pkgTarget := filepath.Join(dir, "emc-sdc-package.zip")
pkgTarget := strings.TrimSuffix(dir, "/") + "/" + "emc-sdc-package.zip"
if !plan.UseRemotePath.ValueBool() {
// upload sw
scpProv := client.NewScpProvisioner(sshP)
Expand Down
3 changes: 1 addition & 2 deletions powerflex/helper/sdc_host_rpm.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package helper
import (
"context"
"fmt"
"path/filepath"
"strings"
"terraform-provider-powerflex/client"
"terraform-provider-powerflex/powerflex/models"
Expand All @@ -36,7 +35,7 @@ func (r *SdcHostResource) CreateRhel(ctx context.Context, plan models.SdcHostMod
if !plan.UseRemotePath.ValueBool() {
// upload sw
scpProv := client.NewScpProvisioner(sshP)
pkgTarget := filepath.Join(dir, "emc-sdc-package.rpm")
pkgTarget := strings.TrimSuffix(dir, "/") + "/" + "emc-sdc-package.rpm"
err := scpProv.Upload(plan.Pkg.ValueString(), pkgTarget, "")
if err != nil {
respDiagnostics.AddError(
Expand Down
38 changes: 26 additions & 12 deletions powerflex/provider/sdc_host_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,18 +95,6 @@ func (r *sdcHostResource) ValidateConfig(ctx context.Context, req resource.Valid
)
}

if !cfg.OS.IsUnknown() && cfg.OS.ValueString() == "windows" && !cfg.Remote.IsNull() {
var remote models.SdcHostRemoteModel
cfg.Remote.As(ctx, &remote, basetypes.ObjectAsOptions{UnhandledNullAsEmpty: true, UnhandledUnknownAsEmpty: true})

if remote.Password == nil {
resp.Diagnostics.AddAttributeError(
path.Root("remote").AtName("password"),
"Password is required for Windows SDC",
"",
)
}
}
}

func (r *sdcHostResource) Schema(_ context.Context, _ resource.SchemaRequest, resp *resource.SchemaResponse) {
Expand Down Expand Up @@ -402,6 +390,19 @@ func (r *sdcHostResource) Create(ctx context.Context, req resource.CreateRequest
return
}

if !plan.OS.IsUnknown() && plan.OS.ValueString() == "windows" && !plan.Remote.IsNull() {
var remote models.SdcHostRemoteModel
plan.Remote.As(ctx, &remote, basetypes.ObjectAsOptions{UnhandledNullAsEmpty: true, UnhandledUnknownAsEmpty: true})

if remote.Password == nil {
resp.Diagnostics.AddAttributeError(
path.Root("remote").AtName("password"),
"Password is required for Windows SDC",
"",
)
return
}
}
//Checking that SDC exist or not
allSdcs, _ := r.system.GetSdc()
for _, sdcData := range allSdcs {
Expand Down Expand Up @@ -599,6 +600,19 @@ func (r *sdcHostResource) Delete(ctx context.Context, req resource.DeleteRequest
if resp.Diagnostics.HasError() {
return
}
if !state.OS.IsUnknown() && state.OS.ValueString() == "windows" && !state.Remote.IsNull() {
var remote models.SdcHostRemoteModel
state.Remote.As(ctx, &remote, basetypes.ObjectAsOptions{UnhandledNullAsEmpty: true, UnhandledUnknownAsEmpty: true})

if remote.Password == nil {
resp.Diagnostics.AddAttributeError(
path.Root("remote").AtName("password"),
"Password is required for Windows SDC",
"",
)
return
}
}

resHelper := helper.SdcHostResource{
System: r.system,
Expand Down
2 changes: 1 addition & 1 deletion powerflex/provider/sdc_host_resource_win_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func TestAccResourceSDCHostNegative(t *testing.T) {
//Create
{
Config: ProviderConfigForTesting + SDCHostConfig1,
ExpectError: regexp.MustCompile(`.*Password is required for Windows SDC.*`),
ExpectError: regexp.MustCompile(`.*Exactly one of these attributes must be configured.*`),
},
{
Config: ProviderConfigForTesting + SDCHostConfig2,
Expand Down

0 comments on commit a70d9f7

Please sign in to comment.