github.com/chnsz/golangsdk@v0.0.0-20240506093406-85a3fbfa605b/openstack/er/v3/routes/requests.go (about) 1 package routes 2 3 import ( 4 "github.com/chnsz/golangsdk" 5 "github.com/chnsz/golangsdk/pagination" 6 ) 7 8 // CreateOpts is the structure used to create the route under a specified route table. 9 type CreateOpts struct { 10 // The destination of the route. 11 Destination string `json:"destination" required:"true"` 12 // The ID of the corresponding attachment. 13 AttachmentId string `json:"attachment_id,omitempty"` 14 // Whether route is the black hole route. 15 IsBlackHole *bool `json:"is_blackhole,omitempty"` 16 } 17 18 var requestOpts = golangsdk.RequestOpts{ 19 MoreHeaders: map[string]string{"Content-Type": "application/json", "X-Language": "en-us"}, 20 } 21 22 // Create is a method to create a new route under a specified route table. 23 func Create(client *golangsdk.ServiceClient, routeTableId string, opts CreateOpts) (*Route, error) { 24 b, err := golangsdk.BuildRequestBody(opts, "route") 25 if err != nil { 26 return nil, err 27 } 28 29 var r createResp 30 _, err = client.Post(rootURL(client, routeTableId), b, &r, &golangsdk.RequestOpts{ 31 MoreHeaders: requestOpts.MoreHeaders, 32 }) 33 return &r.Route, err 34 } 35 36 // Get is a method to obtain the route details using given parameters. 37 func Get(client *golangsdk.ServiceClient, routeTableId, routeId string) (*Route, error) { 38 var r getResp 39 _, err := client.Get(resourceURL(client, routeTableId, routeId), &r, &golangsdk.RequestOpts{ 40 MoreHeaders: requestOpts.MoreHeaders, 41 }) 42 return &r.Route, err 43 } 44 45 // ListOpts allows to filter list data using given parameters. 46 type ListOpts struct { 47 // Number of records to be queried. 48 // The valid value is range from 0 to 2000. 49 Limit int `q:"limit"` 50 // The ID of the route of the last record on the previous page. 51 // If it is empty, it is the first page of the query. 52 // This parameter must be used together with limit. 53 // The valid value is range from 1 to 128. 54 Marker string `q:"marker"` 55 // The list of the destinations, support for querying multiple routes. 56 Destination []string `q:"destination"` 57 // The list of attachment IDs, support for querying multiple routes. 58 AttachmentIds []string `json:"attachment_id"` 59 // The list of attachment resource types, support for querying multiple routes. 60 ResourceType []string `json:"resource_type"` 61 // The list of keyword to sort the associations result, sort by ID by default. 62 // The optional values are as follow: 63 // + id 64 // + name 65 // + state 66 SortKey []string `q:"sort_key"` 67 // The returned results are arranged in ascending or descending order, the default is asc. 68 SortDir []string `q:"sort_dir"` 69 } 70 71 // List is a method to query the list of the routes under a specified route table using given parameters. 72 func List(client *golangsdk.ServiceClient, routeTableId string, opts ListOpts) ([]Route, error) { 73 url := rootURL(client, routeTableId) 74 query, err := golangsdk.BuildQueryString(opts) 75 if err != nil { 76 return nil, err 77 } 78 url += query.String() 79 80 pages, err := pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page { 81 p := RoutePage{pagination.MarkerPageBase{PageResult: r}} 82 p.MarkerPageBase.Owner = p 83 return p 84 }).AllPages() 85 86 if err != nil { 87 return nil, err 88 } 89 return extractRoutes(pages) 90 } 91 92 // UpdateOpts is the structure used to update the route configuration. 93 type UpdateOpts struct { 94 // The ID of the corresponding attachment. 95 AttachmentId string `json:"attachment_id,omitempty"` 96 // Whether route is the black hole route. 97 IsBlackHole *bool `json:"is_blackhole,omitempty"` 98 } 99 100 // Update is a method to update route configuration using update option. 101 func Update(client *golangsdk.ServiceClient, routeTableId, routeId string, opts UpdateOpts) (*Route, error) { 102 b, err := golangsdk.BuildRequestBody(opts, "route") 103 if err != nil { 104 return nil, err 105 } 106 107 var r updateResp 108 _, err = client.Put(resourceURL(client, routeTableId, routeId), b, &r, &golangsdk.RequestOpts{ 109 MoreHeaders: requestOpts.MoreHeaders, 110 }) 111 return &r.Route, err 112 } 113 114 // Delete is a method to remove an existing route from a specified route table. 115 func Delete(client *golangsdk.ServiceClient, routeTableId, routeId string) error { 116 _, err := client.Delete(resourceURL(client, routeTableId, routeId), &golangsdk.RequestOpts{ 117 MoreHeaders: requestOpts.MoreHeaders, 118 }) 119 return err 120 }