Upclapi/inventory.go
2021-08-28 14:45:05 +01:00

132 lines
3.3 KiB
Go

package main
import (
"fmt"
// "log"
"os"
"github.com/UpCloudLtd/upcloud-go-api/upcloud"
"github.com/UpCloudLtd/upcloud-go-api/upcloud/request"
"github.com/UpCloudLtd/upcloud-go-api/upcloud/service"
)
func inventoryOneServer(s *service.Service, floatIP string, server upcloud.Server) (string, error) {
ansible_data := "ansible_port=22 ansible_python_interpreter=/usr/bin/python3 ansible_user=root"
info,err := s.GetServerDetails(&request.GetServerDetailsRequest{
UUID: server.UUID,
})
if err != nil {
fmt.Fprintf(os.Stderr, "Unable to get details from server %#v: %#v\n", server.UUID, err)
return "", err
}
public_ip :=""
private_ip :=""
vpn_ip :=""
host :=""
float := upcloud.False
output := ""
for _, ip := range info.Networking.Interfaces {
for _, addr := range ip.IPAddresses {
if addr.Address == floatIP {
continue
}
switch ip.Type {
case upcloud.NetworkTypePublic:
public_ip = addr.Address
float = addr.Floating
case upcloud.NetworkTypeUtility:
private_ip = addr.Address
case upcloud.NetworkTypePrivate:
vpn_ip = addr.Address
}
}
}
if vpn_ip != "" {
host = vpn_ip
} else {
host = public_ip
}
floating := "no"
if float == upcloud.True {
floating = "yes"
}
output += fmt.Sprintf("%s ansible_host=%s hostname=%s vpn_ip=%s pub_ip=%s private_ip=%s float=%s ",
server.Hostname,
host,
server.Hostname,
vpn_ip,
public_ip,
private_ip,
floating)
output += fmt.Sprintf(" uuid=%s %s\n",info.UUID,ansible_data)
return output, nil
}
func hostsOneServer(s *service.Service, usetype string, floatIP string, server upcloud.Server, target ServerConfig) (string, error) {
info,err := s.GetServerDetails(&request.GetServerDetailsRequest{
UUID: server.UUID,
})
if err != nil {
fmt.Fprintf(os.Stderr, "Unable to get details from server %#v: %#v\n", server.UUID, err)
return "", err
}
public_ip :=""
private_ip :=""
sdn_ip :=""
vpn_ip :=""
vpn_net :=""
float := upcloud.False
output := ""
for i := 0; i < len(info.Networking.Interfaces); i++ {
for _,addr := range info.Networking.Interfaces[i].IPAddresses {
if addr.Address == floatIP {
continue
}
switch info.Networking.Interfaces[i].Type {
case upcloud.NetworkTypePublic:
public_ip = addr.Address
float = addr.Floating
case upcloud.NetworkTypeUtility:
private_ip = addr.Address
case upcloud.NetworkTypePrivate:
sdn_ip = addr.Address
}
}
}
for i := 0; i < len(target.Networks); i++ {
if target.Networks[i].Access == "vpn" {
vpn_ip = target.Networks[i].IPAddress
vpn_net = target.Networks[i].Network
}
}
floating := "no"
if float == upcloud.True {
floating = "yes"
}
suffix := ""
output += fmt.Sprintf("# %s \t %s float=%s | %s\n", server.Hostname, server.UUID, floating, server.Title)
if public_ip != "" {
if usetype == "prv" {
suffix = ".pub"
}
output += fmt.Sprintf("%s \t %s%s \t# PUB\n", public_ip, server.Hostname, suffix)
}
if sdn_ip != "" {
if usetype == "pub" {
suffix = ".prv"
} else {
suffix = ""
}
output += fmt.Sprintf("%s \t %s%s \t# SDN\n", sdn_ip,server.Hostname, suffix)
}
if private_ip != "" {
output += fmt.Sprintf("%s \t %s.pv \t# PV\n", private_ip, server.Hostname)
}
if vpn_ip != "" {
output += fmt.Sprintf("%s \t %s.vpn \t# VPN /%s\n", vpn_ip, server.Hostname, vpn_net)
}
return output,nil
}