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

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