github.com/Axway/agent-sdk@v1.1.101/pkg/apic/apiserver/models/catalog/v1alpha1/AssetMapping.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  	AssetMappingCtx log.ContextField = "assetMapping"
    17  
    18  	_AssetMappingGVK = apiv1.GroupVersionKind{
    19  		GroupKind: apiv1.GroupKind{
    20  			Group: "catalog",
    21  			Kind:  "AssetMapping",
    22  		},
    23  		APIVersion: "v1alpha1",
    24  	}
    25  
    26  	AssetMappingScopes = []string{"Asset"}
    27  )
    28  
    29  const (
    30  	AssetMappingResourceName          = "assetmappings"
    31  	AssetMappingStatusSubResourceName = "status"
    32  )
    33  
    34  func AssetMappingGVK() apiv1.GroupVersionKind {
    35  	return _AssetMappingGVK
    36  }
    37  
    38  func init() {
    39  	apiv1.RegisterGVK(_AssetMappingGVK, AssetMappingScopes[0], AssetMappingResourceName)
    40  	log.RegisterContextField(AssetMappingCtx)
    41  }
    42  
    43  // AssetMapping Resource
    44  type AssetMapping struct {
    45  	apiv1.ResourceMeta
    46  	Owner  *apiv1.Owner       `json:"owner"`
    47  	Spec   AssetMappingSpec   `json:"spec"`
    48  	Status AssetMappingStatus `json:"status"`
    49  }
    50  
    51  // NewAssetMapping creates an empty *AssetMapping
    52  func NewAssetMapping(name, scopeName string) *AssetMapping {
    53  	return &AssetMapping{
    54  		ResourceMeta: apiv1.ResourceMeta{
    55  			Name:             name,
    56  			GroupVersionKind: _AssetMappingGVK,
    57  			Metadata: apiv1.Metadata{
    58  				Scope: apiv1.MetadataScope{
    59  					Name: scopeName,
    60  					Kind: AssetMappingScopes[0],
    61  				},
    62  			},
    63  		},
    64  	}
    65  }
    66  
    67  // AssetMappingFromInstanceArray converts a []*ResourceInstance to a []*AssetMapping
    68  func AssetMappingFromInstanceArray(fromArray []*apiv1.ResourceInstance) ([]*AssetMapping, error) {
    69  	newArray := make([]*AssetMapping, 0)
    70  	for _, item := range fromArray {
    71  		res := &AssetMapping{}
    72  		err := res.FromInstance(item)
    73  		if err != nil {
    74  			return make([]*AssetMapping, 0), err
    75  		}
    76  		newArray = append(newArray, res)
    77  	}
    78  
    79  	return newArray, nil
    80  }
    81  
    82  // AsInstance converts a AssetMapping to a ResourceInstance
    83  func (res *AssetMapping) AsInstance() (*apiv1.ResourceInstance, error) {
    84  	meta := res.ResourceMeta
    85  	meta.GroupVersionKind = AssetMappingGVK()
    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 AssetMapping
   103  func (res *AssetMapping) 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 *AssetMapping) 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["spec"] = res.Spec
   135  	out["status"] = res.Status
   136  
   137  	return json.Marshal(out)
   138  }
   139  
   140  // UnmarshalJSON custom unmarshaller to handle sub resources
   141  func (res *AssetMapping) 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 Status
   166  	if v, ok := aux.SubResources["status"]; ok {
   167  		sr, err = json.Marshal(v)
   168  		if err != nil {
   169  			return err
   170  		}
   171  
   172  		delete(aux.SubResources, "status")
   173  		err = json.Unmarshal(sr, &res.Status)
   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 *AssetMapping) PluralName() string {
   184  	return AssetMappingResourceName
   185  }