117 lines
2.7 KiB
Go
117 lines
2.7 KiB
Go
package client
|
|
|
|
import "fmt"
|
|
|
|
type Responses[T Message] struct {
|
|
Responses []Response[T]
|
|
StatusCode int
|
|
}
|
|
|
|
type Message interface {
|
|
GetMessage() string
|
|
}
|
|
|
|
func (r *Responses[T]) GetData() []T {
|
|
var data []T
|
|
for _, v := range r.Responses {
|
|
if v.HasData() {
|
|
data = append(data, v.Data)
|
|
}
|
|
}
|
|
return data
|
|
}
|
|
|
|
func (r *Responses[T]) GetMessages() []string {
|
|
var messages []string
|
|
for _, v := range r.Responses {
|
|
if v.IsMessage() {
|
|
messages = append(messages, v.Data.GetMessage())
|
|
}
|
|
}
|
|
return messages
|
|
}
|
|
|
|
func (r *Responses[T]) IsSuccessful() bool {
|
|
return r.StatusCode < 400 && r.StatusCode > 0
|
|
}
|
|
|
|
func (r *Responses[T]) Error() error {
|
|
if r.IsSuccessful() {
|
|
return nil
|
|
}
|
|
|
|
return fmt.Errorf("error with status code %v and messages %v", r.StatusCode, r.GetMessages())
|
|
}
|
|
|
|
type Response[T Message] struct {
|
|
Data T `json:"data"`
|
|
}
|
|
|
|
func (res *Response[T]) HasData() bool {
|
|
return !res.IsMessage()
|
|
}
|
|
|
|
func (res *Response[T]) IsMessage() bool {
|
|
return res.Data.GetMessage() != ""
|
|
}
|
|
|
|
type NewAppInstanceInput struct {
|
|
Region string `json:"region"`
|
|
AppInst AppInstance `json:"appinst"`
|
|
}
|
|
|
|
type msg struct {
|
|
Message string `json:"message"`
|
|
}
|
|
|
|
func (msg msg) GetMessage() string {
|
|
return msg.Message
|
|
}
|
|
|
|
type AppInstance struct {
|
|
msg `json:",inline"`
|
|
Key AppInstanceKey `json:"key"`
|
|
AppKey AppKey `json:"app_key,omitzero"`
|
|
Flavor Flavor `json:"flavor,omitzero"`
|
|
State string `json:"state,omitempty"`
|
|
PowerState string `json:"power_state,omitempty"`
|
|
}
|
|
|
|
type AppInstanceKey struct {
|
|
Organization string `json:"organization"`
|
|
Name string `json:"name"`
|
|
CloudletKey CloudletKey `json:"cloudlet_key"`
|
|
}
|
|
|
|
type CloudletKey struct {
|
|
Organization string `json:"organization"`
|
|
Name string `json:"name"`
|
|
}
|
|
|
|
type AppKey struct {
|
|
Organization string `json:"organization"`
|
|
Name string `json:"name,omitempty"`
|
|
Version string `json:"version,omitempty"`
|
|
}
|
|
|
|
type Flavor struct {
|
|
Name string `json:"name"`
|
|
}
|
|
|
|
type NewAppInput struct {
|
|
Region string `json:"region"`
|
|
App App `json:"app"`
|
|
}
|
|
|
|
type App struct {
|
|
msg `json:",inline"`
|
|
Key AppKey `json:"key"`
|
|
Deployment string `json:"deployment,omitempty"`
|
|
ImageType string `json:"image_type,omitempty"`
|
|
ImagePath string `json:"image_path,omitempty"`
|
|
AllowServerless bool `json:"allow_serverless,omitempty"`
|
|
DefaultFlavor Flavor `json:"defaultFlavor,omitempty"`
|
|
ServerlessConfig any `json:"serverless_config,omitempty"`
|
|
DeploymentGenerator string `json:"deployment_generator,omitempty"`
|
|
DeploymentManifest string `json:"deployment_manifest,omitempty"`
|
|
}
|