github.com/huaweicloud/golangsdk@v0.0.0-20210831081626-d823fe11ceba/openstack/networking/v2/peerings/requests.go (about) 1 package peerings 2 3 import ( 4 "reflect" 5 6 "github.com/huaweicloud/golangsdk" 7 "github.com/huaweicloud/golangsdk/pagination" 8 ) 9 10 // ListOpts allows the filtering of paginated collections through 11 // the API. Filtering is achieved by passing in struct field values that map to 12 // the floating IP attributes you want to see returned. 13 type ListOpts struct { 14 //ID is the unique identifier for the vpc_peering_connection. 15 ID string `q:"id"` 16 17 //Name is the human readable name for the vpc_peering_connection. It does not have to be 18 // unique. 19 Name string `q:"name"` 20 21 //Status indicates whether or not a vpc_peering_connection is currently operational. 22 Status string `q:"status"` 23 24 // TenantId indicates vpc_peering_connection avalable in specific tenant. 25 TenantId string `q:"tenant_id"` 26 27 // VpcId indicates vpc_peering_connection avalable in specific vpc. 28 VpcId string `q:"vpc_id"` 29 30 // VpcId indicates vpc_peering_connection available in specific vpc. 31 Peer_VpcId string 32 } 33 34 func FilterVpcIdParam(opts ListOpts) (filter ListOpts) { 35 36 if opts.VpcId != "" { 37 filter.VpcId = opts.VpcId 38 } else { 39 filter.VpcId = opts.Peer_VpcId 40 } 41 42 filter.Name = opts.Name 43 filter.ID = opts.ID 44 filter.Status = opts.Status 45 filter.TenantId = opts.TenantId 46 47 return filter 48 } 49 50 // List returns a Pager which allows you to iterate over a collection of 51 // vpc_peering_connection resources. It accepts a ListOpts struct, which allows you to 52 // filter the returned collection for greater efficiency. 53 func List(c *golangsdk.ServiceClient, opts ListOpts) ([]Peering, error) { 54 filter := FilterVpcIdParam(opts) 55 q, err := golangsdk.BuildQueryString(&filter) 56 if err != nil { 57 return nil, err 58 } 59 u := rootURL(c) + q.String() 60 pages, err := pagination.NewPager(c, u, func(r pagination.PageResult) pagination.Page { 61 return PeeringConnectionPage{pagination.LinkedPageBase{PageResult: r}} 62 }).AllPages() 63 if err != nil { 64 return nil, err 65 } 66 67 allPeeringConns, err := ExtractPeerings(pages) 68 if err != nil { 69 return nil, err 70 } 71 72 return FilterVpcPeeringConns(allPeeringConns, opts) 73 } 74 75 func FilterVpcPeeringConns(peerings []Peering, opts ListOpts) ([]Peering, error) { 76 var refinedPeerings []Peering 77 var matched bool 78 filterMap := map[string]interface{}{} 79 80 if opts.VpcId != "" { 81 filterMap["RequestVpcInfo"] = opts.VpcId 82 } 83 84 if opts.Peer_VpcId != "" { 85 filterMap["AcceptVpcInfo"] = opts.Peer_VpcId 86 } 87 88 if len(filterMap) > 0 && len(peerings) > 0 { 89 for _, peering := range peerings { 90 matched = true 91 92 for key, value := range filterMap { 93 if sVal := getStructNestedField(&peering, key); !(sVal == value) { 94 matched = false 95 } 96 } 97 98 if matched { 99 refinedPeerings = append(refinedPeerings, peering) 100 } 101 102 } 103 } else { 104 refinedPeerings = peerings 105 } 106 107 return refinedPeerings, nil 108 109 } 110 111 func getStructNestedField(v *Peering, field string) string { 112 r := reflect.ValueOf(v) 113 f := reflect.Indirect(r).FieldByName(field).Interface() 114 r1 := reflect.ValueOf(f) 115 f1 := reflect.Indirect(r1).FieldByName("VpcId") 116 return string(f1.String()) 117 } 118 119 func Get(c *golangsdk.ServiceClient, id string) (r GetResult) { 120 _, r.Err = c.Get(resourceURL(c, id), &r.Body, nil) 121 return 122 } 123 124 // Accept is used by a tenant to accept a VPC peering connection request initiated by another tenant. 125 func Accept(c *golangsdk.ServiceClient, id string) (r AcceptResult) { 126 _, r.Err = c.Put(acceptURL(c, id), nil, &r.Body, &golangsdk.RequestOpts{ 127 OkCodes: []int{200}, 128 }) 129 return 130 } 131 132 // Reject is used by a tenant to reject a VPC peering connection request initiated by another tenant. 133 func Reject(c *golangsdk.ServiceClient, id string) (r RejectResult) { 134 _, r.Err = c.Put(rejectURL(c, id), nil, &r.Body, &golangsdk.RequestOpts{ 135 OkCodes: []int{200}, 136 }) 137 return 138 } 139 140 //CreateOptsBuilder is an interface by which can build the request body of vpc peering connection. 141 type CreateOptsBuilder interface { 142 ToPeeringCreateMap() (map[string]interface{}, error) 143 } 144 145 //CreateOpts is a struct which is used to create vpc peering connection. 146 type CreateOpts struct { 147 Name string `json:"name"` 148 RequestVpcInfo VpcInfo `json:"request_vpc_info" required:"true"` 149 AcceptVpcInfo VpcInfo `json:"accept_vpc_info" required:"true"` 150 } 151 152 //ToVpcPeeringCreateMap builds a create request body from CreateOpts. 153 func (opts CreateOpts) ToPeeringCreateMap() (map[string]interface{}, error) { 154 return golangsdk.BuildRequestBody(opts, "peering") 155 } 156 157 //Create is a method by which can access to create the vpc peering connection. 158 func Create(client *golangsdk.ServiceClient, opts CreateOptsBuilder) (r CreateResult) { 159 b, err := opts.ToPeeringCreateMap() 160 if err != nil { 161 r.Err = err 162 return 163 } 164 _, r.Err = client.Post(rootURL(client), b, &r.Body, &golangsdk.RequestOpts{ 165 OkCodes: []int{201}, 166 }) 167 return 168 } 169 170 //Delete is a method by which can be able to delete a vpc peering connection. 171 func Delete(client *golangsdk.ServiceClient, id string) (r DeleteResult) { 172 _, r.Err = client.Delete(resourceURL(client, id), nil) 173 return 174 } 175 176 //UpdateOptsBuilder is an interface by which can be able to build the request body of vpc peering connection. 177 type UpdateOptsBuilder interface { 178 ToVpcPeeringUpdateMap() (map[string]interface{}, error) 179 } 180 181 //UpdateOpts is a struct which represents the request body of update method. 182 type UpdateOpts struct { 183 Name string `json:"name,omitempty"` 184 } 185 186 //ToVpcPeeringUpdateMap builds a update request body from UpdateOpts. 187 func (opts UpdateOpts) ToVpcPeeringUpdateMap() (map[string]interface{}, error) { 188 return golangsdk.BuildRequestBody(opts, "peering") 189 } 190 191 //Update is a method which can be able to update the name of vpc peering connection. 192 func Update(client *golangsdk.ServiceClient, id string, opts UpdateOptsBuilder) (r UpdateResult) { 193 b, err := opts.ToVpcPeeringUpdateMap() 194 if err != nil { 195 r.Err = err 196 return 197 } 198 _, r.Err = client.Put(resourceURL(client, id), b, &r.Body, &golangsdk.RequestOpts{ 199 OkCodes: []int{200}, 200 }) 201 return 202 }