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