github.com/Axway/agent-sdk@v1.1.101/pkg/apic/apiserver/models/catalog/v1alpha1/ProductPlan.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  	ProductPlanCtx log.ContextField = "productPlan"
    17  
    18  	_ProductPlanGVK = apiv1.GroupVersionKind{
    19  		GroupKind: apiv1.GroupKind{
    20  			Group: "catalog",
    21  			Kind:  "ProductPlan",
    22  		},
    23  		APIVersion: "v1alpha1",
    24  	}
    25  
    26  	ProductPlanScopes = []string{""}
    27  )
    28  
    29  const (
    30  	ProductPlanResourceName              = "productplans"
    31  	ProductPlanReferencesSubResourceName = "references"
    32  	ProductPlanStateSubResourceName      = "state"
    33  	ProductPlanStatusSubResourceName     = "status"
    34  )
    35  
    36  func ProductPlanGVK() apiv1.GroupVersionKind {
    37  	return _ProductPlanGVK
    38  }
    39  
    40  func init() {
    41  	apiv1.RegisterGVK(_ProductPlanGVK, ProductPlanScopes[0], ProductPlanResourceName)
    42  	log.RegisterContextField(ProductPlanCtx)
    43  }
    44  
    45  // ProductPlan Resource
    46  type ProductPlan struct {
    47  	apiv1.ResourceMeta
    48  	Owner      *apiv1.Owner          `json:"owner"`
    49  	References ProductPlanReferences `json:"references"`
    50  	Spec       ProductPlanSpec       `json:"spec"`
    51  	State      ProductPlanState      `json:"state"`
    52  	// Status     ProductPlanStatus     `json:"status"`
    53  	Status *apiv1.ResourceStatus `json:"status"`
    54  }
    55  
    56  // NewProductPlan creates an empty *ProductPlan
    57  func NewProductPlan(name string) *ProductPlan {
    58  	return &ProductPlan{
    59  		ResourceMeta: apiv1.ResourceMeta{
    60  			Name:             name,
    61  			GroupVersionKind: _ProductPlanGVK,
    62  		},
    63  	}
    64  }
    65  
    66  // ProductPlanFromInstanceArray converts a []*ResourceInstance to a []*ProductPlan
    67  func ProductPlanFromInstanceArray(fromArray []*apiv1.ResourceInstance) ([]*ProductPlan, error) {
    68  	newArray := make([]*ProductPlan, 0)
    69  	for _, item := range fromArray {
    70  		res := &ProductPlan{}
    71  		err := res.FromInstance(item)
    72  		if err != nil {
    73  			return make([]*ProductPlan, 0), err
    74  		}
    75  		newArray = append(newArray, res)
    76  	}
    77  
    78  	return newArray, nil
    79  }
    80  
    81  // AsInstance converts a ProductPlan to a ResourceInstance
    82  func (res *ProductPlan) AsInstance() (*apiv1.ResourceInstance, error) {
    83  	meta := res.ResourceMeta
    84  	meta.GroupVersionKind = ProductPlanGVK()
    85  	res.ResourceMeta = meta
    86  
    87  	m, err := json.Marshal(res)
    88  	if err != nil {
    89  		return nil, err
    90  	}
    91  
    92  	instance := apiv1.ResourceInstance{}
    93  	err = json.Unmarshal(m, &instance)
    94  	if err != nil {
    95  		return nil, err
    96  	}
    97  
    98  	return &instance, nil
    99  }
   100  
   101  // FromInstance converts a ResourceInstance to a ProductPlan
   102  func (res *ProductPlan) FromInstance(ri *apiv1.ResourceInstance) error {
   103  	if ri == nil {
   104  		res = nil
   105  		return nil
   106  	}
   107  	var err error
   108  	rawResource := ri.GetRawResource()
   109  	if rawResource == nil {
   110  		rawResource, err = json.Marshal(ri)
   111  		if err != nil {
   112  			return err
   113  		}
   114  	}
   115  	err = json.Unmarshal(rawResource, res)
   116  	return err
   117  }
   118  
   119  // MarshalJSON custom marshaller to handle sub resources
   120  func (res *ProductPlan) MarshalJSON() ([]byte, error) {
   121  	m, err := json.Marshal(&res.ResourceMeta)
   122  	if err != nil {
   123  		return nil, err
   124  	}
   125  
   126  	var out map[string]interface{}
   127  	err = json.Unmarshal(m, &out)
   128  	if err != nil {
   129  		return nil, err
   130  	}
   131  
   132  	out["owner"] = res.Owner
   133  	out["references"] = res.References
   134  	out["spec"] = res.Spec
   135  	out["state"] = res.State
   136  	out["status"] = res.Status
   137  
   138  	return json.Marshal(out)
   139  }
   140  
   141  // UnmarshalJSON custom unmarshaller to handle sub resources
   142  func (res *ProductPlan) 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 References
   167  	if v, ok := aux.SubResources["references"]; ok {
   168  		sr, err = json.Marshal(v)
   169  		if err != nil {
   170  			return err
   171  		}
   172  
   173  		delete(aux.SubResources, "references")
   174  		err = json.Unmarshal(sr, &res.References)
   175  		if err != nil {
   176  			return err
   177  		}
   178  	}
   179  
   180  	// marshalling subresource State
   181  	if v, ok := aux.SubResources["state"]; ok {
   182  		sr, err = json.Marshal(v)
   183  		if err != nil {
   184  			return err
   185  		}
   186  
   187  		delete(aux.SubResources, "state")
   188  		err = json.Unmarshal(sr, &res.State)
   189  		if err != nil {
   190  			return err
   191  		}
   192  	}
   193  
   194  	// marshalling subresource Status
   195  	if v, ok := aux.SubResources["status"]; ok {
   196  		sr, err = json.Marshal(v)
   197  		if err != nil {
   198  			return err
   199  		}
   200  
   201  		delete(aux.SubResources, "status")
   202  		// err = json.Unmarshal(sr, &res.Status)
   203  		res.Status = &apiv1.ResourceStatus{}
   204  		err = json.Unmarshal(sr, res.Status)
   205  		if err != nil {
   206  			return err
   207  		}
   208  	}
   209  
   210  	return nil
   211  }
   212  
   213  // PluralName returns the plural name of the resource
   214  func (res *ProductPlan) PluralName() string {
   215  	return ProductPlanResourceName
   216  }