Skip to content
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

xenorchestra_vm unable to set blocked_operations on creation #318

Open
ddelnano opened this issue Mar 18, 2024 · 1 comment
Open

xenorchestra_vm unable to set blocked_operations on creation #318

ddelnano opened this issue Mar 18, 2024 · 1 comment

Comments

@ddelnano
Copy link
Collaborator

The xenorchestra_vm resource has supported blocked_operations, however, our test suite has only been exercising the logic to update a VM to add and remove operations (source).

When a VM is created from scratch the XO api serves the following error:

INTERNAL_ERROR((Failure "Unknown tag/contents"))
This is a XenServer/XCP-ng error

We should add test coverage for this case and fix the bug.

@ddelnano
Copy link
Collaborator Author

I thought this would be an easy fix since the xenstore data is already applied separately from the vm.create call (source). Unfortunately the XO api is unable to handle this and my guess is that it is actually an underlying xapi issue. It seems like the VM needs to be fully created and running in order to accept these "tags".

Here is the diff I tried

ddelnano@ddelnano-desktop:~/code/terraform-provider-xenorchestra (fix-bug-preventing-blocked-operations-on-vm-creation) $ git diff client/
diff --git a/client/vm.go b/client/vm.go
index 6591967..064d6d7 100644
--- a/client/vm.go
+++ b/client/vm.go
@@ -322,14 +322,6 @@ func (c *Client) CreateVm(vmReq Vm, createTime time.Duration) (*Vm, error) {
                params["startDelay"] = startDelay
        }

-       if len(vmReq.BlockedOperations) > 0 {
-               blockedOperations := map[string]string{}
-               for _, v := range vmReq.BlockedOperations {
-                       blockedOperations[v] = "true"
-               }
-               params["blockedOperations"] = blockedOperations
-       }
-
        if installation.Method != "" {
                params["installation"] = map[string]string{
                        "method":     installation.Method,
@@ -361,12 +353,20 @@ func (c *Client) CreateVm(vmReq Vm, createTime time.Duration) (*Vm, error) {
                return nil, err
        }

-       xsParams := map[string]interface{}{
+       postCreateParams := map[string]interface{}{
                "id":           vmId,
                "xenStoreData": vmReq.XenstoreData,
        }
+       if len(vmReq.BlockedOperations) > 0 {
+               blockedOperations := map[string]string{}
+               for _, v := range vmReq.BlockedOperations {
+                       blockedOperations[v] = "true"
+               }
+               postCreateParams["blockedOperations"] = blockedOperations
+       }
+
        var success bool
-       err = c.Call("vm.set", xsParams, &success)
+       err = c.Call("vm.set", postCreateParams, &success)

        if err != nil {
                return nil, err
ddelnano@ddelnano-desktop:~/code/terraform-provider-xenorchestra (fix-bug-preventing-blocked-operations-on-vm-creation) $ git diff xoa
diff --git a/xoa/resource_xenorchestra_vm_test.go b/xoa/resource_xenorchestra_vm_test.go
index 0af101e..154f6b5 100644
--- a/xoa/resource_xenorchestra_vm_test.go
+++ b/xoa/resource_xenorchestra_vm_test.go
@@ -1470,6 +1470,31 @@ func TestAccXenorchestraVm_replaceExistingVifs(t *testing.T) {
        })
 }

+func TestAccXenorchestraVm_createAllowsBlockedOperations(t *testing.T) {
+       resourceName := "xenorchestra_vm.bar"
+       vmName := fmt.Sprintf("%s - %s", accTestPrefix, t.Name())
+       resource.ParallelTest(t, resource.TestCase{
+               PreCheck:     func() { testAccPreCheck(t) },
+               Providers:    testAccProviders,
+               CheckDestroy: testAccCheckXenorchestraVmDestroy,
+               Steps: []resource.TestStep{
+                       {
+                               Config: testAccVmConfigUpdateAttr(
+                                       vmName,
+                                       `
+                              blocked_operations = ["copy"]
+                            `),
+                               Check: resource.ComposeAggregateTestCheckFunc(
+                                       testAccVmExists(resourceName),
+                                       resource.TestCheckResourceAttrSet(resourceName, "id"),
+                                       resource.TestCheckResourceAttr(resourceName, "blocked_operations.#", "1"),
+                                       resource.TestCheckResourceAttr(resourceName, "blocked_operations.0", "copy"),
+                               ),
+                       },
+               },
+       })
+}
+
 func TestAccXenorchestraVm_updatesWithoutReboot(t *testing.T) {
        resourceName := "xenorchestra_vm.bar"

And here is the error from that attempt:

  | Error: jsonrpc2: code -32000 message: INTERNAL_ERROR((Failure "Unknown tag/contents")): {"code":"INTERNAL_ERROR","params":["(Failure \"Unknown tag/contents\")"],"call":{"method":"VM.remove_from_blocked_operations","params":["OpaqueRef:f06b0e97-9691-4494-a780-75802d7a0501","true"]},"message":"INTERNAL_ERROR((Failure \"Unknown tag/contents\"))","name":"XapiError","stack":"XapiError: INTERNAL_ERROR((Failure \"Unknown tag/contents\"))\n    at Function.wrap (file:///usr/local/lib/node_modules/xo-server/node_modules/xen-api/_XapiError.mjs:16:12)\n    at file:///usr/local/lib/node_modules/xo-server/node_modules/xen-api/transports/json-rpc.mjs:35:21\n    at runNextTicks (node:internal/process/task_queues:60:5)\n    at processImmediate (node:internal/timers:447:9)\n    at process.callbackTrampoline (node:internal/async_hooks:128:17)"}

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

No branches or pull requests

1 participant