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