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