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