github.com/opentelekomcloud/gophertelekomcloud@v0.9.3/openstack/networking/v2/ports/requests.go (about) 1 package ports 2 3 import ( 4 "github.com/opentelekomcloud/gophertelekomcloud" 5 "github.com/opentelekomcloud/gophertelekomcloud/pagination" 6 ) 7 8 // ListOptsBuilder allows extensions to add additional parameters to the 9 // List request. 10 type ListOptsBuilder interface { 11 ToPortListQuery() (string, error) 12 } 13 14 // ListOpts allows the filtering and sorting of paginated collections through 15 // the API. Filtering is achieved by passing in struct field values that map to 16 // the port attributes you want to see returned. SortKey allows you to sort 17 // by a particular port attribute. SortDir sets the direction, and is either 18 // `asc' or `desc'. Marker and Limit are used for pagination. 19 type ListOpts struct { 20 Status string `q:"status"` 21 Name string `q:"name"` 22 AdminStateUp *bool `q:"admin_state_up"` 23 NetworkID string `q:"network_id"` 24 TenantID string `q:"tenant_id"` 25 ProjectID string `q:"project_id"` 26 DeviceOwner string `q:"device_owner"` 27 MACAddress string `q:"mac_address"` 28 ID string `q:"id"` 29 DeviceID string `q:"device_id"` 30 Limit int `q:"limit"` 31 Marker string `q:"marker"` 32 SortKey string `q:"sort_key"` 33 SortDir string `q:"sort_dir"` 34 } 35 36 // ToPortListQuery formats a ListOpts into a query string. 37 func (opts ListOpts) ToPortListQuery() (string, error) { 38 q, err := golangsdk.BuildQueryString(opts) 39 if err != nil { 40 return "", err 41 } 42 return q.String(), err 43 } 44 45 // List returns a Pager which allows you to iterate over a collection of 46 // ports. It accepts a ListOpts struct, which allows you to filter and sort 47 // the returned collection for greater efficiency. 48 // 49 // Default policy settings return only those ports that are owned by the tenant 50 // who submits the request, unless the request is submitted by a user with 51 // administrative rights. 52 func List(c *golangsdk.ServiceClient, opts ListOptsBuilder) pagination.Pager { 53 url := listURL(c) 54 if opts != nil { 55 query, err := opts.ToPortListQuery() 56 if err != nil { 57 return pagination.Pager{Err: err} 58 } 59 url += query 60 } 61 return pagination.NewPager(c, url, func(r pagination.PageResult) pagination.Page { 62 return PortPage{pagination.LinkedPageBase{PageResult: r}} 63 }) 64 } 65 66 // Get retrieves a specific port based on its unique ID. 67 func Get(c *golangsdk.ServiceClient, id string) (r GetResult) { 68 _, r.Err = c.Get(getURL(c, id), &r.Body, nil) 69 return 70 } 71 72 // CreateOptsBuilder allows extensions to add additional parameters to the 73 // Create request. 74 type CreateOptsBuilder interface { 75 ToPortCreateMap() (map[string]interface{}, error) 76 } 77 78 // CreateOpts represents the attributes used when creating a new port. 79 type CreateOpts struct { 80 NetworkID string `json:"network_id" required:"true"` 81 Name string `json:"name,omitempty"` 82 AdminStateUp *bool `json:"admin_state_up,omitempty"` 83 MACAddress string `json:"mac_address,omitempty"` 84 FixedIPs interface{} `json:"fixed_ips,omitempty"` 85 DeviceID string `json:"device_id,omitempty"` 86 DeviceOwner string `json:"device_owner,omitempty"` 87 TenantID string `json:"tenant_id,omitempty"` 88 ProjectID string `json:"project_id,omitempty"` 89 SecurityGroups *[]string `json:"security_groups,omitempty"` 90 AllowedAddressPairs []AddressPair `json:"allowed_address_pairs,omitempty"` 91 } 92 93 // ToPortCreateMap builds a request body from CreateOpts. 94 func (opts CreateOpts) ToPortCreateMap() (map[string]interface{}, error) { 95 return golangsdk.BuildRequestBody(opts, "port") 96 } 97 98 // Create accepts a CreateOpts struct and creates a new network using the values 99 // provided. You must remember to provide a NetworkID value. 100 func Create(c *golangsdk.ServiceClient, opts CreateOptsBuilder) (r CreateResult) { 101 b, err := opts.ToPortCreateMap() 102 if err != nil { 103 r.Err = err 104 return 105 } 106 _, r.Err = c.Post(createURL(c), b, &r.Body, nil) 107 return 108 } 109 110 // UpdateOptsBuilder allows extensions to add additional parameters to the 111 // Update request. 112 type UpdateOptsBuilder interface { 113 ToPortUpdateMap() (map[string]interface{}, error) 114 } 115 116 // UpdateOpts represents the attributes used when updating an existing port. 117 type UpdateOpts struct { 118 Name string `json:"name,omitempty"` 119 AdminStateUp *bool `json:"admin_state_up,omitempty"` 120 FixedIPs interface{} `json:"fixed_ips,omitempty"` 121 DeviceID string `json:"device_id,omitempty"` 122 DeviceOwner string `json:"device_owner,omitempty"` 123 SecurityGroups *[]string `json:"security_groups,omitempty"` 124 AllowedAddressPairs *[]AddressPair `json:"allowed_address_pairs,omitempty"` 125 } 126 127 // ToPortUpdateMap builds a request body from UpdateOpts. 128 func (opts UpdateOpts) ToPortUpdateMap() (map[string]interface{}, error) { 129 return golangsdk.BuildRequestBody(opts, "port") 130 } 131 132 // Update accepts a UpdateOpts struct and updates an existing port using the 133 // values provided. 134 func Update(c *golangsdk.ServiceClient, id string, opts UpdateOptsBuilder) (r UpdateResult) { 135 b, err := opts.ToPortUpdateMap() 136 if err != nil { 137 r.Err = err 138 return 139 } 140 _, r.Err = c.Put(updateURL(c, id), b, &r.Body, &golangsdk.RequestOpts{ 141 OkCodes: []int{200, 201}, 142 }) 143 return 144 } 145 146 // Delete accepts a unique ID and deletes the port associated with it. 147 func Delete(c *golangsdk.ServiceClient, id string) (r DeleteResult) { 148 _, r.Err = c.Delete(deleteURL(c, id), nil) 149 return 150 } 151 152 // IDFromName is a convenience function that returns a port's ID, 153 // given its name. 154 func IDFromName(client *golangsdk.ServiceClient, name string) (string, error) { 155 count := 0 156 id := "" 157 158 listOpts := ListOpts{ 159 Name: name, 160 } 161 162 pages, err := List(client, listOpts).AllPages() 163 if err != nil { 164 return "", err 165 } 166 167 all, err := ExtractPorts(pages) 168 if err != nil { 169 return "", err 170 } 171 172 for _, s := range all { 173 if s.Name == name { 174 count++ 175 id = s.ID 176 } 177 } 178 179 switch count { 180 case 0: 181 return "", golangsdk.ErrResourceNotFound{Name: name, ResourceType: "port"} 182 case 1: 183 return id, nil 184 default: 185 return "", golangsdk.ErrMultipleResourcesFound{Name: name, Count: count, ResourceType: "port"} 186 } 187 }