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