github.com/Axway/agent-sdk@v1.1.101/pkg/apic/apiserver/models/catalog/v1alpha1/AccessControlList.go (about) 1 /* 2 * This file is automatically generated 3 */ 4 5 package catalog 6 7 import ( 8 "encoding/json" 9 "fmt" 10 11 apiv1 "github.com/Axway/agent-sdk/pkg/apic/apiserver/models/api/v1" 12 13 "github.com/Axway/agent-sdk/pkg/util/log" 14 ) 15 16 var ( 17 AccessControlListCtx log.ContextField = "accessControlList" 18 19 _AccessControlListGVK = apiv1.GroupVersionKind{ 20 GroupKind: apiv1.GroupKind{ 21 Group: "catalog", 22 Kind: "AccessControlList", 23 }, 24 APIVersion: "v1alpha1", 25 } 26 27 AccessControlListScopes = []string{"Application", "Asset", "AssetRelease", "AuthorizationProfile", "Category", "DocumentResource", "Marketplace", "Product", "ProductPlan", "ProductPlanUnit", "ProductRelease", "Stage", "Subscription", "SubscriptionRequestDefinition", "SupportContact"} 28 ) 29 30 const ( 31 AccessControlListResourceName = "accesscontrollists" 32 ) 33 34 func AccessControlListGVK() apiv1.GroupVersionKind { 35 return _AccessControlListGVK 36 } 37 38 func init() { 39 apiv1.RegisterGVK(_AccessControlListGVK, AccessControlListScopes[0], AccessControlListResourceName) 40 log.RegisterContextField(AccessControlListCtx) 41 } 42 43 // AccessControlList Resource 44 type AccessControlList struct { 45 apiv1.ResourceMeta 46 Owner *apiv1.Owner `json:"owner"` 47 Spec AccessControlListSpec `json:"spec"` 48 } 49 50 // NewAccessControlList creates an empty *AccessControlList 51 func NewAccessControlList(name, scopeKind, scopeName string) (*AccessControlList, error) { 52 validScope := false 53 for _, s := range AccessControlListScopes { 54 if scopeKind == s { 55 validScope = true 56 break 57 } 58 } 59 if !validScope { 60 return nil, fmt.Errorf("scope '%s' not valid for AccessControlList kind", scopeKind) 61 } 62 63 return &AccessControlList{ 64 ResourceMeta: apiv1.ResourceMeta{ 65 Name: name, 66 GroupVersionKind: _AccessControlListGVK, 67 Metadata: apiv1.Metadata{ 68 Scope: apiv1.MetadataScope{ 69 Name: scopeName, 70 Kind: scopeKind, 71 }, 72 }, 73 }, 74 }, nil 75 } 76 77 // AccessControlListFromInstanceArray converts a []*ResourceInstance to a []*AccessControlList 78 func AccessControlListFromInstanceArray(fromArray []*apiv1.ResourceInstance) ([]*AccessControlList, error) { 79 newArray := make([]*AccessControlList, 0) 80 for _, item := range fromArray { 81 res := &AccessControlList{} 82 err := res.FromInstance(item) 83 if err != nil { 84 return make([]*AccessControlList, 0), err 85 } 86 newArray = append(newArray, res) 87 } 88 89 return newArray, nil 90 } 91 92 // AsInstance converts a AccessControlList to a ResourceInstance 93 func (res *AccessControlList) AsInstance() (*apiv1.ResourceInstance, error) { 94 meta := res.ResourceMeta 95 meta.GroupVersionKind = AccessControlListGVK() 96 res.ResourceMeta = meta 97 98 m, err := json.Marshal(res) 99 if err != nil { 100 return nil, err 101 } 102 103 instance := apiv1.ResourceInstance{} 104 err = json.Unmarshal(m, &instance) 105 if err != nil { 106 return nil, err 107 } 108 109 return &instance, nil 110 } 111 112 // FromInstance converts a ResourceInstance to a AccessControlList 113 func (res *AccessControlList) FromInstance(ri *apiv1.ResourceInstance) error { 114 if ri == nil { 115 res = nil 116 return nil 117 } 118 var err error 119 rawResource := ri.GetRawResource() 120 if rawResource == nil { 121 rawResource, err = json.Marshal(ri) 122 if err != nil { 123 return err 124 } 125 } 126 err = json.Unmarshal(rawResource, res) 127 return err 128 } 129 130 // MarshalJSON custom marshaller to handle sub resources 131 func (res *AccessControlList) MarshalJSON() ([]byte, error) { 132 m, err := json.Marshal(&res.ResourceMeta) 133 if err != nil { 134 return nil, err 135 } 136 137 var out map[string]interface{} 138 err = json.Unmarshal(m, &out) 139 if err != nil { 140 return nil, err 141 } 142 143 out["owner"] = res.Owner 144 out["spec"] = res.Spec 145 146 return json.Marshal(out) 147 } 148 149 // UnmarshalJSON custom unmarshaller to handle sub resources 150 func (res *AccessControlList) UnmarshalJSON(data []byte) error { 151 var err error 152 153 aux := &apiv1.ResourceInstance{} 154 err = json.Unmarshal(data, aux) 155 if err != nil { 156 return err 157 } 158 159 res.ResourceMeta = aux.ResourceMeta 160 res.Owner = aux.Owner 161 162 // ResourceInstance holds the spec as a map[string]interface{}. 163 // Convert it to bytes, then convert to the spec type for the resource. 164 sr, err := json.Marshal(aux.Spec) 165 if err != nil { 166 return err 167 } 168 169 err = json.Unmarshal(sr, &res.Spec) 170 if err != nil { 171 return err 172 } 173 174 return nil 175 } 176 177 // PluralName returns the plural name of the resource 178 func (res *AccessControlList) PluralName() string { 179 return AccessControlListResourceName 180 }