github.com/Axway/agent-sdk@v1.1.101/pkg/apic/apiserver/models/catalog/v1alpha1/Application.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 ApplicationCtx log.ContextField = "application" 17 18 _ApplicationGVK = apiv1.GroupVersionKind{ 19 GroupKind: apiv1.GroupKind{ 20 Group: "catalog", 21 Kind: "Application", 22 }, 23 APIVersion: "v1alpha1", 24 } 25 26 ApplicationScopes = []string{""} 27 ) 28 29 const ( 30 ApplicationResourceName = "applications" 31 ApplicationMarketplaceSubResourceName = "marketplace" 32 ApplicationStateSubResourceName = "state" 33 ) 34 35 func ApplicationGVK() apiv1.GroupVersionKind { 36 return _ApplicationGVK 37 } 38 39 func init() { 40 apiv1.RegisterGVK(_ApplicationGVK, ApplicationScopes[0], ApplicationResourceName) 41 log.RegisterContextField(ApplicationCtx) 42 } 43 44 // Application Resource 45 type Application struct { 46 apiv1.ResourceMeta 47 Marketplace ApplicationMarketplace `json:"marketplace"` 48 Owner *apiv1.Owner `json:"owner"` 49 Spec ApplicationSpec `json:"spec"` 50 State ApplicationState `json:"state"` 51 } 52 53 // NewApplication creates an empty *Application 54 func NewApplication(name string) *Application { 55 return &Application{ 56 ResourceMeta: apiv1.ResourceMeta{ 57 Name: name, 58 GroupVersionKind: _ApplicationGVK, 59 }, 60 } 61 } 62 63 // ApplicationFromInstanceArray converts a []*ResourceInstance to a []*Application 64 func ApplicationFromInstanceArray(fromArray []*apiv1.ResourceInstance) ([]*Application, error) { 65 newArray := make([]*Application, 0) 66 for _, item := range fromArray { 67 res := &Application{} 68 err := res.FromInstance(item) 69 if err != nil { 70 return make([]*Application, 0), err 71 } 72 newArray = append(newArray, res) 73 } 74 75 return newArray, nil 76 } 77 78 // AsInstance converts a Application to a ResourceInstance 79 func (res *Application) AsInstance() (*apiv1.ResourceInstance, error) { 80 meta := res.ResourceMeta 81 meta.GroupVersionKind = ApplicationGVK() 82 res.ResourceMeta = meta 83 84 m, err := json.Marshal(res) 85 if err != nil { 86 return nil, err 87 } 88 89 instance := apiv1.ResourceInstance{} 90 err = json.Unmarshal(m, &instance) 91 if err != nil { 92 return nil, err 93 } 94 95 return &instance, nil 96 } 97 98 // FromInstance converts a ResourceInstance to a Application 99 func (res *Application) FromInstance(ri *apiv1.ResourceInstance) error { 100 if ri == nil { 101 res = nil 102 return nil 103 } 104 var err error 105 rawResource := ri.GetRawResource() 106 if rawResource == nil { 107 rawResource, err = json.Marshal(ri) 108 if err != nil { 109 return err 110 } 111 } 112 err = json.Unmarshal(rawResource, res) 113 return err 114 } 115 116 // MarshalJSON custom marshaller to handle sub resources 117 func (res *Application) MarshalJSON() ([]byte, error) { 118 m, err := json.Marshal(&res.ResourceMeta) 119 if err != nil { 120 return nil, err 121 } 122 123 var out map[string]interface{} 124 err = json.Unmarshal(m, &out) 125 if err != nil { 126 return nil, err 127 } 128 129 out["marketplace"] = res.Marketplace 130 out["owner"] = res.Owner 131 out["spec"] = res.Spec 132 out["state"] = res.State 133 134 return json.Marshal(out) 135 } 136 137 // UnmarshalJSON custom unmarshaller to handle sub resources 138 func (res *Application) UnmarshalJSON(data []byte) error { 139 var err error 140 141 aux := &apiv1.ResourceInstance{} 142 err = json.Unmarshal(data, aux) 143 if err != nil { 144 return err 145 } 146 147 res.ResourceMeta = aux.ResourceMeta 148 res.Owner = aux.Owner 149 150 // ResourceInstance holds the spec as a map[string]interface{}. 151 // Convert it to bytes, then convert to the spec type for the resource. 152 sr, err := json.Marshal(aux.Spec) 153 if err != nil { 154 return err 155 } 156 157 err = json.Unmarshal(sr, &res.Spec) 158 if err != nil { 159 return err 160 } 161 162 // marshalling subresource Marketplace 163 if v, ok := aux.SubResources["marketplace"]; ok { 164 sr, err = json.Marshal(v) 165 if err != nil { 166 return err 167 } 168 169 delete(aux.SubResources, "marketplace") 170 err = json.Unmarshal(sr, &res.Marketplace) 171 if err != nil { 172 return err 173 } 174 } 175 176 // marshalling subresource State 177 if v, ok := aux.SubResources["state"]; ok { 178 sr, err = json.Marshal(v) 179 if err != nil { 180 return err 181 } 182 183 delete(aux.SubResources, "state") 184 err = json.Unmarshal(sr, &res.State) 185 if err != nil { 186 return err 187 } 188 } 189 190 return nil 191 } 192 193 // PluralName returns the plural name of the resource 194 func (res *Application) PluralName() string { 195 return ApplicationResourceName 196 }