mirror of
https://github.com/rancher/os.git
synced 2025-09-03 07:44:21 +00:00
Vendor changes, github.com/packethost/packngo v0.1.0
This commit is contained in:
37
vendor/github.com/packethost/packngo/metadata/client.go
generated
vendored
37
vendor/github.com/packethost/packngo/metadata/client.go
generated
vendored
@@ -1,37 +0,0 @@
|
||||
package metadata
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"net/url"
|
||||
|
||||
"github.com/packethost/packngo"
|
||||
)
|
||||
|
||||
const (
|
||||
baseUrl = "https://metadata.packet.net"
|
||||
)
|
||||
|
||||
type Client struct {
|
||||
client *packngo.Client
|
||||
|
||||
Metadata MetadataService
|
||||
Userdata UserdataService
|
||||
}
|
||||
|
||||
type MetadataService interface {
|
||||
Get() (Metadata, error)
|
||||
}
|
||||
|
||||
type UserdataService interface {
|
||||
Get() (string, error)
|
||||
}
|
||||
|
||||
func NewClient(httpClient *http.Client) *Client {
|
||||
c := packngo.NewClient("", "", httpClient)
|
||||
c.BaseURL, _ = url.Parse(baseUrl)
|
||||
return &Client{
|
||||
client: c,
|
||||
Metadata: &MetadataServiceOp{client: c},
|
||||
Userdata: &UserdataServiceOp{client: c},
|
||||
}
|
||||
}
|
187
vendor/github.com/packethost/packngo/metadata/metadata.go
generated
vendored
187
vendor/github.com/packethost/packngo/metadata/metadata.go
generated
vendored
@@ -1,73 +1,156 @@
|
||||
package metadata
|
||||
|
||||
import (
|
||||
"github.com/packethost/packngo"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
const BaseURL = "https://metadata.packet.net"
|
||||
|
||||
func GetMetadata() (*CurrentDevice, error) {
|
||||
res, err := http.Get(BaseURL + "/metadata")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
b, err := ioutil.ReadAll(res.Body)
|
||||
res.Body.Close()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var result struct {
|
||||
Error string `json:"error"`
|
||||
*CurrentDevice
|
||||
}
|
||||
if err := json.Unmarshal(b, &result); err != nil {
|
||||
if res.StatusCode >= 400 {
|
||||
return nil, errors.New(res.Status)
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
if result.Error != "" {
|
||||
return nil, errors.New(result.Error)
|
||||
}
|
||||
return result.CurrentDevice, nil
|
||||
}
|
||||
|
||||
func GetUserData() ([]byte, error) {
|
||||
res, err := http.Get(BaseURL + "/userdata")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
b, err := ioutil.ReadAll(res.Body)
|
||||
res.Body.Close()
|
||||
return b, err
|
||||
}
|
||||
|
||||
type AddressFamily int
|
||||
|
||||
const (
|
||||
metadataBasePath = "/metadata"
|
||||
IPv4 = AddressFamily(4)
|
||||
IPv6 = AddressFamily(6)
|
||||
)
|
||||
|
||||
type Metadata struct {
|
||||
PhoneHomeURL string `json:"phone_home_url"`
|
||||
ApiUrl string `json:"api_url"`
|
||||
Id string `json:"id"`
|
||||
Hostname string `json:"hostname"`
|
||||
Iqn string `json:"iqn"`
|
||||
OperatingSystem OperatingSystem `json:"operating_system"`
|
||||
Plan string `json:"plan"`
|
||||
Facility string `json:"facility"`
|
||||
SshKeys []string `json:"ssh_keys"`
|
||||
Network Network `json:"network"`
|
||||
type AddressInfo struct {
|
||||
ID string `json:"id"`
|
||||
Family AddressFamily `json:"address_family"`
|
||||
Public bool `json:"public"`
|
||||
Management bool `json:"management"`
|
||||
Address net.IP `json:"address"`
|
||||
NetworkMask net.IP `json:"netmask"`
|
||||
Gateway net.IP `json:"gateway"`
|
||||
NetworkBits int `json:"cidr"`
|
||||
|
||||
// These are available, but not really needed:
|
||||
// Network net.IP `json:"network"`
|
||||
}
|
||||
|
||||
type Network struct {
|
||||
Addresses []Address `json:"addresses"`
|
||||
Interfaces []Interface `json:"interfaces"`
|
||||
type BondingMode int
|
||||
|
||||
const (
|
||||
BondingBalanceRR = BondingMode(0)
|
||||
BondingActiveBackup = BondingMode(1)
|
||||
BondingBalanceXOR = BondingMode(2)
|
||||
BondingBroadcast = BondingMode(3)
|
||||
BondingLACP = BondingMode(4)
|
||||
BondingBalanceTLB = BondingMode(5)
|
||||
BondingBalanceALB = BondingMode(6)
|
||||
)
|
||||
|
||||
var bondingModeStrings = map[BondingMode]string{
|
||||
BondingBalanceRR: "balance-rr",
|
||||
BondingActiveBackup: "active-backup",
|
||||
BondingBalanceXOR: "balance-xor",
|
||||
BondingBroadcast: "broadcast",
|
||||
BondingLACP: "802.3ad",
|
||||
BondingBalanceTLB: "balance-tlb",
|
||||
BondingBalanceALB: "balance-alb",
|
||||
}
|
||||
|
||||
type Address struct {
|
||||
Href string `json:"href"`
|
||||
Gateway string `json:"gateway"`
|
||||
Address string `json:"address"`
|
||||
Network string `json:"network"`
|
||||
Id string `json:"id"`
|
||||
AddressFamily int `json:"address_family"`
|
||||
Netmask string `json:"netmask"`
|
||||
Public bool `json:"public"`
|
||||
Cidr int `json:"cidr"`
|
||||
Management bool `json:"management"`
|
||||
Manageable bool `json:"manageable"`
|
||||
AssignedTo Reference `json:"assigned_to"`
|
||||
func (m BondingMode) String() string {
|
||||
if str, ok := bondingModeStrings[m]; ok {
|
||||
return str
|
||||
}
|
||||
return fmt.Sprintf("%d", m)
|
||||
}
|
||||
|
||||
type Reference struct {
|
||||
Href string `json:"href"`
|
||||
type CurrentDevice struct {
|
||||
ID string `json:"id"`
|
||||
Hostname string `json:"hostname"`
|
||||
IQN string `json:"iqn"`
|
||||
Plan string `json:"plan"`
|
||||
Facility string `json:"facility"`
|
||||
Tags []string `json:"tags"`
|
||||
SSHKeys []string `json:"ssh_keys"`
|
||||
OS OperatingSystem `json:"operating_system"`
|
||||
Network NetworkInfo `json:"network"`
|
||||
Volumes []VolumeInfo `json:"volume"`
|
||||
|
||||
// This is available, but is actually inaccurate, currently:
|
||||
// APIBaseURL string `json:"api_url"`
|
||||
}
|
||||
|
||||
type InterfaceInfo struct {
|
||||
Name string `json:"name"`
|
||||
MAC string `json:"mac"`
|
||||
}
|
||||
|
||||
func (i *InterfaceInfo) ParseMAC() (net.HardwareAddr, error) {
|
||||
return net.ParseMAC(i.MAC)
|
||||
}
|
||||
|
||||
type NetworkInfo struct {
|
||||
Interfaces []InterfaceInfo `json:"interfaces"`
|
||||
Addresses []AddressInfo `json:"addresses"`
|
||||
|
||||
Bonding struct {
|
||||
Mode BondingMode `json:"mode"`
|
||||
} `json:"bonding"`
|
||||
}
|
||||
|
||||
func (n *NetworkInfo) BondingMode() BondingMode {
|
||||
return n.Bonding.Mode
|
||||
}
|
||||
|
||||
type OperatingSystem struct {
|
||||
Version string `json:"version"`
|
||||
Distro string `json:"distro"`
|
||||
Slug string `json:"slug"`
|
||||
Distro string `json:"distro"`
|
||||
Version string `json:"version"`
|
||||
}
|
||||
|
||||
type Interface struct {
|
||||
Mac string `json:"mac"`
|
||||
Name string `json:"name"`
|
||||
}
|
||||
|
||||
type MetadataServiceOp struct {
|
||||
client *packngo.Client
|
||||
}
|
||||
|
||||
func (s *MetadataServiceOp) Get() (Metadata, error) {
|
||||
metadata := Metadata{}
|
||||
|
||||
req, err := s.client.NewRequest("GET", metadataBasePath, nil)
|
||||
if err != nil {
|
||||
return metadata, err
|
||||
}
|
||||
|
||||
_, err = s.client.Do(req, &metadata)
|
||||
return metadata, err
|
||||
type VolumeInfo struct {
|
||||
Name string `json:"name"`
|
||||
IQN string `json:"iqn"`
|
||||
IPs []net.IP `json:"ips"`
|
||||
|
||||
Capacity struct {
|
||||
Size int `json:"size,string"`
|
||||
Unit string `json:"unit"`
|
||||
} `json:"capacity"`
|
||||
}
|
||||
|
26
vendor/github.com/packethost/packngo/metadata/userdata.go
generated
vendored
26
vendor/github.com/packethost/packngo/metadata/userdata.go
generated
vendored
@@ -1,26 +0,0 @@
|
||||
package metadata
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
|
||||
"github.com/packethost/packngo"
|
||||
)
|
||||
|
||||
const (
|
||||
userdataBasePath = "/userdata"
|
||||
)
|
||||
|
||||
type UserdataServiceOp struct {
|
||||
client *packngo.Client
|
||||
}
|
||||
|
||||
func (s *UserdataServiceOp) Get() (string, error) {
|
||||
req, err := s.client.NewRequest("GET", userdataBasePath, nil)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
buffer := &bytes.Buffer{}
|
||||
_, err = s.client.Do(req, buffer)
|
||||
return buffer.String(), err
|
||||
}
|
Reference in New Issue
Block a user