github.com/huaweicloud/golangsdk@v0.0.0-20210831081626-d823fe11ceba/openstack/iec/v1/ports/requests.go (about)

     1  package ports
     2  
     3  import (
     4  	"net/http"
     5  
     6  	"github.com/huaweicloud/golangsdk"
     7  	"github.com/huaweicloud/golangsdk/openstack/iec/v1/common"
     8  )
     9  
    10  type CreateOpts struct {
    11  	// Specifies the ID of the network to which the port belongs. The
    12  	// network ID must be a real one in the network environment.
    13  	NetworkId string `json:"network_id" required:"true"`
    14  
    15  	DeviceOwner string `json:"device_owner,omitempty"`
    16  
    17  	FixedIPs []FixIPEntity `json:"fixed_ips,omitempty"`
    18  }
    19  
    20  // FixIPEntity 私有IP的结构体
    21  type FixIPEntity struct {
    22  	SubnetID  string `json:"subnet_id,omitempty"`
    23  	IPAddress string `json:"ip_address,omitempty"`
    24  }
    25  
    26  type CreateOptsBuilder interface {
    27  	ToPortsCreateMap() (map[string]interface{}, error)
    28  }
    29  
    30  func (opts CreateOpts) ToPortsCreateMap() (map[string]interface{}, error) {
    31  	b, err := golangsdk.BuildRequestBody(&opts, "port")
    32  	if err != nil {
    33  		return nil, err
    34  	}
    35  	return b, nil
    36  }
    37  
    38  func Create(client *golangsdk.ServiceClient, opts CreateOptsBuilder) (r CreateResult) {
    39  	b, err := opts.ToPortsCreateMap()
    40  	if err != nil {
    41  		r.Err = err
    42  		return
    43  	}
    44  
    45  	_, r.Err = client.Post(rootURL(client), b, &r.Body, &golangsdk.RequestOpts{
    46  		OkCodes: []int{http.StatusOK, http.StatusCreated},
    47  	})
    48  	return
    49  }
    50  
    51  func Delete(client *golangsdk.ServiceClient, portId string) (r DeleteResult) {
    52  	url := DeleteURL(client, portId)
    53  	_, r.Err = client.Delete(url, nil)
    54  	return
    55  }
    56  
    57  func Get(client *golangsdk.ServiceClient, portId string) (r GetResult) {
    58  	url := GetURL(client, portId)
    59  	_, r.Err = client.Get(url, &r.Body, &golangsdk.RequestOpts{
    60  		OkCodes: []int{http.StatusOK},
    61  	})
    62  	return
    63  }
    64  
    65  type UpdateOpts struct {
    66  	// Specifies the port name. The value can contain no more than 255
    67  	// characters. This parameter is left blank by default.
    68  	Name string `json:"name,omitempty"`
    69  
    70  	// Specifies the UUID of the security group.
    71  	// This attribute is extended.
    72  	SecurityGroups *[]string `json:"security_groups,omitempty"`
    73  
    74  	// 1. Specifies a set of zero or more allowed address pairs. An
    75  	// address pair consists of an IP address and MAC address. This attribute is extended.
    76  	// For details, see parameter?allow_address_pair. 2. The IP address cannot be?0.0.0.0.
    77  	// 3. Configure an independent security group for the port if a large CIDR block (subnet
    78  	// mask less than 24) is configured for parameter?allowed_address_pairs.
    79  	AllowedAddressPairs *[]common.AllowedAddressPair `json:"allowed_address_pairs,omitempty"`
    80  
    81  	// Specifies a set of zero or more extra DHCP option pairs. An
    82  	// option pair consists of an option value and name. This attribute is extended.
    83  	ExtraDhcpOpts []common.ExtraDHCPOpt `json:"extra_dhcp_opts,omitempty"`
    84  }
    85  
    86  type UpdateOptsBuilder interface {
    87  	ToPortsUpdateMap() (map[string]interface{}, error)
    88  }
    89  
    90  func (opts UpdateOpts) ToPortsUpdateMap() (map[string]interface{}, error) {
    91  	b, err := golangsdk.BuildRequestBody(&opts, "port")
    92  	if err != nil {
    93  		return nil, err
    94  	}
    95  	return b, nil
    96  }
    97  
    98  func Update(client *golangsdk.ServiceClient, portId string, opts UpdateOptsBuilder) (r UpdateResult) {
    99  	b, err := opts.ToPortsUpdateMap()
   100  	if err != nil {
   101  		r.Err = err
   102  		return
   103  	}
   104  
   105  	_, r.Err = client.Put(UpdateURL(client, portId), b, &r.Body, &golangsdk.RequestOpts{
   106  		OkCodes: []int{http.StatusOK},
   107  	})
   108  	return
   109  }
   110  
   111  type ListOpts struct {
   112  	Limit          int      `q:"limit"`
   113  	Offset         int      `q:"offset"`
   114  	ID             string   `q:"id"`
   115  	Name           string   `q:"name"`
   116  	AdminStateUp   bool     `q:"admin_state_up"`
   117  	DeviceID       string   `q:"device_id"`
   118  	DeviceOwner    string   `q:"device_owner"`
   119  	FixedIPs       []string `q:"fixed_ips"`
   120  	MacAddress     string   `q:"mac_address"`
   121  	NetworkID      string   `q:"network_id"`
   122  	SecurityGroups string   `q:"security_groups"`
   123  	Status         string   `q:"status"`
   124  }
   125  
   126  type ListPortsOptsBuilder interface {
   127  	ToListPortsQuery() (string, error)
   128  }
   129  
   130  func (opts ListOpts) ToListPortsQuery() (string, error) {
   131  	b, err := golangsdk.BuildQueryString(&opts)
   132  	if err != nil {
   133  		return "", err
   134  	}
   135  	return b.String(), nil
   136  }
   137  
   138  func List(client *golangsdk.ServiceClient, opts ListPortsOptsBuilder) (r ListResult) {
   139  	listPortsURL := rootURL(client)
   140  	if opts != nil {
   141  		query, err := opts.ToListPortsQuery()
   142  		if err != nil {
   143  			r.Err = err
   144  			return r
   145  		}
   146  		listPortsURL += query
   147  	}
   148  
   149  	_, r.Err = client.Get(listPortsURL, &r.Body, &golangsdk.RequestOpts{
   150  		OkCodes: []int{http.StatusOK},
   151  	})
   152  	return
   153  }