Example #1
0
func (os *OpenStack) serverToInstance(server *servers.Server) *compute.Instance {
	instance := &compute.Instance{
		ID:     server.ID,
		Name:   server.Name,
		Status: os.mapInstanceStatus(server.Status),
		IP:     server.AccessIPv4,
	}

	servers.ListAddresses(os.ComputeClient, server.ID).EachPage(func(page pagination.Page) (bool, error) {
		addresses, err := servers.ExtractAddresses(page)
		if err != nil {
			return false, err
		}

		for _, networkAddresses := range addresses {
			for _, addr := range networkAddresses {
				if utils.IsPrivate(addr.Address) {
					if instance.PrivateIP == "" || (utils.GetIPVersion(addr.Address) == 4 && utils.GetIPVersion(instance.PrivateIP) != 4) {
						instance.PrivateIP = addr.Address
					}
				} else if instance.IP == "" || (utils.GetIPVersion(addr.Address) == 4 && utils.GetIPVersion(instance.IP) != 4) {
					instance.IP = addr.Address
				}
			}
		}
		return true, nil
	})

	return instance
}
Example #2
0
func (this *OpenStack) VmInfo(vm *lobster.VirtualMachine) (*lobster.VmInfo, error) {
	server, err := servers.Get(this.ComputeClient, vm.Identification).Extract()
	if err != nil {
		return nil, err
	}

	status := server.Status
	if status == "ACTIVE" {
		status = "Online"
	} else if status == "SHUTOFF" {
		status = "Offline"
	}

	info := lobster.VmInfo{
		Status:       status,
		Hostname:     server.Name,
		LoginDetails: "password: "******"password", "unknown"),
	}

	servers.ListAddresses(this.ComputeClient, vm.Identification).EachPage(func(page pagination.Page) (bool, error) {
		addresses, err := servers.ExtractAddresses(page)
		if err != nil {
			return false, err
		}

		for _, networkAddresses := range addresses {
			for _, addr := range networkAddresses {
				if ipaddr.IsPrivate(addr.Address) {
					info.PrivateIp = addr.Address
				} else {
					info.Ip = addr.Address
				}
			}
		}
		return true, nil
	})

	return &info, nil
}