forked from cloudfoundry/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroute_resource.go
120 lines (103 loc) · 2.83 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
package resources
import (
"encoding/json"
"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
}
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"`
}
ccR := ccRoute{
GUID: r.GUID,
Host: r.Host,
Path: r.Path,
Protocol: r.Protocol,
Port: r.Port,
}
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"`
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
return nil
}