github.com/chnsz/golangsdk@v0.0.0-20240506093406-85a3fbfa605b/openstack/networking/v1/ports/requests.go (about)

     1  package ports
     2  
     3  import (
     4  	"github.com/chnsz/golangsdk"
     5  	"github.com/chnsz/golangsdk/pagination"
     6  )
     7  
     8  // CreateOpts is the structure required by the Create method to build a new network port.
     9  type CreateOpts struct {
    10  	// Specifies the ID of the network to which the port belongs.
    11  	NetworkId string `json:"network_id" required:"true"`
    12  	// Specifies the port name. The value can contain no more than 255 characters.
    13  	// This parameter is left blank by default.
    14  	Name string `json:"name,omitempty"`
    15  	// Specifies the administrative state of the port.
    16  	// The value can only be true, and the default value is true.
    17  	AdminStateUp bool `json:"admin_state_up,omitempty"`
    18  	// Specifies the device to which the port belongs.
    19  	// Currently, only '' and neutron:VIP_PORT are supported.
    20  	// 'neutron:VIP_PORT' indicates the port of a virtual IP address.
    21  	DeviceOwner string `json:"device_owner,omitempty"`
    22  	// Specifies the port IP address.
    23  	// A port supports only one fixed IP address that cannot be changed.
    24  	FixedIps []FixedIp `json:"fixed_ips,omitempty"`
    25  	// Specifies the UUID of the security group.
    26  	SecurityGroups []string `json:"security_groups,omitempty"`
    27  	// Specifies a set of zero or more allowed address pairs.
    28  	AllowedAddressPairs []AddressPair `json:"allowed_address_pairs,omitempty"`
    29  	// Specifies the extended option (extended attribute) of DHCP.
    30  	ExtraDhcpOpts []ExtraDhcpOpt `json:"extra_dhcp_opts,omitempty"`
    31  }
    32  
    33  // FixedIp is an Object specifying the IP information of the port.
    34  type FixedIp struct {
    35  	// Specifies the subnet ID.
    36  	// You cannot change the parameter value.
    37  	SubnetId string `json:"subnet_id,omitempty"`
    38  	// Specifies the port IP address.
    39  	// You cannot change the parameter value.
    40  	IpAddress string `json:"ip_address,omitempty"`
    41  }
    42  
    43  // AddressPair is an Object specifying the IP/Mac addresses pair.
    44  type AddressPair struct {
    45  	// Specifies the IP address.
    46  	// The IP address cannot be 0.0.0.0/0.
    47  	// Configure an independent security group for the port if a large CIDR block (subnet mask less than 24) is
    48  	// configured for parameter AllowedAddressPairs.
    49  	IpAddress string `json:"ip_address" required:"true"`
    50  	// Specifies the MAC address.
    51  	MacAddress string `json:"mac_address,omitempty"`
    52  }
    53  
    54  // ExtraDhcpOpt is an Object specifying the DHCP extended properties.
    55  type ExtraDhcpOpt struct {
    56  	// Specifies the DHCP option name.
    57  	// Currently, only '51' is supported to indicate the DHCP lease time.
    58  	OptName string `json:"opt_name,omitempty"`
    59  	// Specifies the DHCP option value.
    60  	// When 'OptName' is '51', the parameter format is 'Xh', indicating that the DHCP lease time is X hours.
    61  	// The value range of 'X' is '1~30000' or '-1', '-1' means the DHCP lease time is infinite.
    62  	OptValue string `json:"opt_value,omitempty"`
    63  }
    64  
    65  var requestOpts = golangsdk.RequestOpts{
    66  	MoreHeaders: map[string]string{"Content-Type": "application/json", "X-Language": "en-us"},
    67  }
    68  
    69  // Create is a method to build a new network port.
    70  func Create(c *golangsdk.ServiceClient, opts CreateOpts) (*Port, error) {
    71  	b, err := golangsdk.BuildRequestBody(opts, "port")
    72  	if err != nil {
    73  		return nil, err
    74  	}
    75  
    76  	var rst golangsdk.Result
    77  	_, err = c.Post(rootURL(c), b, &rst.Body, &golangsdk.RequestOpts{
    78  		MoreHeaders: requestOpts.MoreHeaders,
    79  	})
    80  	if err == nil {
    81  		var r Port
    82  		rst.ExtractIntoStructPtr(&r, "port")
    83  		return &r, nil
    84  	}
    85  	return nil, err
    86  }
    87  
    88  // Get is a method to obtain the network port details.
    89  func Get(c *golangsdk.ServiceClient, portID string) (*Port, error) {
    90  	var rst golangsdk.Result
    91  	_, err := c.Get(resourceURL(c, portID), &rst.Body, &golangsdk.RequestOpts{
    92  		MoreHeaders: requestOpts.MoreHeaders,
    93  	})
    94  	if err == nil {
    95  		var r Port
    96  		rst.ExtractIntoStructPtr(&r, "port")
    97  		return &r, nil
    98  	}
    99  	return nil, err
   100  }
   101  
   102  // UpdateOpts is the structure required by the Update method to update the configuration of the specified network port.
   103  type UpdateOpts struct {
   104  	// Specifies the name of the port.
   105  	Name string `json:"name"`
   106  	// Specifies an array of one or more security group IDs.
   107  	SecurityGroups []string `json:"security_groups"`
   108  	// Specifies a set of zero or more allowed address pairs.
   109  	AllowedAddressPairs []AddressPair `json:"allowed_address_pairs"`
   110  	// Specifies the extended option (extended attribute) of DHCP.
   111  	ExtraDhcpOpts []ExtraDhcpOpt `json:"extra_dhcp_opts"`
   112  }
   113  
   114  // Update is a method to update the existing network port.
   115  func Update(c *golangsdk.ServiceClient, portID string, opts UpdateOpts) (*Port, error) {
   116  	b, err := golangsdk.BuildRequestBody(opts, "port")
   117  	if err != nil {
   118  		return nil, err
   119  	}
   120  
   121  	var rst golangsdk.Result
   122  	_, err = c.Put(resourceURL(c, portID), b, &rst.Body, &golangsdk.RequestOpts{
   123  		MoreHeaders: requestOpts.MoreHeaders,
   124  	})
   125  	if err == nil {
   126  		var r Port
   127  		rst.ExtractIntoStructPtr(&r, "port")
   128  		return &r, nil
   129  	}
   130  	return nil, err
   131  }
   132  
   133  // Delete is a method to remove an existing network port by ID.
   134  func Delete(c *golangsdk.ServiceClient, portID string) *golangsdk.ErrResult {
   135  	var r golangsdk.ErrResult
   136  	_, r.Err = c.Delete(resourceURL(c, portID), &golangsdk.RequestOpts{
   137  		MoreHeaders: requestOpts.MoreHeaders,
   138  	})
   139  	return &r
   140  }
   141  
   142  // ListOptsBuilder allows extensions to add additional parameters to the
   143  // List request.
   144  type ListOptsBuilder interface {
   145  	ToPortListQuery() (string, error)
   146  }
   147  
   148  // ListOpts allows the filtering of paginated collections through the API.
   149  // Filtering is achieved by passing in struct field values that map to the port
   150  // attributes you want to see returned. Marker and Limit are used for pagination.
   151  type ListOpts struct {
   152  	ID                  string `q:"id"`
   153  	Name                string `q:"name"`
   154  	NetworkID           string `q:"network_id"`
   155  	MACAddress          string `q:"mac_address"`
   156  	AdminStateUp        *bool  `q:"admin_state_up"`
   157  	DeviceID            string `q:"device_id"`
   158  	DeviceOwner         string `q:"device_owner"`
   159  	Status              string `q:"status"`
   160  	Marker              string `q:"marker"`
   161  	Limit               int    `q:"limit"`
   162  	EnterpriseProjectID string `q:"enterprise_project_id"`
   163  	// fixed_ips=ip_address={ip_address}&fixed_ips=subnet_id={subnet_id}
   164  	FixedIps []string `q:"fixed_ips"`
   165  }
   166  
   167  // ToPortListQuery formats a ListOpts into a query string.
   168  func (opts ListOpts) ToPortListQuery() (string, error) {
   169  	q, err := golangsdk.BuildQueryString(opts)
   170  	return q.String(), err
   171  }
   172  
   173  // List returns a Pager which allows you to iterate over a collection of
   174  // ports. It accepts a ListOpts struct, which allows you to filter the
   175  // returned collection for greater efficiency.
   176  func List(c *golangsdk.ServiceClient, opts ListOptsBuilder) pagination.Pager {
   177  	url := rootURL(c)
   178  	if opts != nil {
   179  		query, err := opts.ToPortListQuery()
   180  		if err != nil {
   181  			return pagination.Pager{Err: err}
   182  		}
   183  		url += query
   184  	}
   185  
   186  	return pagination.NewPager(c, url, func(r pagination.PageResult) pagination.Page {
   187  		p := PortPage{pagination.MarkerPageBase{PageResult: r}}
   188  		p.MarkerPageBase.Owner = p
   189  		return p
   190  	})
   191  }