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

     1  package subnets
     2  
     3  import (
     4  	"net/http"
     5  
     6  	"github.com/huaweicloud/golangsdk"
     7  )
     8  
     9  type CreateOpts struct {
    10  	// Specifies the subnet name. The value is a string of 1 to 64
    11  	// characters that can contain letters, digits, underscores (_), and hyphens (-).
    12  	Name string `json:"name" required:"true"`
    13  
    14  	// Specifies the network segment on which the subnet resides. The
    15  	// value must be in CIDR format. The value must be within the CIDR block of the VPC. The
    16  	// subnet mask cannot be greater than 28.
    17  	Cidr string `json:"cidr" required:"true"`
    18  
    19  	// Specifies the gateway of the subnet. The value must be a valid
    20  	// IP address. The value must be an IP address in the subnet segment.
    21  	GatewayIP string `json:"gateway_ip" required:"true"`
    22  
    23  	// Specifies whether the DHCP function is enabled for the subnet.
    24  	// The value can be true or false. If this parameter is left blank, it is set to true by
    25  	// default.
    26  	DhcpEnable *bool `json:"dhcp_enable,omitempty"`
    27  
    28  	// Specifies the IP address of DNS server 1 on the subnet. The
    29  	// value must be a valid IP address.
    30  	PrimaryDNS string `json:"primary_dns,omitempty"`
    31  
    32  	// Specifies the IP address of DNS server 2 on the subnet. The
    33  	// value must be a valid IP address.
    34  	SecondaryDNS string `json:"secondary_dns,omitempty"`
    35  
    36  	// Specifies the DNS server address list of a subnet. This field
    37  	// is required if you need to use more than two DNS servers. This parameter value is the
    38  	// superset of both DNS server address 1 and DNS server address 2.
    39  	DNSList []string `json:"dnsList,omitempty"`
    40  
    41  	// Specifies the ID of the VPC to which the subnet belongs.
    42  	VpcID string `json:"vpc_id" required:"true"`
    43  
    44  	SiteID string `json:"site_id,omitempty" required:"true"`
    45  }
    46  
    47  type CreateOptsBuilder interface {
    48  	ToCreateMap() (map[string]interface{}, error)
    49  }
    50  
    51  func (opts CreateOpts) ToCreateMap() (map[string]interface{}, error) {
    52  	b, err := golangsdk.BuildRequestBody(opts, "subnet")
    53  	if err != nil {
    54  		return nil, err
    55  	}
    56  	return b, nil
    57  }
    58  
    59  type VpcResponse struct {
    60  	Code    string `json:"code"`
    61  	Message string `json:"message"`
    62  }
    63  
    64  func Create(client *golangsdk.ServiceClient, opts CreateOptsBuilder) (r CreateResult) {
    65  	b, err := opts.ToCreateMap()
    66  	if err != nil {
    67  		r.Err = err
    68  		return
    69  	}
    70  
    71  	createURL := rootURL(client)
    72  
    73  	var resp *http.Response
    74  	resp, r.Err = client.Post(createURL, b, &r.Body, &golangsdk.RequestOpts{
    75  		OkCodes: []int{http.StatusOK},
    76  	})
    77  	if r.Err != nil {
    78  		return
    79  	}
    80  	defer resp.Body.Close()
    81  
    82  	return
    83  }
    84  
    85  func Delete(client *golangsdk.ServiceClient, subnetId string) (r DeleteResult) {
    86  	deleteURL := DeleteURL(client, subnetId)
    87  
    88  	var resp *http.Response
    89  	resp, r.Err = client.Delete(deleteURL, &golangsdk.RequestOpts{
    90  		OkCodes: []int{http.StatusNoContent},
    91  	})
    92  	if r.Err != nil {
    93  		return
    94  	}
    95  	defer resp.Body.Close()
    96  
    97  	return
    98  }
    99  
   100  func Get(client *golangsdk.ServiceClient, subnetId string) (r GetResult) {
   101  	getURL := GetURL(client, subnetId)
   102  
   103  	var resp *http.Response
   104  	resp, r.Err = client.Get(getURL, &r.Body, &golangsdk.RequestOpts{
   105  		OkCodes: []int{http.StatusOK},
   106  	})
   107  	if r.Err != nil {
   108  		return
   109  	}
   110  	defer resp.Body.Close()
   111  
   112  	return
   113  }
   114  
   115  type UpdateOpts struct {
   116  	// Specifies the subnet name. The value is a string of 1 to 64
   117  	// characters that can contain letters, digits, underscores (_), and hyphens (-).
   118  	Name string `json:"name" required:"true"`
   119  
   120  	// Specifies whether the DHCP function is enabled for the subnet.
   121  	// The value can be true or false. If this parameter is left blank, it is set to true by
   122  	// default.
   123  	DhcpEnable *bool `json:"dhcp_enable,omitempty"`
   124  
   125  	// Specifies the IP address of DNS server 1 on the subnet. The
   126  	// value must be a valid IP address.
   127  	PrimaryDNS string `json:"primary_dns,omitempty"`
   128  
   129  	// Specifies the IP address of DNS server 2 on the subnet. The
   130  	// value must be a valid IP address.
   131  	SecondaryDNS string `json:"secondary_dns,omitempty"`
   132  
   133  	// Specifies the DNS server address list of a subnet. This field
   134  	// is required if you need to use more than two DNS servers. This parameter value is the
   135  	// superset of both DNS server address 1 and DNS server address 2.
   136  	DNSList *[]string `json:"dnsList,omitempty"`
   137  }
   138  
   139  type UpdateOptsBuilder interface {
   140  	ToUpdateMap() (map[string]interface{}, error)
   141  }
   142  
   143  func (opts UpdateOpts) ToUpdateMap() (map[string]interface{}, error) {
   144  	b, err := golangsdk.BuildRequestBody(opts, "subnet")
   145  	if err != nil {
   146  		return nil, err
   147  	}
   148  	return b, nil
   149  }
   150  
   151  func Update(client *golangsdk.ServiceClient, subnetId string, opts UpdateOptsBuilder) (r UpdateResult) {
   152  	b, err := opts.ToUpdateMap()
   153  	if err != nil {
   154  		r.Err = err
   155  		return
   156  	}
   157  	updateURL := UpdateURL(client, subnetId)
   158  
   159  	var resp *http.Response
   160  	resp, r.Err = client.Put(updateURL, b, &r.Body, &golangsdk.RequestOpts{
   161  		OkCodes: []int{http.StatusOK},
   162  	})
   163  	if r.Err != nil {
   164  		return
   165  	}
   166  	defer resp.Body.Close()
   167  
   168  	return
   169  }
   170  
   171  // ListOpts allows the filtering collections through the API.
   172  type ListOpts struct {
   173  	Limit  int    `q:"limit"`
   174  	Offset int    `q:"offset"`
   175  	VpcID  string `q:"vpc_id"`
   176  	SiteID string `q:"site_id"`
   177  }
   178  
   179  type ListSubnetsOptsBuilder interface {
   180  	ToListSubnetsQuery() (string, error)
   181  }
   182  
   183  // ToListSubnetsQuery converts ListSubnetsOpts structures to query string
   184  func (opts ListOpts) ToListSubnetsQuery() (string, error) {
   185  	q, err := golangsdk.BuildQueryString(opts)
   186  	if err != nil {
   187  		return "", err
   188  	}
   189  
   190  	return q.String(), err
   191  }
   192  
   193  // List returns collection of subnets.
   194  func List(client *golangsdk.ServiceClient, opts ListSubnetsOptsBuilder) (r ListResult) {
   195  	listURL := rootURL(client)
   196  	if opts != nil {
   197  		query, err := opts.ToListSubnetsQuery()
   198  		if err != nil {
   199  			r.Err = err
   200  			return
   201  		}
   202  		listURL += query
   203  	}
   204  
   205  	_, r.Err = client.Get(listURL, &r.Body, &golangsdk.RequestOpts{
   206  		OkCodes: []int{http.StatusOK},
   207  	})
   208  	return
   209  }