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

     1  package vpcs
     2  
     3  import (
     4  	"reflect"
     5  
     6  	"github.com/opentelekomcloud/gophertelekomcloud"
     7  	"github.com/opentelekomcloud/gophertelekomcloud/pagination"
     8  )
     9  
    10  // ListOpts allows the filtering and sorting 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. SortKey allows you to
    13  // sort by a particular network attribute. SortDir sets the direction, and is
    14  // either `asc' or `desc'. Marker and Limit are used for pagination.
    15  
    16  type ListOpts struct {
    17  	// ID is the unique identifier for the vpc.
    18  	ID string `json:"id"`
    19  
    20  	// Name is the human-readable name for the vpc. It does not have to be
    21  	// unique.
    22  	Name string `json:"name"`
    23  
    24  	// Specifies the range of available subnets in the VPC.
    25  	CIDR string `json:"cidr"`
    26  
    27  	// Status indicates whether a vpc is currently operational.
    28  	Status string `json:"status"`
    29  }
    30  
    31  // List returns collection of
    32  // vpcs. It accepts a ListOpts struct, which allows you to filter and sort
    33  // the returned collection for greater efficiency.
    34  //
    35  // Default policy settings return only those vpcs that are owned by the
    36  // tenant who submits the request, unless an admin user submits the request.
    37  func List(c *golangsdk.ServiceClient, opts ListOpts) ([]Vpc, error) {
    38  	u := rootURL(c)
    39  	pages, err := pagination.NewPager(c, u, func(r pagination.PageResult) pagination.Page {
    40  		return VpcPage{pagination.LinkedPageBase{PageResult: r}}
    41  	}).AllPages()
    42  
    43  	if err != nil {
    44  		return nil, err
    45  	}
    46  
    47  	allVpcs, err := ExtractVpcs(pages)
    48  	if err != nil {
    49  		return nil, err
    50  	}
    51  
    52  	return FilterVPCs(allVpcs, opts)
    53  }
    54  
    55  func FilterVPCs(vpcs []Vpc, opts ListOpts) ([]Vpc, error) {
    56  
    57  	var refinedVPCs []Vpc
    58  	var matched bool
    59  	m := map[string]interface{}{}
    60  
    61  	if opts.ID != "" {
    62  		m["ID"] = opts.ID
    63  	}
    64  	if opts.Name != "" {
    65  		m["Name"] = opts.Name
    66  	}
    67  	if opts.Status != "" {
    68  		m["Status"] = opts.Status
    69  	}
    70  	if opts.CIDR != "" {
    71  		m["CIDR"] = opts.CIDR
    72  	}
    73  
    74  	if len(m) > 0 && len(vpcs) > 0 {
    75  		for _, vpc := range vpcs {
    76  			matched = true
    77  
    78  			for key, value := range m {
    79  				if sVal := getStructField(&vpc, key); !(sVal == value) {
    80  					matched = false
    81  				}
    82  			}
    83  
    84  			if matched {
    85  				refinedVPCs = append(refinedVPCs, vpc)
    86  			}
    87  		}
    88  
    89  	} else {
    90  		refinedVPCs = vpcs
    91  	}
    92  
    93  	return refinedVPCs, nil
    94  }
    95  
    96  func getStructField(v *Vpc, field string) string {
    97  	r := reflect.ValueOf(v)
    98  	f := reflect.Indirect(r).FieldByName(field)
    99  	return f.String()
   100  }
   101  
   102  // CreateOptsBuilder allows extensions to add additional parameters to the
   103  // Create request.
   104  type CreateOptsBuilder interface {
   105  	ToVpcCreateMap() (map[string]interface{}, error)
   106  }
   107  
   108  // CreateOpts contains all the values needed to create a new vpc. There are
   109  // no required values.
   110  type CreateOpts struct {
   111  	Name        string `json:"name,omitempty"`
   112  	Description string `json:"description,omitempty"`
   113  	CIDR        string `json:"cidr,omitempty"`
   114  }
   115  
   116  // ToVpcCreateMap builds a create request body from CreateOpts.
   117  func (opts CreateOpts) ToVpcCreateMap() (map[string]interface{}, error) {
   118  	return golangsdk.BuildRequestBody(opts, "vpc")
   119  }
   120  
   121  // Create accepts a CreateOpts struct and uses the values to create a new
   122  // logical vpc. When it is created, the vpc does not have an internal
   123  // interface - it is not associated to any subnet.
   124  //
   125  // You can optionally specify an external gateway for a vpc using the
   126  // GatewayInfo struct. The external gateway for the vpc must be plugged into
   127  // an external network (it is external if its `vpc:external' field is set to
   128  // true).
   129  func Create(c *golangsdk.ServiceClient, opts CreateOptsBuilder) (r CreateResult) {
   130  	b, err := opts.ToVpcCreateMap()
   131  	if err != nil {
   132  		r.Err = err
   133  		return
   134  	}
   135  	_, r.Err = c.Post(rootURL(c), b, &r.Body, &golangsdk.RequestOpts{
   136  		OkCodes: []int{200},
   137  	})
   138  	return
   139  }
   140  
   141  // Get retrieves a particular vpc based on its unique ID.
   142  func Get(c *golangsdk.ServiceClient, id string) (r GetResult) {
   143  	_, r.Err = c.Get(resourceURL(c, id), &r.Body, nil)
   144  	return
   145  }
   146  
   147  // UpdateOptsBuilder allows extensions to add additional parameters to the
   148  // Update request.
   149  type UpdateOptsBuilder interface {
   150  	ToVpcUpdateMap() (map[string]interface{}, error)
   151  }
   152  
   153  // UpdateOpts contains the values used when updating a vpc.
   154  type UpdateOpts struct {
   155  	Name             string  `json:"name,omitempty"`
   156  	Description      *string `json:"description,omitempty"`
   157  	CIDR             string  `json:"cidr,omitempty"`
   158  	EnableSharedSnat *bool   `json:"enable_shared_snat,omitempty"`
   159  }
   160  
   161  // ToVpcUpdateMap builds an update body based on UpdateOpts.
   162  func (opts UpdateOpts) ToVpcUpdateMap() (map[string]interface{}, error) {
   163  	return golangsdk.BuildRequestBody(opts, "vpc")
   164  }
   165  
   166  // Update allows vpcs to be updated. You can update the name, administrative
   167  // state, and the external gateway. For more information about how to set the
   168  // external gateway for a vpc, see Create. This operation does not enable
   169  // the update of vpc interfaces. To do this, use the AddInterface and
   170  // RemoveInterface functions.
   171  func Update(c *golangsdk.ServiceClient, id string, opts UpdateOptsBuilder) (r UpdateResult) {
   172  	b, err := opts.ToVpcUpdateMap()
   173  	if err != nil {
   174  		r.Err = err
   175  		return
   176  	}
   177  	_, r.Err = c.Put(resourceURL(c, id), b, &r.Body, &golangsdk.RequestOpts{
   178  		OkCodes: []int{200},
   179  	})
   180  	return
   181  }
   182  
   183  // Delete will permanently delete a particular vpc based on its unique ID.
   184  func Delete(c *golangsdk.ServiceClient, id string) (r DeleteResult) {
   185  	_, r.Err = c.Delete(resourceURL(c, id), nil)
   186  	return
   187  }