diff --git a/domains.go b/domains.go index e1d3421..8eb51ef 100644 --- a/domains.go +++ b/domains.go @@ -89,3 +89,68 @@ func FindDomains(token string, query string) ([]MarketDomain, error) { return response.Domains, nil } + +// Checks the task status given a task id +func CheckTask(token string, id string) (string, error) { + params := map[string]interface{}{ + "id": id, + } + + data, err := Request(token, "check-task", params) + if err != nil { + return "", err + } + + type Response struct { + id string `json:"id"` + status string `json:"status"` + } + + var response Response + err = json.Unmarshal(data, &response) + if err != nil { + return "", err + } + + return response.status, nil +} + +// Registers a domain given a domain name and desired term length +func RegisterDomain(token string, domain string, years int) (error) { + params := map[string]interface{}{ + "domain": domain, + "years": years, + } + + data, err := Request(token, "register-domain", params) + if err != nil { + return err + } + + type Response struct { + task string `json:"task"` + } + + var response Response + err = json.Unmarshal(data, &response) + if err != nil { + return err + } + + var status string + for true { + status, err = CheckTask(token, response.task) + if err != nil { + return err + } + + if status == "active" { + break + } + + time.Sleep(5 * time.Second) + } + + return nil +} + diff --git a/servers.go b/servers.go new file mode 100644 index 0000000..fa37ec7 --- /dev/null +++ b/servers.go @@ -0,0 +1,219 @@ +package gonjalla + +import ( + "encoding/json" +) + +// Server struct contains data returned by api calls that deal with server state +type Server struct { + Name string `json:"name"` + Type string `json:"type"` + ID string `json:"id"` + Status string `json:"status"` + Os string `json:"os"` + Expiry string `json:"expiry"` + Autorenew bool `json:"autorenew"` + SSHKey string `json:"ssh_key"` + Ips []string `json:"ips"` + ReverseName string `json:"reverse_name"` + OsState string `json:"os_state"` +} + +// ListServers returns a listing of all servers for a given account +func ListServers(token string) ([]Server, error) { + params := map[string]interface{}{} + + data, err := Request(token, "list-servers", params) + if err != nil { + return nil, err + } + + type Response struct { + Servers []Server `json:"servers"` + } + + var response Response + err = json.Unmarshal(data, &response) + if err != nil { + return nil, err + } + + return response.Servers, nil +} + +// ListServerImages returns a listing of the avaliable server images +func ListServerImages(token string) ([]string, error) { + params := map[string]interface{}{} + + data, err := Request(token, "list-server-images", params) + if err != nil { + return nil, err + } + + type Response struct { + Images []string `json:"images"` + } + + var response Response + err = json.Unmarshal(data, &response) + if err != nil { + return nil, err + } + + return response.Images, nil +} + +// ListServerTypes returns a listing of the avaliable server types +func ListServerTypes(token string) ([]string, error) { + params := map[string]interface{}{} + + data, err := Request(token, "list-server-types", params) + if err != nil { + return nil, err + } + + type Response struct { + Types []string `json:"types"` + } + + var response Response + err = json.Unmarshal(data, &response) + if err != nil { + return nil, err + } + + return response.Types, nil +} + +// StopServer stops a server from running. Server data will not be destroyed. +func StopServer(token string, id string) (Server, error) { + params := map[string]interface{}{ + "id": id, + } + + var server Server + + data, err := Request(token, "stop-server", params) + if err != nil { + return server, err + } + + err = json.Unmarshal(data, &server) + if err != nil { + return server, err + } + + return server, nil +} + +// StartServer starts a server +func StartServer(token string, id string) (Server, error) { + params := map[string]interface{}{ + "id": id, + } + + var server Server + + data, err := Request(token, "start-server", params) + if err != nil { + return server, err + } + + err = json.Unmarshal(data, &server) + if err != nil { + return server, err + } + + return server, nil +} + +// RestartServer restarts a server +func RestartServer(token string, id string) (Server, error) { + params := map[string]interface{}{ + "id": id, + } + + var server Server + + data, err := Request(token, "restart-server", params) + if err != nil { + return server, err + } + + err = json.Unmarshal(data, &server) + if err != nil { + return server, err + } + + return server, nil +} + +// ResetServer resets a server with new server settings. Server data WILL be destroyed +func ResetServer(token string, id string, os string, publicKey string, instanceType string) (Server, error) { + params := map[string]interface{}{ + "id": id, + "os": os, + "ssh_key": publicKey, + "type": instanceType, + } + + var server Server + + data, err := Request(token, "reset-server", params) + if err != nil { + return server, err + } + + err = json.Unmarshal(data, &server) + if err != nil { + return server, err + } + + return server, nil +} + +// AddServer creates a new server. +func AddServer(token string, name string, instanceType string, os string, publicKey string, months int) (Server, error) { + params := map[string]interface{}{ + "name": name, + "type": instanceType, + "os": os, + "ssh_key": publicKey, + "months": months, + } + + var server Server + + data, err := Request(token, "add-server", params) + if err != nil { + return server, err + } + + err = json.Unmarshal(data, &server) + if err != nil { + return server, err + } + + return server, nil +} + +// RemoveServer removes a server. Server data WILL be destroyed. +func RemoveServer(token string, id string) (Server, error) { + params := map[string]interface{}{ + "id": id, + } + + var server Server + + data, err := Request(token, "remove-server", params) + if err != nil { + return server, err + } + + err = json.Unmarshal(data, &server) + if err != nil { + return server, err + } + + return server, nil +}