-
Notifications
You must be signed in to change notification settings - Fork 944
/
Copy pathroute_resource.go
138 lines (120 loc) · 3.39 KB
/
route_resource.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
126
127
128
129
130
131
132
133
134
135
136
137
138
package resources
import (
"encoding/json"
"strings"
"code.cloudfoundry.org/cli/api/cloudcontroller"
)
type RouteDestinationApp struct {
GUID string
Process struct {
Type string
}
}
type RouteDestination struct {
GUID string
App RouteDestinationApp
Port int
Protocol string
}
type Route struct {
GUID string
SpaceGUID string
DomainGUID string
Host string
Path string
Protocol string
Port int
URL string
Destinations []RouteDestination
Metadata *Metadata
Options map[string]*string
}
func (r Route) MarshalJSON() ([]byte, error) {
type Data struct {
GUID string `json:"guid,omitempty"`
}
type RelationshipData struct {
Data Data `json:"data,omitempty"`
}
type Relationships struct {
Space RelationshipData `json:"space,omitempty"`
Domain RelationshipData `json:"domain,omitempty"`
}
// Building up the request body in ccRoute
type ccRoute struct {
GUID string `json:"guid,omitempty"`
Host string `json:"host,omitempty"`
Path string `json:"path,omitempty"`
Protocol string `json:"protocol,omitempty"`
Port int `json:"port,omitempty"`
Relationships *Relationships `json:"relationships,omitempty"`
Options map[string]*string `json:"options,omitempty"`
}
ccR := ccRoute{
GUID: r.GUID,
Host: r.Host,
Path: r.Path,
Protocol: r.Protocol,
Port: r.Port,
Options: r.Options,
}
if r.SpaceGUID != "" {
ccR.Relationships = &Relationships{
Space: RelationshipData{Data{GUID: r.SpaceGUID}},
Domain: RelationshipData{Data{GUID: r.DomainGUID}},
}
}
return json.Marshal(ccR)
}
func (r *Route) UnmarshalJSON(data []byte) error {
var alias struct {
GUID string `json:"guid,omitempty"`
Protocol string `json:"protocol,omitempty"`
Host string `json:"host,omitempty"`
Path string `json:"path,omitempty"`
Port int `json:"port,omitempty"`
URL string `json:"url,omitempty"`
Destinations []RouteDestination `json:"destinations,omitempty"`
Metadata *Metadata `json:"metadata,omitempty"`
Options map[string]*string `json:"options,omitempty"`
Relationships struct {
Space struct {
Data struct {
GUID string `json:"guid,omitempty"`
} `json:"data,omitempty"`
} `json:"space,omitempty"`
Domain struct {
Data struct {
GUID string `json:"guid,omitempty"`
} `json:"data,omitempty"`
} `json:"domain,omitempty"`
} `json:"relationships,omitempty"`
}
err := cloudcontroller.DecodeJSON(data, &alias)
if err != nil {
return err
}
r.GUID = alias.GUID
r.Protocol = alias.Protocol
r.Host = alias.Host
r.SpaceGUID = alias.Relationships.Space.Data.GUID
r.DomainGUID = alias.Relationships.Domain.Data.GUID
r.Path = alias.Path
r.Port = alias.Port
r.URL = alias.URL
r.Destinations = alias.Destinations
r.Metadata = alias.Metadata
r.Options = alias.Options
return nil
}
func (r *Route) FormattedOptions() string {
var routeOpts = []string{}
formattedOptions := ""
if r.Options != nil && len(r.Options) > 0 {
for optKey, optVal := range r.Options {
routeOpts = append(routeOpts, optKey+"="+*optVal)
}
formattedOptions = " {" + strings.Join(routeOpts, ", ") + "}"
}
return formattedOptions
}