github.com/Axway/agent-sdk@v1.1.101/pkg/apic/apiserver/models/management/v1/APISpecLintingJob.go (about)

     1  /*
     2   * This file is automatically generated
     3   */
     4  
     5  package management
     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  	APISpecLintingJobCtx log.ContextField = "apiSpecLintingJob"
    17  
    18  	_APISpecLintingJobGVK = apiv1.GroupVersionKind{
    19  		GroupKind: apiv1.GroupKind{
    20  			Group: "management",
    21  			Kind:  "APISpecLintingJob",
    22  		},
    23  		APIVersion: "v1",
    24  	}
    25  
    26  	APISpecLintingJobScopes = []string{"Environment"}
    27  )
    28  
    29  const (
    30  	APISpecLintingJobResourceName            = "apispeclintingjobs"
    31  	ApiSpecLintingJobArchivedSubResourceName = "archived"
    32  	ApiSpecLintingJobResultSubResourceName   = "result"
    33  	ApiSpecLintingJobStateSubResourceName    = "state"
    34  )
    35  
    36  func APISpecLintingJobGVK() apiv1.GroupVersionKind {
    37  	return _APISpecLintingJobGVK
    38  }
    39  
    40  func init() {
    41  	apiv1.RegisterGVK(_APISpecLintingJobGVK, APISpecLintingJobScopes[0], APISpecLintingJobResourceName)
    42  	log.RegisterContextField(APISpecLintingJobCtx)
    43  }
    44  
    45  // APISpecLintingJob Resource
    46  type APISpecLintingJob struct {
    47  	apiv1.ResourceMeta
    48  	Archived interface{}             `json:"archived"`
    49  	Owner    *apiv1.Owner            `json:"owner"`
    50  	Result   ApiSpecLintingJobResult `json:"result"`
    51  	Spec     ApiSpecLintingJobSpec   `json:"spec"`
    52  	State    ApiSpecLintingJobState  `json:"state"`
    53  }
    54  
    55  // NewAPISpecLintingJob creates an empty *APISpecLintingJob
    56  func NewAPISpecLintingJob(name, scopeName string) *APISpecLintingJob {
    57  	return &APISpecLintingJob{
    58  		ResourceMeta: apiv1.ResourceMeta{
    59  			Name:             name,
    60  			GroupVersionKind: _APISpecLintingJobGVK,
    61  			Metadata: apiv1.Metadata{
    62  				Scope: apiv1.MetadataScope{
    63  					Name: scopeName,
    64  					Kind: APISpecLintingJobScopes[0],
    65  				},
    66  			},
    67  		},
    68  	}
    69  }
    70  
    71  // APISpecLintingJobFromInstanceArray converts a []*ResourceInstance to a []*APISpecLintingJob
    72  func APISpecLintingJobFromInstanceArray(fromArray []*apiv1.ResourceInstance) ([]*APISpecLintingJob, error) {
    73  	newArray := make([]*APISpecLintingJob, 0)
    74  	for _, item := range fromArray {
    75  		res := &APISpecLintingJob{}
    76  		err := res.FromInstance(item)
    77  		if err != nil {
    78  			return make([]*APISpecLintingJob, 0), err
    79  		}
    80  		newArray = append(newArray, res)
    81  	}
    82  
    83  	return newArray, nil
    84  }
    85  
    86  // AsInstance converts a APISpecLintingJob to a ResourceInstance
    87  func (res *APISpecLintingJob) AsInstance() (*apiv1.ResourceInstance, error) {
    88  	meta := res.ResourceMeta
    89  	meta.GroupVersionKind = APISpecLintingJobGVK()
    90  	res.ResourceMeta = meta
    91  
    92  	m, err := json.Marshal(res)
    93  	if err != nil {
    94  		return nil, err
    95  	}
    96  
    97  	instance := apiv1.ResourceInstance{}
    98  	err = json.Unmarshal(m, &instance)
    99  	if err != nil {
   100  		return nil, err
   101  	}
   102  
   103  	return &instance, nil
   104  }
   105  
   106  // FromInstance converts a ResourceInstance to a APISpecLintingJob
   107  func (res *APISpecLintingJob) FromInstance(ri *apiv1.ResourceInstance) error {
   108  	if ri == nil {
   109  		res = nil
   110  		return nil
   111  	}
   112  	var err error
   113  	rawResource := ri.GetRawResource()
   114  	if rawResource == nil {
   115  		rawResource, err = json.Marshal(ri)
   116  		if err != nil {
   117  			return err
   118  		}
   119  	}
   120  	err = json.Unmarshal(rawResource, res)
   121  	return err
   122  }
   123  
   124  // MarshalJSON custom marshaller to handle sub resources
   125  func (res *APISpecLintingJob) MarshalJSON() ([]byte, error) {
   126  	m, err := json.Marshal(&res.ResourceMeta)
   127  	if err != nil {
   128  		return nil, err
   129  	}
   130  
   131  	var out map[string]interface{}
   132  	err = json.Unmarshal(m, &out)
   133  	if err != nil {
   134  		return nil, err
   135  	}
   136  
   137  	out["archived"] = res.Archived
   138  	out["owner"] = res.Owner
   139  	out["result"] = res.Result
   140  	out["spec"] = res.Spec
   141  	out["state"] = res.State
   142  
   143  	return json.Marshal(out)
   144  }
   145  
   146  // UnmarshalJSON custom unmarshaller to handle sub resources
   147  func (res *APISpecLintingJob) UnmarshalJSON(data []byte) error {
   148  	var err error
   149  
   150  	aux := &apiv1.ResourceInstance{}
   151  	err = json.Unmarshal(data, aux)
   152  	if err != nil {
   153  		return err
   154  	}
   155  
   156  	res.ResourceMeta = aux.ResourceMeta
   157  	res.Owner = aux.Owner
   158  
   159  	// ResourceInstance holds the spec as a map[string]interface{}.
   160  	// Convert it to bytes, then convert to the spec type for the resource.
   161  	sr, err := json.Marshal(aux.Spec)
   162  	if err != nil {
   163  		return err
   164  	}
   165  
   166  	err = json.Unmarshal(sr, &res.Spec)
   167  	if err != nil {
   168  		return err
   169  	}
   170  
   171  	// marshalling subresource Archived
   172  	if v, ok := aux.SubResources["archived"]; ok {
   173  		sr, err = json.Marshal(v)
   174  		if err != nil {
   175  			return err
   176  		}
   177  
   178  		delete(aux.SubResources, "archived")
   179  		err = json.Unmarshal(sr, &res.Archived)
   180  		if err != nil {
   181  			return err
   182  		}
   183  	}
   184  
   185  	// marshalling subresource Result
   186  	if v, ok := aux.SubResources["result"]; ok {
   187  		sr, err = json.Marshal(v)
   188  		if err != nil {
   189  			return err
   190  		}
   191  
   192  		delete(aux.SubResources, "result")
   193  		err = json.Unmarshal(sr, &res.Result)
   194  		if err != nil {
   195  			return err
   196  		}
   197  	}
   198  
   199  	// marshalling subresource State
   200  	if v, ok := aux.SubResources["state"]; ok {
   201  		sr, err = json.Marshal(v)
   202  		if err != nil {
   203  			return err
   204  		}
   205  
   206  		delete(aux.SubResources, "state")
   207  		err = json.Unmarshal(sr, &res.State)
   208  		if err != nil {
   209  			return err
   210  		}
   211  	}
   212  
   213  	return nil
   214  }
   215  
   216  // PluralName returns the plural name of the resource
   217  func (res *APISpecLintingJob) PluralName() string {
   218  	return APISpecLintingJobResourceName
   219  }