// Code generated by go-swagger; DO NOT EDIT.

package models

// This file was generated by the swagger tool.
// Editing this file might prove futile when you re-run the swagger generate command

import (
	"context"

	"github.com/go-openapi/errors"
	"github.com/go-openapi/strfmt"
	"github.com/go-openapi/swag"
	"github.com/go-openapi/validate"
)

// MachineNetwork A network that all hosts belonging to the cluster should have an interface with IP address in. The VIPs (if exist) belong to this network.
//
// swagger:model machine_network
type MachineNetwork struct {

	// The IP block address pool.
	Cidr Subnet `json:"cidr,omitempty" gorm:"primaryKey"`

	// The cluster that this network is associated with.
	// Format: uuid
	ClusterID strfmt.UUID `json:"cluster_id,omitempty" gorm:"primaryKey"`
}

// Validate validates this machine network
func (m *MachineNetwork) Validate(formats strfmt.Registry) error {
	var res []error

	if err := m.validateCidr(formats); err != nil {
		res = append(res, err)
	}

	if err := m.validateClusterID(formats); err != nil {
		res = append(res, err)
	}

	if len(res) > 0 {
		return errors.CompositeValidationError(res...)
	}
	return nil
}

func (m *MachineNetwork) validateCidr(formats strfmt.Registry) error {
	if swag.IsZero(m.Cidr) { // not required
		return nil
	}

	if err := m.Cidr.Validate(formats); err != nil {
		if ve, ok := err.(*errors.Validation); ok {
			return ve.ValidateName("cidr")
		} else if ce, ok := err.(*errors.CompositeError); ok {
			return ce.ValidateName("cidr")
		}
		return err
	}

	return nil
}

func (m *MachineNetwork) validateClusterID(formats strfmt.Registry) error {
	if swag.IsZero(m.ClusterID) { // not required
		return nil
	}

	if err := validate.FormatOf("cluster_id", "body", "uuid", m.ClusterID.String(), formats); err != nil {
		return err
	}

	return nil
}

// ContextValidate validate this machine network based on the context it is used
func (m *MachineNetwork) ContextValidate(ctx context.Context, formats strfmt.Registry) error {
	var res []error

	if err := m.contextValidateCidr(ctx, formats); err != nil {
		res = append(res, err)
	}

	if len(res) > 0 {
		return errors.CompositeValidationError(res...)
	}
	return nil
}

func (m *MachineNetwork) contextValidateCidr(ctx context.Context, formats strfmt.Registry) error {

	if err := m.Cidr.ContextValidate(ctx, formats); err != nil {
		if ve, ok := err.(*errors.Validation); ok {
			return ve.ValidateName("cidr")
		} else if ce, ok := err.(*errors.CompositeError); ok {
			return ce.ValidateName("cidr")
		}
		return err
	}

	return nil
}

// MarshalBinary interface implementation
func (m *MachineNetwork) MarshalBinary() ([]byte, error) {
	if m == nil {
		return nil, nil
	}
	return swag.WriteJSON(m)
}

// UnmarshalBinary interface implementation
func (m *MachineNetwork) UnmarshalBinary(b []byte) error {
	var res MachineNetwork
	if err := swag.ReadJSON(b, &res); err != nil {
		return err
	}
	*m = res
	return nil
}
