Skip to content

Commit

Permalink
CA-403474: Improve check before pool eject (#84)
Browse files Browse the repository at this point in the history
Signed-off-by: xueqingz <[email protected]>
  • Loading branch information
xueqingz authored Jan 13, 2025
1 parent d92a91d commit 9661695
Show file tree
Hide file tree
Showing 4 changed files with 143 additions and 135 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ export NFS_SERVER_PATH=<nfs-server-path>
export SMB_SERVER_PATH=<smb-server-path>
export SMB_SERVER_USERNAME=<smb-server-username>
export SMB_SERVER_PASSWORD=<smb-server-password>
export SUPPORTER_HOST=<supporter-ip>
export SUPPORTER_USERNAME=<supporter-username>
export SUPPORTER_PASSWORD=<supporter-password>
```

Run `"make testacc"`. *Note:* Acceptance tests generate actual resources and frequently incur costs when run.
Expand Down
14 changes: 11 additions & 3 deletions xenserver/pool_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,13 @@ func (r *poolResource) Configure(_ context.Context, req resource.ConfigureReques
}

func (r *poolResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
tflog.Debug(ctx, "---> Create Pool resource")
var plan poolResourceModel
resp.Diagnostics.Append(req.Plan.Get(ctx, &plan)...)
if resp.Diagnostics.HasError() {
return
}

tflog.Debug(ctx, "Creating pool...")
poolParams := getPoolParams(plan)

poolRef, err := getPoolRef(r.session)
Expand All @@ -79,6 +79,7 @@ func (r *poolResource) Create(ctx context.Context, req resource.CreateRequest, r
return
}

tflog.Debug(ctx, "----> Start Pool join")
err = poolJoin(ctx, r.session, r.coordinatorConf, plan)
if err != nil {
resp.Diagnostics.AddError(
Expand All @@ -88,6 +89,7 @@ func (r *poolResource) Create(ctx context.Context, req resource.CreateRequest, r
return
}

tflog.Debug(ctx, "----> Start Pool eject")
err = poolEject(ctx, r.session, plan)
if err != nil {
resp.Diagnostics.AddError(
Expand All @@ -97,6 +99,7 @@ func (r *poolResource) Create(ctx context.Context, req resource.CreateRequest, r
return
}

tflog.Debug(ctx, "----> Start Pool setting")
err = setPool(r.session, poolRef, poolParams)
if err != nil {
resp.Diagnostics.AddError(
Expand Down Expand Up @@ -166,6 +169,7 @@ func (r *poolResource) Read(ctx context.Context, req resource.ReadRequest, resp
}

func (r *poolResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) {
tflog.Debug(ctx, "---> Update Pool resource")
var plan, state poolResourceModel
resp.Diagnostics.Append(req.Plan.Get(ctx, &plan)...)
if resp.Diagnostics.HasError() {
Expand All @@ -187,6 +191,7 @@ func (r *poolResource) Update(ctx context.Context, req resource.UpdateRequest, r
return
}

tflog.Debug(ctx, "----> Start Pool join")
err = poolJoin(ctx, r.session, r.coordinatorConf, plan)
if err != nil {
resp.Diagnostics.AddError(
Expand All @@ -196,6 +201,7 @@ func (r *poolResource) Update(ctx context.Context, req resource.UpdateRequest, r
return
}

tflog.Debug(ctx, "----> Start Pool eject")
err = poolEject(ctx, r.session, plan)
if err != nil {
resp.Diagnostics.AddError(
Expand All @@ -205,6 +211,7 @@ func (r *poolResource) Update(ctx context.Context, req resource.UpdateRequest, r
return
}

tflog.Debug(ctx, "----> Start Pool setting")
err = setPool(r.session, poolRef, poolParams)
if err != nil {
resp.Diagnostics.AddError(
Expand Down Expand Up @@ -237,26 +244,27 @@ func (r *poolResource) Update(ctx context.Context, req resource.UpdateRequest, r
}

func (r *poolResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) {
tflog.Debug(ctx, "---> Delete Pool resource")
var state poolResourceModel
resp.Diagnostics.Append(req.State.Get(ctx, &state)...)
if resp.Diagnostics.HasError() {
return
}

tflog.Debug(ctx, "Deleting pool...")
poolRef, err := xenapi.Pool.GetByUUID(r.session, state.UUID.ValueString())
if err != nil {
resp.Diagnostics.AddError("Unable to get pool ref", err.Error())
return
}

tflog.Debug(ctx, "----> Clean pool resource")
err = cleanupPoolResource(r.session, poolRef)
if err != nil {
resp.Diagnostics.AddError("Unable to cleanup pool resource", err.Error())
return
}

tflog.Debug(ctx, "Pool deleted")
tflog.Debug(ctx, "---> Pool deleted")
}

func (r *poolResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) {
Expand Down
109 changes: 45 additions & 64 deletions xenserver/pool_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,63 +43,44 @@ data "xenserver_pif" "pif" {
`, eth_index, eth_index)
}

