This repository has been archived by the owner on Oct 12, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservice.go
108 lines (91 loc) · 2.45 KB
/
service.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package arukas
import (
"encoding/json"
"strconv"
"strings"
"time"
)
type PortMapping struct {
ContainerPort int `json:"container_port"`
ServicePort int `json:"service_port"`
Host string `json:"host"`
}
type TaskPorts []PortMapping
type PortMappings []TaskPorts
type Env struct {
Key string `json:"key"`
Value string `json:"value"`
}
type Environment []*Env
type Port struct {
Protocol string `json:"protocol"`
Number int `json:"number"`
}
func (port *Port) MarshalJSON() ([]byte, error) {
str := strconv.Itoa(port.Number)
if port.Protocol == "udp" {
str += "/udp"
}
return []byte(`"` + str + `"`), nil
}
type Ports []*Port
func (ports *Ports) UnmarshalJSON(data []byte) error {
np := new(APIPortFormat)
var err error
if err = json.Unmarshal(data, np); err != nil {
return err
}
if *ports, err = np.toPorts(); err != nil {
return err
}
return nil
}
func parsePortFormat(str string) (*Port, error) {
var protocol string
var parsedInt int64
var number int
var err error
splitted := strings.Split(str, "/")
if len(splitted) <= 1 {
protocol = "tcp"
} else {
protocol = splitted[1]
}
if parsedInt, err = strconv.ParseInt(splitted[0], 10, 32); err != nil {
return nil, err
}
number = int(parsedInt)
return &Port{Protocol: protocol, Number: number}, nil
}
type APIPortFormat []string
func (pf APIPortFormat) toPorts() (Ports, error) {
ports := make(Ports, 0)
for _, p := range pf {
var (
parsedPort *Port
err error
)
if parsedPort, err = parsePortFormat(p); err != nil {
return nil, err
}
ports = append(ports, parsedPort)
}
return ports, nil
}
type Service struct {
ClientID string `jsonapi:"lid"`
Command string `jsonapi:"attr,command"`
Endpoint string `jsonapi:"attr,endpoint,readonly"`
Environment Environment `jsonapi:"attr,environment"`
ID string `jsonapi:"primary,services"`
Image string `jsonapi:"attr,image"`
Instances int `jsonapi:"attr,instances"`
Ports Ports `jsonapi:"attr,ports"`
PortMappings PortMappings `jsonapi:"attr,port-mappings,readonly"`
ServicePlan *ServicePlan `jsonapi:"relation,service-plan"`
Subdomain string `jsonapi:"attr,subdomain"`
Status string `jsonapi:"attr,status,readonly"`
CreatedAt time.Time `jsonapi:"attr,created-at,iso8601,readonly"`
UpdatedAt time.Time `jsonapi:"attr,updated-at,iso8601,readonly"`
}
type Services []*Service