github.com/Axway/agent-sdk@v1.1.101/pkg/apic/apiserver/models/catalog/v1alpha1/Credential.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 CredentialCtx log.ContextField = "credential" 17 18 _CredentialGVK = apiv1.GroupVersionKind{ 19 GroupKind: apiv1.GroupKind{ 20 Group: "catalog", 21 Kind: "Credential", 22 }, 23 APIVersion: "v1alpha1", 24 } 25 26 CredentialScopes = []string{"Application"} 27 ) 28 29 const ( 30 CredentialResourceName = "credentials" 31 CredentialDataSubResourceName = "data" 32 CredentialExpirationSubResourceName = "expiration" 33 CredentialMarketplaceSubResourceName = "marketplace" 34 CredentialPoliciesSubResourceName = "policies" 35 CredentialReferencesSubResourceName = "references" 36 CredentialStateSubResourceName = "state" 37 CredentialStatusSubResourceName = "status" 38 ) 39 40 func CredentialGVK() apiv1.GroupVersionKind { 41 return _CredentialGVK 42 } 43 44 func init() { 45 apiv1.RegisterGVK(_CredentialGVK, CredentialScopes[0], CredentialResourceName) 46 log.RegisterContextField(CredentialCtx) 47 } 48 49 // Credential Resource 50 type Credential struct { 51 apiv1.ResourceMeta 52 Data interface{} `json:"data"` 53 Expiration CredentialExpiration `json:"expiration"` 54 Marketplace CredentialMarketplace `json:"marketplace"` 55 Owner *apiv1.Owner `json:"owner"` 56 Policies CredentialPolicies `json:"policies"` 57 References CredentialReferences `json:"references"` 58 Spec CredentialSpec `json:"spec"` 59 State CredentialState `json:"state"` 60 // Status CredentialStatus `json:"status"` 61 Status *apiv1.ResourceStatus `json:"status"` 62 } 63 64 // NewCredential creates an empty *Credential 65 func NewCredential(name, scopeName string) *Credential { 66 return &Credential{ 67 ResourceMeta: apiv1.ResourceMeta{ 68 Name: name, 69 GroupVersionKind: _CredentialGVK, 70 Metadata: apiv1.Metadata{ 71 Scope: apiv1.MetadataScope{ 72 Name: scopeName, 73 Kind: CredentialScopes[0], 74 }, 75 }, 76 }, 77 } 78 } 79 80 // CredentialFromInstanceArray converts a []*ResourceInstance to a []*Credential 81 func CredentialFromInstanceArray(fromArray []*apiv1.ResourceInstance) ([]*Credential, error) { 82 newArray := make([]*Credential, 0) 83 for _, item := range fromArray { 84 res := &Credential{} 85 err := res.FromInstance(item) 86 if err != nil { 87 return make([]*Credential, 0), err 88 } 89 newArray = append(newArray, res) 90 } 91 92 return newArray, nil 93 } 94 95 // AsInstance converts a Credential to a ResourceInstance 96 func (res *Credential) AsInstance() (*apiv1.ResourceInstance, error) { 97 meta := res.ResourceMeta 98 meta.GroupVersionKind = CredentialGVK() 99 res.ResourceMeta = meta 100 101 m, err := json.Marshal(res) 102 if err != nil { 103 return nil, err 104 } 105 106 instance := apiv1.ResourceInstance{} 107 err = json.Unmarshal(m, &instance) 108 if err != nil { 109 return nil, err 110 } 111 112 return &instance, nil 113 } 114 115 // FromInstance converts a ResourceInstance to a Credential 116 func (res *Credential) FromInstance(ri *apiv1.ResourceInstance) error { 117 if ri == nil { 118 res = nil 119 return nil 120 } 121 var err error 122 rawResource := ri.GetRawResource() 123 if rawResource == nil { 124 rawResource, err = json.Marshal(ri) 125 if err != nil { 126 return err 127 } 128 } 129 err = json.Unmarshal(rawResource, res) 130 return err 131 } 132 133 // MarshalJSON custom marshaller to handle sub resources 134 func (res *Credential) MarshalJSON() ([]byte, error) { 135 m, err := json.Marshal(&res.ResourceMeta) 136 if err != nil { 137 return nil, err 138 } 139 140 var out map[string]interface{} 141 err = json.Unmarshal(m, &out) 142 if err != nil { 143 return nil, err 144 } 145 146 out["data"] = res.Data 147 out["expiration"] = res.Expiration 148 out["marketplace"] = res.Marketplace 149 out["owner"] = res.Owner 150 out["policies"] = res.Policies 151 out["references"] = res.References 152 out["spec"] = res.Spec 153 out["state"] = res.State 154 out["status"] = res.Status 155 156 return json.Marshal(out) 157 } 158 159 // UnmarshalJSON custom unmarshaller to handle sub resources 160 func (res *Credential) UnmarshalJSON(data []byte) error { 161 var err error 162 163 aux := &apiv1.ResourceInstance{} 164 err = json.Unmarshal(data, aux) 165 if err != nil { 166 return err 167 } 168 169 res.ResourceMeta = aux.ResourceMeta 170 res.Owner = aux.Owner 171 172 // ResourceInstance holds the spec as a map[string]interface{}. 173 // Convert it to bytes, then convert to the spec type for the resource. 174 sr, err := json.Marshal(aux.Spec) 175 if err != nil { 176 return err 177 } 178 179 err = json.Unmarshal(sr, &res.Spec) 180 if err != nil { 181 return err 182 } 183 184 // marshalling subresource Data 185 if v, ok := aux.SubResources["data"]; ok { 186 sr, err = json.Marshal(v) 187 if err != nil { 188 return err 189 } 190 191 delete(aux.SubResources, "data") 192 err = json.Unmarshal(sr, &res.Data) 193 if err != nil { 194 return err 195 } 196 } 197 198 // marshalling subresource Expiration 199 if v, ok := aux.SubResources["expiration"]; ok { 200 sr, err = json.Marshal(v) 201 if err != nil { 202 return err 203 } 204 205 delete(aux.SubResources, "expiration") 206 err = json.Unmarshal(sr, &res.Expiration) 207 if err != nil { 208 return err 209 } 210 } 211 212 // marshalling subresource Marketplace 213 if v, ok := aux.SubResources["marketplace"]; ok { 214 sr, err = json.Marshal(v) 215 if err != nil { 216 return err 217 } 218 219 delete(aux.SubResources, "marketplace") 220 err = json.Unmarshal(sr, &res.Marketplace) 221 if err != nil { 222 return err 223 } 224 } 225 226 // marshalling subresource Policies 227 if v, ok := aux.SubResources["policies"]; ok { 228 sr, err = json.Marshal(v) 229 if err != nil { 230 return err 231 } 232 233 delete(aux.SubResources, "policies") 234 err = json.Unmarshal(sr, &res.Policies) 235 if err != nil { 236 return err 237 } 238 } 239 240 // marshalling subresource References 241 if v, ok := aux.SubResources["references"]; ok { 242 sr, err = json.Marshal(v) 243 if err != nil { 244 return err 245 } 246 247 delete(aux.SubResources, "references") 248 err = json.Unmarshal(sr, &res.References) 249 if err != nil { 250 return err 251 } 252 } 253 254 // marshalling subresource State 255 if v, ok := aux.SubResources["state"]; ok { 256 sr, err = json.Marshal(v) 257 if err != nil { 258 return err 259 } 260 261 delete(aux.SubResources, "state") 262 err = json.Unmarshal(sr, &res.State) 263 if err != nil { 264 return err 265 } 266 } 267 268 // marshalling subresource Status 269 if v, ok := aux.SubResources["status"]; ok { 270 sr, err = json.Marshal(v) 271 if err != nil { 272 return err 273 } 274 275 delete(aux.SubResources, "status") 276 // err = json.Unmarshal(sr, &res.Status) 277 res.Status = &apiv1.ResourceStatus{} 278 err = json.Unmarshal(sr, res.Status) 279 if err != nil { 280 return err 281 } 282 } 283 284 return nil 285 } 286 287 // PluralName returns the plural name of the resource 288 func (res *Credential) PluralName() string { 289 return CredentialResourceName 290 }