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

     1  package peerings
     2  
     3  import (
     4  	"reflect"
     5  
     6  	"github.com/opentelekomcloud/gophertelekomcloud"
     7  	"github.com/opentelekomcloud/gophertelekomcloud/pagination"
     8  )
     9  
    10  // ListOpts allows the filtering  of paginated collections through
    11  // the API. Filtering is achieved by passing in struct field values that map to
    12  // the floating IP attributes you want to see returned.
    13  type ListOpts struct {
    14  	// ID is the unique identifier for the vpc_peering_connection.
    15  	ID string `q:"id"`
    16  
    17  	// Name is the human readable name for the vpc_peering_connection. It does not have to be
    18  	// unique.
    19  	Name string `q:"name"`
    20  
    21  	// Status indicates whether or not a vpc_peering_connection is currently operational.
    22  	Status string `q:"status"`
    23  
    24  	// TenantId indicates  vpc_peering_connection avalable in specific tenant.
    25  	TenantId string `q:"tenant_id"`
    26  
    27  	// VpcId indicates vpc_peering_connection avalable in specific vpc.
    28  	VpcId string `q:"vpc_id"`
    29  
    30  	// VpcId indicates vpc_peering_connection available in specific vpc.
    31  	Peer_VpcId string
    32  }
    33  
    34  func FilterVpcIdParam(opts ListOpts) (filter ListOpts) {
    35  
    36  	if opts.VpcId != "" {
    37  		filter.VpcId = opts.VpcId
    38  	} else {
    39  		filter.VpcId = opts.Peer_VpcId
    40  	}
    41  
    42  	filter.Name = opts.Name
    43  	filter.ID = opts.ID
    44  	filter.Status = opts.Status
    45  	filter.TenantId = opts.TenantId
    46  
    47  	return filter
    48  }
    49  
    50  // List returns a Pager which allows you to iterate over a collection of
    51  // vpc_peering_connection  resources. It accepts a ListOpts struct, which allows you to
    52  // filter  the returned collection for greater efficiency.
    53  func List(c *golangsdk.ServiceClient, opts ListOpts) ([]Peering, error) {
    54  	filter := FilterVpcIdParam(opts)
    55  	q, err := golangsdk.BuildQueryString(&filter)
    56  	if err != nil {
    57  		return nil, err
    58  	}
    59  	u := rootURL(c) + q.String()
    60  	pages, err := pagination.NewPager(c, u, func(r pagination.PageResult) pagination.Page {
    61  		return PeeringConnectionPage{pagination.LinkedPageBase{PageResult: r}}
    62  	}).AllPages()
    63  
    64  	if err != nil {
    65  		return nil, err
    66  	}
    67  
    68  	allPeeringConns, err := ExtractPeerings(pages)
    69  	if err != nil {
    70  		return nil, err
    71  	}
    72  
    73  	return FilterVpcPeeringConns(allPeeringConns, opts)
    74  }
    75  
    76  func FilterVpcPeeringConns(peerings []Peering, opts ListOpts) ([]Peering, error) {
    77  	var refinedPeerings []Peering
    78  	var matched bool
    79  	filterMap := map[string]interface{}{}
    80  
    81  	if opts.VpcId != "" {
    82  		filterMap["RequestVpcInfo"] = opts.VpcId
    83  	}
    84  
    85  	if opts.Peer_VpcId != "" {
    86  		filterMap["AcceptVpcInfo"] = opts.Peer_VpcId
    87  	}
    88  
    89  	if len(filterMap) > 0 && len(peerings) > 0 {
    90  		for _, peering := range peerings {
    91  			matched = true
    92  
    93  			for key, value := range filterMap {
    94  				if sVal := getStructNestedField(&peering, key); !(sVal == value) {
    95  					matched = false
    96  				}
    97  			}
    98  
    99  			if matched {
   100  				refinedPeerings = append(refinedPeerings, peering)
   101  			}
   102  
   103  		}
   104  	} else {
   105  		refinedPeerings = peerings
   106  	}
   107  
   108  	return refinedPeerings, nil
   109  
   110  }
   111  
   112  func getStructNestedField(v *Peering, field string) string {
   113  	r := reflect.ValueOf(v)
   114  	f := reflect.Indirect(r).FieldByName(field).Interface()
   115  	r1 := reflect.ValueOf(f)
   116  	f1 := reflect.Indirect(r1).FieldByName("VpcId")
   117  	return f1.String()
   118  }
   119  
   120  func Get(c *golangsdk.ServiceClient, id string) (r GetResult) {
   121  	_, r.Err = c.Get(resourceURL(c, id), &r.Body, nil)
   122  	return
   123  }
   124  
   125  // Accept is used by a tenant to accept a VPC peering connection request initiated by another tenant.
   126  func Accept(c *golangsdk.ServiceClient, id string) (r AcceptResult) {
   127  	_, r.Err = c.Put(acceptURL(c, id), nil, &r.Body, &golangsdk.RequestOpts{
   128  		OkCodes: []int{200},
   129  	})
   130  	return
   131  }
   132  
   133  // Reject is used by a tenant to reject a VPC peering connection request initiated by another tenant.
   134  func Reject(c *golangsdk.ServiceClient, id string) (r RejectResult) {
   135  	_, r.Err = c.Put(rejectURL(c, id), nil, &r.Body, &golangsdk.RequestOpts{
   136  		OkCodes: []int{200},
   137  	})
   138  	return
   139  }
   140  
   141  // CreateOptsBuilder is an interface by which can build the request body of vpc peering connection.
   142  type CreateOptsBuilder interface {
   143  	ToPeeringCreateMap() (map[string]interface{}, error)
   144  }
   145  
   146  // CreateOpts is a struct which is used to create vpc peering connection.
   147  type CreateOpts struct {
   148  	Name           string  `json:"name"`
   149  	RequestVpcInfo VpcInfo `json:"request_vpc_info" required:"true"`
   150  	AcceptVpcInfo  VpcInfo `json:"accept_vpc_info" required:"true"`
   151  }
   152  
   153  // ToVpcPeeringCreateMap builds a create request body from CreateOpts.
   154  func (opts CreateOpts) ToPeeringCreateMap() (map[string]interface{}, error) {
   155  	return golangsdk.BuildRequestBody(opts, "peering")
   156  }
   157  
   158  // Create is a method by which can access to create the vpc peering connection.
   159  func Create(client *golangsdk.ServiceClient, opts CreateOptsBuilder) (r CreateResult) {
   160  	b, err := opts.ToPeeringCreateMap()
   161  	if err != nil {
   162  		r.Err = err
   163  		return
   164  	}
   165  	_, r.Err = client.Post(rootURL(client), b, &r.Body, &golangsdk.RequestOpts{
   166  		OkCodes: []int{201},
   167  	})
   168  	return
   169  }
   170  
   171  // Delete is a method by which can be able to delete a vpc peering connection.
   172  func Delete(client *golangsdk.ServiceClient, id string) (r DeleteResult) {
   173  	_, r.Err = client.Delete(resourceURL(client, id), nil)
   174  	return
   175  }
   176  
   177  // UpdateOptsBuilder is an interface by which can be able to build the request body of vpc peering connection.
   178  type UpdateOptsBuilder interface {
   179  	ToVpcPeeringUpdateMap() (map[string]interface{}, error)
   180  }
   181  
   182  // UpdateOpts is a struct which represents the request body of update method.
   183  type UpdateOpts struct {
   184  	Name string `json:"name,omitempty"`
   185  }
   186  
   187  // ToVpcPeeringUpdateMap builds a update request body from UpdateOpts.
   188  func (opts UpdateOpts) ToVpcPeeringUpdateMap() (map[string]interface{}, error) {
   189  	return golangsdk.BuildRequestBody(opts, "peering")
   190  }
   191  
   192  // Update is a method which can be able to update the name of vpc peering connection.
   193  func Update(client *golangsdk.ServiceClient, id string, opts UpdateOptsBuilder) (r UpdateResult) {
   194  	b, err := opts.ToVpcPeeringUpdateMap()
   195  	if err != nil {
   196  		r.Err = err
   197  		return
   198  	}
   199  	_, r.Err = client.Put(resourceURL(client, id), b, &r.Body, &golangsdk.RequestOpts{
   200  		OkCodes: []int{200},
   201  	})
   202  	return
   203  }