github.com/vnpaycloud-console/gophercloud/v2@v2.0.5/openstack/dns/v2/recordsets/requests.go (about) 1 package recordsets 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 additional parameters to the 11 // List request. 12 type ListOptsBuilder interface { 13 ToRecordSetListQuery() (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. Marker and Limit are used 19 // for pagination. 20 // https://developer.openstack.org/api-ref/dns/ 21 type ListOpts struct { 22 // Integer value for the limit of values to return. 23 Limit int `q:"limit"` 24 25 // UUID of the recordset at which you want to set a marker. 26 Marker string `q:"marker"` 27 28 Data string `q:"data"` 29 Description string `q:"description"` 30 Name string `q:"name"` 31 SortDir string `q:"sort_dir"` 32 SortKey string `q:"sort_key"` 33 Status string `q:"status"` 34 TTL int `q:"ttl"` 35 Type string `q:"type"` 36 ZoneID string `q:"zone_id"` 37 } 38 39 // ToRecordSetListQuery formats a ListOpts into a query string. 40 func (opts ListOpts) ToRecordSetListQuery() (string, error) { 41 q, err := gophercloud.BuildQueryString(opts) 42 return q.String(), err 43 } 44 45 // ListByZone implements the recordset list request. 46 func ListByZone(client *gophercloud.ServiceClient, zoneID string, opts ListOptsBuilder) pagination.Pager { 47 url := baseURL(client, zoneID) 48 if opts != nil { 49 query, err := opts.ToRecordSetListQuery() 50 if err != nil { 51 return pagination.Pager{Err: err} 52 } 53 url += query 54 } 55 return pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page { 56 return RecordSetPage{pagination.LinkedPageBase{PageResult: r}} 57 }) 58 } 59 60 // Get implements the recordset Get request. 61 func Get(ctx context.Context, client *gophercloud.ServiceClient, zoneID string, rrsetID string) (r GetResult) { 62 resp, err := client.Get(ctx, rrsetURL(client, zoneID, rrsetID), &r.Body, nil) 63 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 64 return 65 } 66 67 // CreateOptsBuilder allows extensions to add additional attributes to the 68 // Create request. 69 type CreateOptsBuilder interface { 70 ToRecordSetCreateMap() (map[string]any, error) 71 } 72 73 // CreateOpts specifies the base attributes that may be used to create a 74 // RecordSet. 75 type CreateOpts struct { 76 // Name is the name of the RecordSet. 77 Name string `json:"name" required:"true"` 78 79 // Description is a description of the RecordSet. 80 Description string `json:"description,omitempty"` 81 82 // Records are the DNS records of the RecordSet. 83 Records []string `json:"records,omitempty"` 84 85 // TTL is the time to live of the RecordSet. 86 TTL int `json:"ttl,omitempty"` 87 88 // Type is the RRTYPE of the RecordSet. 89 Type string `json:"type,omitempty"` 90 } 91 92 // ToRecordSetCreateMap formats an CreateOpts structure into a request body. 93 func (opts CreateOpts) ToRecordSetCreateMap() (map[string]any, error) { 94 b, err := gophercloud.BuildRequestBody(opts, "") 95 if err != nil { 96 return nil, err 97 } 98 99 return b, nil 100 } 101 102 // Create creates a recordset in a given zone. 103 func Create(ctx context.Context, client *gophercloud.ServiceClient, zoneID string, opts CreateOptsBuilder) (r CreateResult) { 104 b, err := opts.ToRecordSetCreateMap() 105 if err != nil { 106 r.Err = err 107 return 108 } 109 resp, err := client.Post(ctx, baseURL(client, zoneID), &b, &r.Body, &gophercloud.RequestOpts{ 110 OkCodes: []int{201, 202}, 111 }) 112 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 113 return 114 } 115 116 // UpdateOptsBuilder allows extensions to add additional attributes to the 117 // Update request. 118 type UpdateOptsBuilder interface { 119 ToRecordSetUpdateMap() (map[string]any, error) 120 } 121 122 // UpdateOpts specifies the base attributes that may be updated on an existing 123 // RecordSet. 124 type UpdateOpts struct { 125 // Description is a description of the RecordSet. 126 Description *string `json:"description,omitempty"` 127 128 // TTL is the time to live of the RecordSet. 129 TTL *int `json:"ttl,omitempty"` 130 131 // Records are the DNS records of the RecordSet. 132 Records []string `json:"records,omitempty"` 133 } 134 135 // ToRecordSetUpdateMap formats an UpdateOpts structure into a request body. 136 func (opts UpdateOpts) ToRecordSetUpdateMap() (map[string]any, error) { 137 b, err := gophercloud.BuildRequestBody(opts, "") 138 if err != nil { 139 return nil, err 140 } 141 142 // If opts.TTL was actually set, use 0 as a special value to send "null", 143 // even though the result from the API is 0. 144 // 145 // Otherwise, don't send the TTL field. 146 if opts.TTL != nil { 147 ttl := *(opts.TTL) 148 if ttl > 0 { 149 b["ttl"] = ttl 150 } else { 151 b["ttl"] = nil 152 } 153 } 154 155 return b, nil 156 } 157 158 // Update updates a recordset in a given zone 159 func Update(ctx context.Context, client *gophercloud.ServiceClient, zoneID string, rrsetID string, opts UpdateOptsBuilder) (r UpdateResult) { 160 b, err := opts.ToRecordSetUpdateMap() 161 if err != nil { 162 r.Err = err 163 return 164 } 165 resp, err := client.Put(ctx, rrsetURL(client, zoneID, rrsetID), &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 removes an existing RecordSet. 173 func Delete(ctx context.Context, client *gophercloud.ServiceClient, zoneID string, rrsetID string) (r DeleteResult) { 174 resp, err := client.Delete(ctx, rrsetURL(client, zoneID, rrsetID), &gophercloud.RequestOpts{ 175 OkCodes: []int{202}, 176 }) 177 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 178 return 179 }