github.com/Axway/agent-sdk@v1.1.101/pkg/apic/apiserver/models/catalog/v1alpha1/PublishedProduct.go (about) 1 /* 2 * This file is automatically generated 3 */ 4 5 package catalog 6 7 import ( 8 "encoding/json" 9 10 apiv1 "github.com/Axway/agent-sdk/pkg/apic/apiserver/models/api/v1" 11 12 "github.com/Axway/agent-sdk/pkg/util/log" 13 ) 14 15 var ( 16 PublishedProductCtx log.ContextField = "publishedProduct" 17 18 _PublishedProductGVK = apiv1.GroupVersionKind{ 19 GroupKind: apiv1.GroupKind{ 20 Group: "catalog", 21 Kind: "PublishedProduct", 22 }, 23 APIVersion: "v1alpha1", 24 } 25 26 PublishedProductScopes = []string{"Marketplace"} 27 ) 28 29 const ( 30 PublishedProductResourceName = "publishedproducts" 31 PublishedProductReferencesSubResourceName = "references" 32 PublishedProductStatusSubResourceName = "status" 33 ) 34 35 func PublishedProductGVK() apiv1.GroupVersionKind { 36 return _PublishedProductGVK 37 } 38 39 func init() { 40 apiv1.RegisterGVK(_PublishedProductGVK, PublishedProductScopes[0], PublishedProductResourceName) 41 log.RegisterContextField(PublishedProductCtx) 42 } 43 44 // PublishedProduct Resource 45 type PublishedProduct struct { 46 apiv1.ResourceMeta 47 Owner *apiv1.Owner `json:"owner"` 48 References PublishedProductReferences `json:"references"` 49 Spec PublishedProductSpec `json:"spec"` 50 // Status PublishedProductStatus `json:"status"` 51 Status *apiv1.ResourceStatus `json:"status"` 52 } 53 54 // NewPublishedProduct creates an empty *PublishedProduct 55 func NewPublishedProduct(name, scopeName string) *PublishedProduct { 56 return &PublishedProduct{ 57 ResourceMeta: apiv1.ResourceMeta{ 58 Name: name, 59 GroupVersionKind: _PublishedProductGVK, 60 Metadata: apiv1.Metadata{ 61 Scope: apiv1.MetadataScope{ 62 Name: scopeName, 63 Kind: PublishedProductScopes[0], 64 }, 65 }, 66 }, 67 } 68 } 69 70 // PublishedProductFromInstanceArray converts a []*ResourceInstance to a []*PublishedProduct 71 func PublishedProductFromInstanceArray(fromArray []*apiv1.ResourceInstance) ([]*PublishedProduct, error) { 72 newArray := make([]*PublishedProduct, 0) 73 for _, item := range fromArray { 74 res := &PublishedProduct{} 75 err := res.FromInstance(item) 76 if err != nil { 77 return make([]*PublishedProduct, 0), err 78 } 79 newArray = append(newArray, res) 80 } 81 82 return newArray, nil 83 } 84 85 // AsInstance converts a PublishedProduct to a ResourceInstance 86 func (res *PublishedProduct) AsInstance() (*apiv1.ResourceInstance, error) { 87 meta := res.ResourceMeta 88 meta.GroupVersionKind = PublishedProductGVK() 89 res.ResourceMeta = meta 90 91 m, err := json.Marshal(res) 92 if err != nil { 93 return nil, err 94 } 95 96 instance := apiv1.ResourceInstance{} 97 err = json.Unmarshal(m, &instance) 98 if err != nil { 99 return nil, err 100 } 101 102 return &instance, nil 103 } 104 105 // FromInstance converts a ResourceInstance to a PublishedProduct 106 func (res *PublishedProduct) FromInstance(ri *apiv1.ResourceInstance) error { 107 if ri == nil { 108 res = nil 109 return nil 110 } 111 var err error 112 rawResource := ri.GetRawResource() 113 if rawResource == nil { 114 rawResource, err = json.Marshal(ri) 115 if err != nil { 116 return err 117 } 118 } 119 err = json.Unmarshal(rawResource, res) 120 return err 121 } 122 123 // MarshalJSON custom marshaller to handle sub resources 124 func (res *PublishedProduct) MarshalJSON() ([]byte, error) { 125 m, err := json.Marshal(&res.ResourceMeta) 126 if err != nil { 127 return nil, err 128 } 129 130 var out map[string]interface{} 131 err = json.Unmarshal(m, &out) 132 if err != nil { 133 return nil, err 134 } 135 136 out["owner"] = res.Owner 137 out["references"] = res.References 138 out["spec"] = res.Spec 139 out["status"] = res.Status 140 141 return json.Marshal(out) 142 } 143 144 // UnmarshalJSON custom unmarshaller to handle sub resources 145 func (res *PublishedProduct) UnmarshalJSON(data []byte) error { 146 var err error 147 148 aux := &apiv1.ResourceInstance{} 149 err = json.Unmarshal(data, aux) 150 if err != nil { 151 return err 152 } 153 154 res.ResourceMeta = aux.ResourceMeta 155 res.Owner = aux.Owner 156 157 // ResourceInstance holds the spec as a map[string]interface{}. 158 // Convert it to bytes, then convert to the spec type for the resource. 159 sr, err := json.Marshal(aux.Spec) 160 if err != nil { 161 return err 162 } 163 164 err = json.Unmarshal(sr, &res.Spec) 165 if err != nil { 166 return err 167 } 168 169 // marshalling subresource References 170 if v, ok := aux.SubResources["references"]; ok { 171 sr, err = json.Marshal(v) 172 if err != nil { 173 return err 174 } 175 176 delete(aux.SubResources, "references") 177 err = json.Unmarshal(sr, &res.References) 178 if err != nil { 179 return err 180 } 181 } 182 183 // marshalling subresource Status 184 if v, ok := aux.SubResources["status"]; ok { 185 sr, err = json.Marshal(v) 186 if err != nil { 187 return err 188 } 189 190 delete(aux.SubResources, "status") 191 // err = json.Unmarshal(sr, &res.Status) 192 res.Status = &apiv1.ResourceStatus{} 193 err = json.Unmarshal(sr, res.Status) 194 if err != nil { 195 return err 196 } 197 } 198 199 return nil 200 } 201 202 // PluralName returns the plural name of the resource 203 func (res *PublishedProduct) PluralName() string { 204 return PublishedProductResourceName 205 }