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

     1  package subnets
     2  
     3  import (
     4  	"encoding/json"
     5  	"reflect"
     6  
     7  	"github.com/opentelekomcloud/gophertelekomcloud"
     8  	"github.com/opentelekomcloud/gophertelekomcloud/pagination"
     9  )
    10  
    11  // ListOpts allows the filtering and sorting of paginated collections through
    12  // the API. Filtering is achieved by passing in struct field values that map to
    13  // the floating IP attributes you want to see returned. SortKey allows you to
    14  // sort by a particular network attribute. SortDir sets the direction, and is
    15  // either `asc' or `desc'. Marker and Limit are used for pagination.
    16  type ListOpts struct {
    17  	// ID is the unique identifier for the subnet.
    18  	ID string `json:",omitempty"`
    19  
    20  	// Name is the human readable name for the subnet. It does not have to be
    21  	// unique.
    22  	Name string `json:",omitempty"`
    23  
    24  	// Specifies the network segment on which the subnet resides.
    25  	CIDR string `json:",omitempty"`
    26  
    27  	// Status indicates whether or not a subnet is currently operational.
    28  	Status string `json:",omitempty"`
    29  
    30  	// Specifies the gateway of the subnet.
    31  	GatewayIP string `json:",omitempty"`
    32  
    33  	// Specifies the IP address of DNS server 1 on the subnet.
    34  	PrimaryDNS string `json:",omitempty"`
    35  
    36  	// Specifies the IP address of DNS server 2 on the subnet.
    37  	SecondaryDNS string `json:",omitempty"`
    38  
    39  	// Identifies the availability zone (AZ) to which the subnet belongs.
    40  	AvailabilityZone string `json:",omitempty"`
    41  
    42  	// Specifies the ID of the VPC to which the subnet belongs.
    43  	VpcID string `json:",omitempty"`
    44  }
    45  
    46  // List returns collection of
    47  // subnets. It accepts a ListOpts struct, which allows you to filter and sort
    48  // the returned collection for greater efficiency.
    49  //
    50  // Default policy settings return only those subnets that are owned by the
    51  // tenant who submits the request, unless an admin user submits the request.
    52  func List(client *golangsdk.ServiceClient, opts ListOpts) ([]Subnet, error) {
    53  	url := rootURL(client)
    54  	pages, err := pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page {
    55  		return SubnetPage{pagination.LinkedPageBase{PageResult: r}}
    56  	}).AllPages()
    57  	if err != nil {
    58  		return nil, err
    59  	}
    60  
    61  	allSubnets, err := ExtractSubnets(pages)
    62  	if err != nil {
    63  		return nil, err
    64  	}
    65  
    66  	return FilterSubnets(allSubnets, opts)
    67  }
    68  
    69  func FilterSubnets(subnets []Subnet, opts ListOpts) ([]Subnet, error) {
    70  	matchOptsByte, err := json.Marshal(opts)
    71  	if err != nil {
    72  		return nil, err
    73  	}
    74  	var matchOpts map[string]interface{}
    75  	err = json.Unmarshal(matchOptsByte, &matchOpts)
    76  	if err != nil {
    77  		return nil, err
    78  	}
    79  
    80  	if len(matchOpts) == 0 {
    81  		return subnets, nil
    82  	}
    83  
    84  	var refinedSubnets []Subnet
    85  	for _, subnet := range subnets {
    86  		if subnetMatchesFilter(&subnet, matchOpts) {
    87  			refinedSubnets = append(refinedSubnets, subnet)
    88  		}
    89  	}
    90  	return refinedSubnets, nil
    91  }
    92  
    93  func subnetMatchesFilter(subnet *Subnet, filter map[string]interface{}) bool {
    94  	for key, expectedValue := range filter {
    95  		if getStructField(subnet, key) != expectedValue {
    96  			return false
    97  		}
    98  	}
    99  	return true
   100  }
   101  
   102  func getStructField(v *Subnet, field string) string {
   103  	r := reflect.ValueOf(v)
   104  	f := reflect.Indirect(r).FieldByName(field)
   105  	return f.String()
   106  }
   107  
   108  // CreateOptsBuilder allows extensions to add additional parameters to the
   109  // Create request.
   110  type CreateOptsBuilder interface {
   111  	ToSubnetCreateMap() (map[string]interface{}, error)
   112  }
   113  
   114  // CreateOpts contains all the values needed to create a new subnets. There are
   115  // no required values.
   116  type CreateOpts struct {
   117  	Name             string         `json:"name" required:"true"`
   118  	Description      string         `json:"description,omitempty"`
   119  	CIDR             string         `json:"cidr" required:"true"`
   120  	DNSList          []string       `json:"dnsList,omitempty"`
   121  	EnableIpv6       *bool          `json:"ipv6_enable,omitempty"`
   122  	GatewayIP        string         `json:"gateway_ip" required:"true"`
   123  	EnableDHCP       *bool          `json:"dhcp_enable,omitempty"`
   124  	PrimaryDNS       string         `json:"primary_dns,omitempty"`
   125  	SecondaryDNS     string         `json:"secondary_dns,omitempty"`
   126  	AvailabilityZone string         `json:"availability_zone,omitempty"`
   127  	VpcID            string         `json:"vpc_id" required:"true"`
   128  	ExtraDHCPOpts    []ExtraDHCPOpt `json:"extra_dhcp_opts,omitempty"`
   129  }
   130  
   131  type ExtraDHCPOpt struct {
   132  	OptName  string `json:"opt_name" required:"true"`
   133  	OptValue string `json:"opt_value,omitempty"`
   134  }
   135  
   136  // ToSubnetCreateMap builds a create request body from CreateOpts.
   137  func (opts CreateOpts) ToSubnetCreateMap() (map[string]interface{}, error) {
   138  	return golangsdk.BuildRequestBody(opts, "subnet")
   139  }
   140  
   141  // Create accepts a CreateOpts struct and uses the values to create a new
   142  // logical subnets. When it is created, the subnets does not have an internal
   143  // interface - it is not associated to any subnet.
   144  func Create(client *golangsdk.ServiceClient, opts CreateOptsBuilder) (r CreateResult) {
   145  	b, err := opts.ToSubnetCreateMap()
   146  	if err != nil {
   147  		r.Err = err
   148  		return
   149  	}
   150  	_, r.Err = client.Post(rootURL(client), b, &r.Body, &golangsdk.RequestOpts{
   151  		OkCodes: []int{200},
   152  	})
   153  	return
   154  }
   155  
   156  // Get retrieves a particular subnets based on its unique ID.
   157  func Get(client *golangsdk.ServiceClient, id string) (r GetResult) {
   158  	_, r.Err = client.Get(resourceURL(client, id), &r.Body, nil)
   159  	return
   160  }
   161  
   162  // UpdateOptsBuilder allows extensions to add additional parameters to the
   163  // Update request.
   164  type UpdateOptsBuilder interface {
   165  	ToSubnetUpdateMap() (map[string]interface{}, error)
   166  }
   167  
   168  // UpdateOpts contains the values used when updating a subnets.
   169  type UpdateOpts struct {
   170  	Name          string         `json:"name,omitempty"`
   171  	Description   *string        `json:"description,omitempty"`
   172  	EnableDHCP    *bool          `json:"dhcp_enable,omitempty"`
   173  	EnableIpv6    *bool          `json:"ipv6_enable,omitempty"`
   174  	PrimaryDNS    string         `json:"primary_dns,omitempty"`
   175  	SecondaryDNS  string         `json:"secondary_dns,omitempty"`
   176  	DNSList       []string       `json:"dnsList,omitempty"`
   177  	ExtraDhcpOpts []ExtraDHCPOpt `json:"extra_dhcp_opts,omitempty"`
   178  }
   179  
   180  // ToSubnetUpdateMap builds an update body based on UpdateOpts.
   181  func (opts UpdateOpts) ToSubnetUpdateMap() (map[string]interface{}, error) {
   182  	return golangsdk.BuildRequestBody(opts, "subnet")
   183  }
   184  
   185  // Update allows subnets to be updated. You can update the name, administrative
   186  // state, and the external gateway.
   187  func Update(client *golangsdk.ServiceClient, vpcID string, id string, opts UpdateOptsBuilder) (r UpdateResult) {
   188  	b, err := opts.ToSubnetUpdateMap()
   189  	if err != nil {
   190  		r.Err = err
   191  		return
   192  	}
   193  	_, r.Err = client.Put(updateURL(client, vpcID, id), b, &r.Body, &golangsdk.RequestOpts{
   194  		OkCodes: []int{200},
   195  	})
   196  	return
   197  }
   198  
   199  // Delete will permanently delete a particular subnets based on its unique ID.
   200  func Delete(client *golangsdk.ServiceClient, vpcID string, id string) (r DeleteResult) {
   201  	_, r.Err = client.Delete(updateURL(client, vpcID, id), nil)
   202  	return
   203  }