github.com/vnpaycloud-console/gophercloud/v2@v2.0.5/openstack/dns/v2/zones/requests.go (about) 1 package zones 2 3 import ( 4 "context" 5 6 "github.com/vnpaycloud-console/gophercloud/v2" 7 "github.com/vnpaycloud-console/gophercloud/v2/pagination" 8 ) 9 10 // ListOptsBuilder allows extensions to add parameters to the List request. 11 type ListOptsBuilder interface { 12 ToZoneListQuery() (string, error) 13 } 14 15 // ListOpts allows the filtering and sorting of paginated collections through 16 // the API. Filtering is achieved by passing in struct field values that map to 17 // the server attributes you want to see returned. Marker and Limit are used 18 // for pagination. 19 // https://developer.openstack.org/api-ref/dns/ 20 type ListOpts struct { 21 // Integer value for the limit of values to return. 22 Limit int `q:"limit"` 23 24 // UUID of the zone at which you want to set a marker. 25 Marker string `q:"marker"` 26 27 Description string `q:"description"` 28 Email string `q:"email"` 29 Name string `q:"name"` 30 SortDir string `q:"sort_dir"` 31 SortKey string `q:"sort_key"` 32 Status string `q:"status"` 33 TTL int `q:"ttl"` 34 Type string `q:"type"` 35 } 36 37 // ToZoneListQuery formats a ListOpts into a query string. 38 func (opts ListOpts) ToZoneListQuery() (string, error) { 39 q, err := gophercloud.BuildQueryString(opts) 40 return q.String(), err 41 } 42 43 // List implements a zone List request. 44 func List(client *gophercloud.ServiceClient, opts ListOptsBuilder) pagination.Pager { 45 url := baseURL(client) 46 if opts != nil { 47 query, err := opts.ToZoneListQuery() 48 if err != nil { 49 return pagination.Pager{Err: err} 50 } 51 url += query 52 } 53 return pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page { 54 return ZonePage{pagination.LinkedPageBase{PageResult: r}} 55 }) 56 } 57 58 // Get returns information about a zone, given its ID. 59 func Get(ctx context.Context, client *gophercloud.ServiceClient, zoneID string) (r GetResult) { 60 resp, err := client.Get(ctx, zoneURL(client, zoneID), &r.Body, nil) 61 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 62 return 63 } 64 65 // CreateOptsBuilder allows extensions to add additional attributes to the 66 // Create request. 67 type CreateOptsBuilder interface { 68 ToZoneCreateMap() (map[string]any, error) 69 } 70 71 // CreateOpts specifies the attributes used to create a zone. 72 type CreateOpts struct { 73 // Attributes are settings that supply hints and filters for the zone. 74 Attributes map[string]string `json:"attributes,omitempty"` 75 76 // Email contact of the zone. 77 Email string `json:"email,omitempty"` 78 79 // Description of the zone. 80 Description string `json:"description,omitempty"` 81 82 // Name of the zone. 83 Name string `json:"name" required:"true"` 84 85 // Masters specifies zone masters if this is a secondary zone. 86 Masters []string `json:"masters,omitempty"` 87 88 // TTL is the time to live of the zone. 89 TTL int `json:"-"` 90 91 // Type specifies if this is a primary or secondary zone. 92 Type string `json:"type,omitempty"` 93 } 94 95 // ToZoneCreateMap formats an CreateOpts structure into a request body. 96 func (opts CreateOpts) ToZoneCreateMap() (map[string]any, error) { 97 b, err := gophercloud.BuildRequestBody(opts, "") 98 if err != nil { 99 return nil, err 100 } 101 102 if opts.TTL > 0 { 103 b["ttl"] = opts.TTL 104 } 105 106 return b, nil 107 } 108 109 // Create implements a zone create request. 110 func Create(ctx context.Context, client *gophercloud.ServiceClient, opts CreateOptsBuilder) (r CreateResult) { 111 b, err := opts.ToZoneCreateMap() 112 if err != nil { 113 r.Err = err 114 return 115 } 116 resp, err := client.Post(ctx, baseURL(client), &b, &r.Body, &gophercloud.RequestOpts{ 117 OkCodes: []int{201, 202}, 118 }) 119 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 120 return 121 } 122 123 // UpdateOptsBuilder allows extensions to add additional attributes to the 124 // Update request. 125 type UpdateOptsBuilder interface { 126 ToZoneUpdateMap() (map[string]any, error) 127 } 128 129 // UpdateOpts specifies the attributes to update a zone. 130 type UpdateOpts struct { 131 // Email contact of the zone. 132 Email string `json:"email,omitempty"` 133 134 // TTL is the time to live of the zone. 135 TTL int `json:"-"` 136 137 // Masters specifies zone masters if this is a secondary zone. 138 Masters []string `json:"masters,omitempty"` 139 140 // Description of the zone. 141 Description *string `json:"description,omitempty"` 142 } 143 144 // ToZoneUpdateMap formats an UpdateOpts structure into a request body. 145 func (opts UpdateOpts) ToZoneUpdateMap() (map[string]any, error) { 146 b, err := gophercloud.BuildRequestBody(opts, "") 147 if err != nil { 148 return nil, err 149 } 150 151 if opts.TTL > 0 { 152 b["ttl"] = opts.TTL 153 } 154 155 return b, nil 156 } 157 158 // Update implements a zone update request. 159 func Update(ctx context.Context, client *gophercloud.ServiceClient, zoneID string, opts UpdateOptsBuilder) (r UpdateResult) { 160 b, err := opts.ToZoneUpdateMap() 161 if err != nil { 162 r.Err = err 163 return 164 } 165 resp, err := client.Patch(ctx, zoneURL(client, zoneID), &b, &r.Body, &gophercloud.RequestOpts{ 166 OkCodes: []int{200, 202}, 167 }) 168 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 169 return 170 } 171 172 // Delete implements a zone delete request. 173 func Delete(ctx context.Context, client *gophercloud.ServiceClient, zoneID string) (r DeleteResult) { 174 resp, err := client.Delete(ctx, zoneURL(client, zoneID), &gophercloud.RequestOpts{ 175 OkCodes: []int{202}, 176 JSONResponse: &r.Body, 177 }) 178 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 179 return 180 } 181 182 // request body for sharing a zone. 183 type ShareOptsBuilder interface { 184 ToShareMap() (map[string]interface{}, error) 185 } 186 187 // ShareZoneOpts specifies the target project for sharing a zone. 188 type ShareZoneOpts struct { 189 // TargetProjectID is the ID of the project to share the zone with. 190 TargetProjectID string `json:"target_project_id" required:"true"` 191 } 192 193 // ToShareMap constructs a request body from a ShareZoneOpts. 194 func (opts ShareZoneOpts) ToShareMap() (map[string]interface{}, error) { 195 return map[string]interface{}{ 196 "target_project_id": opts.TargetProjectID, 197 }, nil 198 } 199 200 // Share shares a zone with another project. 201 func Share(ctx context.Context, client *gophercloud.ServiceClient, zoneID string, opts ShareOptsBuilder) (r gophercloud.ErrResult) { 202 body, err := gophercloud.BuildRequestBody(opts, "") 203 if err != nil { 204 r.Err = err 205 return 206 } 207 208 resp, err := client.Post(ctx, zoneShareURL(client, zoneID), body, nil, &gophercloud.RequestOpts{ 209 OkCodes: []int{201}, 210 }) 211 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 212 return 213 } 214 215 // Unshare removes a share for a zone. 216 func Unshare(ctx context.Context, client *gophercloud.ServiceClient, zoneID, shareID string) (r gophercloud.ErrResult) { 217 resp, err := client.Delete(ctx, zoneUnshareURL(client, zoneID, shareID), &gophercloud.RequestOpts{ 218 OkCodes: []int{204}, 219 }) 220 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 221 return 222 }