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