github.com/vnpaycloud-console/gophercloud/v2@v2.0.5/openstack/dns/v2/transfer/request/requests.go (about) 1 package request 2 3 import ( 4 "context" 5 "net/http" 6 7 "github.com/vnpaycloud-console/gophercloud/v2" 8 "github.com/vnpaycloud-console/gophercloud/v2/pagination" 9 ) 10 11 // ListOptsBuilder allows extensions to add parameters to the List request. 12 type ListOptsBuilder interface { 13 ToTransferRequestListQuery() (string, error) 14 } 15 16 // ListOpts allows the filtering and sorting of paginated collections through 17 // the API. Filtering is achieved by passing in struct field values that map to 18 // the server attributes you want to see returned. 19 // https://developer.openstack.org/api-ref/dns/ 20 type ListOpts struct { 21 Status string `q:"status"` 22 } 23 24 // ToTransferRequestListQuery formats a ListOpts into a query string. 25 func (opts ListOpts) ToTransferRequestListQuery() (string, error) { 26 q, err := gophercloud.BuildQueryString(opts) 27 return q.String(), err 28 } 29 30 // List implements a transfer request List request. 31 func List(client *gophercloud.ServiceClient, opts ListOptsBuilder) pagination.Pager { 32 url := baseURL(client) 33 if opts != nil { 34 query, err := opts.ToTransferRequestListQuery() 35 if err != nil { 36 return pagination.Pager{Err: err} 37 } 38 url += query 39 } 40 return pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page { 41 return TransferRequestPage{pagination.LinkedPageBase{PageResult: r}} 42 }) 43 } 44 45 // Get returns information about a transfer request, given its ID. 46 func Get(ctx context.Context, client *gophercloud.ServiceClient, transferRequestID string) (r GetResult) { 47 resp, err := client.Get(ctx, resourceURL(client, transferRequestID), &r.Body, nil) 48 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 49 return 50 } 51 52 // CreateOptsBuilder allows extensions to add additional attributes to the 53 // Create request. 54 type CreateOptsBuilder interface { 55 ToTransferRequestCreateMap() (map[string]any, error) 56 } 57 58 // CreateOpts specifies the attributes used to create a transfer request. 59 type CreateOpts struct { 60 // TargetProjectID is ID that the request will be limited to. No other project 61 // will be allowed to accept this request. 62 TargetProjectID string `json:"target_project_id,omitempty"` 63 64 // Description of the transfer request. 65 Description string `json:"description,omitempty"` 66 } 67 68 // ToTransferRequestCreateMap formats an CreateOpts structure into a request body. 69 func (opts CreateOpts) ToTransferRequestCreateMap() (map[string]any, error) { 70 b, err := gophercloud.BuildRequestBody(opts, "") 71 if err != nil { 72 return nil, err 73 } 74 return b, nil 75 } 76 77 // Create implements a transfer request create request. 78 func Create(ctx context.Context, client *gophercloud.ServiceClient, zoneID string, opts CreateOptsBuilder) (r CreateResult) { 79 b, err := opts.ToTransferRequestCreateMap() 80 if err != nil { 81 r.Err = err 82 return 83 } 84 resp, err := client.Post(ctx, createURL(client, zoneID), &b, &r.Body, &gophercloud.RequestOpts{ 85 OkCodes: []int{http.StatusCreated, http.StatusAccepted}, 86 }) 87 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 88 return 89 } 90 91 // UpdateOptsBuilder allows extensions to add additional attributes to the 92 // Update request. 93 type UpdateOptsBuilder interface { 94 ToTransferRequestUpdateMap() (map[string]any, error) 95 } 96 97 // UpdateOpts specifies the attributes to update a transfer request. 98 type UpdateOpts struct { 99 // TargetProjectID is ID that the request will be limited to. No other project 100 // will be allowed to accept this request. 101 TargetProjectID string `json:"target_project_id,omitempty"` 102 103 // Description of the transfer request. 104 Description string `json:"description,omitempty"` 105 } 106 107 // ToTransferRequestUpdateMap formats an UpdateOpts structure into a request body. 108 func (opts UpdateOpts) ToTransferRequestUpdateMap() (map[string]any, error) { 109 b, err := gophercloud.BuildRequestBody(opts, "") 110 if err != nil { 111 return nil, err 112 } 113 return b, nil 114 } 115 116 // Update implements a transfer request update request. 117 func Update(ctx context.Context, client *gophercloud.ServiceClient, transferID string, opts UpdateOptsBuilder) (r UpdateResult) { 118 b, err := opts.ToTransferRequestUpdateMap() 119 if err != nil { 120 r.Err = err 121 return 122 } 123 resp, err := client.Patch(ctx, resourceURL(client, transferID), &b, &r.Body, &gophercloud.RequestOpts{ 124 OkCodes: []int{http.StatusOK, http.StatusAccepted}, 125 }) 126 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 127 return 128 } 129 130 // Delete implements a transfer request delete request. 131 func Delete(ctx context.Context, client *gophercloud.ServiceClient, transferID string) (r DeleteResult) { 132 resp, err := client.Delete(ctx, resourceURL(client, transferID), &gophercloud.RequestOpts{ 133 OkCodes: []int{http.StatusNoContent}, 134 }) 135 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 136 return 137 }