github.com/Axway/agent-sdk@v1.1.101/pkg/apic/apiserver/models/catalog/v1alpha1/AuthorizationProfile.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  	AuthorizationProfileCtx log.ContextField = "authorizationProfile"
    17  
    18  	_AuthorizationProfileGVK = apiv1.GroupVersionKind{
    19  		GroupKind: apiv1.GroupKind{
    20  			Group: "catalog",
    21  			Kind:  "AuthorizationProfile",
    22  		},
    23  		APIVersion: "v1alpha1",
    24  	}
    25  
    26  	AuthorizationProfileScopes = []string{""}
    27  )
    28  
    29  const (
    30  	AuthorizationProfileResourceName              = "authprofiles"
    31  	AuthorizationProfilePoliciesSubResourceName   = "policies"
    32  	AuthorizationProfileReferencesSubResourceName = "references"
    33  )
    34  
    35  func AuthorizationProfileGVK() apiv1.GroupVersionKind {
    36  	return _AuthorizationProfileGVK
    37  }
    38  
    39  func init() {
    40  	apiv1.RegisterGVK(_AuthorizationProfileGVK, AuthorizationProfileScopes[0], AuthorizationProfileResourceName)
    41  	log.RegisterContextField(AuthorizationProfileCtx)
    42  }
    43  
    44  // AuthorizationProfile Resource
    45  type AuthorizationProfile struct {
    46  	apiv1.ResourceMeta
    47  	Owner      *apiv1.Owner                   `json:"owner"`
    48  	Policies   AuthorizationProfilePolicies   `json:"policies"`
    49  	References AuthorizationProfileReferences `json:"references"`
    50  	Spec       AuthorizationProfileSpec       `json:"spec"`
    51  }
    52  
    53  // NewAuthorizationProfile creates an empty *AuthorizationProfile
    54  func NewAuthorizationProfile(name string) *AuthorizationProfile {
    55  	return &AuthorizationProfile{
    56  		ResourceMeta: apiv1.ResourceMeta{
    57  			Name:             name,
    58  			GroupVersionKind: _AuthorizationProfileGVK,
    59  		},
    60  	}
    61  }
    62  
    63  // AuthorizationProfileFromInstanceArray converts a []*ResourceInstance to a []*AuthorizationProfile
    64  func AuthorizationProfileFromInstanceArray(fromArray []*apiv1.ResourceInstance) ([]*AuthorizationProfile, error) {
    65  	newArray := make([]*AuthorizationProfile, 0)
    66  	for _, item := range fromArray {
    67  		res := &AuthorizationProfile{}
    68  		err := res.FromInstance(item)
    69  		if err != nil {
    70  			return make([]*AuthorizationProfile, 0), err
    71  		}
    72  		newArray = append(newArray, res)
    73  	}
    74  
    75  	return newArray, nil
    76  }
    77  
    78  // AsInstance converts a AuthorizationProfile to a ResourceInstance
    79  func (res *AuthorizationProfile) AsInstance() (*apiv1.ResourceInstance, error) {
    80  	meta := res.ResourceMeta
    81  	meta.GroupVersionKind = AuthorizationProfileGVK()
    82  	res.ResourceMeta = meta
    83  
    84  	m, err := json.Marshal(res)
    85  	if err != nil {
    86  		return nil, err
    87  	}
    88  
    89  	instance := apiv1.ResourceInstance{}
    90  	err = json.Unmarshal(m, &instance)
    91  	if err != nil {
    92  		return nil, err
    93  	}
    94  
    95  	return &instance, nil
    96  }
    97  
    98  // FromInstance converts a ResourceInstance to a AuthorizationProfile
    99  func (res *AuthorizationProfile) FromInstance(ri *apiv1.ResourceInstance) error {
   100  	if ri == nil {
   101  		res = nil
   102  		return nil
   103  	}
   104  	var err error
   105  	rawResource := ri.GetRawResource()
   106  	if rawResource == nil {
   107  		rawResource, err = json.Marshal(ri)
   108  		if err != nil {
   109  			return err
   110  		}
   111  	}
   112  	err = json.Unmarshal(rawResource, res)
   113  	return err
   114  }
   115  
   116  // MarshalJSON custom marshaller to handle sub resources
   117  func (res *AuthorizationProfile) MarshalJSON() ([]byte, error) {
   118  	m, err := json.Marshal(&res.ResourceMeta)
   119  	if err != nil {
   120  		return nil, err
   121  	}
   122  
   123  	var out map[string]interface{}
   124  	err = json.Unmarshal(m, &out)
   125  	if err != nil {
   126  		return nil, err
   127  	}
   128  
   129  	out["owner"] = res.Owner
   130  	out["policies"] = res.Policies
   131  	out["references"] = res.References
   132  	out["spec"] = res.Spec
   133  
   134  	return json.Marshal(out)
   135  }
   136  
   137  // UnmarshalJSON custom unmarshaller to handle sub resources
   138  func (res *AuthorizationProfile) UnmarshalJSON(data []byte) error {
   139  	var err error
   140  
   141  	aux := &apiv1.ResourceInstance{}
   142  	err = json.Unmarshal(data, aux)
   143  	if err != nil {
   144  		return err
   145  	}
   146  
   147  	res.ResourceMeta = aux.ResourceMeta
   148  	res.Owner = aux.Owner
   149  
   150  	// ResourceInstance holds the spec as a map[string]interface{}.
   151  	// Convert it to bytes, then convert to the spec type for the resource.
   152  	sr, err := json.Marshal(aux.Spec)
   153  	if err != nil {
   154  		return err
   155  	}
   156  
   157  	err = json.Unmarshal(sr, &res.Spec)
   158  	if err != nil {
   159  		return err
   160  	}
   161  
   162  	// marshalling subresource Policies
   163  	if v, ok := aux.SubResources["policies"]; ok {
   164  		sr, err = json.Marshal(v)
   165  		if err != nil {
   166  			return err
   167  		}
   168  
   169  		delete(aux.SubResources, "policies")
   170  		err = json.Unmarshal(sr, &res.Policies)
   171  		if err != nil {
   172  			return err
   173  		}
   174  	}
   175  
   176  	// marshalling subresource References
   177  	if v, ok := aux.SubResources["references"]; ok {
   178  		sr, err = json.Marshal(v)
   179  		if err != nil {
   180  			return err
   181  		}
   182  
   183  		delete(aux.SubResources, "references")
   184  		err = json.Unmarshal(sr, &res.References)
   185  		if err != nil {
   186  			return err
   187  		}
   188  	}
   189  
   190  	return nil
   191  }
   192  
   193  // PluralName returns the plural name of the resource
   194  func (res *AuthorizationProfile) PluralName() string {
   195  	return AuthorizationProfileResourceName
   196  }