[GO] 使用Golang开发CloudStack API的最佳实践

[GO] 使用Golang开发CloudStack API的最佳实践 大家好,

我叫朱利安。我是法国人。我的英语说得不太好。抱歉。

我访问了这个GitHub仓库:https://github.com/mindjiver/gopherstack

我很喜欢它。这个“Gopherstack”目录满足了我的需求。

只是有个小问题,我们只能根据给定的ID恢复单台虚拟机VM的IP和状态……

所以,我的问题是: 我如何才能恢复我所有VM的IP和状态,而不仅仅是一台?

你有什么想法吗?拜托了?

谢谢!

朱利安。


更多关于[GO] 使用Golang开发CloudStack API的最佳实践的实战教程也可以访问 https://www.itying.com/category-94-b0.html

3 回复

感谢您的帮助。我找到了。我们可以关闭这个主题了。

更多关于[GO] 使用Golang开发CloudStack API的最佳实践的实战系列教程也可以访问 https://www.itying.com/category-94-b0.html


cs.ListVirtualMachines(vmid)

只接受一台机器的ID,然后调用此函数。

// 返回虚拟机状态的Cloudstack字符串表示
func (c CloudstackClient) ListVirtualMachines(id string) (ListVirtualMachinesResponse, error) {
	var resp ListVirtualMachinesResponse
	params := url.Values{}
	params.Set("id", id)
	response, err := NewRequest(c, "listVirtualMachines", params)
	if err != nil {
		return resp, err
	}
	resp = response.(ListVirtualMachinesResponse)
	return resp, err
}

但ID并不是API的必需参数:https://cloudstack.apache.org/api/apidocs-4.13/apis/listVirtualMachines.html

因此,也许你可以添加自己的函数。

func (c CloudstackClient) ListAllVirtualMachines() (ListVirtualMachinesResponse, error) {
	var resp ListVirtualMachinesResponse
	params := url.Values{}
	response, err := NewRequest(c, "listVirtualMachines", params)
	if err != nil {
		return resp, err
	}
	resp = response.(ListVirtualMachinesResponse)
	return resp, err
}

但是,如果你将其放在自己的包中,需要在所有类型前添加gopherstack包,并将gopherstack.CloudstackClient作为参数而不是接收器。

func ListAllVirtualMachines(c gopherstack.CloudstackClient) (gopherstack.ListVirtualMachinesResponse, error) {
	var resp gopherstack.ListVirtualMachinesResponse
	params := url.Values{}
	response, err := gopherstack.NewRequest(c, "listVirtualMachines", params)
	if err != nil {
		return resp, err
	}
	resp = response.(gopherstack.ListVirtualMachinesResponse)
	return resp, err
}

这只是一个想法,我还没有测试过。

根据你的需求,可以使用CloudStack的listVirtualMachines API来获取所有虚拟机的信息。以下是使用Gopherstack库的示例代码:

package main

import (
    "fmt"
    "github.com/mindjiver/gopherstack"
)

func main() {
    // 初始化CloudStack客户端
    client := gopherstack.CloudstackClient{
        BaseURL:    "http://your-cloudstack-server:8080/client/api",
        APIKey:     "your-api-key",
        SecretKey:  "your-secret-key",
        Timeout:    30,
    }

    // 调用listVirtualMachines API
    response, err := client.ListVirtualMachines(gopherstack.ListVirtualMachinesOptions{
        ListAll: true, // 获取所有虚拟机
    })

    if err != nil {
        fmt.Printf("Error: %v\n", err)
        return
    }

    // 遍历所有虚拟机并打印IP和状态
    for _, vm := range response.Listvirtualmachinesresponse.VirtualMachine {
        fmt.Printf("VM Name: %s\n", vm.Name)
        fmt.Printf("State: %s\n", vm.State)
        
        // 获取主IP地址
        if len(vm.Nic) > 0 {
            fmt.Printf("IP Address: %s\n", vm.Nic[0].Ipaddress)
        }
        
        fmt.Println("---")
    }
}

需要确保在ListVirtualMachinesOptions中设置ListAll: true来获取所有虚拟机,而不仅仅是当前页面的结果。

如果你需要更多过滤条件,可以添加其他参数:

options := gopherstack.ListVirtualMachinesOptions{
    ListAll:  true,
    ZoneID:   "your-zone-id",      // 按区域过滤
    ProjectID: "your-project-id",  // 按项目过滤
    Account:  "your-account",      // 按账户过滤
}

这样就能获取所有虚拟机的IP地址和状态信息了。

回到顶部