<commit_before>package proxmox

import (
	"bytes"
	"crypto/tls"
	"encoding/json"
	"errors"
	"fmt"
	"io/ioutil"
	"math/rand"
	"net/http"
	"net/http/cookiejar"
	"net/url"
	"strconv"
	"strings"

	_ "github.com/bndr/gopencils"
	_ "github.com/davecgh/go-spew/spew"
)

type ProxMox struct {
	Hostname                      string
	Username                      string
	password                      string
	VerifySSL                     bool
	BaseURL                       string
	connectionCSRFPreventionToken string
	ConnectionTicket              string
	client                        *http.Client
}

func NewProxMox(HostName string, UserName string, Password string) (*ProxMox, error) {
	var err error
	var proxmox *ProxMox
	var data map[string]interface{}
	var form url.Values
	var cookies []*http.Cookie
	var testcookies []*http.Cookie
	var tr *http.Transport
	var domain string
	//fmt.Println("!NewProxMox")

	if !strings.HasSuffix(UserName, "@pam") && !strings.HasSuffix(UserName, "@pve") {
		UserName = UserName + "@pam"
	}

	proxmox = new(ProxMox)
	proxmox.Hostname = HostName
	proxmox.Username = UserName
	proxmox.password = Password
	proxmox.VerifySSL = false
	proxmox.BaseURL = "https://" + proxmox.Hostname + ":8006/api2/json/"

	if proxmox.VerifySSL {
		tr = &http.Transport{}
	} else {
		tr = &http.Transport{
			TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
		}
	}
	proxmox.client = &http.Client{Transport: tr}
	form = url.Values{
		"username": {proxmox.Username},
		"password": {<PASSWORD>},
	}

	data, err = proxmox.PostForm("access/ticket", form)
	if err != nil {
		return nil, err
	} else {
		proxmox.connectionCSRFPreventionToken = data["CSRFPreventionToken"].(string)
		proxmox.ConnectionTicket = data["ticket"].(string)
		proxmox.client.Jar, err = cookiejar.New(nil)
		domain = proxmox.Hostname
		cookie := &http.Cookie{
			Name:   "PVEAuthCookie",
			Value:  data["ticket"].(string),
			Path:   "/",
			Domain: domain,
		}
		cookies = append(cookies, cookie)
		cookie = &http.Cookie{
			Name:   "CSRFPreventionToken",
			Value:  data["CSRFPreventionToken"].(string),
			Path:   "/",
			Domain: domain,
		}
		cookies = append(cookies, cookie)
		cookieURL, err := url.Parse("https://" + domain + "/")
		if err != nil {
			return nil, err
		}
		proxmox.client.Jar.SetCookies(cookieURL, cookies)

		domain = "requestb.in"
		cookie = &http.Cookie{
			Name:   "PVEAuthCookie",
			Value:  data["ticket"].(string),
			Path:   "/",
			Domain: domain,
		}
		testcookies = append(testcookies, cookie)
		cookie = &http.Cookie{
			Name:   "CSRFPreventionToken",
			Value:  data["CSRFPreventionToken"].(string),
			Path:   "/",
			Domain: domain,
		}
		testcookies = append(testcookies, cookie)
		cookieURL, err = url.Parse("https://" + domain + "/")
		if err != nil {
			return nil, err
		}
		proxmox.client.Jar.SetCookies(cookieURL, testcookies)

		return proxmox, nil
	}
}

func (proxmox ProxMox) Nodes() (NodeList, error) {
	var err error
	var data map[string]interface{}
	var list NodeList
	var node Node
	var results []interface{}

	//fmt.Println("!Nodes")

	data, err = proxmox.Get("nodes")
	if err != nil {
		return nil, err
	}
	list = make(NodeList)
	results = data["data"].([]interface{})
	for _, v0 := range results {
		v := v0.(map[string]interface{})
		node = Node{
			Mem:      v["mem"].(float64),
			MaxDisk:  v["maxdisk"].(float64),
			Node:     v["node"].(string),
			MaxCPU:   v["maxcpu"].(float64),
			Uptime:   v["uptime"].(float64),
			Id:       v["id"].(string),
			CPU:      v["cpu"].(float64),
			Level:    v["level"].(string),
			NodeType: v["type"].(string),
			Disk:     v["disk"].(float64),
			MaxMem:   v["maxmem"].(float64),
			Proxmox:  proxmox,
		}
		list[node.Node] = node
	}
	return list, nil
}

