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

     1  package eips
     2  
     3  import (
     4  	"github.com/chnsz/golangsdk"
     5  	"github.com/chnsz/golangsdk/openstack/common/structs"
     6  	"github.com/chnsz/golangsdk/pagination"
     7  )
     8  
     9  // ApplyOptsBuilder is an interface by which can build the request body of public ip
    10  // application
    11  type ApplyOptsBuilder interface {
    12  	ToPublicIpApplyMap() (map[string]interface{}, error)
    13  }
    14  
    15  // ApplyOpts is a struct which is used to create public ip
    16  type ApplyOpts struct {
    17  	IP                  PublicIpOpts  `json:"publicip" required:"true"`
    18  	Bandwidth           BandwidthOpts `json:"bandwidth" required:"true"`
    19  	EnterpriseProjectID string        `json:"enterprise_project_id,omitempty"`
    20  
    21  	//ExtendParam is only valid by v2.0 client
    22  	ExtendParam *structs.ChargeInfo `json:"extendParam,omitempty"`
    23  }
    24  
    25  type PublicIpOpts struct {
    26  	Type      string `json:"type" required:"true"`
    27  	Address   string `json:"ip_address,omitempty"`
    28  	Alias     string `json:"alias,omitempty"`
    29  	IPVersion int    `json:"ip_version,omitempty"`
    30  }
    31  
    32  type BandwidthOpts struct {
    33  	Name       string `json:"name,omitempty"`
    34  	Size       int    `json:"size,omitempty"`
    35  	Id         string `json:"id,omitempty"`
    36  	ShareType  string `json:"share_type" required:"true"`
    37  	ChargeMode string `json:"charge_mode,omitempty"`
    38  }
    39  
    40  func (opts ApplyOpts) ToPublicIpApplyMap() (map[string]interface{}, error) {
    41  	return golangsdk.BuildRequestBody(opts, "")
    42  }
    43  
    44  // Apply is a method by which can access to apply the public ip
    45  func Apply(client *golangsdk.ServiceClient, opts ApplyOptsBuilder) (r ApplyResult) {
    46  	b, err := opts.ToPublicIpApplyMap()
    47  	if err != nil {
    48  		r.Err = err
    49  		return
    50  	}
    51  	_, r.Err = client.Post(rootURL(client), b, &r.Body, &golangsdk.RequestOpts{
    52  		OkCodes: []int{200},
    53  	})
    54  	return
    55  }
    56  
    57  // Get is a method by which can get the detailed information of public ip
    58  func Get(client *golangsdk.ServiceClient, id string) (r GetResult) {
    59  	_, r.Err = client.Get(resourceURL(client, id), &r.Body, nil)
    60  	return
    61  }
    62  
    63  // Delete is a method by which can be able to delete a private ip
    64  func Delete(client *golangsdk.ServiceClient, id string) (r DeleteResult) {
    65  	_, r.Err = client.Delete(resourceURL(client, id), nil)
    66  	return
    67  }
    68  
    69  // UpdateOptsBuilder is an interface by which can be able to build the request
    70  // body
    71  type UpdateOptsBuilder interface {
    72  	ToPublicIpUpdateMap() (map[string]interface{}, error)
    73  }
    74  
    75  // UpdateOpts is a struct which represents the request body of update method
    76  // NOTE: ip_version and port_id can not be updated at the same time
    77  type UpdateOpts struct {
    78  	PortID    string  `json:"port_id,omitempty"`
    79  	Alias     *string `json:"alias,omitempty"`
    80  	IPVersion int     `json:"ip_version,omitempty"`
    81  }
    82  
    83  func (opts UpdateOpts) ToPublicIpUpdateMap() (map[string]interface{}, error) {
    84  	return golangsdk.BuildRequestBody(opts, "publicip")
    85  }
    86  
    87  // Update is a method which can be able to update the port of public ip
    88  func Update(client *golangsdk.ServiceClient, id string, opts UpdateOptsBuilder) (r UpdateResult) {
    89  	b, err := opts.ToPublicIpUpdateMap()
    90  	if err != nil {
    91  		r.Err = err
    92  		return
    93  	}
    94  	_, r.Err = client.Put(resourceURL(client, id), b, &r.Body, &golangsdk.RequestOpts{
    95  		OkCodes: []int{200},
    96  	})
    97  	return
    98  }
    99  
   100  type ListOpts struct {
   101  	// Specifies the resource ID of pagination query. If the parameter
   102  	// is left blank, only resources on the first page are queried.
   103  	Marker string `q:"marker"`
   104  
   105  	// Specifies the number of records returned on each page. The
   106  	// value ranges from 0 to intmax.
   107  	Limit int `q:"limit"`
   108  
   109  	// Value range: 4, 6, respectively, to create ipv4 and ipv6, when not created ipv4 by default
   110  	IPVersion int `q:"ip_version"`
   111  
   112  	// Associated port id
   113  	PortId []string `q:"port_id"`
   114  
   115  	// Public IP address
   116  	PublicIp []string `q:"public_ip_address"`
   117  
   118  	// private IP address
   119  	PrivateIp []string `q:"private_ip_address"`
   120  
   121  	// ID
   122  	Id []string `q:"id"`
   123  
   124  	AllowShareBandwidthTypeAny []string `q:"allow_share_bandwidth_type_any"`
   125  
   126  	// enterprise_project_id
   127  	// You can use this field to filter the elastic public IP under an enterprise project.
   128  	EnterpriseProjectId string `q:"enterprise_project_id"`
   129  }
   130  
   131  type ListOptsBuilder interface {
   132  	ToListPublicIPQuery() (string, error)
   133  }
   134  
   135  func (opts ListOpts) ToListPublicIPQuery() (string, error) {
   136  	q, err := golangsdk.BuildQueryString(opts)
   137  	return q.String(), err
   138  }
   139  
   140  func List(client *golangsdk.ServiceClient, opts ListOptsBuilder) pagination.Pager {
   141  	url := listURL(client)
   142  	if opts != nil {
   143  		query, err := opts.ToListPublicIPQuery()
   144  		if err != nil {
   145  			return pagination.Pager{Err: err}
   146  		}
   147  		url += query
   148  	}
   149  
   150  	return pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page {
   151  		return PublicIPPage{pagination.LinkedPageBase{PageResult: r}}
   152  	})
   153  }
   154  
   155  type ChangeToPeriodOpts struct {
   156  	PublicIPIDs []string           `json:"publicip_ids" required:"true"`
   157  	ExtendParam structs.ChargeInfo `json:"extendParam" required:"true"`
   158  }
   159  
   160  // ChangeToPeriod is is used to change the EIP to prePaid billing mode
   161  func ChangeToPeriod(c *golangsdk.ServiceClient, opts ChangeToPeriodOpts) (r ChangeResult) {
   162  	body, err := golangsdk.BuildRequestBody(opts, "")
   163  	if err != nil {
   164  		r.Err = err
   165  		return
   166  	}
   167  
   168  	_, r.Err = c.Post(changeToPeriodURL(c), body, &r.Body, nil)
   169  	return
   170  }