github.com/chnsz/golangsdk@v0.0.0-20240506093406-85a3fbfa605b/openstack/er/v3/routetables/requests.go (about) 1 package routetables 2 3 import ( 4 "github.com/chnsz/golangsdk" 5 "github.com/chnsz/golangsdk/openstack/common/tags" 6 "github.com/chnsz/golangsdk/pagination" 7 ) 8 9 // CreateOpts is the structure required by the 'Create' method to create a route table under a specified ER instance. 10 type CreateOpts struct { 11 // The name of the route table. 12 // The value can contain 1 to 64 characters, only english and chinese letters, digits, underscore (_), hyphens (-) 13 // and dots (.) are allowed. 14 Name string `json:"name" required:"true"` 15 // The description of the route table. 16 // The value contain a maximum of 255 characters, and the angle brackets (< and >) are not allowed. 17 Description string `json:"description,omitempty"` 18 // The configuration of the BGP route selection. 19 BgpOptions BgpOptions `json:"bgp_options,omitempty"` 20 // The key/value pairs to associate with the route table. 21 Tags []tags.ResourceTag `json:"tags,omitempty"` 22 } 23 24 // BgpOptions is an object that represents the BGP configuration for routing. 25 type BgpOptions struct { 26 // Whether the AS path attributes of the routes are not compared during load balancing. 27 LoadBalancingAsPathIgnore *bool `json:"load_balancing_as_path_ignore,omitempty"` 28 // Whether the AS path attributes of the same length are not compared during load balancing. 29 LoadBalancingAsPathRelax *bool `json:"load_balancing_as_path_relax,omitempty"` 30 } 31 32 var requestOpts = golangsdk.RequestOpts{ 33 MoreHeaders: map[string]string{"Content-Type": "application/json", "X-Language": "en-us"}, 34 } 35 36 // Create is a method to create a new route table under a specified ER instance using given parameters. 37 func Create(client *golangsdk.ServiceClient, instanceId string, opts CreateOpts) (*RouteTable, error) { 38 b, err := golangsdk.BuildRequestBody(opts, "route_table") 39 if err != nil { 40 return nil, err 41 } 42 43 var r createResp 44 _, err = client.Post(rootURL(client, instanceId), b, &r, &golangsdk.RequestOpts{ 45 MoreHeaders: requestOpts.MoreHeaders, 46 }) 47 return &r.RouteTable, err 48 } 49 50 // Get is a method to obtain the route table details under a specified ER instance. 51 func Get(client *golangsdk.ServiceClient, instanceId, routeTableId string) (*RouteTable, error) { 52 var r getResp 53 _, err := client.Get(resourceURL(client, instanceId, routeTableId), &r, &golangsdk.RequestOpts{ 54 MoreHeaders: requestOpts.MoreHeaders, 55 }) 56 return &r.RouteTable, err 57 } 58 59 // ListOpts allows to filter list data using given parameters. 60 type ListOpts struct { 61 // Number of records to be queried. 62 // The valid value is range from 0 to 2000. 63 Limit int `q:"limit"` 64 // The ID of the route table of the last record on the previous page. 65 // If it is empty, it is the first page of the query. 66 // This parameter must be used together with limit. 67 // The valid value is range from 1 to 128. 68 Marker string `q:"marker"` 69 // The list of current status of the route tables, support for querying multiple route tables. 70 Status []string `q:"state"` 71 // Whether this route table is the default association route table. 72 IsDefaultAssociation bool `q:"is_default_association"` 73 // Whether this route table is the default propagation route table. 74 IsDefaultPropagation bool `q:"is_default_propagation"` 75 // The list of keyword to sort the route tables result, sort by ID by default. 76 // The optional values are as follow: 77 // + id 78 // + name 79 // + state 80 SortKey []string `q:"sort_key"` 81 // The returned results are arranged in ascending or descending order, the default is asc. 82 SortDir []string `q:"sort_dir"` 83 } 84 85 // List is a method to query the list of the route tables under a specified ER instance using given parameters. 86 func List(client *golangsdk.ServiceClient, instanceId string, opts ListOpts) ([]RouteTable, error) { 87 url := rootURL(client, instanceId) 88 query, err := golangsdk.BuildQueryString(opts) 89 if err != nil { 90 return nil, err 91 } 92 url += query.String() 93 94 pages, err := pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page { 95 p := RouteTablePage{pagination.MarkerPageBase{PageResult: r}} 96 p.MarkerPageBase.Owner = p 97 return p 98 }).AllPages() 99 100 if err != nil { 101 return nil, err 102 } 103 return extractRouteTables(pages) 104 } 105 106 // UpdateOpts is the structure required by the 'Update' method to update the route table configuration. 107 type UpdateOpts struct { 108 // The name of the route table. 109 // The value can contain 1 to 64 characters, only english and chinese letters, digits, underscore (_), hyphens (-) 110 // and dots (.) are allowed. 111 Name string `json:"name,omitempty"` 112 // The description of the route table. 113 // The value contain a maximum of 255 characters, and the angle brackets (< and >) are not allowed. 114 Description *string `json:"description,omitempty"` 115 } 116 117 // Update is a method to update the route table under a specified ER instance using parameters. 118 func Update(client *golangsdk.ServiceClient, instanceId, routeTableId string, opts UpdateOpts) (*RouteTable, error) { 119 b, err := golangsdk.BuildRequestBody(opts, "route_table") 120 if err != nil { 121 return nil, err 122 } 123 124 var r updateResp 125 _, err = client.Put(resourceURL(client, instanceId, routeTableId), b, &r, &golangsdk.RequestOpts{ 126 MoreHeaders: requestOpts.MoreHeaders, 127 }) 128 return &r.RouteTable, err 129 } 130 131 // Delete is a method to remove an existing route table under a specified ER instance. 132 func Delete(client *golangsdk.ServiceClient, instanceId, routeTableId string) error { 133 _, err := client.Delete(resourceURL(client, instanceId, routeTableId), &golangsdk.RequestOpts{ 134 MoreHeaders: requestOpts.MoreHeaders, 135 }) 136 return err 137 }