github.com/opentelekomcloud/gophertelekomcloud@v0.9.3/openstack/networking/v2/extensions/security/groups/requests.go (about) 1 package groups 2 3 import ( 4 "time" 5 6 "github.com/opentelekomcloud/gophertelekomcloud" 7 "github.com/opentelekomcloud/gophertelekomcloud/pagination" 8 ) 9 10 // ListOpts allows the filtering and sorting of paginated collections through 11 // the API. Filtering is achieved by passing in struct field values that map to 12 // the group attributes you want to see returned. SortKey allows you to 13 // sort by a particular network attribute. SortDir sets the direction, and is 14 // either `asc' or `desc'. Marker and Limit are used for pagination. 15 type ListOpts struct { 16 ID string `q:"id"` 17 Name string `q:"name"` 18 TenantID string `q:"tenant_id"` 19 ProjectID string `q:"project_id"` 20 Limit int `q:"limit"` 21 Marker string `q:"marker"` 22 SortKey string `q:"sort_key"` 23 SortDir string `q:"sort_dir"` 24 } 25 26 // List returns a Pager which allows you to iterate over a collection of 27 // security groups. It accepts a ListOpts struct, which allows you to filter 28 // and sort the returned collection for greater efficiency. 29 func List(c *golangsdk.ServiceClient, opts ListOpts) pagination.Pager { 30 q, err := golangsdk.BuildQueryString(&opts) 31 if err != nil { 32 return pagination.Pager{Err: err} 33 } 34 u := rootURL(c) + q.String() 35 return pagination.NewPager(c, u, func(r pagination.PageResult) pagination.Page { 36 return SecGroupPage{pagination.LinkedPageBase{PageResult: r}} 37 }) 38 } 39 40 // CreateOptsBuilder allows extensions to add additional parameters to the 41 // Create request. 42 type CreateOptsBuilder interface { 43 ToSecGroupCreateMap() (map[string]interface{}, error) 44 } 45 46 // CreateOpts contains all the values needed to create a new security group. 47 type CreateOpts struct { 48 // Human-readable name for the Security Group. Does not have to be unique. 49 Name string `json:"name" required:"true"` 50 51 // TenantID is the UUID of the project who owns the Group. 52 // Only administrative users can specify a tenant UUID other than their own. 53 TenantID string `json:"tenant_id,omitempty"` 54 55 // ProjectID is the UUID of the project who owns the Group. 56 // Only administrative users can specify a tenant UUID other than their own. 57 ProjectID string `json:"project_id,omitempty"` 58 59 // Describes the security group. 60 Description string `json:"description,omitempty"` 61 } 62 63 // ToSecGroupCreateMap builds a request body from CreateOpts. 64 func (opts CreateOpts) ToSecGroupCreateMap() (map[string]interface{}, error) { 65 return golangsdk.BuildRequestBody(opts, "security_group") 66 } 67 68 // Create is an operation which provisions a new security group with default 69 // security group rules for the IPv4 and IPv6 ether types. 70 func Create(c *golangsdk.ServiceClient, opts CreateOptsBuilder) (r CreateResult) { 71 b, err := opts.ToSecGroupCreateMap() 72 if err != nil { 73 r.Err = err 74 return 75 } 76 _, r.Err = c.Post(rootURL(c), b, &r.Body, nil) 77 return 78 } 79 80 // UpdateOptsBuilder allows extensions to add additional parameters to the 81 // Update request. 82 type UpdateOptsBuilder interface { 83 ToSecGroupUpdateMap() (map[string]interface{}, error) 84 } 85 86 // UpdateOpts contains all the values needed to update an existing security 87 // group. 88 type UpdateOpts struct { 89 // Human-readable name for the Security Group. Does not have to be unique. 90 Name string `json:"name,omitempty"` 91 92 // Describes the security group. 93 Description string `json:"description,omitempty"` 94 } 95 96 // ToSecGroupUpdateMap builds a request body from UpdateOpts. 97 func (opts UpdateOpts) ToSecGroupUpdateMap() (map[string]interface{}, error) { 98 return golangsdk.BuildRequestBody(opts, "security_group") 99 } 100 101 // Update is an operation which updates an existing security group. 102 func Update(c *golangsdk.ServiceClient, id string, opts UpdateOptsBuilder) (r UpdateResult) { 103 b, err := opts.ToSecGroupUpdateMap() 104 if err != nil { 105 r.Err = err 106 return 107 } 108 109 _, r.Err = c.Put(resourceURL(c, id), b, &r.Body, &golangsdk.RequestOpts{ 110 OkCodes: []int{200}, 111 }) 112 return 113 } 114 115 // Get retrieves a particular security group based on its unique ID. 116 func Get(c *golangsdk.ServiceClient, id string) (r GetResult) { 117 _, r.Err = c.Get(resourceURL(c, id), &r.Body, nil) 118 return 119 } 120 121 // Delete will permanently delete a particular security group based on its 122 // unique ID. 123 func Delete(c *golangsdk.ServiceClient, id string) (r DeleteResult) { 124 _, r.Err = c.Delete(resourceURL(c, id), nil) 125 return 126 } 127 128 // DeleteWithRetry will try to permanently delete a particular security 129 // group based on its unique ID and RetryTimeout. 130 func DeleteWithRetry(c *golangsdk.ServiceClient, id string, timeout int) error { 131 return golangsdk.WaitFor(timeout, func() (bool, error) { 132 _, err := c.Delete(resourceURL(c, id), nil) 133 if err != nil { 134 if _, ok := err.(golangsdk.ErrDefault409); ok { 135 time.Sleep(10 * time.Second) 136 return false, nil 137 } 138 return false, err 139 } 140 return true, nil 141 }) 142 } 143 144 // IDFromName is a convenience function that returns a security group's ID, 145 // given its name. 146 func IDFromName(client *golangsdk.ServiceClient, name string) (string, error) { 147 count := 0 148 id := "" 149 150 listOpts := ListOpts{ 151 Name: name, 152 } 153 154 pages, err := List(client, listOpts).AllPages() 155 if err != nil { 156 return "", err 157 } 158 159 all, err := ExtractGroups(pages) 160 if err != nil { 161 return "", err 162 } 163 164 for _, s := range all { 165 if s.Name == name { 166 count++ 167 id = s.ID 168 } 169 } 170 171 switch count { 172 case 0: 173 return "", golangsdk.ErrResourceNotFound{Name: name, ResourceType: "security group"} 174 case 1: 175 return id, nil 176 default: 177 return "", golangsdk.ErrMultipleResourcesFound{Name: name, Count: count, ResourceType: "security group"} 178 } 179 }