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

     1  package subnets
     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 subnet.
    18  	ID string `json:"id"`
    19  
    20  	// Name is the human readable name for the subnet. It does not have to be
    21  	// unique.
    22  	Name string `json:"name"`
    23  
    24  	//Specifies the network segment on which the subnet resides.
    25  	CIDR string `json:"cidr"`
    26  
    27  	// Status indicates whether or not a subnet is currently operational.
    28  	Status string `json:"status"`
    29  
    30  	//Specifies the gateway of the subnet.
    31  	GatewayIP string `json:"gateway_ip"`
    32  
    33  	//Specifies the IP address of DNS server 1 on the subnet.
    34  	PRIMARY_DNS string `json:"primary_dns"`
    35  
    36  	//Specifies the IP address of DNS server 2 on the subnet.
    37  	SECONDARY_DNS string `json:"secondary_dns"`
    38  
    39  	//Identifies the availability zone (AZ) to which the subnet belongs.
    40  	AvailabilityZone string `json:"availability_zone"`
    41  
    42  	//Specifies the ID of the VPC to which the subnet belongs.
    43  	VPC_ID string `json:"vpc_id"`
    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  
    53  func List(c *golangsdk.ServiceClient, opts ListOpts) ([]Subnet, error) {
    54  	u := rootURL(c)
    55  	pages, err := pagination.NewPager(c, u, func(r pagination.PageResult) pagination.Page {
    56  		return SubnetPage{pagination.LinkedPageBase{PageResult: r}}
    57  	}).AllPages()
    58  	if err != nil {
    59  		return nil, err
    60  	}
    61  
    62  	allSubnets, err := ExtractSubnets(pages)
    63  	if err != nil {
    64  		return nil, err
    65  	}
    66  
    67  	return FilterSubnets(allSubnets, opts)
    68  }
    69  
    70  func FilterSubnets(subnets []Subnet, opts ListOpts) ([]Subnet, error) {
    71  
    72  	var refinedSubnets []Subnet
    73  	var matched bool
    74  	m := map[string]interface{}{}
    75  
    76  	if opts.ID != "" {
    77  		m["ID"] = opts.ID
    78  	}
    79  	if opts.Name != "" {
    80  		m["Name"] = opts.Name
    81  	}
    82  	if opts.CIDR != "" {
    83  		m["CIDR"] = opts.CIDR
    84  	}
    85  	if opts.Status != "" {
    86  		m["Status"] = opts.Status
    87  	}
    88  	if opts.GatewayIP != "" {
    89  		m["GatewayIP"] = opts.GatewayIP
    90  	}
    91  	if opts.PRIMARY_DNS != "" {
    92  		m["PRIMARY_DNS"] = opts.PRIMARY_DNS
    93  	}
    94  	if opts.SECONDARY_DNS != "" {
    95  		m["SECONDARY_DNS"] = opts.SECONDARY_DNS
    96  	}
    97  	if opts.AvailabilityZone != "" {
    98  		m["AvailabilityZone"] = opts.AvailabilityZone
    99  	}
   100  	if opts.VPC_ID != "" {
   101  		m["VPC_ID"] = opts.VPC_ID
   102  	}
   103  
   104  	if len(m) > 0 && len(subnets) > 0 {
   105  		for _, subnet := range subnets {
   106  			matched = true
   107  
   108  			for key, value := range m {
   109  				if sVal := getStructField(&subnet, key); !(sVal == value) {
   110  					matched = false
   111  				}
   112  			}
   113  
   114  			if matched {
   115  				refinedSubnets = append(refinedSubnets, subnet)
   116  			}
   117  		}
   118  
   119  	} else {
   120  		refinedSubnets = subnets
   121  	}
   122  
   123  	return refinedSubnets, nil
   124  }
   125  
   126  func getStructField(v *Subnet, field string) string {
   127  	r := reflect.ValueOf(v)
   128  	f := reflect.Indirect(r).FieldByName(field)
   129  	return string(f.String())
   130  }
   131  
   132  // CreateOptsBuilder allows extensions to add additional parameters to the
   133  // Create request.
   134  type CreateOptsBuilder interface {
   135  	ToSubnetCreateMap() (map[string]interface{}, error)
   136  }
   137  
   138  // CreateOpts contains all the values needed to create a new subnets. There are
   139  // no required values.
   140  type CreateOpts struct {
   141  	Name             string         `json:"name" required:"true"`
   142  	CIDR             string         `json:"cidr" required:"true"`
   143  	DnsList          []string       `json:"dnsList,omitempty"`
   144  	GatewayIP        string         `json:"gateway_ip" required:"true"`
   145  	EnableIPv6       *bool          `json:"ipv6_enable,omitempty"`
   146  	EnableDHCP       bool           `json:"dhcp_enable" no_default:"y"`
   147  	PRIMARY_DNS      string         `json:"primary_dns,omitempty"`
   148  	SECONDARY_DNS    string         `json:"secondary_dns,omitempty"`
   149  	AvailabilityZone string         `json:"availability_zone,omitempty"`
   150  	VPC_ID           string         `json:"vpc_id" required:"true"`
   151  	ExtraDhcpOpts    []ExtraDhcpOpt `json:"extra_dhcp_opts,omitempty"`
   152  }
   153  
   154  type ExtraDhcpOpt struct {
   155  	OptName  string `json:"opt_name" required:"true"`
   156  	OptValue string `json:"opt_value,omitempty"`
   157  }
   158  
   159  // ToSubnetCreateMap builds a create request body from CreateOpts.
   160  func (opts CreateOpts) ToSubnetCreateMap() (map[string]interface{}, error) {
   161  	return golangsdk.BuildRequestBody(opts, "subnet")
   162  }
   163  
   164  // Create accepts a CreateOpts struct and uses the values to create a new
   165  // logical subnets. When it is created, the subnets does not have an internal
   166  // interface - it is not associated to any subnet.
   167  //
   168  func Create(c *golangsdk.ServiceClient, opts CreateOptsBuilder) (r CreateResult) {
   169  	b, err := opts.ToSubnetCreateMap()
   170  	if err != nil {
   171  		r.Err = err
   172  		return
   173  	}
   174  	reqOpt := &golangsdk.RequestOpts{OkCodes: []int{200}}
   175  	_, r.Err = c.Post(rootURL(c), b, &r.Body, reqOpt)
   176  	return
   177  }
   178  
   179  // Get retrieves a particular subnets based on its unique ID.
   180  func Get(c *golangsdk.ServiceClient, id string) (r GetResult) {
   181  	_, r.Err = c.Get(resourceURL(c, id), &r.Body, nil)
   182  	return
   183  }
   184  
   185  // UpdateOptsBuilder allows extensions to add additional parameters to the
   186  // Update request.
   187  type UpdateOptsBuilder interface {
   188  	//ToSubnetUpdateMap() (map[string]interface{}, error)
   189  	ToSubnetUpdateMap() (map[string]interface{}, error)
   190  }
   191  
   192  // UpdateOpts contains the values used when updating a subnets.
   193  type UpdateOpts struct {
   194  	Name          string         `json:"name,omitempty"`
   195  	EnableIPv6    *bool          `json:"ipv6_enable,omitempty"`
   196  	EnableDHCP    bool           `json:"dhcp_enable"`
   197  	PRIMARY_DNS   string         `json:"primary_dns,omitempty"`
   198  	SECONDARY_DNS string         `json:"secondary_dns,omitempty"`
   199  	DnsList       *[]string      `json:"dnsList,omitempty"`
   200  	ExtraDhcpOpts []ExtraDhcpOpt `json:"extra_dhcp_opts,omitempty"`
   201  }
   202  
   203  // ToSubnetUpdateMap builds an update body based on UpdateOpts.
   204  func (opts UpdateOpts) ToSubnetUpdateMap() (map[string]interface{}, error) {
   205  	return golangsdk.BuildRequestBody(opts, "subnet")
   206  }
   207  
   208  // Update allows subnets to be updated. You can update the name, administrative
   209  // state, and the external gateway.
   210  func Update(c *golangsdk.ServiceClient, vpcid string, id string, opts UpdateOptsBuilder) (r UpdateResult) {
   211  	b, err := opts.ToSubnetUpdateMap()
   212  	if err != nil {
   213  		r.Err = err
   214  		return
   215  	}
   216  	_, r.Err = c.Put(updateURL(c, vpcid, id), b, &r.Body, &golangsdk.RequestOpts{
   217  		OkCodes: []int{200},
   218  	})
   219  	return
   220  }
   221  
   222  // Delete will permanently delete a particular subnets based on its unique ID.
   223  func Delete(c *golangsdk.ServiceClient, vpcid string, id string) (r DeleteResult) {
   224  	_, r.Err = c.Delete(updateURL(c, vpcid, id), nil)
   225  	return
   226  }