github.com/huaweicloud/golangsdk@v0.0.0-20210831081626-d823fe11ceba/openstack/networking/v1/routables/requests.go (about) 1 package routables 2 3 import ( 4 "github.com/huaweicloud/golangsdk" 5 "github.com/huaweicloud/golangsdk/pagination" 6 ) 7 8 // ListOptsBuilder is an interface by which can build the request body of listing route tables 9 type ListOptsBuilder interface { 10 ToRouteTableListQuery() (string, error) 11 } 12 13 // ListOpts allows to query all route tables or filter collections by parameters 14 // Marker and Limit are used for pagination. 15 type ListOpts struct { 16 // ID is the unique identifier for the route table 17 ID string `q:"id"` 18 // VpcID is the unique identifier for the vpc 19 VpcID string `q:"vpc_id"` 20 // SubnetID the unique identifier for the subnet 21 SubnetID string `q:"subnet_id"` 22 // Limit is the number of records returned for each page query, the value range is 0~intmax 23 Limit int `q:"limit"` 24 // Marker is the starting resource ID of the paging query, 25 // which means that the query starts from the next record of the specified resource 26 Marker string `q:"marker"` 27 } 28 29 // ToRouteTableListQuery formats a ListOpts into a query string 30 func (opts ListOpts) ToRouteTableListQuery() (string, error) { 31 q, err := golangsdk.BuildQueryString(opts) 32 if err != nil { 33 return "", err 34 } 35 return q.String(), nil 36 } 37 38 // List returns a Pager which allows you to iterate over a collection of 39 // vpc route tables. It accepts a ListOpts struct, which allows you to 40 // filter the returned collection for greater efficiency. 41 func List(c *golangsdk.ServiceClient, opts ListOptsBuilder) pagination.Pager { 42 url := rootURL(c) 43 44 if opts != nil { 45 query, err := opts.ToRouteTableListQuery() 46 if err != nil { 47 return pagination.Pager{Err: err} 48 } 49 url += query 50 } 51 52 return pagination.NewPager(c, url, func(r pagination.PageResult) pagination.Page { 53 p := RouteTablePage{pagination.MarkerPageBase{PageResult: r}} 54 p.MarkerPageBase.Owner = p 55 return p 56 }) 57 } 58 59 // CreateOptsBuilder allows extensions to add additional parameters to the 60 // Create request. 61 type CreateOptsBuilder interface { 62 ToRouteTableCreateMap() (map[string]interface{}, error) 63 } 64 65 // RouteOpts contains all the values needed to manage a vpc route 66 type RouteOpts struct { 67 // The destination CIDR block. The destination of each route must be unique. 68 // The destination cannot overlap with any subnet CIDR block in the VPC. 69 Destination string `json:"destination" required:"true"` 70 // the type of the next hop. value range: 71 // ecs, eni, vip, nat, peering, vpn, dc, cc, egw 72 Type string `json:"type" required:"true"` 73 // the instance ID of next hop 74 NextHop string `json:"nexthop" required:"true"` 75 // The supplementary information about the route. The description can contain 76 // a maximum of 255 characters and cannot contain angle brackets (< or >). 77 Description *string `json:"description,omitempty"` 78 } 79 80 // CreateOpts contains all the values needed to create a new route table 81 type CreateOpts struct { 82 // The VPC ID that the route table belongs to 83 VpcID string `json:"vpc_id" required:"true"` 84 // The name of the route table. The name can contain a maximum of 64 characters, 85 // which may consist of letters, digits, underscores (_), hyphens (-), and periods (.). 86 // The name cannot contain spaces. 87 Name string `json:"name" required:"true"` 88 // The supplementary information about the route table. The description can contain 89 // a maximum of 255 characters and cannot contain angle brackets (< or >). 90 Description string `json:"description,omitempty"` 91 // The route information 92 Routes []RouteOpts `json:"routes,omitempty"` 93 } 94 95 // ToRouteTableCreateMap builds a create request body from CreateOpts 96 func (opts CreateOpts) ToRouteTableCreateMap() (map[string]interface{}, error) { 97 return golangsdk.BuildRequestBody(opts, "routetable") 98 } 99 100 // Create accepts a CreateOpts struct and uses the values to create a new 101 // route table 102 func Create(c *golangsdk.ServiceClient, opts CreateOptsBuilder) (r CreateResult) { 103 b, err := opts.ToRouteTableCreateMap() 104 if err != nil { 105 r.Err = err 106 return 107 } 108 109 _, r.Err = c.Post(rootURL(c), b, &r.Body, nil) 110 return 111 } 112 113 // Get retrieves a particular route table based on its unique ID 114 func Get(c *golangsdk.ServiceClient, id string) (r GetResult) { 115 _, r.Err = c.Get(resourceURL(c, id), &r.Body, nil) 116 return 117 } 118 119 // UpdateOptsBuilder allows extensions to add additional parameters to the 120 // Update request 121 type UpdateOptsBuilder interface { 122 ToRouteTableUpdateMap() (map[string]interface{}, error) 123 } 124 125 // UpdateOpts contains the values used when updating a route table 126 type UpdateOpts struct { 127 Name string `json:"name,omitempty"` 128 Description *string `json:"description,omitempty"` 129 Routes map[string][]RouteOpts `json:"routes,omitempty"` 130 } 131 132 // ToRouteTableUpdateMap builds an update body based on UpdateOpts 133 func (opts UpdateOpts) ToRouteTableUpdateMap() (map[string]interface{}, error) { 134 return golangsdk.BuildRequestBody(opts, "routetable") 135 } 136 137 // Update allows route tables to be updated 138 func Update(c *golangsdk.ServiceClient, id string, opts UpdateOptsBuilder) (r UpdateResult) { 139 b, err := opts.ToRouteTableUpdateMap() 140 if err != nil { 141 r.Err = err 142 return 143 } 144 _, r.Err = c.Put(resourceURL(c, id), b, &r.Body, &golangsdk.RequestOpts{ 145 OkCodes: []int{200}, 146 }) 147 return 148 } 149 150 // Delete will permanently delete a particular route table based on its unique ID 151 func Delete(c *golangsdk.ServiceClient, id string) (r DeleteResult) { 152 _, r.Err = c.Delete(resourceURL(c, id), nil) 153 return 154 } 155 156 // ActionOptsBuilder allows extensions to add additional parameters to the 157 // Action request: associate or disassociate subnets with a route table 158 type ActionOptsBuilder interface { 159 ToRouteTableActionMap() (map[string]interface{}, error) 160 } 161 162 // ActionSubnetsOpts contains the subnets list that associate or disassociate with a route tabl 163 type ActionSubnetsOpts struct { 164 Associate []string `json:"associate,omitempty"` 165 Disassociate []string `json:"disassociate,omitempty"` 166 } 167 168 // ActionOpts contains the values used when associating or disassociating subnets with a route table 169 type ActionOpts struct { 170 Subnets ActionSubnetsOpts `json:"subnets" required:"true"` 171 } 172 173 // ToRouteTableActionMap builds an update body based on UpdateOpts. 174 func (opts ActionOpts) ToRouteTableActionMap() (map[string]interface{}, error) { 175 return golangsdk.BuildRequestBody(opts, "routetable") 176 } 177 178 // Action will associate or disassociate subnets with a particular route table based on its unique ID 179 func Action(c *golangsdk.ServiceClient, id string, opts ActionOptsBuilder) (r ActionResult) { 180 b, err := opts.ToRouteTableActionMap() 181 if err != nil { 182 r.Err = err 183 return 184 } 185 _, r.Err = c.Put(resourceURL(c, id), b, &r.Body, &golangsdk.RequestOpts{ 186 OkCodes: []int{200}, 187 }) 188 return 189 }