Skip to content

CP-53859 Implement a new TF resource xenserver_vm_import for VHD #95

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

acefei
Copy link
Member

@acefei acefei commented Apr 28, 2025

216508 pass

@acefei acefei force-pushed the private/feis/CP-53859 branch from 647654a to 55ae1e7 Compare April 28, 2025 06:23
@@ -0,0 +1,47 @@
locals {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This a good example file , but seems this file type is not right. And more import, customer can't see this example on web documentation.
I suggest we can move this example to examples/resources/xenserver_import_raw_vdi/resource.tf or xamples/resources/xenserver_vm/resource.tf , and give more messages about what this resource used for.

@@ -0,0 +1,47 @@
locals {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This a good example file , but seems this file type is not right. And more import, customer can't see this example on web documentation.
I suggest we can move this example to examples/resources/xenserver_import_raw_vdi/resource.tf or xamples/resources/xenserver_vm/resource.tf , and give more messages about what this resource used for.


func (r *importRawVdiResource) Schema(_ context.Context, _ resource.SchemaRequest, resp *resource.SchemaResponse) {
resp.Schema = schema.Schema{
MarkdownDescription: "import Raw/VHD vdi resource.",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"Provides a virtual disk image resource which can be imported from Raw/VHD" ?
and does this mean this resource also support .raw type?

return
}

defaultSr, err := getDefaultSR(r.session)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we allow customer to choose SR like xenserver_vdi resource? add a parameter "SR" in this resource?

func importRawVdiSchema() map[string]schema.Attribute {
return map[string]schema.Attribute{
"raw_vdi_path": schema.StringAttribute{
Description: "The path to the raw VDI file.",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this path only support .vhd? suggest we can add a support type list here.


// Configure HTTP client with appropriate timeouts and TLS settings
// #nosec G402 - InsecureSkipVerify is required for self-signed certificates
transport := &http.Transport{
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks good , can we move the client call to a common function? in case we have other task in future.

}

// Wait for task completion - remove the unnecessary Sleep
timeout := 60 * 60 // 10 minutes in seconds
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

10 min?

if err != nil {
return errors.New("task failed but couldn't get error info: " + err.Error())
}
return fmt.Errorf("import task failed: %s", errorInfo)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the "errorInfo" is a string set, can it be used like this ?

@@ -611,3 +611,15 @@ func smbResourceModelUpdate(session *xenapi.Session, ref xenapi.SRRef, data smbR

return nil
}

func getDefaultSR(session *xenapi.Session) (xenapi.SRRef, error) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same used in "snapshot_resource"

return vdiRef, nil
}

func removeVDI(session *xenapi.Session, vdiRef xenapi.VDIRef) error {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why add this? we already have "cleanupVDIResource", may be it can be combined?

@@ -207,3 +209,40 @@ func cleanupVDIResource(session *xenapi.Session, ref xenapi.VDIRef) error {
}
return nil
}

func createVDI(session *xenapi.Session, nameLabel string, fileSize int, srRef xenapi.SRRef) (xenapi.VDIRef, error) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is not used in xenserver/vdi_resource.go, we can move it to xenserver/import_raw_vdi_utils.go

// check if host, username, password are non-empty
if host == "" || username == "" || password == "" {
return nil, errors.New("host, username, password cannot be empty")
return nil, "", errors.New("host, username, password cannot be empty")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the type here may not be correct, you can add
var sessionRef xenapi.SessionRef
add return
return nil, sessionRef, errors.New("host, username, password cannot be empty")
same for below.

// download the raw vdi file to the local machine
// and set the VDI_PATH environment variable to its location
// Example: os.Setenv("VDI_PATH", "/path/to/your/raw.vdi")
vdi_path := os.Getenv("VDI_PATH")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add in README file as you add a new env value.


func (r *importRawVdiResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) {
resp.Diagnostics.AddError(
"Update Not Supported",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should also add this "not support update" message to parameter Description. like:
"\n\n-> **Note:** raw_vdi_path is not allowed to be updated.",

}

func importRawVdiSchema() map[string]schema.Attribute {
return map[string]schema.Attribute{
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Simply like vdi resource, this resource is also create a vdi in final, should we add some other parameter like “NameLabel ", "NameDescription" .etc?

I‘m thinking , actually this resource is also create a VDI and follow a import task, can we move the "raw_vdi_path" as a option filed in xenserver_vdi resource, and do import when this filed is not empty?

tflog.Debug(ctx, "Failed to destroy VDI for UUID: "+data.UUID.ValueString())
return
}
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is that possible we can record the raw_vdi_path in vdi other-config , so we can do read and import ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants