forked from cloudfoundry/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpackage_resource.go
105 lines (88 loc) · 3.1 KB
/
package_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
package resources
import (
"encoding/json"
"code.cloudfoundry.org/cli/api/cloudcontroller"
"code.cloudfoundry.org/cli/api/cloudcontroller/ccv3/constant"
)
// Package represents a Cloud Controller V3 Package.
type Package struct {
// CreatedAt is the time with zone when the object was created.
CreatedAt string
// DockerImage is the registry address of the docker image.
DockerImage string
// DockerPassword is the password for the docker image's registry.
DockerPassword string
// DockerUsername is the username for the docker image's registry.
DockerUsername string
// GUID is the unique identifier of the package.
GUID string
// Links are links to related resources.
Links APILinks
// Relationships are a list of relationships to other resources.
Relationships Relationships
// State is the state of the package.
State constant.PackageState
// Type is the package type.
Type constant.PackageType
}
// MarshalJSON converts a Package into a Cloud Controller Package.
func (p Package) MarshalJSON() ([]byte, error) {
type ccPackageData struct {
Image string `json:"image,omitempty"`
Username string `json:"username,omitempty"`
Password string `json:"password,omitempty"`
}
var ccPackage struct {
GUID string `json:"guid,omitempty"`
CreatedAt string `json:"created_at,omitempty"`
Links APILinks `json:"links,omitempty"`
Relationships Relationships `json:"relationships,omitempty"`
State constant.PackageState `json:"state,omitempty"`
Type constant.PackageType `json:"type,omitempty"`
Data *ccPackageData `json:"data,omitempty"`
}
ccPackage.GUID = p.GUID
ccPackage.CreatedAt = p.CreatedAt
ccPackage.Links = p.Links
ccPackage.Relationships = p.Relationships
ccPackage.State = p.State
ccPackage.Type = p.Type
if p.DockerImage != "" {
ccPackage.Data = &ccPackageData{
Image: p.DockerImage,
Username: p.DockerUsername,
Password: p.DockerPassword,
}
}
return json.Marshal(ccPackage)
}
// UnmarshalJSON helps unmarshal a Cloud Controller Package response.
func (p *Package) UnmarshalJSON(data []byte) error {
var ccPackage struct {
GUID string `json:"guid,omitempty"`
CreatedAt string `json:"created_at,omitempty"`
Links APILinks `json:"links,omitempty"`
Relationships Relationships `json:"relationships,omitempty"`
State constant.PackageState `json:"state,omitempty"`
Type constant.PackageType `json:"type,omitempty"`
Data struct {
Image string `json:"image"`
Username string `json:"username"`
Password string `json:"password"`
} `json:"data"`
}
err := cloudcontroller.DecodeJSON(data, &ccPackage)
if err != nil {
return err
}
p.GUID = ccPackage.GUID
p.CreatedAt = ccPackage.CreatedAt
p.Links = ccPackage.Links
p.Relationships = ccPackage.Relationships
p.State = ccPackage.State
p.Type = ccPackage.Type
p.DockerImage = ccPackage.Data.Image
p.DockerUsername = ccPackage.Data.Username
p.DockerPassword = ccPackage.Data.Password
return nil
}