github.com/Axway/agent-sdk@v1.1.101/pkg/apic/apiserver/models/catalog/v1alpha1/Document.go (about)

     1  /*
     2   * This file is automatically generated
     3   */
     4  
     5  package catalog
     6  
     7  import (
     8  	"encoding/json"
     9  	"fmt"
    10  
    11  	apiv1 "github.com/Axway/agent-sdk/pkg/apic/apiserver/models/api/v1"
    12  
    13  	"github.com/Axway/agent-sdk/pkg/util/log"
    14  )
    15  
    16  var (
    17  	DocumentCtx log.ContextField = "document"
    18  
    19  	_DocumentGVK = apiv1.GroupVersionKind{
    20  		GroupKind: apiv1.GroupKind{
    21  			Group: "catalog",
    22  			Kind:  "Document",
    23  		},
    24  		APIVersion: "v1alpha1",
    25  	}
    26  
    27  	DocumentScopes = []string{"Product", "ProductRelease"}
    28  )
    29  
    30  const (
    31  	DocumentResourceName          = "documents"
    32  	DocumentIconSubResourceName   = "icon"
    33  	DocumentStatusSubResourceName = "status"
    34  )
    35  
    36  func DocumentGVK() apiv1.GroupVersionKind {
    37  	return _DocumentGVK
    38  }
    39  
    40  func init() {
    41  	apiv1.RegisterGVK(_DocumentGVK, DocumentScopes[0], DocumentResourceName)
    42  	log.RegisterContextField(DocumentCtx)
    43  }
    44  
    45  // Document Resource
    46  type Document struct {
    47  	apiv1.ResourceMeta
    48  	Icon  interface{}  `json:"icon"`
    49  	Owner *apiv1.Owner `json:"owner"`
    50  	Spec  DocumentSpec `json:"spec"`
    51  	// Status DocumentStatus `json:"status"`
    52  	Status *apiv1.ResourceStatus `json:"status"`
    53  }
    54  
    55  // NewDocument creates an empty *Document
    56  func NewDocument(name, scopeKind, scopeName string) (*Document, error) {
    57  	validScope := false
    58  	for _, s := range DocumentScopes {
    59  		if scopeKind == s {
    60  			validScope = true
    61  			break
    62  		}
    63  	}
    64  	if !validScope {
    65  		return nil, fmt.Errorf("scope '%s' not valid for Document kind", scopeKind)
    66  	}
    67  
    68  	return &Document{
    69  		ResourceMeta: apiv1.ResourceMeta{
    70  			Name:             name,
    71  			GroupVersionKind: _DocumentGVK,
    72  			Metadata: apiv1.Metadata{
    73  				Scope: apiv1.MetadataScope{
    74  					Name: scopeName,
    75  					Kind: scopeKind,
    76  				},
    77  			},
    78  		},
    79  	}, nil
    80  }
    81  
    82  // DocumentFromInstanceArray converts a []*ResourceInstance to a []*Document
    83  func DocumentFromInstanceArray(fromArray []*apiv1.ResourceInstance) ([]*Document, error) {
    84  	newArray := make([]*Document, 0)
    85  	for _, item := range fromArray {
    86  		res := &Document{}
    87  		err := res.FromInstance(item)
    88  		if err != nil {
    89  			return make([]*Document, 0), err
    90  		}
    91  		newArray = append(newArray, res)
    92  	}
    93  
    94  	return newArray, nil
    95  }
    96  
    97  // AsInstance converts a Document to a ResourceInstance
    98  func (res *Document) AsInstance() (*apiv1.ResourceInstance, error) {
    99  	meta := res.ResourceMeta
   100  	meta.GroupVersionKind = DocumentGVK()
   101  	res.ResourceMeta = meta
   102  
   103  	m, err := json.Marshal(res)
   104  	if err != nil {
   105  		return nil, err
   106  	}
   107  
   108  	instance := apiv1.ResourceInstance{}
   109  	err = json.Unmarshal(m, &instance)
   110  	if err != nil {
   111  		return nil, err
   112  	}
   113  
   114  	return &instance, nil
   115  }
   116  
   117  // FromInstance converts a ResourceInstance to a Document
   118  func (res *Document) FromInstance(ri *apiv1.ResourceInstance) error {
   119  	if ri == nil {
   120  		res = nil
   121  		return nil
   122  	}
   123  	var err error
   124  	rawResource := ri.GetRawResource()
   125  	if rawResource == nil {
   126  		rawResource, err = json.Marshal(ri)
   127  		if err != nil {
   128  			return err
   129  		}
   130  	}
   131  	err = json.Unmarshal(rawResource, res)
   132  	return err
   133  }
   134  
   135  // MarshalJSON custom marshaller to handle sub resources
   136  func (res *Document) MarshalJSON() ([]byte, error) {
   137  	m, err := json.Marshal(&res.ResourceMeta)
   138  	if err != nil {
   139  		return nil, err
   140  	}
   141  
   142  	var out map[string]interface{}
   143  	err = json.Unmarshal(m, &out)
   144  	if err != nil {
   145  		return nil, err
   146  	}
   147  
   148  	out["icon"] = res.Icon
   149  	out["owner"] = res.Owner
   150  	out["spec"] = res.Spec
   151  	out["status"] = res.Status
   152  
   153  	return json.Marshal(out)
   154  }
   155  
   156  // UnmarshalJSON custom unmarshaller to handle sub resources
   157  func (res *Document) UnmarshalJSON(data []byte) error {
   158  	var err error
   159  
   160  	aux := &apiv1.ResourceInstance{}
   161  	err = json.Unmarshal(data, aux)
   162  	if err != nil {
   163  		return err
   164  	}
   165  
   166  	res.ResourceMeta = aux.ResourceMeta
   167  	res.Owner = aux.Owner
   168  
   169  	// ResourceInstance holds the spec as a map[string]interface{}.
   170  	// Convert it to bytes, then convert to the spec type for the resource.
   171  	sr, err := json.Marshal(aux.Spec)
   172  	if err != nil {
   173  		return err
   174  	}
   175  
   176  	err = json.Unmarshal(sr, &res.Spec)
   177  	if err != nil {
   178  		return err
   179  	}
   180  
   181  	// marshalling subresource Icon
   182  	if v, ok := aux.SubResources["icon"]; ok {
   183  		sr, err = json.Marshal(v)
   184  		if err != nil {
   185  			return err
   186  		}
   187  
   188  		delete(aux.SubResources, "icon")
   189  		err = json.Unmarshal(sr, &res.Icon)
   190  		if err != nil {
   191  			return err
   192  		}
   193  	}
   194  
   195  	// marshalling subresource Status
   196  	if v, ok := aux.SubResources["status"]; ok {
   197  		sr, err = json.Marshal(v)
   198  		if err != nil {
   199  			return err
   200  		}
   201  
   202  		delete(aux.SubResources, "status")
   203  		// err = json.Unmarshal(sr, &res.Status)
   204  		res.Status = &apiv1.ResourceStatus{}
   205  		err = json.Unmarshal(sr, res.Status)
   206  		if err != nil {
   207  			return err
   208  		}
   209  	}
   210  
   211  	return nil
   212  }
   213  
   214  // PluralName returns the plural name of the resource
   215  func (res *Document) PluralName() string {
   216  	return DocumentResourceName
   217  }