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

     1  package networks
     2  
     3  import (
     4  	"github.com/huaweicloud/golangsdk"
     5  	"github.com/huaweicloud/golangsdk/pagination"
     6  )
     7  
     8  // ListOptsBuilder allows extensions to add additional parameters to the
     9  // List request.
    10  type ListOptsBuilder interface {
    11  	ToNetworkListQuery() (string, error)
    12  }
    13  
    14  // ListOpts allows the filtering and sorting of paginated collections through
    15  // the API. Filtering is achieved by passing in struct field values that map to
    16  // the network attributes you want to see returned. SortKey allows you to sort
    17  // by a particular network attribute. SortDir sets the direction, and is either
    18  // `asc' or `desc'. Marker and Limit are used for pagination.
    19  type ListOpts struct {
    20  	Status       string `q:"status"`
    21  	Name         string `q:"name"`
    22  	AdminStateUp *bool  `q:"admin_state_up"`
    23  	TenantID     string `q:"tenant_id"`
    24  	ProjectID    string `q:"project_id"`
    25  	Shared       *bool  `q:"shared"`
    26  	ID           string `q:"id"`
    27  	Marker       string `q:"marker"`
    28  	Limit        int    `q:"limit"`
    29  	SortKey      string `q:"sort_key"`
    30  	SortDir      string `q:"sort_dir"`
    31  }
    32  
    33  // ToNetworkListQuery formats a ListOpts into a query string.
    34  func (opts ListOpts) ToNetworkListQuery() (string, error) {
    35  	q, err := golangsdk.BuildQueryString(opts)
    36  	return q.String(), err
    37  }
    38  
    39  // List returns a Pager which allows you to iterate over a collection of
    40  // networks. It accepts a ListOpts struct, which allows you to filter and sort
    41  // the returned collection for greater efficiency.
    42  func List(c *golangsdk.ServiceClient, opts ListOptsBuilder) pagination.Pager {
    43  	url := listURL(c)
    44  	if opts != nil {
    45  		query, err := opts.ToNetworkListQuery()
    46  		if err != nil {
    47  			return pagination.Pager{Err: err}
    48  		}
    49  		url += query
    50  	}
    51  	return pagination.NewPager(c, url, func(r pagination.PageResult) pagination.Page {
    52  		return NetworkPage{pagination.LinkedPageBase{PageResult: r}}
    53  	})
    54  }
    55  
    56  // Get retrieves a specific network based on its unique ID.
    57  func Get(c *golangsdk.ServiceClient, id string) (r GetResult) {
    58  	_, r.Err = c.Get(getURL(c, id), &r.Body, nil)
    59  	return
    60  }
    61  
    62  // CreateOptsBuilder allows extensions to add additional parameters to the
    63  // Create request.
    64  type CreateOptsBuilder interface {
    65  	ToNetworkCreateMap() (map[string]interface{}, error)
    66  }
    67  
    68  // CreateOpts represents options used to create a network.
    69  type CreateOpts struct {
    70  	AdminStateUp          *bool    `json:"admin_state_up,omitempty"`
    71  	Name                  string   `json:"name,omitempty"`
    72  	Shared                *bool    `json:"shared,omitempty"`
    73  	TenantID              string   `json:"tenant_id,omitempty"`
    74  	ProjectID             string   `json:"project_id,omitempty"`
    75  	AvailabilityZoneHints []string `json:"availability_zone_hints,omitempty"`
    76  }
    77  
    78  // ToNetworkCreateMap builds a request body from CreateOpts.
    79  func (opts CreateOpts) ToNetworkCreateMap() (map[string]interface{}, error) {
    80  	return golangsdk.BuildRequestBody(opts, "network")
    81  }
    82  
    83  // Create accepts a CreateOpts struct and creates a new network using the values
    84  // provided. This operation does not actually require a request body, i.e. the
    85  // CreateOpts struct argument can be empty.
    86  //
    87  // The tenant ID that is contained in the URI is the tenant that creates the
    88  // network. An admin user, however, has the option of specifying another tenant
    89  // ID in the CreateOpts struct.
    90  func Create(c *golangsdk.ServiceClient, opts CreateOptsBuilder) (r CreateResult) {
    91  	b, err := opts.ToNetworkCreateMap()
    92  	if err != nil {
    93  		r.Err = err
    94  		return
    95  	}
    96  	_, r.Err = c.Post(createURL(c), b, &r.Body, nil)
    97  	return
    98  }
    99  
   100  // UpdateOptsBuilder allows extensions to add additional parameters to the
   101  // Update request.
   102  type UpdateOptsBuilder interface {
   103  	ToNetworkUpdateMap() (map[string]interface{}, error)
   104  }
   105  
   106  // UpdateOpts represents options used to update a network.
   107  type UpdateOpts struct {
   108  	AdminStateUp *bool  `json:"admin_state_up,omitempty"`
   109  	Name         string `json:"name,omitempty"`
   110  	Shared       *bool  `json:"shared,omitempty"`
   111  }
   112  
   113  // ToNetworkUpdateMap builds a request body from UpdateOpts.
   114  func (opts UpdateOpts) ToNetworkUpdateMap() (map[string]interface{}, error) {
   115  	return golangsdk.BuildRequestBody(opts, "network")
   116  }
   117  
   118  // Update accepts a UpdateOpts struct and updates an existing network using the
   119  // values provided. For more information, see the Create function.
   120  func Update(c *golangsdk.ServiceClient, networkID string, opts UpdateOptsBuilder) (r UpdateResult) {
   121  	b, err := opts.ToNetworkUpdateMap()
   122  	if err != nil {
   123  		r.Err = err
   124  		return
   125  	}
   126  	_, r.Err = c.Put(updateURL(c, networkID), b, &r.Body, &golangsdk.RequestOpts{
   127  		OkCodes: []int{200, 201},
   128  	})
   129  	return
   130  }
   131  
   132  // Delete accepts a unique ID and deletes the network associated with it.
   133  func Delete(c *golangsdk.ServiceClient, networkID string) (r DeleteResult) {
   134  	_, r.Err = c.Delete(deleteURL(c, networkID), nil)
   135  	return
   136  }
   137  
   138  // IDFromName is a convenience function that returns a network's ID, given
   139  // its name.
   140  func IDFromName(client *golangsdk.ServiceClient, name string) (string, error) {
   141  	count := 0
   142  	id := ""
   143  
   144  	listOpts := ListOpts{
   145  		Name: name,
   146  	}
   147  
   148  	pages, err := List(client, listOpts).AllPages()
   149  	if err != nil {
   150  		return "", err
   151  	}
   152  
   153  	all, err := ExtractNetworks(pages)
   154  	if err != nil {
   155  		return "", err
   156  	}
   157  
   158  	for _, s := range all {
   159  		if s.Name == name {
   160  			count++
   161  			id = s.ID
   162  		}
   163  	}
   164  
   165  	switch count {
   166  	case 0:
   167  		return "", golangsdk.ErrResourceNotFound{Name: name, ResourceType: "network"}
   168  	case 1:
   169  		return id, nil
   170  	default:
   171  		return "", golangsdk.ErrMultipleResourcesFound{Name: name, Count: count, ResourceType: "network"}
   172  	}
   173  }