github.com/Axway/agent-sdk@v1.1.101/pkg/apic/apiserver/models/catalog/v1alpha1/Product.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  	ProductCtx log.ContextField = "product"
    17  
    18  	_ProductGVK = apiv1.GroupVersionKind{
    19  		GroupKind: apiv1.GroupKind{
    20  			Group: "catalog",
    21  			Kind:  "Product",
    22  		},
    23  		APIVersion: "v1alpha1",
    24  	}
    25  
    26  	ProductScopes = []string{""}
    27  )
    28  
    29  const (
    30  	ProductResourceName                 = "products"
    31  	ProductIconSubResourceName          = "icon"
    32  	ProductLatestreleaseSubResourceName = "latestrelease"
    33  	ProductReferencesSubResourceName    = "references"
    34  	ProductStateSubResourceName         = "state"
    35  	ProductStatusSubResourceName        = "status"
    36  )
    37  
    38  func ProductGVK() apiv1.GroupVersionKind {
    39  	return _ProductGVK
    40  }
    41  
    42  func init() {
    43  	apiv1.RegisterGVK(_ProductGVK, ProductScopes[0], ProductResourceName)
    44  	log.RegisterContextField(ProductCtx)
    45  }
    46  
    47  // Product Resource
    48  type Product struct {
    49  	apiv1.ResourceMeta
    50  	Icon          interface{}          `json:"icon"`
    51  	Latestrelease ProductLatestrelease `json:"latestrelease"`
    52  	Owner         *apiv1.Owner         `json:"owner"`
    53  	References    ProductReferences    `json:"references"`
    54  	Spec          ProductSpec          `json:"spec"`
    55  	State         ProductState         `json:"state"`
    56  	// Status        ProductStatus        `json:"status"`
    57  	Status *apiv1.ResourceStatus `json:"status"`
    58  }
    59  
    60  // NewProduct creates an empty *Product
    61  func NewProduct(name string) *Product {
    62  	return &Product{
    63  		ResourceMeta: apiv1.ResourceMeta{
    64  			Name:             name,
    65  			GroupVersionKind: _ProductGVK,
    66  		},
    67  	}
    68  }
    69  
    70  // ProductFromInstanceArray converts a []*ResourceInstance to a []*Product
    71  func ProductFromInstanceArray(fromArray []*apiv1.ResourceInstance) ([]*Product, error) {
    72  	newArray := make([]*Product, 0)
    73  	for _, item := range fromArray {
    74  		res := &Product{}
    75  		err := res.FromInstance(item)
    76  		if err != nil {
    77  			return make([]*Product, 0), err
    78  		}
    79  		newArray = append(newArray, res)
    80  	}
    81  
    82  	return newArray, nil
    83  }
    84  
    85  // AsInstance converts a Product to a ResourceInstance
    86  func (res *Product) AsInstance() (*apiv1.ResourceInstance, error) {
    87  	meta := res.ResourceMeta
    88  	meta.GroupVersionKind = ProductGVK()
    89  	res.ResourceMeta = meta
    90  
    91  	m, err := json.Marshal(res)
    92  	if err != nil {
    93  		return nil, err
    94  	}
    95  
    96  	instance := apiv1.ResourceInstance{}
    97  	err = json.Unmarshal(m, &instance)
    98  	if err != nil {
    99  		return nil, err
   100  	}
   101  
   102  	return &instance, nil
   103  }
   104  
   105  // FromInstance converts a ResourceInstance to a Product
   106  func (res *Product) FromInstance(ri *apiv1.ResourceInstance) error {
   107  	if ri == nil {
   108  		res = nil
   109  		return nil
   110  	}
   111  	var err error
   112  	rawResource := ri.GetRawResource()
   113  	if rawResource == nil {
   114  		rawResource, err = json.Marshal(ri)
   115  		if err != nil {
   116  			return err
   117  		}
   118  	}
   119  	err = json.Unmarshal(rawResource, res)
   120  	return err
   121  }
   122  
   123  // MarshalJSON custom marshaller to handle sub resources
   124  func (res *Product) MarshalJSON() ([]byte, error) {
   125  	m, err := json.Marshal(&res.ResourceMeta)
   126  	if err != nil {
   127  		return nil, err
   128  	}
   129  
   130  	var out map[string]interface{}
   131  	err = json.Unmarshal(m, &out)
   132  	if err != nil {
   133  		return nil, err
   134  	}
   135  
   136  	out["icon"] = res.Icon
   137  	out["latestrelease"] = res.Latestrelease
   138  	out["owner"] = res.Owner
   139  	out["references"] = res.References
   140  	out["spec"] = res.Spec
   141  	out["state"] = res.State
   142  	out["status"] = res.Status
   143  
   144  	return json.Marshal(out)
   145  }
   146  
   147  // UnmarshalJSON custom unmarshaller to handle sub resources
   148  func (res *Product) UnmarshalJSON(data []byte) error {
   149  	var err error
   150  
   151  	aux := &apiv1.ResourceInstance{}
   152  	err = json.Unmarshal(data, aux)
   153  	if err != nil {
   154  		return err
   155  	}
   156  
   157  	res.ResourceMeta = aux.ResourceMeta
   158  	res.Owner = aux.Owner
   159  
   160  	// ResourceInstance holds the spec as a map[string]interface{}.
   161  	// Convert it to bytes, then convert to the spec type for the resource.
   162  	sr, err := json.Marshal(aux.Spec)
   163  	if err != nil {
   164  		return err
   165  	}
   166  
   167  	err = json.Unmarshal(sr, &res.Spec)
   168  	if err != nil {
   169  		return err
   170  	}
   171  
   172  	// marshalling subresource Icon
   173  	if v, ok := aux.SubResources["icon"]; ok {
   174  		sr, err = json.Marshal(v)
   175  		if err != nil {
   176  			return err
   177  		}
   178  
   179  		delete(aux.SubResources, "icon")
   180  		err = json.Unmarshal(sr, &res.Icon)
   181  		if err != nil {
   182  			return err
   183  		}
   184  	}
   185  
   186  	// marshalling subresource Latestrelease
   187  	if v, ok := aux.SubResources["latestrelease"]; ok {
   188  		sr, err = json.Marshal(v)
   189  		if err != nil {
   190  			return err
   191  		}
   192  
   193  		delete(aux.SubResources, "latestrelease")
   194  		err = json.Unmarshal(sr, &res.Latestrelease)
   195  		if err != nil {
   196  			return err
   197  		}
   198  	}
   199  
   200  	// marshalling subresource References
   201  	if v, ok := aux.SubResources["references"]; ok {
   202  		sr, err = json.Marshal(v)
   203  		if err != nil {
   204  			return err
   205  		}
   206  
   207  		delete(aux.SubResources, "references")
   208  		err = json.Unmarshal(sr, &res.References)
   209  		if err != nil {
   210  			return err
   211  		}
   212  	}
   213  
   214  	// marshalling subresource State
   215  	if v, ok := aux.SubResources["state"]; ok {
   216  		sr, err = json.Marshal(v)
   217  		if err != nil {
   218  			return err
   219  		}
   220  
   221  		delete(aux.SubResources, "state")
   222  		err = json.Unmarshal(sr, &res.State)
   223  		if err != nil {
   224  			return err
   225  		}
   226  	}
   227  
   228  	// marshalling subresource Status
   229  	if v, ok := aux.SubResources["status"]; ok {
   230  		sr, err = json.Marshal(v)
   231  		if err != nil {
   232  			return err
   233  		}
   234  
   235  		delete(aux.SubResources, "status")
   236  		// err = json.Unmarshal(sr, &res.Status)
   237  		res.Status = &apiv1.ResourceStatus{}
   238  		err = json.Unmarshal(sr, res.Status)
   239  		if err != nil {
   240  			return err
   241  		}
   242  	}
   243  
   244  	return nil
   245  }
   246  
   247  // PluralName returns the plural name of the resource
   248  func (res *Product) PluralName() string {
   249  	return ProductResourceName
   250  }