func managementNetwork(index string) string {
func managementNetwork(name_label string, name_description string, host_index string) string {
return fmt.Sprintf(`
resource "xenserver_pool" "pool" {
name_label = "%s"
name_description = "%s"
default_sr = xenserver_sr_nfs.nfs.uuid
management_network = data.xenserver_pif.pif.data_items[%s].network
`, index)
}
`, name_label, name_description, host_index)
}

func testPoolResource(name_label string,
name_description string,
storage_location string,
management_network string,
supporter_params string,
eject_supporter string) string {
func testPoolResource(storage_location string, extra string) string {
return fmt.Sprintf(`
resource "xenserver_sr_nfs" "nfs" {
name_label = "NFS"
name_label = "NFS for pool test"
version = "3"
storage_location = "%s"
}
data "xenserver_host" "supporter" {
is_coordinator = false
%s
`, storage_location, extra)
}

func joinSupporterParams(name_label string, name_description string, supporterHost string, supporterUsername string, supporterPassowd string) string {
return fmt.Sprintf(`
resource "xenserver_pool" "pool" {
name_label = "%s"
name_description = "%s"
default_sr = xenserver_sr_nfs.nfs.uuid
%s
%s
%s
}
`, storage_location,
name_label,
name_description,
management_network,
supporter_params,
eject_supporter)
}

func testJoinSupporterParams(supporterHost string, supporterUsername string, supporterPassowd string) string {
return fmt.Sprintf(`
join_supporters = [
{
host = "%s"
username = "%s"
password = "%s"
}
]
`, supporterHost, supporterUsername, supporterPassowd)
}

func ejectSupporterParams(index string) string {
return fmt.Sprintf(`
eject_supporters = [
data.xenserver_host.supporter.data_items[%s].uuid
]
`, index)
}
`, name_label, name_description, supporterHost, supporterUsername, supporterPassowd)
}

func TestAccPoolResource(t *testing.T) {
Expand All @@ -109,52 +90,52 @@ func TestAccPoolResource(t *testing.T) {
}

storageLocation := os.Getenv("NFS_SERVER") + ":" + os.Getenv("NFS_SERVER_PATH")
joinSupporterParams := testJoinSupporterParams(os.Getenv("SUPPORTER_HOST"), os.Getenv("SUPPORTER_USERNAME"), os.Getenv("SUPPORTER_PASSWORD"))
resource.Test(t, resource.TestCase{
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
Steps: []resource.TestStep{
// Create and Read testing for Default SR and Pool Join
// Pool Join
{
Config: providerConfig + testPoolResource("Test Pool A",
Config: providerConfig + testPoolResource(storageLocation, joinSupporterParams(
"Test Pool A",
"Test Pool Join",
storageLocation,
"",
joinSupporterParams,
""),
os.Getenv("SUPPORTER_HOST"),
os.Getenv("SUPPORTER_USERNAME"),
os.Getenv("SUPPORTER_PASSWORD"))),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr("xenserver_pool.pool", "name_label", "Test Pool A"),
resource.TestCheckResourceAttr("xenserver_pool.pool", "name_description", "Test Pool Join"),
resource.TestCheckResourceAttrSet("xenserver_pool.pool", "default_sr"),
),
},
// ImportState testing
{
ResourceName: "xenserver_pool.pool",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"join_supporters"},
},
// Update and Read testing For Pool eject supporter
{
Config: providerConfig + testPoolResource("Test Pool B",
"Test Pool Eject",
storageLocation,
"",
"",
ejectSupporterParams("1")),
Config: providerConfig + testPoolResource(storageLocation, joinSupporterParams(
"Test Pool B",
"Test Pool Join again",
os.Getenv("SUPPORTER_HOST"),
os.Getenv("SUPPORTER_USERNAME"),
os.Getenv("SUPPORTER_PASSWORD"))),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr("xenserver_pool.pool", "name_label", "Test Pool B"),
resource.TestCheckResourceAttr("xenserver_pool.pool", "name_description", "Test Pool Eject"),
resource.TestCheckResourceAttr("xenserver_pool.pool", "name_description", "Test Pool Join again"),
),
},
// Update and Read testing For Pool Management Network
// Delete testing automatically occurs in TestCase
},
})
}

func TestAccPoolManagementNetwork(t *testing.T) {
// skip test if TEST_POOL is not set
if os.Getenv("TEST_POOL") == "" {
t.Skip("Skipping TestAccPoolManagementNetwork test due to TEST_POOL not set")
}

storageLocation := os.Getenv("NFS_SERVER") + ":" + os.Getenv("NFS_SERVER_PATH")
resource.Test(t, resource.TestCase{
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
Steps: []resource.TestStep{
// Pool Management Network
{
Config: providerConfig + pifResource("3") + testPoolResource("Test Pool C",
"Test Pool Management Network",
storageLocation,
managementNetwork("2"),
"",
""),
Config: providerConfig + pifResource("3") + testPoolResource(storageLocation, managementNetwork("Test Pool C", "Test Pool Management Network", "2")),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr("xenserver_pool.pool", "name_label", "Test Pool C"),
resource.TestCheckResourceAttr("xenserver_pool.pool", "name_description", "Test Pool Management Network"),
Expand Down
Loading

0 comments on commit 9661695

Please sign in to comment.