github.com/opentelekomcloud/gophertelekomcloud@v0.9.3/openstack/networking/v2/networks/requests.go (about) 1 package networks 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 ToNetworkListQuery() (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 network attributes you want to see returned. SortKey allows you to sort 17 // by a particular network 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 TenantID string `q:"tenant_id"` 24 ProjectID string `q:"project_id"` 25 Shared *bool `q:"shared"` 26 ID string `q:"id"` 27 Marker string `q:"marker"` 28 Limit int `q:"limit"` 29 SortKey string `q:"sort_key"` 30 SortDir string `q:"sort_dir"` 31 } 32 33 // ToNetworkListQuery formats a ListOpts into a query string. 34 func (opts ListOpts) ToNetworkListQuery() (string, error) { 35 q, err := golangsdk.BuildQueryString(opts) 36 if err != nil { 37 return "", err 38 } 39 return q.String(), err 40 } 41 42 // List returns a Pager which allows you to iterate over a collection of 43 // networks. It accepts a ListOpts struct, which allows you to filter and sort 44 // the returned collection for greater efficiency. 45 func List(c *golangsdk.ServiceClient, opts ListOptsBuilder) pagination.Pager { 46 url := listURL(c) 47 if opts != nil { 48 query, err := opts.ToNetworkListQuery() 49 if err != nil { 50 return pagination.Pager{Err: err} 51 } 52 url += query 53 } 54 return pagination.NewPager(c, url, func(r pagination.PageResult) pagination.Page { 55 return NetworkPage{pagination.LinkedPageBase{PageResult: r}} 56 }) 57 } 58 59 // Get retrieves a specific network based on its unique ID. 60 func Get(c *golangsdk.ServiceClient, id string) (r GetResult) { 61 _, r.Err = c.Get(getURL(c, id), &r.Body, nil) 62 return 63 } 64 65 // CreateOptsBuilder allows extensions to add additional parameters to the 66 // Create request. 67 type CreateOptsBuilder interface { 68 ToNetworkCreateMap() (map[string]interface{}, error) 69 } 70 71 // CreateOpts represents options used to create a network. 72 type CreateOpts struct { 73 AdminStateUp *bool `json:"admin_state_up,omitempty"` 74 Name string `json:"name,omitempty"` 75 Shared *bool `json:"shared,omitempty"` 76 TenantID string `json:"tenant_id,omitempty"` 77 ProjectID string `json:"project_id,omitempty"` 78 AvailabilityZoneHints []string `json:"availability_zone_hints,omitempty"` 79 } 80 81 // ToNetworkCreateMap builds a request body from CreateOpts. 82 func (opts CreateOpts) ToNetworkCreateMap() (map[string]interface{}, error) { 83 return golangsdk.BuildRequestBody(opts, "network") 84 } 85 86 // Create accepts a CreateOpts struct and creates a new network using the values 87 // provided. This operation does not actually require a request body, i.e. the 88 // CreateOpts struct argument can be empty. 89 // 90 // The tenant ID that is contained in the URI is the tenant that creates the 91 // network. An admin user, however, has the option of specifying another tenant 92 // ID in the CreateOpts struct. 93 func Create(c *golangsdk.ServiceClient, opts CreateOptsBuilder) (r CreateResult) { 94 b, err := opts.ToNetworkCreateMap() 95 if err != nil { 96 r.Err = err 97 return 98 } 99 _, r.Err = c.Post(createURL(c), b, &r.Body, nil) 100 return 101 } 102 103 // UpdateOptsBuilder allows extensions to add additional parameters to the 104 // Update request. 105 type UpdateOptsBuilder interface { 106 ToNetworkUpdateMap() (map[string]interface{}, error) 107 } 108 109 // UpdateOpts represents options used to update a network. 110 type UpdateOpts struct { 111 AdminStateUp *bool `json:"admin_state_up,omitempty"` 112 Name string `json:"name,omitempty"` 113 Shared *bool `json:"shared,omitempty"` 114 } 115 116 // ToNetworkUpdateMap builds a request body from UpdateOpts. 117 func (opts UpdateOpts) ToNetworkUpdateMap() (map[string]interface{}, error) { 118 return golangsdk.BuildRequestBody(opts, "network") 119 } 120 121 // Update accepts a UpdateOpts struct and updates an existing network using the 122 // values provided. For more information, see the Create function. 123 func Update(c *golangsdk.ServiceClient, networkID string, opts UpdateOptsBuilder) (r UpdateResult) { 124 b, err := opts.ToNetworkUpdateMap() 125 if err != nil { 126 r.Err = err 127 return 128 } 129 _, r.Err = c.Put(updateURL(c, networkID), b, &r.Body, &golangsdk.RequestOpts{ 130 OkCodes: []int{200, 201}, 131 }) 132 return 133 } 134 135 // Delete accepts a unique ID and deletes the network associated with it. 136 func Delete(c *golangsdk.ServiceClient, networkID string) (r DeleteResult) { 137 _, r.Err = c.Delete(deleteURL(c, networkID), nil) 138 return 139 } 140 141 // IDFromName is a convenience function that returns a network's ID, given 142 // its name. 143 func IDFromName(client *golangsdk.ServiceClient, name string) (string, error) { 144 count := 0 145 id := "" 146 147 listOpts := ListOpts{ 148 Name: name, 149 } 150 151 pages, err := List(client, listOpts).AllPages() 152 if err != nil { 153 return "", err 154 } 155 156 all, err := ExtractNetworks(pages) 157 if err != nil { 158 return "", err 159 } 160 161 for _, s := range all { 162 if s.Name == name { 163 count++ 164 id = s.ID 165 } 166 } 167 168 switch count { 169 case 0: 170 return "", golangsdk.ErrResourceNotFound{Name: name, ResourceType: "network"} 171 case 1: 172 return id, nil 173 default: 174 return "", golangsdk.ErrMultipleResourcesFound{Name: name, Count: count, ResourceType: "network"} 175 } 176 }