/
types.go
125 lines (107 loc) · 4.01 KB
/
types.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package container
import (
"context"
"encoding/json"
"fmt"
"io"
"github.com/docker/docker/api/types/mount"
"github.com/docker/docker/client"
)
// The PortBinding struct defines port bindings of a container.
// ExposedPort is the port in the container, while PortBinding is the port
// on the host.
type PortBinding struct {
ExposedPort uint16 `json:"exposedPort" binding:"required,gte=0,lte=65535"`
PortBinding uint16 `json:"portBinding" binding:"required,gte=0,lte=65535"`
}
// PortRange defines a range of ports from 0 to 65535.
type PortRange struct {
From uint16 `json:"from" binding:"required,gte=0,lte=65535"`
To uint16 `json:"to" binding:"required,gtefield=From,lte=65535"`
}
// PortRangeBinding defines port range bindings of a container.
// Internal is the port in the container, while External is the port
// on the host.
type PortRangeBinding struct {
Internal PortRange `json:"internal" binding:"required"`
External PortRange `json:"external" binding:"required"`
}
// RegistryAuth defines an image registry and the authentication information
// associated with it.
type RegistryAuth struct {
Name string `json:"name" binding:"required"`
URL string `json:"url" binding:"required"`
User string `json:"user" binding:"required"`
Password string `json:"password" binding:"required"`
}
// RestartPolicyName defines the restart policy used by a container.
type RestartPolicyName string
const (
EmptyRestartPolicy RestartPolicyName = ""
AlwaysRestartPolicy RestartPolicyName = "always"
RestartUnlessStoppedRestartPolicy RestartPolicyName = "unless-stopped"
NoRestartPolicy RestartPolicyName = "no"
OnFailureRestartPolicy RestartPolicyName = "on-failure"
)
// RestartPolicyUnmarshalInvalidError represents custom error regarding restart policy
type ErrRestartPolicyUnmarshalInvalid struct{}
func (e *ErrRestartPolicyUnmarshalInvalid) Error() string {
return "restart policy invalid value provided"
}
// policyToString static mapping enum type into the docker supported string values
var policyToString = map[RestartPolicyName]string{
EmptyRestartPolicy: "unless-stopped",
RestartUnlessStoppedRestartPolicy: "unless-stopped",
NoRestartPolicy: "no",
AlwaysRestartPolicy: "always",
OnFailureRestartPolicy: "on-failure",
}
// policyToID static mapping string values eg. from JSON into enums
var policyToID = map[string]RestartPolicyName{
"": RestartUnlessStoppedRestartPolicy,
"unless-stopped": RestartUnlessStoppedRestartPolicy,
"no": NoRestartPolicy,
"always": AlwaysRestartPolicy,
"on-failure": OnFailureRestartPolicy,
}
// custom enum marshal JSON interface implementation
func (policy RestartPolicyName) MarshalJSON() ([]byte, error) {
str, ok := policyToString[policy]
if !ok {
return nil, &ErrRestartPolicyUnmarshalInvalid{}
}
return []byte(fmt.Sprintf(`%q`, str)), nil
}
// custom enum unmarshal JSON interface implementation
func (policy *RestartPolicyName) UnmarshalJSON(b []byte) error {
var j string
err := json.Unmarshal(b, &j)
if err != nil {
return err
}
if _, ok := policyToID[j]; ok {
*policy = policyToID[j]
} else {
*policy = RestartPolicyName("")
err = &ErrRestartPolicyUnmarshalInvalid{}
}
return err
}
// ImagePullResponse is not explicit
type ImagePullResponse struct {
ID string `json:"id"`
Status string `json:"status"`
ProgressDetail struct {
Current int64 `json:"current"`
Total int64 `json:"total"`
} `json:"progressDetail"`
Progress string `json:"progress"`
}
// Hook function which can be used to add custom logic before and after events of the lifecycle of a container.
// 'containerId' can be nil depending on the hook.
type LifecycleFunc func(ctx context.Context, client *client.Client, containerName string,
containerId *string, mountList []mount.Mount, logger *io.StringWriter) error
// ContainerWaitResult with the status code from the container
type ContainerWaitResult struct {
StatusCode int64
}