func (proxmox ProxMox) maxVMId() (float64, error) {
	var id float64
	var maxId float64
	var err error
	var nodes NodeList
	var node Node

	//fmt.Println("!maxVMId")
	maxId = 0

	nodes, err = proxmox.Nodes()
	if err != nil {
		return 0, err
	}
	for _, node = range nodes {
		id, err = node.MaxQemuId()
		if err != nil {<commit_msg>don't crash when node is offline<commit_after>package proxmox

import (
	"bytes"
	"crypto/tls"
	"encoding/json"
	"errors"
	"fmt"
	"io/ioutil"
	"math/rand"
	"net/http"
	"net/http/cookiejar"
	"net/url"
	"strconv"
	"strings"

	_ "github.com/bndr/gopencils"
	_ "github.com/davecgh/go-spew/spew"
)

type ProxMox struct {
	Hostname                      string
	Username                      string
	password                      string
	VerifySSL                     bool
	BaseURL                       string
	connectionCSRFPreventionToken string
	ConnectionTicket              string
	client                        *http.Client
}

func NewProxMox(HostName string, UserName string, Password string) (*ProxMox, error) {
	var err error
	var proxmox *ProxMox
	var data map[string]interface{}
	var form url.Values
	var cookies []*http.Cookie
	var testcookies []*http.Cookie
	var tr *http.Transport
	var domain string
	//fmt.Println("!NewProxMox")

	if !strings.HasSuffix(UserName, "@pam") && !strings.HasSuffix(UserName, "@pve") {
		UserName = UserName + "@pam"
	}

	proxmox = new(ProxMox)
	proxmox.Hostname = HostName
	proxmox.Username = UserName
	proxmox.password = Password
	proxmox.VerifySSL = false
	proxmox.BaseURL = "https://" + proxmox.Hostname + ":8006/api2/json/"

	if proxmox.VerifySSL {
		tr = &http.Transport{}
	} else {
		tr = &http.Transport{
			TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
		}
	}
	proxmox.client = &http.Client{Transport: tr}
	form = url.Values{
		"username": {proxmox.Username},
		"password": {<PASSWORD>},
	}

	data, err = proxmox.PostForm("access/ticket", form)
	if err != nil {
		return nil, err
	} else {
		proxmox.connectionCSRFPreventionToken = data["CSRFPreventionToken"].(string)
		proxmox.ConnectionTicket = data["ticket"].(string)
		proxmox.client.Jar, err = cookiejar.New(nil)
		domain = proxmox.Hostname
		cookie := &http.Cookie{
			Name:   "PVEAuthCookie",
			Value:  data["ticket"].(string),
			Path:   "/",
			Domain: domain,
		}
		cookies = append(cookies, cookie)
		cookie = &http.Cookie{
			Name:   "CSRFPreventionToken",
			Value:  data["CSRFPreventionToken"].(string),
			Path:   "/",
			Domain: domain,
		}
		cookies = append(cookies, cookie)
		cookieURL, err := url.Parse("https://" + domain + "/")
		if err != nil {
			return nil, err
		}
		proxmox.client.Jar.SetCookies(cookieURL, cookies)

		domain = "requestb.in"
		cookie = &http.Cookie{
			Name:   "PVEAuthCookie",
			Value:  data["ticket"].(string),
			Path:   "/",
			Domain: domain,
		}
		testcookies = append(testcookies, cookie)
		cookie = &http.Cookie{
			Name:   "CSRFPreventionToken",
			Value:  data["CSRFPreventionToken"].(string),
			Path:   "/",
			Domain: domain,
		}
		testcookies = append(testcookies, cookie)
		cookieURL, err = url.Parse("https://" + domain + "/")
		if err != nil {
			return nil, err
		}
		proxmox.client.Jar.SetCookies(cookieURL, testcookies)

		return proxmox, nil
	}
}

func (proxmox ProxMox) Nodes() (NodeList, error) {
	var err error
	var data map[string]interface{}
	var list NodeList
	var node Node
	var results []interface{}

	//fmt.Println("!Nodes")

	data, err = proxmox.Get("nodes")
	if err != nil {
		return nil, err
	}
	list = make(NodeList)
	results = data["data"].([]interface{})
	for _, v0 := range results {
		v := v0.(map[string]interface{})
		Block{
			Try: func() {
				node = Node{
					Mem:      v["mem"].(float64),
					MaxDisk:  v["maxdisk"].(float64),
					Node:     v["node"].(string),
					MaxCPU:   v["maxcpu"].(float64),
					Uptime:   v["uptime"].(float64),
					Id:       v["id"].(string),
					CPU:      v["cpu"].(float64),
					Level:    v["level"].(string),
					NodeType: v["type"].(string),
					Disk:     v["disk"].(float64),
					MaxMem:   v["maxmem"].(float64),
					Proxmox:  proxmox,
				}
			},
			Catch: func(e Exception){},
		}.Do()
		list[node.Node] = node
	}
	return list, nil
}

func (proxmox ProxMox) maxVMId() (float64, error) {
	var id float64
	var maxId float64
	var err error
	var nodes NodeList
	var node Node

	//fmt.Println("!maxVMId")
	maxId = 0

	nodes, err = proxmox.Nodes()
	if err != nil {
		return 0, err
	}
	for _, node = range nodes {
		id, err = node.MaxQemuId()
		if err != nil {
{'fraction_non_alphanumeric': 0.15172413793103448, 'fraction_numerical': 0.007017543859649123, 'mean_word_length': 3.0659124446630597, 'pattern_counts': {'":': 6, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 6, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3766966', 'n_tokens_mistral': 3417, 'n_tokens_neox': 3094, 'n_words': 658}