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

package model

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

import (
	"context"
	"strconv"

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

// AccesspointPublicEndpoint accesspoint public endpoint
//
// swagger:model AccesspointPublicEndpoint
type AccesspointPublicEndpoint struct {

	// The host of standard connection.
	// Example: tidb.f69f3808.acea1f2a.us-east-1.shared.aws.tidbcloud.com
	Host string `json:"host,omitempty"`

	// The IP allowlist for the public endpoint.
	IPAllowlist []*AccesspointPublicEndpointIPAllowlistItems0 `json:"ip_allowlist"`

	// The TiDB port for connection. The port must be in the range of 1024-65535 except 10080.
	//
	// Example: 4000
	// Maximum: 65535
	// Minimum: 1024
	Port int32 `json:"port,omitempty"`
}

// Validate validates this accesspoint public endpoint
func (m *AccesspointPublicEndpoint) Validate(formats strfmt.Registry) error {
	var res []error

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

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

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

func (m *AccesspointPublicEndpoint) validateIPAllowlist(formats strfmt.Registry) error {
	if swag.IsZero(m.IPAllowlist) { // not required
		return nil
	}

	for i := 0; i < len(m.IPAllowlist); i++ {
		if swag.IsZero(m.IPAllowlist[i]) { // not required
			continue
		}

		if m.IPAllowlist[i] != nil {
			if err := m.IPAllowlist[i].Validate(formats); err != nil {
				if ve, ok := err.(*errors.Validation); ok {
					return ve.ValidateName("ip_allowlist" + "." + strconv.Itoa(i))
				} else if ce, ok := err.(*errors.CompositeError); ok {
					return ce.ValidateName("ip_allowlist" + "." + strconv.Itoa(i))
				}
				return err
			}
		}

	}

	return nil
}

func (m *AccesspointPublicEndpoint) validatePort(formats strfmt.Registry) error {
	if swag.IsZero(m.Port) { // not required
		return nil
	}

	if err := validate.MinimumInt("port", "body", int64(m.Port), 1024, false); err != nil {
		return err
	}

	if err := validate.MaximumInt("port", "body", int64(m.Port), 65535, false); err != nil {
		return err
	}

	return nil
}

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

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

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

func (m *AccesspointPublicEndpoint) contextValidateIPAllowlist(ctx context.Context, formats strfmt.Registry) error {

	for i := 0; i < len(m.IPAllowlist); i++ {

		if m.IPAllowlist[i] != nil {

			if swag.IsZero(m.IPAllowlist[i]) { // not required
				return nil
			}

			if err := m.IPAllowlist[i].ContextValidate(ctx, formats); err != nil {
				if ve, ok := err.(*errors.Validation); ok {
					return ve.ValidateName("ip_allowlist" + "." + strconv.Itoa(i))
				} else if ce, ok := err.(*errors.CompositeError); ok {
					return ce.ValidateName("ip_allowlist" + "." + strconv.Itoa(i))
				}
				return err
			}
		}

	}

	return nil
}

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

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

// AccesspointPublicEndpointIPAllowlistItems0 accesspoint public endpoint IP allowlist items0
//
// swagger:model AccesspointPublicEndpointIPAllowlistItems0
type AccesspointPublicEndpointIPAllowlistItems0 struct {

	// CIDR.
	// Example: 0.0.0.0/0
	Cidr string `json:"cidr,omitempty"`
}

// Validate validates this accesspoint public endpoint IP allowlist items0
func (m *AccesspointPublicEndpointIPAllowlistItems0) Validate(formats strfmt.Registry) error {
	return nil
}

// ContextValidate validates this accesspoint public endpoint IP allowlist items0 based on context it is used
func (m *AccesspointPublicEndpointIPAllowlistItems0) ContextValidate(ctx context.Context, formats strfmt.Registry) error {
	return nil
}

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

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