github.com/opentelekomcloud/gophertelekomcloud@v0.9.3/openstack/networking/v1/eips/requests.go (about)

     1  package eips
     2  
     3  import (
     4  	"encoding/json"
     5  	"reflect"
     6  
     7  	"github.com/opentelekomcloud/gophertelekomcloud"
     8  	"github.com/opentelekomcloud/gophertelekomcloud/pagination"
     9  )
    10  
    11  // ListOptsBuilder allows extensions to add additional parameters to the
    12  // List request.
    13  type ListOptsBuilder interface {
    14  	ToEipListQuery() (string, error)
    15  }
    16  
    17  // ListOpts filters the results returned by the List() function.
    18  type ListOpts struct {
    19  	// ID is the unique identifier for the ElasticIP.
    20  	ID string `json:",omitempty"`
    21  
    22  	// Status indicates whether or not a ElasticIP is currently operational.
    23  	Status string `json:",omitempty"`
    24  
    25  	// PrivateAddress of the resource with assigned ElasticIP.
    26  	PrivateAddress string `json:",omitempty"`
    27  
    28  	// PublicAddress of the ElasticIP.
    29  	PublicAddress string `json:",omitempty"`
    30  
    31  	// PortID of the resource with assigned ElasticIP.
    32  	PortID string `json:",omitempty"`
    33  
    34  	// BandwidthID of the ElasticIP.
    35  	BandwidthID string `json:",omitempty"`
    36  }
    37  
    38  // ToEipListQuery formats a ListOpts into a query string.
    39  func (opts ListOpts) ToEipListQuery() (string, error) {
    40  	q, err := golangsdk.BuildQueryString(opts)
    41  	if err != nil {
    42  		return "", err
    43  	}
    44  	return q.String(), err
    45  }
    46  
    47  // List instructs OpenStack to provide a list of flavors.
    48  func List(client *golangsdk.ServiceClient, opts ListOpts) ([]PublicIp, error) {
    49  	url := rootURL(client)
    50  	pages, err := pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page {
    51  		p := EipPage{pagination.MarkerPageBase{PageResult: r}}
    52  		p.MarkerPageBase.Owner = p
    53  		return p
    54  	}).AllPages()
    55  	if err != nil {
    56  		return nil, err
    57  	}
    58  
    59  	allPublicIPs, err := ExtractEips(pages)
    60  	if err != nil {
    61  		return nil, err
    62  	}
    63  
    64  	return FilterPublicIPs(allPublicIPs, opts)
    65  }
    66  
    67  func FilterPublicIPs(publicIPs []PublicIp, opts ListOpts) ([]PublicIp, error) {
    68  	matchOptsByte, err := json.Marshal(opts)
    69  	if err != nil {
    70  		return nil, err
    71  	}
    72  	var matchOpts map[string]interface{}
    73  	err = json.Unmarshal(matchOptsByte, &matchOpts)
    74  	if err != nil {
    75  		return nil, err
    76  	}
    77  
    78  	if len(matchOpts) == 0 {
    79  		return publicIPs, nil
    80  	}
    81  
    82  	var refinedPublicIPs []PublicIp
    83  	for _, publicIP := range publicIPs {
    84  		if publicIPMatchesFilter(&publicIP, matchOpts) {
    85  			refinedPublicIPs = append(refinedPublicIPs, publicIP)
    86  		}
    87  	}
    88  	return refinedPublicIPs, nil
    89  }
    90  
    91  func publicIPMatchesFilter(publicIP *PublicIp, filter map[string]interface{}) bool {
    92  	for key, expectedValue := range filter {
    93  		if getStructField(publicIP, key) != expectedValue {
    94  			return false
    95  		}
    96  	}
    97  	return true
    98  }
    99  
   100  func getStructField(v *PublicIp, field string) string {
   101  	r := reflect.ValueOf(v)
   102  	f := reflect.Indirect(r).FieldByName(field)
   103  	return f.String()
   104  }
   105  
   106  // ApplyOptsBuilder is an interface by which can build the request body of public ip
   107  // application
   108  type ApplyOptsBuilder interface {
   109  	ToPublicIpApplyMap() (map[string]interface{}, error)
   110  }
   111  
   112  // ApplyOpts is a struct which is used to create public ip
   113  type ApplyOpts struct {
   114  	IP        PublicIpOpts  `json:"publicip" required:"true"`
   115  	Bandwidth BandwidthOpts `json:"bandwidth" required:"true"`
   116  }
   117  
   118  type PublicIpOpts struct {
   119  	Type    string `json:"type" required:"true"`
   120  	Address string `json:"ip_address,omitempty"`
   121  	Version string `json:"ip_version,omitempty"`
   122  	PortID  string `json:"port_id,omitempty"`
   123  	Name    string `json:"alias,omitempty"`
   124  }
   125  
   126  type BandwidthOpts struct {
   127  	Name       string `json:"name,omitempty"`
   128  	Size       int    `json:"size,omitempty"`
   129  	Id         string `json:"id,omitempty"`
   130  	ShareType  string `json:"share_type" required:"true"`
   131  	ChargeMode string `json:"charge_mode,omitempty"`
   132  }
   133  
   134  func (opts ApplyOpts) ToPublicIpApplyMap() (map[string]interface{}, error) {
   135  	return golangsdk.BuildRequestBody(opts, "")
   136  }
   137  
   138  // Apply is a method by which can access to apply the public ip
   139  func Apply(client *golangsdk.ServiceClient, opts ApplyOptsBuilder) (r ApplyResult) {
   140  	b, err := opts.ToPublicIpApplyMap()
   141  	if err != nil {
   142  		r.Err = err
   143  		return
   144  	}
   145  	_, r.Err = client.Post(rootURL(client), b, &r.Body, &golangsdk.RequestOpts{
   146  		OkCodes: []int{200},
   147  	})
   148  	return
   149  }
   150  
   151  // Get is a method by which can get the detailed information of public ip
   152  func Get(client *golangsdk.ServiceClient, id string) (r GetResult) {
   153  	_, r.Err = client.Get(resourceURL(client, id), &r.Body, nil)
   154  	return
   155  }
   156  
   157  // Delete is a method by which can be able to delete a private ip
   158  func Delete(client *golangsdk.ServiceClient, id string) (r DeleteResult) {
   159  	_, r.Err = client.Delete(resourceURL(client, id), nil)
   160  	return
   161  }
   162  
   163  // UpdateOptsBuilder is an interface by which can be able to build the request
   164  // body
   165  type UpdateOptsBuilder interface {
   166  	ToPublicIpUpdateMap() (map[string]interface{}, error)
   167  }
   168  
   169  // UpdateOpts is a struct which represents the request body of update method
   170  type UpdateOpts struct {
   171  	PortID string `json:"port_id,omitempty"`
   172  }
   173  
   174  func (opts UpdateOpts) ToPublicIpUpdateMap() (map[string]interface{}, error) {
   175  	return golangsdk.BuildRequestBody(opts, "publicip")
   176  }
   177  
   178  // Update is a method which can be able to update the port of public ip
   179  func Update(client *golangsdk.ServiceClient, id string, opts UpdateOptsBuilder) (r UpdateResult) {
   180  	b, err := opts.ToPublicIpUpdateMap()
   181  	if err != nil {
   182  		r.Err = err
   183  		return
   184  	}
   185  	_, r.Err = client.Put(resourceURL(client, id), b, &r.Body, &golangsdk.RequestOpts{
   186  		OkCodes: []int{200},
   187  	})
   188  	return
   189  }