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

     1  package publicips
     2  
     3  import (
     4  	"net/http"
     5  
     6  	"github.com/huaweicloud/golangsdk"
     7  )
     8  
     9  type PublicIPRequest struct {
    10  	// Specifies the type of the elastic IP address. The value can the
    11  	// 5_telcom, 5_union, 5_bgp, or 5_sbgp. China Northeast: 5_telcom and 5_union China
    12  	// South: 5_sbgp China East: 5_sbgp China North: 5_bgp and 5_sbgp The value must be a
    13  	// type supported by the system. The value can be 5_telcom, 5_union, or 5_bgp.
    14  	Type string `json:"type"`
    15  
    16  	// Specifies the elastic IP address to be obtained. The value must
    17  	// be a valid IP address in the available IP address segment.
    18  	IpAddress string `json:"ip_address,omitempty"`
    19  
    20  	//Value range: 4, 6, respectively, to create ipv4 and ipv6, when not created ipv4 by default
    21  	IPVersion string `json:"ip_version,omitempty"`
    22  
    23  	SiteID string `json:"site_id,omitempty"`
    24  }
    25  
    26  type BandWidth struct {
    27  	// Specifies the bandwidth name. The value is a string of 1 to 64
    28  	// characters that can contain letters, digits, underscores (_), and hyphens (-). This
    29  	// parameter is mandatory when share_type is set to PER and is optional when share_type
    30  	// is set to WHOLE with an ID specified.
    31  	Name string `json:"name,omitempty"`
    32  
    33  	// Specifies the bandwidth size. The value ranges from 1 Mbit/s to
    34  	// 300 Mbit/s. This parameter is mandatory when share_type is set to PER and is optional
    35  	// when share_type is set to WHOLE with an ID specified.
    36  	Size int `json:"size,omitempty"`
    37  
    38  	// Specifies the ID of the bandwidth. You can specify an earlier
    39  	// shared bandwidth when applying for an elastic IP address for the bandwidth whose type
    40  	// is set to WHOLE. The bandwidth whose type is set to WHOLE exclusively uses its own
    41  	// ID. The value can be the ID of the bandwidth whose type is set to WHOLE.
    42  	ID string `json:"id,omitempty"`
    43  
    44  	// Specifies whether the bandwidth is shared or exclusive. The
    45  	// value can be PER or WHOLE.
    46  	ShareType string `json:"share_type"`
    47  
    48  	// Specifies the charging mode (by traffic or by bandwidth). The
    49  	// value can be bandwidth or traffic. If the value is an empty character string or no
    50  	// value is specified, default value bandwidth is used.
    51  	ChargeMode string `json:"charge_mode,omitempty"`
    52  }
    53  
    54  type CreateOpts struct {
    55  	// Specifies the elastic IP address objects.
    56  	Publicip PublicIPRequest `json:"publicip" required:"true"`
    57  
    58  	// Specifies the bandwidth objects.
    59  	Bandwidth BandWidth `json:"bandwidth"`
    60  }
    61  
    62  type CreateOptsBuilder interface {
    63  	ToCreatePublicIPMap() (map[string]interface{}, error)
    64  }
    65  
    66  func (opts CreateOpts) ToCreatePublicIPMap() (map[string]interface{}, error) {
    67  	b, err := golangsdk.BuildRequestBody(opts, "")
    68  	if err != nil {
    69  		return nil, err
    70  	}
    71  	return b, nil
    72  }
    73  
    74  func Create(client *golangsdk.ServiceClient, opts CreateOptsBuilder) (r CreateResult) {
    75  	b, err := opts.ToCreatePublicIPMap()
    76  	if err != nil {
    77  		r.Err = err
    78  		return
    79  	}
    80  
    81  	createURL := rootURL(client)
    82  
    83  	var resp *http.Response
    84  	resp, r.Err = client.Post(createURL, b, &r.Body, &golangsdk.RequestOpts{OkCodes: []int{http.StatusOK}})
    85  	if r.Err != nil {
    86  		return
    87  	}
    88  	defer resp.Body.Close()
    89  
    90  	return
    91  }
    92  
    93  func Delete(client *golangsdk.ServiceClient, publicipId string) (r DeleteResult) {
    94  	deleteURL := DeleteURL(client, publicipId)
    95  
    96  	var resp *http.Response
    97  	resp, r.Err = client.Delete(deleteURL, &golangsdk.RequestOpts{OkCodes: []int{http.StatusNoContent}})
    98  	if r.Err != nil {
    99  		return
   100  	}
   101  	defer resp.Body.Close()
   102  
   103  	return
   104  }
   105  
   106  func Get(client *golangsdk.ServiceClient, publicipId string) (r GetResult) {
   107  	getURL := GetURL(client, publicipId)
   108  
   109  	var resp *http.Response
   110  	resp, r.Err = client.Get(getURL, &r.Body, &golangsdk.RequestOpts{OkCodes: []int{http.StatusOK}})
   111  	if r.Err != nil {
   112  		return
   113  	}
   114  	defer resp.Body.Close()
   115  
   116  	return
   117  }
   118  
   119  type UpdateOpts struct {
   120  	// Specifies the port ID.
   121  	PortId string `json:"port_id,omitempty"`
   122  
   123  	//Value range: 4, 6, respectively, to create ipv4 and ipv6, when not created ipv4 by default
   124  	IPVersion int `json:"ip_version,omitempty"`
   125  }
   126  
   127  type UpdateOptsBuilder interface {
   128  	ToUpdatePublicIPMap() (map[string]interface{}, error)
   129  }
   130  
   131  func (opts UpdateOpts) ToUpdatePublicIPMap() (map[string]interface{}, error) {
   132  	b, err := golangsdk.BuildRequestBody(opts, "publicip")
   133  	if err != nil {
   134  		return nil, err
   135  	}
   136  	return b, nil
   137  }
   138  
   139  func Update(client *golangsdk.ServiceClient, publicipId string, opts UpdateOptsBuilder) (r UpdateResult) {
   140  	b, err := opts.ToUpdatePublicIPMap()
   141  	if err != nil {
   142  		r.Err = err
   143  		return
   144  	}
   145  	updateURL := UpdateURL(client, publicipId)
   146  
   147  	var resp *http.Response
   148  	resp, r.Err = client.Put(updateURL, b, &r.Body, &golangsdk.RequestOpts{OkCodes: []int{http.StatusOK}})
   149  	if r.Err != nil {
   150  		return
   151  	}
   152  	defer resp.Body.Close()
   153  
   154  	return
   155  }
   156  
   157  type ListOpts struct {
   158  	Limit  int    `q:"limit"`
   159  	Offset int    `q:"offset"`
   160  	PortID string `q:"port_id"`
   161  	SiteID string `q:"site_id"`
   162  }
   163  
   164  type ListPublicIPsOptsBuilder interface {
   165  	ToListPublicIPsQuery() (string, error)
   166  }
   167  
   168  func (opts ListOpts) ToListPublicIPsQuery() (string, error) {
   169  	b, err := golangsdk.BuildQueryString(&opts)
   170  	if err != nil {
   171  		return "", err
   172  	}
   173  	return b.String(), nil
   174  }
   175  
   176  func List(client *golangsdk.ServiceClient, opts ListPublicIPsOptsBuilder) (r ListResult) {
   177  	listURL := rootURL(client)
   178  	if opts != nil {
   179  		query, err := opts.ToListPublicIPsQuery()
   180  		if err != nil {
   181  			r.Err = err
   182  			return r
   183  		}
   184  		listURL += query
   185  	}
   186  
   187  	_, r.Err = client.Get(listURL, &r.Body, &golangsdk.RequestOpts{
   188  		OkCodes: []int{http.StatusOK},
   189  	})
   190  	return
   191  }