github.com/cloudfoundry-community/cloudfoundry-cli@v6.44.1-0.20240130060226-cda5ed8e89a5+incompatible/resources/package_resource.go (about) 1 package resources 2 3 import ( 4 "encoding/json" 5 6 "code.cloudfoundry.org/cli/api/cloudcontroller" 7 "code.cloudfoundry.org/cli/api/cloudcontroller/ccv3/constant" 8 ) 9 10 // Package represents a Cloud Controller V3 Package. 11 type Package struct { 12 // CreatedAt is the time with zone when the object was created. 13 CreatedAt string 14 15 // DockerImage is the registry address of the docker image. 16 DockerImage string 17 18 // DockerPassword is the password for the docker image's registry. 19 DockerPassword string 20 21 // DockerUsername is the username for the docker image's registry. 22 DockerUsername string 23 24 // GUID is the unique identifier of the package. 25 GUID string 26 27 // Links are links to related resources. 28 Links APILinks 29 30 // Relationships are a list of relationships to other resources. 31 Relationships Relationships 32 33 // State is the state of the package. 34 State constant.PackageState 35 36 // Type is the package type. 37 Type constant.PackageType 38 } 39 40 // MarshalJSON converts a Package into a Cloud Controller Package. 41 func (p Package) MarshalJSON() ([]byte, error) { 42 type ccPackageData struct { 43 Image string `json:"image,omitempty"` 44 Username string `json:"username,omitempty"` 45 Password string `json:"password,omitempty"` 46 } 47 var ccPackage struct { 48 GUID string `json:"guid,omitempty"` 49 CreatedAt string `json:"created_at,omitempty"` 50 Links APILinks `json:"links,omitempty"` 51 Relationships Relationships `json:"relationships,omitempty"` 52 State constant.PackageState `json:"state,omitempty"` 53 Type constant.PackageType `json:"type,omitempty"` 54 Data *ccPackageData `json:"data,omitempty"` 55 } 56 57 ccPackage.GUID = p.GUID 58 ccPackage.CreatedAt = p.CreatedAt 59 ccPackage.Links = p.Links 60 ccPackage.Relationships = p.Relationships 61 ccPackage.State = p.State 62 ccPackage.Type = p.Type 63 if p.DockerImage != "" { 64 ccPackage.Data = &ccPackageData{ 65 Image: p.DockerImage, 66 Username: p.DockerUsername, 67 Password: p.DockerPassword, 68 } 69 } 70 71 return json.Marshal(ccPackage) 72 } 73 74 // UnmarshalJSON helps unmarshal a Cloud Controller Package response. 75 func (p *Package) UnmarshalJSON(data []byte) error { 76 var ccPackage struct { 77 GUID string `json:"guid,omitempty"` 78 CreatedAt string `json:"created_at,omitempty"` 79 Links APILinks `json:"links,omitempty"` 80 Relationships Relationships `json:"relationships,omitempty"` 81 State constant.PackageState `json:"state,omitempty"` 82 Type constant.PackageType `json:"type,omitempty"` 83 Data struct { 84 Image string `json:"image"` 85 Username string `json:"username"` 86 Password string `json:"password"` 87 } `json:"data"` 88 } 89 err := cloudcontroller.DecodeJSON(data, &ccPackage) 90 if err != nil { 91 return err 92 } 93 94 p.GUID = ccPackage.GUID 95 p.CreatedAt = ccPackage.CreatedAt 96 p.Links = ccPackage.Links 97 p.Relationships = ccPackage.Relationships 98 p.State = ccPackage.State 99 p.Type = ccPackage.Type 100 p.DockerImage = ccPackage.Data.Image 101 p.DockerUsername = ccPackage.Data.Username 102 p.DockerPassword = ccPackage.Data.Password 103 104 return nil 105 }