github.com/huaweicloud/golangsdk@v0.0.0-20210831081626-d823fe11ceba/openstack/networking/v2/extensions/extradhcpopts/requests.go (about)

     1  package extradhcpopts
     2  
     3  import (
     4  	"github.com/huaweicloud/golangsdk"
     5  	"github.com/huaweicloud/golangsdk/openstack/networking/v2/ports"
     6  )
     7  
     8  // CreateOptsExt adds extra DHCP options to the base ports.CreateOpts.
     9  type CreateOptsExt struct {
    10  	// CreateOptsBuilder is the interface options structs have to satisfy in order
    11  	// to be used in the main Create operation in this package.
    12  	ports.CreateOptsBuilder
    13  
    14  	// ExtraDHCPOpts field is a set of DHCP options for a single port.
    15  	ExtraDHCPOpts []CreateExtraDHCPOpt `json:"extra_dhcp_opts,omitempty"`
    16  }
    17  
    18  // CreateExtraDHCPOpt represents the options required to create an extra DHCP
    19  // option on a port.
    20  type CreateExtraDHCPOpt struct {
    21  	// OptName is the name of a DHCP option.
    22  	OptName string `json:"opt_name" required:"true"`
    23  
    24  	// OptValue is the value of the DHCP option.
    25  	OptValue string `json:"opt_value" required:"true"`
    26  
    27  	// IPVersion is the IP protocol version of a DHCP option.
    28  	IPVersion golangsdk.IPVersion `json:"ip_version,omitempty"`
    29  }
    30  
    31  // ToPortCreateMap casts a CreateOptsExt struct to a map.
    32  func (opts CreateOptsExt) ToPortCreateMap() (map[string]interface{}, error) {
    33  	base, err := opts.CreateOptsBuilder.ToPortCreateMap()
    34  	if err != nil {
    35  		return nil, err
    36  	}
    37  
    38  	port := base["port"].(map[string]interface{})
    39  
    40  	// Convert opts.ExtraDHCPOpts to a slice of maps.
    41  	if opts.ExtraDHCPOpts != nil {
    42  		extraDHCPOpts := make([]map[string]interface{}, len(opts.ExtraDHCPOpts))
    43  		for i, opt := range opts.ExtraDHCPOpts {
    44  			b, err := golangsdk.BuildRequestBody(opt, "")
    45  			if err != nil {
    46  				return nil, err
    47  			}
    48  			extraDHCPOpts[i] = b
    49  		}
    50  		port["extra_dhcp_opts"] = extraDHCPOpts
    51  	}
    52  
    53  	return base, nil
    54  }
    55  
    56  // UpdateOptsExt adds extra DHCP options to the base ports.UpdateOpts.
    57  type UpdateOptsExt struct {
    58  	// UpdateOptsBuilder is the interface options structs have to satisfy in order
    59  	// to be used in the main Update operation in this package.
    60  	ports.UpdateOptsBuilder
    61  
    62  	// ExtraDHCPOpts field is a set of DHCP options for a single port.
    63  	ExtraDHCPOpts []UpdateExtraDHCPOpt `json:"extra_dhcp_opts,omitempty"`
    64  }
    65  
    66  // UpdateExtraDHCPOpt represents the options required to update an extra DHCP
    67  // option on a port.
    68  type UpdateExtraDHCPOpt struct {
    69  	// OptName is the name of a DHCP option.
    70  	OptName string `json:"opt_name" required:"true"`
    71  
    72  	// OptValue is the value of the DHCP option.
    73  	OptValue *string `json:"opt_value"`
    74  
    75  	// IPVersion is the IP protocol version of a DHCP option.
    76  	IPVersion golangsdk.IPVersion `json:"ip_version,omitempty"`
    77  }
    78  
    79  // ToPortUpdateMap casts an UpdateOpts struct to a map.
    80  func (opts UpdateOptsExt) ToPortUpdateMap() (map[string]interface{}, error) {
    81  	base, err := opts.UpdateOptsBuilder.ToPortUpdateMap()
    82  	if err != nil {
    83  		return nil, err
    84  	}
    85  
    86  	port := base["port"].(map[string]interface{})
    87  
    88  	// Convert opts.ExtraDHCPOpts to a slice of maps.
    89  	if opts.ExtraDHCPOpts != nil {
    90  		extraDHCPOpts := make([]map[string]interface{}, len(opts.ExtraDHCPOpts))
    91  		for i, opt := range opts.ExtraDHCPOpts {
    92  			b, err := golangsdk.BuildRequestBody(opt, "")
    93  			if err != nil {
    94  				return nil, err
    95  			}
    96  			extraDHCPOpts[i] = b
    97  		}
    98  		port["extra_dhcp_opts"] = extraDHCPOpts
    99  	}
   100  
   101  	return base, nil
   102  }